Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set modtime on any write #56

Merged
merged 1 commit into from
Jan 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions memmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
}
}
}