diff --git a/s3sync.go b/s3sync.go index fbf3fc8..0fd572a 100644 --- a/s3sync.go +++ b/s3sync.go @@ -220,7 +220,6 @@ func (m *Manager) download(file *fileInfo, sourcePath *s3Path, destPath string) } writer, err := os.Create(targetFilename) - if err != nil { return err } @@ -238,7 +237,11 @@ func (m *Manager) download(file *fileInfo, sourcePath *s3Path, destPath string) Bucket: aws.String(sourcePath.bucket), Key: aws.String(sourceFile), }) + if err != nil { + return err + } + err = os.Chtimes(targetFilename, file.lastModified, file.lastModified) if err != nil { return err } diff --git a/s3sync_test.go b/s3sync_test.go index 70d7c1a..ae60981 100644 --- a/s3sync_test.go +++ b/s3sync_test.go @@ -22,6 +22,7 @@ import ( "sort" "sync/atomic" "testing" + "time" ) const dummyFilename = "README.md" @@ -60,6 +61,8 @@ func TestS3sync(t *testing.T) { t.Fatal("Failed to write", err) } + tBeforeSync := time.Now() + // The dummy s3 bucket has following files. // // s3://example-bucket/ @@ -74,9 +77,16 @@ func TestS3sync(t *testing.T) { } fileHasSize(t, destOnlyFilename, destOnlyFileSize) - fileHasSize(t, filepath.Join(temp, dummyFilename), dummyFileSize) - fileHasSize(t, filepath.Join(temp, "foo", dummyFilename), dummyFileSize) - fileHasSize(t, filepath.Join(temp, "bar/baz", dummyFilename), dummyFileSize) + + for _, filename := range []string{ + filepath.Join(temp, dummyFilename), + filepath.Join(temp, "foo", dummyFilename), + filepath.Join(temp, "bar/baz", dummyFilename), + } { + fileHasSize(t, filename, dummyFileSize) + // Files must be made before test + fileModTimeBefore(t, filename, tBeforeSync) + } }) t.Run("DownloadSkipDirectory", func(t *testing.T) { temp, err := ioutil.TempDir("", "s3synctest") @@ -654,14 +664,3 @@ func (d *dummyLogger) Logf(format string, v ...interface{}) { func createLoggerWithLogFunc(log func(v ...interface{})) LoggerIF { return &dummyLogger{log: log} } - -func fileHasSize(t *testing.T, filename string, expectedSize int) { - data, err := ioutil.ReadFile(filename) - if err != nil { - t.Error(filename, "is not synced") - return - } - if len(data) != expectedSize { - t.Error(filename, "is not synced") - } -} diff --git a/util_test.go b/util_test.go index 73127fd..08a110e 100644 --- a/util_test.go +++ b/util_test.go @@ -13,8 +13,11 @@ package s3sync import ( + "io/ioutil" + "os" "sort" "testing" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" @@ -96,3 +99,25 @@ func listObjectsSorted(t *testing.T, bucket string) []s3Object { sort.Sort(s3ObjectList(objs)) return objs } + +func fileHasSize(t *testing.T, filename string, expectedSize int) { + data, err := ioutil.ReadFile(filename) + if err != nil { + t.Error(filename, "is not synced") + return + } + if n := len(data); n != expectedSize { + t.Errorf("%s is not synced (file size is expected to be %d, actual %d)", filename, expectedSize, n) + } +} + +func fileModTimeBefore(t *testing.T, filename string, t0 time.Time) { + info, err := os.Stat(filename) + if err != nil { + t.Error("Failed to get stat:", err) + return + } + if t1 := info.ModTime(); !t1.Before(t0) { + t.Errorf("File modification time %v is later than %v", t1, t0) + } +}