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

Introduce a flag to preserve timestamps of files #534

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions command/cp.go
Expand Up @@ -100,6 +100,12 @@ Examples:

21. Upload a file to S3 with a content-type and content-encoding header
> s5cmd --content-type "text/css" --content-encoding "br" myfile.css.br s3://bucket/

22. Upload a file to S3 preserving the timestamp on disk
> s5cmd --preserve-timestamp myfile.css.br s3://bucket/

22. Download a file from S3 preserving the timestamp it was originally uplaoded with
> s5cmd --preserve-timestamp s3://bucket/myfile.css.br myfile.css.br
`

func NewSharedFlags() []cli.Flag {
Expand Down Expand Up @@ -182,6 +188,10 @@ func NewSharedFlags() []cli.Flag {
DefaultText: "0",
Hidden: true,
},
&cli.BoolFlag{
Name: "preserve-timestamp",
Usage: "preserve the timestamp on disk while uploading and set the timestamp from s3 while downloading.",
},
}
}

Expand Down Expand Up @@ -265,6 +275,7 @@ type Copy struct {
expires string
contentType string
contentEncoding string
preserveTimestamp bool

// region settings
srcRegion string
Expand Down Expand Up @@ -304,6 +315,7 @@ func NewCopy(c *cli.Context, deleteSource bool) Copy {
expires: c.String("expires"),
contentType: c.String("content-type"),
contentEncoding: c.String("content-encoding"),
preserveTimestamp: c.Bool("preserve-timestamp"),
// region settings
srcRegion: c.String("source-region"),
dstRegion: c.String("destination-region"),
Expand Down Expand Up @@ -537,6 +549,17 @@ func (c Copy) doDownload(ctx context.Context, srcurl *url.URL, dsturl *url.URL)
_ = srcClient.Delete(ctx, srcurl)
}

if c.preserveTimestamp {
obj, err := srcClient.Stat(ctx, srcurl)
if err != nil {
return err
}
err = storage.SetFileTime(dsturl.Absolute(), *obj.AccessTime, *obj.ModTime, *obj.CreateTime)
if err != nil {
return err
}
}

msg := log.InfoMessage{
Operation: c.op,
Source: srcurl,
Expand Down Expand Up @@ -585,6 +608,14 @@ func (c Copy) doUpload(ctx context.Context, srcurl *url.URL, dsturl *url.URL) er
SetCacheControl(c.cacheControl).
SetExpires(c.expires)

if c.preserveTimestamp {
aTime, mTime, cTime, err := storage.GetFileTime(srcurl.Absolute())
if err != nil {
return err
}
metadata.SetPreserveTimestamp(aTime, mTime, cTime)
}

if c.contentType != "" {
metadata.SetContentType(c.contentType)
} else {
Expand Down
13 changes: 9 additions & 4 deletions command/sync.go
Expand Up @@ -114,8 +114,9 @@ type Sync struct {
fullCommand string

// flags
delete bool
sizeOnly bool
delete bool
sizeOnly bool
preserveTimestamp bool

// s3 options
storageOpts storage.Options
Expand All @@ -137,8 +138,9 @@ func NewSync(c *cli.Context) Sync {
fullCommand: commandFromContext(c),

// flags
delete: c.Bool("delete"),
sizeOnly: c.Bool("size-only"),
delete: c.Bool("delete"),
sizeOnly: c.Bool("size-only"),
preserveTimestamp: c.Bool("preserve-timestamp"),

// flags
followSymlinks: !c.Bool("no-follow-symlinks"),
Expand Down Expand Up @@ -358,6 +360,9 @@ func (s Sync) planRun(
defaultFlags := map[string]interface{}{
"raw": true,
}
if s.preserveTimestamp {
defaultFlags["preserve-timestamp"] = s.preserveTimestamp
}

// only in source
for _, srcurl := range onlySource {
Expand Down
47 changes: 47 additions & 0 deletions storage/fs_darwin.go
@@ -0,0 +1,47 @@
//go:build darwin

package storage

import (
"os"
"syscall"
"time"
)

func GetFileTime(filename string) (time.Time, time.Time, time.Time, error) {
fi, err := os.Stat(filename)
if err != nil {
return time.Time{}, time.Time{}, time.Time{}, err
}

stat := fi.Sys().(*syscall.Stat_t)
cTime := time.Unix(int64(stat.Ctimespec.Sec), int64(stat.Ctimespec.Nsec))
aTime := time.Unix(int64(stat.Atimespec.Sec), int64(stat.Atimespec.Nsec))

mTime := fi.ModTime()

return aTime, mTime, cTime, nil
}

func SetFileTime(filename string, accessTime, modificationTime, creationTime time.Time) error {
var err error
if accessTime.IsZero() && modificationTime.IsZero() {
// Nothing recorded in s3. Return fast.
return nil
} else if accessTime.IsZero() {
accessTime, _, _, err = GetFileTime(filename)
if err != nil {
return err
}
} else if modificationTime.IsZero() {
_, modificationTime, _, err = GetFileTime(filename)
if err != nil {
return err
}
}
err = os.Chtimes(filename, accessTime, modificationTime)
if err != nil {
return err
}
return nil
}
49 changes: 49 additions & 0 deletions storage/fs_linux.go
@@ -0,0 +1,49 @@
//go:build linux

package storage

import (
"os"
"syscall"
"time"
)

func GetFileTime(filename string) (time.Time, time.Time, time.Time, error) {
fi, err := os.Stat(filename)
if err != nil {
return time.Time{}, time.Time{}, time.Time{}, err
}

stat := fi.Sys().(*syscall.Stat_t)
cTime := time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))

mTime := fi.ModTime()

return aTime, mTime, cTime, nil
}

func SetFileTime(filename string, accessTime, modificationTime, creationTime time.Time) error {
if accessTime.IsZero() && modificationTime.IsZero() {
// Nothing recorded in s3. Return fast.
return nil
}
var err error
if accessTime.IsZero() {
accessTime, _, _, err = GetFileTime(filename)
if err != nil {
return err
}
}
if modificationTime.IsZero() {
_, modificationTime, _, err = GetFileTime(filename)
if err != nil {
return err
}
}
err = os.Chtimes(filename, accessTime, modificationTime)
if err != nil {
return err
}
return nil
}
64 changes: 64 additions & 0 deletions storage/fs_windows.go
@@ -0,0 +1,64 @@
//go:build windows

package storage

import (
"os"
"syscall"
"time"
)

func GetFileTime(filename string) (time.Time, time.Time, time.Time, error) {
fi, err := os.Stat(filename)
if err != nil {
return time.Time{}, time.Time{}, time.Time{}, err
}

d := fi.Sys().(*syscall.Win32FileAttributeData)
cTime := time.Unix(0, d.CreationTime.Nanoseconds())
aTime := time.Unix(0, d.LastAccessTime.Nanoseconds())

mTime := fi.ModTime()

return aTime, mTime, cTime, nil
}

func SetFileTime(filename string, accessTime, modificationTime, creationTime time.Time) error {
var err error
if accessTime.IsZero() && modificationTime.IsZero() && creationTime.IsZero() {
// Nothing recorded in s3. Return fast.
return nil
} else if accessTime.IsZero() {
accessTime, _, _, err = GetFileTime(filename)
if err != nil {
return err
}
} else if modificationTime.IsZero() {
_, modificationTime, _, err = GetFileTime(filename)
if err != nil {
return err
}
} else if creationTime.IsZero() {
_, _, creationTime, err = GetFileTime(filename)
if err != nil {
return err
}
}

aft := syscall.NsecToFiletime(accessTime.UnixNano())
mft := syscall.NsecToFiletime(modificationTime.UnixNano())
cft := syscall.NsecToFiletime(creationTime.UnixNano())

fd, err := syscall.Open(filename, os.O_RDWR, 0775)
if err != nil {
return err
}
err = syscall.SetFileTime(fd, &cft, &aft, &mft)

defer syscall.Close(fd)

if err != nil {
return err
}
return nil
}
69 changes: 65 additions & 4 deletions storage/s3.go
Expand Up @@ -131,10 +131,42 @@ func (s *S3) Stat(ctx context.Context, url *url.URL) (*Object, error) {
mod := aws.TimeValue(output.LastModified)

obj := &Object{
URL: url,
Etag: strings.Trim(etag, `"`),
ModTime: &mod,
Size: aws.Int64Value(output.ContentLength),
URL: url,
Etag: strings.Trim(etag, `"`),
ModTime: &mod,
Size: aws.Int64Value(output.ContentLength),
CreateTime: &time.Time{},
AccessTime: &time.Time{},
}

cTimeS := aws.StringValue(output.Metadata["file-ctime"])
if cTimeS != "" {
ctime, err := strconv.ParseInt(cTimeS, 10, 64)
if err != nil {
return nil, err
}
creationTime := time.Unix(0, ctime)
obj.CreateTime = &creationTime
}

mTimeS := aws.StringValue(output.Metadata["file-mtime"])
if mTimeS != "" {
mtime, err := strconv.ParseInt(mTimeS, 10, 64)
if err != nil {
return nil, err
}
modificationTime := time.Unix(0, mtime)
obj.ModTime = &modificationTime
}

aTimeS := aws.StringValue(output.Metadata["file-atime"])
if aTimeS != "" {
atime, err := strconv.ParseInt(aTimeS, 10, 64)
if err != nil {
return nil, err
}
accessTime := time.Unix(0, atime)
obj.AccessTime = &accessTime
}

if s.noSuchUploadRetryCount > 0 {
Expand Down Expand Up @@ -389,6 +421,20 @@ func (s *S3) Copy(ctx context.Context, from, to *url.URL, metadata Metadata) err
input.Expires = aws.Time(t)
}

ctime := metadata.cTime()
if ctime != "" {
input.Metadata["file-ctime"] = aws.String(ctime)
}

mtime := metadata.mTime()
if ctime != "" {
input.Metadata["file-mtime"] = aws.String(mtime)
}

atime := metadata.aTime()
if ctime != "" {
input.Metadata["file-atime"] = aws.String(atime)
}
_, err := s.api.CopyObject(input)
return err
}
Expand Down Expand Up @@ -554,6 +600,21 @@ func (s *S3) Put(
input.Expires = aws.Time(t)
}

ctime := metadata.cTime()
if ctime != "" {
input.Metadata["file-ctime"] = aws.String(ctime)
}

mtime := metadata.mTime()
if ctime != "" {
input.Metadata["file-mtime"] = aws.String(mtime)
}

atime := metadata.aTime()
if ctime != "" {
input.Metadata["file-atime"] = aws.String(atime)
}

sseEncryption := metadata.SSE()
if sseEncryption != "" {
input.ServerSideEncryption = aws.String(sseEncryption)
Expand Down