Merge pull request #56 from vetinari/close-modtime

Set modtime on any write
This commit is contained in:
Martin Bertschler 2016-01-04 19:51:02 +01:00
commit d8bbf358ca
2 changed files with 50 additions and 0 deletions

View File

@ -99,6 +99,9 @@ func (f *File) Open() error {
func (f *File) Close() error { func (f *File) Close() error {
f.fileData.Lock() f.fileData.Lock()
f.closed = true f.closed = true
if !f.readOnly {
SetModTime(f.fileData, time.Now())
}
f.fileData.Unlock() f.fileData.Unlock()
return nil return nil
} }
@ -180,6 +183,9 @@ func (f *File) Truncate(size int64) error {
if f.closed == true { if f.closed == true {
return ErrFileClosed return ErrFileClosed
} }
if f.readOnly {
return &os.PathError{"truncate", f.fileData.name, errors.New("file handle is read only")}
}
if size < 0 { if size < 0 {
return ErrOutOfRange return ErrOutOfRange
} }
@ -189,6 +195,7 @@ func (f *File) Truncate(size int64) error {
} else { } else {
f.fileData.data = f.fileData.data[0:size] f.fileData.data = f.fileData.data[0:size]
} }
SetModTime(f.fileData, time.Now())
return nil 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[:cur], b...)
f.fileData.data = append(f.fileData.data, tail...) f.fileData.data = append(f.fileData.data, tail...)
} }
SetModTime(f.fileData, time.Now())
atomic.StoreInt64(&f.at, int64(len(f.fileData.data))) atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
return return

View File

@ -208,3 +208,45 @@ func TestReadOnly(t *testing.T) {
f.Close() 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()")
}
}
}