From 017533298765cb2f664c4f663acfff25f51bcaf7 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 6 Mar 2018 20:47:11 +0000 Subject: [PATCH] vfs: fix applying modtime for an open Write Handle The symptom of this was that the time set when the file was open was lost. This was causing one of the mount tests to fail too. --- vfs/read_write_test.go | 25 +++++++++++++++++++++++++ vfs/write.go | 5 ++++- vfs/write_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/vfs/read_write_test.go b/vfs/read_write_test.go index 075a24cb3..e13ebcbe9 100644 --- a/vfs/read_write_test.go +++ b/vfs/read_write_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "testing" + "time" "github.com/ncw/rclone/fs" "github.com/ncw/rclone/fstest" @@ -562,3 +563,27 @@ func TestRWFileHandleOpenTests(t *testing.T) { testRWFileHandleOpenTest(t, vfs, &test) } } + +// tests mod time on open files +func TestRWFileModTimeWithOpenWriters(t *testing.T) { + r := fstest.NewRun(t) + defer r.Finalise() + vfs, fh := rwHandleCreateWriteOnly(t, r) + + mtime := time.Date(2012, 11, 18, 17, 32, 31, 0, time.UTC) + + _, err := fh.Write([]byte{104, 105}) + require.NoError(t, err) + + err = fh.Node().SetModTime(mtime) + require.NoError(t, err) + + err = fh.Close() + require.NoError(t, err) + + info, err := vfs.Stat("file1") + require.NoError(t, err) + + // avoid errors because of timezone differences + assert.Equal(t, info.ModTime().Unix(), mtime.Unix()) +} diff --git a/vfs/write.go b/vfs/write.go index b96037a92..fd4c2cf11 100644 --- a/vfs/write.go +++ b/vfs/write.go @@ -181,7 +181,10 @@ func (fh *WriteFileHandle) close() (err error) { } fh.closed = true // leave writer open until file is transferred - defer fh.file.delWriter(fh, false) + defer func() { + fh.file.delWriter(fh, false) + fh.file.finishWriterClose() + }() // If file not opened and not safe to truncate then then leave file intact if !fh.opened && !fh.safeToTruncate() { return nil diff --git a/vfs/write_test.go b/vfs/write_test.go index 6acc1ecce..10f213fe9 100644 --- a/vfs/write_test.go +++ b/vfs/write_test.go @@ -3,6 +3,7 @@ package vfs import ( "os" "testing" + "time" "github.com/ncw/rclone/fs" "github.com/ncw/rclone/fstest" @@ -214,3 +215,27 @@ func TestWriteFileHandleRelease(t *testing.T) { assert.NoError(t, err) assert.True(t, fh.closed) } + +// tests mod time on open files +func TestWriteFileModTimeWithOpenWriters(t *testing.T) { + r := fstest.NewRun(t) + defer r.Finalise() + vfs, fh := writeHandleCreate(t, r) + + mtime := time.Date(2012, 11, 18, 17, 32, 31, 0, time.UTC) + + _, err := fh.Write([]byte{104, 105}) + require.NoError(t, err) + + err = fh.Node().SetModTime(mtime) + require.NoError(t, err) + + err = fh.Close() + require.NoError(t, err) + + info, err := vfs.Stat("file1") + require.NoError(t, err) + + // avoid errors because of timezone differences + assert.Equal(t, info.ModTime().Unix(), mtime.Unix()) +}