Skip to content

Commit

Permalink
Merge pull request #324 from SimonXming/fix-emptied-dir-permission
Browse files Browse the repository at this point in the history
Uid&Gid info lost for emptied directory while unpack image
Rui Yang authored Aug 25, 2022
2 parents 59651be + c01c1eb commit 1989dd3
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions commands/unpack.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/concourse/go-archive/tarfs"
@@ -119,6 +120,12 @@ func extractLayer(dest string, layer v1.Layer, bar *mpb.Bar, chown bool) error {
return err
}

if runtime.GOOS != "windows" && chown {
err = os.Lchown(dir, hdr.Uid, hdr.Gid)
if err != nil {
return err
}
}
continue
} else if strings.HasPrefix(base, whiteoutPrefix) {
// layer has marked a file as deleted
12 changes: 12 additions & 0 deletions in_test.go
Original file line number Diff line number Diff line change
@@ -229,6 +229,18 @@ var _ = Describe("In", func() {
stat, err = os.Stat(rootfsPath("top-dir-3", "nested-dir"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.IsDir()).To(BeTrue())

stat, err = os.Stat(rootfsPath("top-dir-4"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.IsDir()).To(BeTrue())

if os.Geteuid() != 0 {
Skip("Must be run as root to validate file ownership")
}
sys, ok := stat.Sys().(*syscall.Stat_t)
Expect(ok).To(BeTrue())
Expect(sys.Uid).To(Equal(uint32(1000)))
Expect(sys.Gid).To(Equal(uint32(1000)))
})
})

2 changes: 2 additions & 0 deletions testdata/whiteout/Dockerfile
Original file line number Diff line number Diff line change
@@ -17,10 +17,12 @@ RUN touch top-dir-2/nested-dir-gone/nested-file-gone
RUN rm -rf top-dir-2
RUN mkdir -p top-dir-3/nested-dir-gone
RUN rm -r top-dir-3 && mkdir -p top-dir-3/nested-dir
RUN mkdir top-dir-4 && chown 1000:1000 top-dir-4

# resulting file tree should be:
# /top-dir-1/nested-file
# /top-dir-1/nested-dir/file-here
# /top-dir-1/nested-dir/file-recreated
# /top-dir-1/nested-dir/file-then-dir
# /top-dir-3/nested-dir
# /top-dir-4

0 comments on commit 1989dd3

Please sign in to comment.