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

fix correcting timestamps for created destination dir after copy #214

Merged
merged 1 commit into from
Oct 28, 2024
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
35 changes: 27 additions & 8 deletions copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -79,8 +80,10 @@ func Copy(ctx context.Context, srcRoot, src, dstRoot, dst string, opts ...Opt) e
if err != nil {
return err
}
if err := MkdirAll(ensureDstPath, 0755, ci.Chown, ci.Utime); err != nil {
if createdDirs, err := MkdirAll(ensureDstPath, 0755, ci.Chown, ci.Utime); err != nil {
return err
} else {
defer fixCreatedParentDirs(createdDirs, ci.Utime)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to docker/buildx#2745 (comment), could we apply ownership as well?:

Suggested change
defer fixCreatedParentDirs(createdDirs, ci.Utime)
defer fixCreatedParentDirs(createdDirs, ci.Chown, ci.Utime)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that although target directories are created with requested timestamp, when files are written into them it changes the modified time. So the modified time needs to be reset again after the copy. This problem does not exist for chown.

There is another weird case in this code where target dirs are always created with default 755 mode, even if --chmod is set. Not sure if this is correct, but not directly part of this issue.

}
}

Expand Down Expand Up @@ -120,10 +123,11 @@ func Copy(ctx context.Context, srcRoot, src, dstRoot, dst string, opts ...Opt) e
if err != nil {
return err
}
dst, err := c.prepareTargetDir(srcFollowed, src, dst, ci.CopyDirContents)
dst, createdDirs, err := c.prepareTargetDir(srcFollowed, src, dst, ci.CopyDirContents)
if err != nil {
return err
}
defer fixCreatedParentDirs(createdDirs, ci.Utime)
if err := c.copy(ctx, srcFollowed, "", dst, false, patternmatcher.MatchInfo{}, patternmatcher.MatchInfo{}); err != nil {
return err
}
Expand All @@ -132,16 +136,16 @@ func Copy(ctx context.Context, srcRoot, src, dstRoot, dst string, opts ...Opt) e
return nil
}

func (c *copier) prepareTargetDir(srcFollowed, src, destPath string, copyDirContents bool) (string, error) {
func (c *copier) prepareTargetDir(srcFollowed, src, destPath string, copyDirContents bool) (string, []string, error) {
fiSrc, err := os.Lstat(srcFollowed)
if err != nil {
return "", err
return "", nil, err
}

fiDest, err := os.Stat(destPath)
if err != nil {
if !os.IsNotExist(err) {
return "", errors.Wrap(err, "failed to lstat destination path")
return "", nil, errors.Wrap(err, "failed to lstat destination path")
}
}

Expand All @@ -154,11 +158,14 @@ func (c *copier) prepareTargetDir(srcFollowed, src, destPath string, copyDirCont
if copyDirContents && fiSrc.IsDir() && fiDest == nil {
target = destPath
}
if err := MkdirAll(target, 0755, c.chown, c.utime); err != nil {
return "", err
var createdDirs []string
if dirs, err := MkdirAll(target, 0755, c.chown, c.utime); err != nil {
return "", nil, err
} else {
createdDirs = dirs
}

return destPath, nil
return destPath, createdDirs, nil
}

type User struct {
Expand Down Expand Up @@ -689,3 +696,15 @@ func rel(basepath, targpath string) (string, error) {
}
return filepath.Rel(basepath, targpath)
}

func fixCreatedParentDirs(dirs []string, tm *time.Time) error {
slices.Reverse(dirs)
for _, d := range dirs {
if tm != nil {
if err := Utimes(d, tm); err != nil {
return err
}
}
}
return nil
}
22 changes: 22 additions & 0 deletions copy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,28 @@ func TestCopyIncludeExclude(t *testing.T) {
}
}

func TestCopyFileWithPathTimestamp(t *testing.T) {
timestamp := time.Unix(0, 0)
apply := fstest.Apply(
fstest.CreateDir("/foo/", 0755),
fstest.CreateFile("/foo/bar", []byte{}, 0644),
)

t1 := t.TempDir()
t2 := t.TempDir()

require.NoError(t, apply.Apply(t1))
require.NoError(t, Copy(context.TODO(), t1, "/foo/bar", t2, "/test1/test2/test3/bar", WithCopyInfo(CopyInfo{
CopyDirContents: true,
Utime: &timestamp,
})))

for _, s := range []string{"/test1/test2/test3/bar", "/test1/test2/test3", "/test1/test2", "/test1"} {
stat, _ := os.Stat(filepath.Join(t2, s))
require.Equal(t, timestamp, stat.ModTime(), "path: %s", s)
}
}

type changeCollector struct {
changes []string
}
Expand Down
25 changes: 14 additions & 11 deletions copy/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

// MkdirAll is forked os.MkdirAll
func MkdirAll(path string, perm os.FileMode, user Chowner, tm *time.Time) error {
func MkdirAll(path string, perm os.FileMode, user Chowner, tm *time.Time) ([]string, error) {
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
dir, err := os.Stat(path)
if err == nil {
if dir.IsDir() {
return nil
return nil, nil
}
return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
return nil, &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
}

// Slow path: make sure parent exists and then call Mkdir for path.
Expand All @@ -28,17 +28,19 @@ func MkdirAll(path string, perm os.FileMode, user Chowner, tm *time.Time) error
j--
}

var createdDirs []string

if j > 1 {
// Create parent.
err = MkdirAll(fixRootDirectory(path[:j-1]), perm, user, tm)
createdDirs, err = MkdirAll(fixRootDirectory(path[:j-1]), perm, user, tm)
if err != nil {
return err
return nil, err
}
}

dir, err1 := os.Lstat(path)
if err1 == nil && dir.IsDir() {
return nil
return createdDirs, nil
}

// Parent now exists; invoke Mkdir and use its result.
Expand All @@ -48,18 +50,19 @@ func MkdirAll(path string, perm os.FileMode, user Chowner, tm *time.Time) error
// double-checking that directory doesn't exist.
dir, err1 := os.Lstat(path)
if err1 == nil && dir.IsDir() {
return nil
return createdDirs, nil
}
return err
return nil, err
}
createdDirs = append(createdDirs, path)

if err := Chown(path, nil, user); err != nil {
return err
return nil, err
}

if err := Utimes(path, tm); err != nil {
return err
return nil, err
}

return nil
return createdDirs, nil
}