Skip to content

Commit

Permalink
fix: zero option
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Nov 20, 2022
1 parent 6c77377 commit 716fc1a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 37 deletions.
19 changes: 9 additions & 10 deletions lib/shred/shred.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,31 @@ func (s *Shredder) Write(pathName string) error {
// Stat the file for the file length
fstat, err := os.Stat(pathName)
if err != nil {
return fmt.Errorf("shredding failed: %w", err)
return fmt.Errorf("file stat failed: %w", err)
}

// Open the file
file, err := os.OpenFile(pathName, os.O_WRONLY, fstat.Mode())
if err != nil {
return fmt.Errorf("shredding failed: %w", err)
return fmt.Errorf("file opening failed: %w", err)
}
defer file.Close()

err = s.shred(fstat, file)
if err != nil {
return fmt.Errorf("shredding failed: %w", err)
}

if s.options.Zero {
if err := os.Truncate(pathName, 0); err != nil {
return fmt.Errorf("truncate failed: %w", err)
}
}

return nil
}

func (s *Shredder) shred(fstat FileInfo, file File) error {
defer file.Close()
fSize := fstat.Size()

// Avoid shredding if the file is already empty
Expand Down Expand Up @@ -102,12 +108,5 @@ func (s *Shredder) shred(fstat FileInfo, file File) error {
}
}

if s.options.Zero {
_, err := file.Write([]byte{})
if err != nil {
return err
}
}

return nil
}
29 changes: 2 additions & 27 deletions lib/shred/shred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func TestShredder_Write(t *testing.T) {
{
name: "test with non-existing file",
input: "testdata/fake.log",
wantError: errors.New("shredding failed: stat testdata/fake.log: no such file or directory"),
wantError: errors.New("file stat failed: stat testdata/fake.log: no such file or directory"),
},
{
name: "test with non-file path",
input: "testdata/",
wantError: errors.New("shredding failed: open testdata/: is a directory"),
wantError: errors.New("file opening failed: open testdata/: is a directory"),
},
}

Expand Down Expand Up @@ -57,7 +57,6 @@ func TestShredder_shred(t *testing.T) {
},
mocks: func(fakeFileInfo *mocks.FileInfo, fakeFile *mocks.File) {
fakeFileInfo.On("Size").Return(int64(0)).Times(1)
fakeFile.On("Close").Return(nil).Times(1)
},
},
{
Expand All @@ -71,7 +70,6 @@ func TestShredder_shred(t *testing.T) {
fakeFileInfo.On("Size").Return(int64(64)).Times(4)

fakeFile.On("Seek", int64(0), 0).Return(int64(0), nil).Times(3)
fakeFile.On("Close").Return(nil).Times(1)
fakeFile.On("Sync").Return(nil).Times(3)
fakeFile.On("Write", mock.MatchedBy(func(b []byte) bool {
return len(b) != 0
Expand All @@ -89,7 +87,6 @@ func TestShredder_shred(t *testing.T) {
fakeFileInfo.On("Size").Return(int64(2000000)).Times(11)

fakeFile.On("Seek", int64(0), 0).Return(int64(0), nil).Times(10)
fakeFile.On("Close").Return(nil).Times(1)
fakeFile.On("Sync").Return(nil).Times(10)
fakeFile.On("Write", mock.MatchedBy(func(b []byte) bool {
return len(b) != 0
Expand All @@ -107,34 +104,12 @@ func TestShredder_shred(t *testing.T) {
fakeFileInfo.On("Size").Return(int64(2000)).Times(2)

fakeFile.On("Seek", int64(0), 0).Return(int64(0), nil).Times(1)
fakeFile.On("Close").Return(nil).Times(1)
fakeFile.On("Write", mock.MatchedBy(func(b []byte) bool {
return len(b) != 0
})).Return(0, errors.New("dummy error"))
},
wantError: errors.New("dummy error"),
},
{
name: "test writing a 2Kb file with zero option",
options: ShredderOptions{
Zero: true,
Iterations: 5,
Unlink: false,
},
mocks: func(fakeFileInfo *mocks.FileInfo, fakeFile *mocks.File) {
fakeFileInfo.On("Size").Return(int64(2000)).Times(6)

fakeFile.On("Close").Return(nil).Times(1)
fakeFile.On("Seek", int64(0), 0).Return(int64(0), nil).Times(5)
fakeFile.On("Sync").Return(nil).Times(5)
fakeFile.On("Write", mock.MatchedBy(func(b []byte) bool {
return len(b) > 0
})).Return(0, nil)
fakeFile.On("Write", mock.MatchedBy(func(b []byte) bool {
return len(b) == 0
})).Return(0, nil).Once()
},
},
}

for _, tt := range cases {
Expand Down
File renamed without changes.
Empty file removed lib/shred/testdata/protected.log
Empty file.

0 comments on commit 716fc1a

Please sign in to comment.