Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit f55a417

Browse files
committed
pkg/archive:CopyTo(): fix for long dest filename
As reported in docker/for-linux/issues/484, since Docker 18.06 docker cp with a destination file name fails with the following error: > archive/tar: cannot encode header: Format specifies USTAR; and USTAR cannot encode Name="a_very_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_long_filename_that_is_101_characters" The problem is caused by changes in Go 1.10 archive/tar, which mis-guesses the tar stream format as USTAR (rather than PAX), which, in turn, leads to inability to specify file names longer than 100 characters. This tar stream is sent by TarWithOptions() (which, since we switched to Go 1.10, explicitly sets format=PAX for every file, see FileInfoHeader(), and before Go 1.10 it was PAX by default). Unfortunately, the receiving side, RebaseArchiveEntries(), which calls tar.Next(), mistakenly guesses header format as USTAR, which leads to the above error. The fix is easy: set the format to PAX in RebaseArchiveEntries() where we read the tar stream and change the file name. A unit test is added to prevent future regressions. NOTE this code is not used by dockerd, but rather but docker cli (also possibly other clients), so this needs to be re-vendored to cli in order to take effect. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent 8d7889e commit f55a417

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

pkg/archive/copy.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ func RebaseArchiveEntries(srcContent io.Reader, oldBase, newBase string) io.Read
336336
return
337337
}
338338

339+
// srcContent tar stream, as served by TarWithOptions(), is
340+
// definitely in PAX format, but tar.Next() mistakenly guesses it
341+
// as USTAR, which creates a problem: if the newBase is >100
342+
// characters long, WriteHeader() returns an error like
343+
// "archive/tar: cannot encode header: Format specifies USTAR; and USTAR cannot encode Name=...".
344+
//
345+
// To fix, set the format to PAX here. See docker/for-linux issue #484.
346+
hdr.Format = tar.FormatPAX
339347
hdr.Name = strings.Replace(hdr.Name, oldBase, newBase, 1)
340348
if hdr.Typeflag == tar.TypeLink {
341349
hdr.Linkname = strings.Replace(hdr.Linkname, oldBase, newBase, 1)

pkg/archive/copy_unix_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,30 @@ func TestCopyErrDstNotDir(t *testing.T) {
257257
}
258258
}
259259

260+
// Test to check if CopyTo works with a long (>100 characters) destination file name.
261+
// This is a regression (see https://github.com/docker/for-linux/issues/484).
262+
func TestCopyLongDstFilename(t *testing.T) {
263+
const longName = "a_very_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_long_filename_that_is_101_characters"
264+
tmpDirA, tmpDirB := getTestTempDirs(t)
265+
defer removeAllPaths(tmpDirA, tmpDirB)
266+
267+
// Load A with some sample files and directories.
268+
createSampleDir(t, tmpDirA)
269+
270+
srcInfo := CopyInfo{Path: filepath.Join(tmpDirA, "file1"), Exists: true, IsDir: false}
271+
272+
content, err := TarResource(srcInfo)
273+
if err != nil {
274+
t.Fatalf("unexpected error %T: %s", err, err)
275+
}
276+
defer content.Close()
277+
278+
err = CopyTo(content, srcInfo, filepath.Join(tmpDirB, longName))
279+
if err != nil {
280+
t.Fatalf("unexpected error %T: %s", err, err)
281+
}
282+
}
283+
260284
// Possibilities are reduced to the remaining 10 cases:
261285
//
262286
// case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action

0 commit comments

Comments
 (0)