From bb3fe399666c8efb9dd739c0f0296b763cf655c4 Mon Sep 17 00:00:00 2001 From: Hanno Hecker Date: Mon, 4 Jan 2016 19:34:24 +0100 Subject: [PATCH] set modtime on any write --- mem/file.go | 8 ++++++++ memmap_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/mem/file.go b/mem/file.go index 7d43a52..be3673e 100644 --- a/mem/file.go +++ b/mem/file.go @@ -99,6 +99,9 @@ func (f *File) Open() error { func (f *File) Close() error { f.fileData.Lock() f.closed = true + if !f.readOnly { + SetModTime(f.fileData, time.Now()) + } f.fileData.Unlock() return nil } @@ -180,6 +183,9 @@ func (f *File) Truncate(size int64) error { if f.closed == true { return ErrFileClosed } + if f.readOnly { + return &os.PathError{"truncate", f.fileData.name, errors.New("file handle is read only")} + } if size < 0 { return ErrOutOfRange } @@ -189,6 +195,7 @@ func (f *File) Truncate(size int64) error { } else { f.fileData.data = f.fileData.data[0:size] } + SetModTime(f.fileData, time.Now()) return nil } @@ -227,6 +234,7 @@ func (f *File) Write(b []byte) (n int, err error) { f.fileData.data = append(f.fileData.data[:cur], b...) f.fileData.data = append(f.fileData.data, tail...) } + SetModTime(f.fileData, time.Now()) atomic.StoreInt64(&f.at, int64(len(f.fileData.data))) return diff --git a/memmap_test.go b/memmap_test.go index c0e6825..f090df7 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -208,3 +208,45 @@ func TestReadOnly(t *testing.T) { f.Close() } } + +func TestWriteCloseTime(t *testing.T) { + defer removeAllTestFiles(t) + const fileName = "afero-demo.txt" + + for _, fs := range Fss { + dir := testDir(fs) + path := filepath.Join(dir, fileName) + + f, err := fs.Create(path) + if err != nil { + t.Error(fs.Name()+":", "fs.Create failed: "+err.Error()) + } + f.Close() + + f, err = fs.Create(path) + if err != nil { + t.Error(fs.Name()+":", "fs.Create failed: "+err.Error()) + } + fi, err := f.Stat() + if err != nil { + t.Error(fs.Name()+":", "Stat failed: "+err.Error()) + } + timeBefore := fi.ModTime() + // sorry for the delay, but we have to make sure time advances, also + // on non Un*x systems... + time.Sleep(2 * time.Second) + + _, err = f.Write([]byte("test")) + if err != nil { + t.Error(fs.Name()+":", "Write failed: "+err.Error()) + } + f.Close() + fi, err = fs.Stat(path) + if err != nil { + t.Error(fs.Name()+":", "fs.Stat failed: "+err.Error()) + } + if fi.ModTime().Equal(timeBefore) { + t.Error(fs.Name()+":", "ModTime was not set on Close()") + } + } +}