Fixing ‘docker cp’ to allow new target file name in a host symlinked directory
| ... | ... |
@@ -9,6 +9,7 @@ import ( |
| 9 | 9 |
"os" |
| 10 | 10 |
"path/filepath" |
| 11 | 11 |
"runtime" |
| 12 |
+ "strings" |
|
| 12 | 13 |
"syscall" |
| 13 | 14 |
"testing" |
| 14 | 15 |
|
| ... | ... |
@@ -261,3 +262,58 @@ func TestTarUntarWithXattr(t *testing.T) {
|
| 261 | 261 |
} |
| 262 | 262 |
} |
| 263 | 263 |
} |
| 264 |
+ |
|
| 265 |
+func TestCopyInfoDestinationPathSymlink(t *testing.T) {
|
|
| 266 |
+ tmpDir, _ := getTestTempDirs(t) |
|
| 267 |
+ defer removeAllPaths(tmpDir) |
|
| 268 |
+ |
|
| 269 |
+ root := strings.TrimRight(tmpDir, "/") + "/" |
|
| 270 |
+ |
|
| 271 |
+ type FileTestData struct {
|
|
| 272 |
+ resource FileData |
|
| 273 |
+ file string |
|
| 274 |
+ expected CopyInfo |
|
| 275 |
+ } |
|
| 276 |
+ |
|
| 277 |
+ testData := []FileTestData{
|
|
| 278 |
+ //Create a directory: /tmp/archive-copy-test*/dir1 |
|
| 279 |
+ //Test will "copy" file1 to dir1 |
|
| 280 |
+ {resource: FileData{filetype: Dir, path: "dir1", permissions: 0740}, file: "file1", expected: CopyInfo{Path: root + "dir1/file1", Exists: false, IsDir: false}},
|
|
| 281 |
+ |
|
| 282 |
+ //Create a symlink directory to dir1: /tmp/archive-copy-test*/dirSymlink -> dir1 |
|
| 283 |
+ //Test will "copy" file2 to dirSymlink |
|
| 284 |
+ {resource: FileData{filetype: Symlink, path: "dirSymlink", contents: root + "dir1", permissions: 0600}, file: "file2", expected: CopyInfo{Path: root + "dirSymlink/file2", Exists: false, IsDir: false}},
|
|
| 285 |
+ |
|
| 286 |
+ //Create a file in tmp directory: /tmp/archive-copy-test*/file1 |
|
| 287 |
+ //Test to cover when the full file path already exists. |
|
| 288 |
+ {resource: FileData{filetype: Regular, path: "file1", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "file1", Exists: true}},
|
|
| 289 |
+ |
|
| 290 |
+ //Create a directory: /tmp/archive-copy*/dir2 |
|
| 291 |
+ //Test to cover when the full directory path already exists |
|
| 292 |
+ {resource: FileData{filetype: Dir, path: "dir2", permissions: 0740}, file: "", expected: CopyInfo{Path: root + "dir2", Exists: true, IsDir: true}},
|
|
| 293 |
+ |
|
| 294 |
+ //Create a symlink to a non-existent target: /tmp/archive-copy*/symlink1 -> noSuchTarget |
|
| 295 |
+ //Negative test to cover symlinking to a target that does not exit |
|
| 296 |
+ {resource: FileData{filetype: Symlink, path: "symlink1", contents: "noSuchTarget", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "noSuchTarget", Exists: false}},
|
|
| 297 |
+ |
|
| 298 |
+ //Create a file in tmp directory for next test: /tmp/existingfile |
|
| 299 |
+ {resource: FileData{filetype: Regular, path: "existingfile", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "existingfile", Exists: true}},
|
|
| 300 |
+ |
|
| 301 |
+ //Create a symlink to an existing file: /tmp/archive-copy*/symlink2 -> /tmp/existingfile |
|
| 302 |
+ //Test to cover when the parent directory of a new file is a symlink |
|
| 303 |
+ {resource: FileData{filetype: Symlink, path: "symlink2", contents: "existingfile", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "existingfile", Exists: true}},
|
|
| 304 |
+ } |
|
| 305 |
+ |
|
| 306 |
+ var dirs []FileData |
|
| 307 |
+ for _, data := range testData {
|
|
| 308 |
+ dirs = append(dirs, data.resource) |
|
| 309 |
+ } |
|
| 310 |
+ provisionSampleDir(t, tmpDir, dirs) |
|
| 311 |
+ |
|
| 312 |
+ for _, info := range testData {
|
|
| 313 |
+ p := filepath.Join(tmpDir, info.resource.path, info.file) |
|
| 314 |
+ ci, err := CopyInfoDestinationPath(p) |
|
| 315 |
+ assert.NoError(t, err) |
|
| 316 |
+ assert.Equal(t, info.expected, ci) |
|
| 317 |
+ } |
|
| 318 |
+} |
| ... | ... |
@@ -50,32 +50,35 @@ type FileData struct {
|
| 50 | 50 |
|
| 51 | 51 |
func createSampleDir(t *testing.T, root string) {
|
| 52 | 52 |
files := []FileData{
|
| 53 |
- {Regular, "file1", "file1\n", 0600},
|
|
| 54 |
- {Regular, "file2", "file2\n", 0666},
|
|
| 55 |
- {Regular, "file3", "file3\n", 0404},
|
|
| 56 |
- {Regular, "file4", "file4\n", 0600},
|
|
| 57 |
- {Regular, "file5", "file5\n", 0600},
|
|
| 58 |
- {Regular, "file6", "file6\n", 0600},
|
|
| 59 |
- {Regular, "file7", "file7\n", 0600},
|
|
| 60 |
- {Dir, "dir1", "", 0740},
|
|
| 61 |
- {Regular, "dir1/file1-1", "file1-1\n", 01444},
|
|
| 62 |
- {Regular, "dir1/file1-2", "file1-2\n", 0666},
|
|
| 63 |
- {Dir, "dir2", "", 0700},
|
|
| 64 |
- {Regular, "dir2/file2-1", "file2-1\n", 0666},
|
|
| 65 |
- {Regular, "dir2/file2-2", "file2-2\n", 0666},
|
|
| 66 |
- {Dir, "dir3", "", 0700},
|
|
| 67 |
- {Regular, "dir3/file3-1", "file3-1\n", 0666},
|
|
| 68 |
- {Regular, "dir3/file3-2", "file3-2\n", 0666},
|
|
| 69 |
- {Dir, "dir4", "", 0700},
|
|
| 70 |
- {Regular, "dir4/file3-1", "file4-1\n", 0666},
|
|
| 71 |
- {Regular, "dir4/file3-2", "file4-2\n", 0666},
|
|
| 72 |
- {Symlink, "symlink1", "target1", 0666},
|
|
| 73 |
- {Symlink, "symlink2", "target2", 0666},
|
|
| 74 |
- {Symlink, "symlink3", root + "/file1", 0666},
|
|
| 75 |
- {Symlink, "symlink4", root + "/symlink3", 0666},
|
|
| 76 |
- {Symlink, "dirSymlink", root + "/dir1", 0740},
|
|
| 77 |
- } |
|
| 53 |
+ {filetype: Regular, path: "file1", contents: "file1\n", permissions: 0600},
|
|
| 54 |
+ {filetype: Regular, path: "file2", contents: "file2\n", permissions: 0666},
|
|
| 55 |
+ {filetype: Regular, path: "file3", contents: "file3\n", permissions: 0404},
|
|
| 56 |
+ {filetype: Regular, path: "file4", contents: "file4\n", permissions: 0600},
|
|
| 57 |
+ {filetype: Regular, path: "file5", contents: "file5\n", permissions: 0600},
|
|
| 58 |
+ {filetype: Regular, path: "file6", contents: "file6\n", permissions: 0600},
|
|
| 59 |
+ {filetype: Regular, path: "file7", contents: "file7\n", permissions: 0600},
|
|
| 60 |
+ {filetype: Dir, path: "dir1", contents: "", permissions: 0740},
|
|
| 61 |
+ {filetype: Regular, path: "dir1/file1-1", contents: "file1-1\n", permissions: 01444},
|
|
| 62 |
+ {filetype: Regular, path: "dir1/file1-2", contents: "file1-2\n", permissions: 0666},
|
|
| 63 |
+ {filetype: Dir, path: "dir2", contents: "", permissions: 0700},
|
|
| 64 |
+ {filetype: Regular, path: "dir2/file2-1", contents: "file2-1\n", permissions: 0666},
|
|
| 65 |
+ {filetype: Regular, path: "dir2/file2-2", contents: "file2-2\n", permissions: 0666},
|
|
| 66 |
+ {filetype: Dir, path: "dir3", contents: "", permissions: 0700},
|
|
| 67 |
+ {filetype: Regular, path: "dir3/file3-1", contents: "file3-1\n", permissions: 0666},
|
|
| 68 |
+ {filetype: Regular, path: "dir3/file3-2", contents: "file3-2\n", permissions: 0666},
|
|
| 69 |
+ {filetype: Dir, path: "dir4", contents: "", permissions: 0700},
|
|
| 70 |
+ {filetype: Regular, path: "dir4/file3-1", contents: "file4-1\n", permissions: 0666},
|
|
| 71 |
+ {filetype: Regular, path: "dir4/file3-2", contents: "file4-2\n", permissions: 0666},
|
|
| 72 |
+ {filetype: Symlink, path: "symlink1", contents: "target1", permissions: 0666},
|
|
| 73 |
+ {filetype: Symlink, path: "symlink2", contents: "target2", permissions: 0666},
|
|
| 74 |
+ {filetype: Symlink, path: "symlink3", contents: root + "/file1", permissions: 0666},
|
|
| 75 |
+ {filetype: Symlink, path: "symlink4", contents: root + "/symlink3", permissions: 0666},
|
|
| 76 |
+ {filetype: Symlink, path: "dirSymlink", contents: root + "/dir1", permissions: 0740},
|
|
| 77 |
+ } |
|
| 78 |
+ provisionSampleDir(t, root, files) |
|
| 79 |
+} |
|
| 78 | 80 |
|
| 81 |
+func provisionSampleDir(t *testing.T, root string, files []FileData) {
|
|
| 79 | 82 |
now := time.Now() |
| 80 | 83 |
for _, info := range files {
|
| 81 | 84 |
p := path.Join(root, info.path) |
| ... | ... |
@@ -223,7 +223,7 @@ func CopyInfoDestinationPath(path string) (info CopyInfo, err error) {
|
| 223 | 223 |
// Ensure destination parent dir exists. |
| 224 | 224 |
dstParent, _ := SplitPathDirEntry(path) |
| 225 | 225 |
|
| 226 |
- parentDirStat, err := os.Lstat(dstParent) |
|
| 226 |
+ parentDirStat, err := os.Stat(dstParent) |
|
| 227 | 227 |
if err != nil {
|
| 228 | 228 |
return CopyInfo{}, err
|
| 229 | 229 |
} |