diff --git a/lib/shred/shred.go b/lib/shred/shred.go index 6d8f5e7..29376eb 100644 --- a/lib/shred/shred.go +++ b/lib/shred/shred.go @@ -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 @@ -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 } diff --git a/lib/shred/shred_test.go b/lib/shred/shred_test.go index 85f9977..a6c2a13 100644 --- a/lib/shred/shred_test.go +++ b/lib/shred/shred_test.go @@ -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"), }, } @@ -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) }, }, { @@ -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 @@ -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 @@ -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 { diff --git a/lib/shred/testdata/empty.log b/lib/shred/testdata/.gitkeep similarity index 100% rename from lib/shred/testdata/empty.log rename to lib/shred/testdata/.gitkeep diff --git a/lib/shred/testdata/protected.log b/lib/shred/testdata/protected.log deleted file mode 100644 index e69de29..0000000