Skip to content

Commit

Permalink
Set timestamp of download files (#87)
Browse files Browse the repository at this point in the history
* Set file timestamp according to S3 timestamp on download

* Check file timestamp on the test

* Tweak format
  • Loading branch information
at-wat authored Oct 28, 2020
1 parent bf93abb commit 2ea0458
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
5 changes: 4 additions & 1 deletion s3sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ func (m *Manager) download(file *fileInfo, sourcePath *s3Path, destPath string)
}

writer, err := os.Create(targetFilename)

if err != nil {
return err
}
Expand All @@ -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
}
Expand Down
27 changes: 13 additions & 14 deletions s3sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sort"
"sync/atomic"
"testing"
"time"
)

const dummyFilename = "README.md"
Expand Down Expand Up @@ -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/
Expand All @@ -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")
Expand Down Expand Up @@ -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")
}
}
25 changes: 25 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 2ea0458

Please sign in to comment.