mirror of https://github.com/spf13/afero.git
Merge pull request #56 from vetinari/close-modtime
Set modtime on any write
This commit is contained in:
commit
d8bbf358ca
|
@ -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
|
||||
|
|
|
@ -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()")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue