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

receiver: re-apply directory timestamps after copying #139

Merged
merged 1 commit into from
Nov 10, 2022
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
40 changes: 29 additions & 11 deletions diskwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"hash"
"io"
gofs "io/fs"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -33,10 +34,11 @@ type DiskWriter struct {
opt DiskWriterOpt
dest string

ctx context.Context
cancel func()
eg *errgroup.Group
filter FilterFunc
ctx context.Context
cancel func()
eg *errgroup.Group
filter FilterFunc
dirModTimes map[string]int64
}

func NewDiskWriter(ctx context.Context, dest string, opt DiskWriterOpt) (*DiskWriter, error) {
Expand All @@ -51,17 +53,32 @@ func NewDiskWriter(ctx context.Context, dest string, opt DiskWriterOpt) (*DiskWr
eg, ctx := errgroup.WithContext(ctx)

return &DiskWriter{
opt: opt,
dest: dest,
eg: eg,
ctx: ctx,
cancel: cancel,
filter: opt.Filter,
opt: opt,
dest: dest,
eg: eg,
ctx: ctx,
cancel: cancel,
filter: opt.Filter,
dirModTimes: map[string]int64{},
}, nil
}

func (dw *DiskWriter) Wait(ctx context.Context) error {
return dw.eg.Wait()
if err := dw.eg.Wait(); err != nil {
return err
}
return filepath.WalkDir(dw.dest, func(path string, d gofs.DirEntry, prevErr error) error {
if prevErr != nil {
return prevErr
}
if !d.IsDir() {
return nil
}
if mtime, ok := dw.dirModTimes[path]; ok {
return chtimes(path, mtime)
}
return nil
})
}

func (dw *DiskWriter) HandleChange(kind ChangeKind, p string, fi os.FileInfo, err error) (retErr error) {
Expand Down Expand Up @@ -147,6 +164,7 @@ func (dw *DiskWriter) HandleChange(kind ChangeKind, p string, fi os.FileInfo, er
if err := os.Mkdir(newPath, fi.Mode()); err != nil {
return errors.Wrapf(err, "failed to create dir %s", newPath)
}
dw.dirModTimes[destPath] = statCopy.ModTime
case fi.Mode()&os.ModeDevice != 0 || fi.Mode()&os.ModeNamedPipe != 0:
if err := handleTarTypeBlockCharFifo(newPath, &statCopy); err != nil {
return errors.Wrapf(err, "failed to create device %s", newPath)
Expand Down
36 changes: 36 additions & 0 deletions receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,42 @@ func TestCopyWithSubDir(t *testing.T) {
assert.Equal(t, "data1", string(dt))
}

func TestCopyDirectoryTimestamps(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD foo dir",
"ADD foo/bar file data1",
}))
assert.NoError(t, err)
defer os.RemoveAll(d)

timestamp := time.Unix(0, 0)
require.NoError(t, os.Chtimes(filepath.Join(d, "foo"), timestamp, timestamp))

dest := t.TempDir()

eg, ctx := errgroup.WithContext(context.Background())
s1, s2 := sockPairProto(ctx)

eg.Go(func() error {
defer s1.(*fakeConnProto).closeSend()
return Send(ctx, s1, NewFS(d, nil), nil)
})
eg.Go(func() error {
return Receive(ctx, s2, dest, ReceiveOpt{})
})

err = eg.Wait()
assert.NoError(t, err)

dt, err := os.ReadFile(filepath.Join(dest, "foo/bar"))
assert.NoError(t, err)
assert.Equal(t, "data1", string(dt))

stat, err := os.Stat(filepath.Join(dest, "foo"))
require.NoError(t, err)
assert.Equal(t, timestamp, stat.ModTime())
}

func TestCopySwitchDirToFile(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD foo file data1",
Expand Down