Skip to content

Commit

Permalink
Merge pull request #3738 from nalind/is-not-a-directory-1.23
Browse files Browse the repository at this point in the history
[release-1.23] copier.Put: check for is-not-a-directory using lstat, not stat; RemoveAll possibly-directories
  • Loading branch information
openshift-merge-robot authored Jan 24, 2022
2 parents 867c1bc + 4a83110 commit 4b73e0d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
14 changes: 7 additions & 7 deletions copier/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
// only check the length if there wasn't an error, which we'll
// check along with errors for other types of entries
if err == nil && written != hdr.Size {
return errors.Errorf("copier: put: error creating %q: incorrect length (%d != %d)", path, written, hdr.Size)
return errors.Errorf("copier: put: error creating regular file %q: incorrect length (%d != %d)", path, written, hdr.Size)
}
case tar.TypeLink:
var linkTarget string
Expand All @@ -1681,7 +1681,7 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
break
}
}
if err = os.Remove(path); err == nil {
if err = os.RemoveAll(path); err == nil {
err = os.Link(linkTarget, path)
}
}
Expand All @@ -1696,7 +1696,7 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
break
}
}
if err = os.Remove(path); err == nil {
if err = os.RemoveAll(path); err == nil {
err = os.Symlink(filepath.FromSlash(hdr.Linkname), filepath.FromSlash(path))
}
}
Expand All @@ -1711,7 +1711,7 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
break
}
}
if err = os.Remove(path); err == nil {
if err = os.RemoveAll(path); err == nil {
err = mknod(path, chrMode(0600), int(mkdev(devMajor, devMinor)))
}
}
Expand All @@ -1726,14 +1726,14 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
break
}
}
if err = os.Remove(path); err == nil {
if err = os.RemoveAll(path); err == nil {
err = mknod(path, blkMode(0600), int(mkdev(devMajor, devMinor)))
}
}
case tar.TypeDir:
if err = os.Mkdir(path, 0700); err != nil && os.IsExist(err) {
var st os.FileInfo
if st, err = os.Stat(path); err == nil && !st.IsDir() {
if st, err = os.Lstat(path); err == nil && !st.IsDir() {
// it's not a directory, so remove it and mkdir
if err = os.Remove(path); err == nil {
err = os.Mkdir(path, 0700)
Expand All @@ -1758,7 +1758,7 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
break
}
}
if err = os.Remove(path); err == nil {
if err = os.RemoveAll(path); err == nil {
err = mkfifo(path, 0600)
}
}
Expand Down
4 changes: 4 additions & 0 deletions copier/copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,12 @@ func testPut(t *testing.T) {
for _, typeFlag := range []byte{tar.TypeReg, tar.TypeLink, tar.TypeSymlink, tar.TypeChar, tar.TypeBlock, tar.TypeFifo} {
t.Run(fmt.Sprintf("overwrite=%v,type=%c", overwrite, typeFlag), func(t *testing.T) {
archive := makeArchiveSlice([]tar.Header{
{Name: "target", Typeflag: tar.TypeSymlink, Mode: 0755, Linkname: "target", ModTime: testDate},
{Name: "target", Typeflag: tar.TypeDir, Mode: 0755, ModTime: testDate},
{Name: "target", Typeflag: tar.TypeSymlink, Mode: 0755, Linkname: "target", ModTime: testDate},
{Name: "target", Typeflag: tar.TypeReg, Size: 123, Mode: 0755, ModTime: testDate},
{Name: "test", Typeflag: tar.TypeDir, Size: 0, Mode: 0755, ModTime: testDate},
{Name: "test/content", Typeflag: tar.TypeReg, Size: 0, Mode: 0755, ModTime: testDate},
{Name: "test", Typeflag: typeFlag, Size: 0, Mode: 0755, Linkname: "target", ModTime: testDate},
})
tmp, err := ioutil.TempDir("", "copier-test-")
Expand Down
5 changes: 5 additions & 0 deletions tests/conformance/conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2870,4 +2870,9 @@ var internalTestCases = []testCase{
contextDir: "dockerignore/exceptions-skip",
fsSkip: []string{"(dir):volume:mtime"},
},

{
name: "replace-symlink-with-directory",
contextDir: "replace/symlink-with-directory",
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM scratch
COPY tree1/ /
COPY tree2/ /
Empty file.

0 comments on commit 4b73e0d

Please sign in to comment.