Skip to content

Commit

Permalink
Fix copying ownership (GoogleContainerTools#1725)
Browse files Browse the repository at this point in the history
* fix uid, gid overriding

* fix ownership for staging building

* add integration test

* add check for ignored files

* improve errors
  • Loading branch information
kvaps authored and gcalmettes-fbox committed Dec 24, 2021
1 parent 31214af commit 819bc70
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
20 changes: 20 additions & 0 deletions integration/dockerfiles-with-context/issue-1315/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM alpine:3.11 as builder

RUN mkdir -p /myapp/somedir \
&& touch /myapp/somedir/somefile \
&& chown 123:123 /myapp/somedir \
&& chown 321:321 /myapp/somedir/somefile

FROM alpine:3.11
COPY --from=builder /myapp /myapp
RUN printf "%s\n" \
"0 0 /myapp/" \
"123 123 /myapp/somedir" \
"321 321 /myapp/somedir/somefile" \
> /tmp/expected \
&& stat -c "%u %g %n" \
/myapp/ \
/myapp/somedir \
/myapp/somedir/somefile \
> /tmp/got \
&& diff -u /tmp/got /tmp/expected
61 changes: 59 additions & 2 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func CopyDir(src, dest string, context FileContext, uid, gid int64) ([]string, e
logrus.Tracef("Creating directory %s", destPath)

mode := fi.Mode()
uid, gid = DetermineTargetFileOwnership(fi, uid, gid)
uid, gid := DetermineTargetFileOwnership(fi, uid, gid)
if err := mkdirAllWithPermissions(destPath, mode, uid, gid); err != nil {
return nil, err
}
Expand Down Expand Up @@ -901,7 +901,64 @@ func CopyFileOrSymlink(src string, destDir string, root string) error {
}
return os.Symlink(link, destFile)
}
return otiai10Cpy.Copy(src, destFile)
err := otiai10Cpy.Copy(src, destFile)
if err != nil {
return errors.Wrap(err, "copying file")
}
err = CopyOwnership(src, destDir)
if err != nil {
return errors.Wrap(err, "copying ownership")
}
return nil
}

// CopyOwnership copies the file or directory ownership recursively at src to dest
func CopyOwnership(src string, destDir string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if IsSymlink(info) {
return nil
}
relPath, err := filepath.Rel(filepath.Dir(src), path)
if err != nil {
return err
}
destPath := filepath.Join(destDir, relPath)

if CheckIgnoreList(src) && CheckIgnoreList(destPath) {
if !isExist(destPath) {
logrus.Debugf("Path %s ignored, but not exists", destPath)
return nil
}
if info.IsDir() {
return filepath.SkipDir
}
logrus.Debugf("Not copying ownership for %s, as it's ignored", destPath)
return nil
}
if CheckIgnoreList(destDir) && CheckIgnoreList(path) {
if !isExist(path) {
logrus.Debugf("Path %s ignored, but not exists", path)
return nil
}
if info.IsDir() {
return filepath.SkipDir
}
logrus.Debugf("Not copying ownership for %s, as it's ignored", path)
return nil
}

info, err = os.Stat(path)
if err != nil {
return errors.Wrap(err, "reading ownership")
}
stat := info.Sys().(*syscall.Stat_t)
err = os.Chown(destPath, int(stat.Uid), int(stat.Gid))

return nil
})
}

func createParentDirectory(path string) error {
Expand Down

0 comments on commit 819bc70

Please sign in to comment.