diff --git a/pkg/sentry/fsimpl/tmpfs/tar.go b/pkg/sentry/fsimpl/tmpfs/tar.go index 3fac494a7b..730145f1d7 100644 --- a/pkg/sentry/fsimpl/tmpfs/tar.go +++ b/pkg/sentry/fsimpl/tmpfs/tar.go @@ -77,8 +77,10 @@ func (fs *filesystem) readFromTar(ctx context.Context, tr *tar.Reader) error { if n != header.Size { return fmt.Errorf("failed to read all file content, got %d bytes, want %d", n, header.Size) } + if header.Size > 0 { + fileToContent[header.Name] = &buffer + } fileToHeader[header.Name] = header - fileToContent[header.Name] = &buffer case tar.TypeFifo, tar.TypeBlock, tar.TypeChar: fileToHeader[header.Name] = header case tar.TypeSymlink: diff --git a/runsc/container/container_test.go b/runsc/container/container_test.go index 430e53f19d..23b3b59a2a 100644 --- a/runsc/container/container_test.go +++ b/runsc/container/container_test.go @@ -4035,11 +4035,10 @@ func TestTarRootfsUpperLayer(t *testing.T) { if err := cont.Start(conf); err != nil { t.Fatalf("error starting container: %v", err) } - // Exec the command in the container. execArgs := &control.ExecArgs{ Filename: app, - Argv: []string{app, "fsTreeCreate", "--depth=3", "--file-per-level=2", "--file-size=1470", "--create-symlink"}, + Argv: []string{app, "fsTreeCreate", "--depth=3", "--file-per-level=2", "--file-size=1470", "--create-symlink", "--add-empty-files"}, } if ws, err := cont.executeSync(conf, execArgs); err != nil { t.Fatalf("error exec'ing: %v", err) diff --git a/test/cmd/test_app/main.go b/test/cmd/test_app/main.go index 161027ac18..7c2faf7eaf 100644 --- a/test/cmd/test_app/main.go +++ b/test/cmd/test_app/main.go @@ -67,6 +67,7 @@ type fsTreeCreator struct { fileSize uint targetDir string createSymlink bool + addEmptyFiles bool } // Name implements subcommands.Command.Name. @@ -91,6 +92,7 @@ func (c *fsTreeCreator) SetFlags(f *flag.FlagSet) { f.UintVar(&c.fileSize, "file-size", 4096, "size of each file") f.StringVar(&c.targetDir, "target-dir", "/", "directory under which to create the filesystem tree") f.BoolVar(&c.createSymlink, "create-symlink", false, "create symlinks other than the first file per level") + f.BoolVar(&c.addEmptyFiles, "add-empty-files", false, "add empty file to each level") } // Execute implements subcommands.Command.Execute. @@ -117,6 +119,12 @@ func (c *fsTreeCreator) Execute(ctx context.Context, f *flag.FlagSet, args ...an } } } + if c.addEmptyFiles { + emptyPath := filepath.Join(curDir, fmt.Sprintf("empty%d", i)) + if err := os.WriteFile(emptyPath, nil, 0666); err != nil { + log.Fatalf("error writing empty file %q: %v", emptyPath, err) + } + } nextDir := filepath.Join(curDir, "dir") if err := os.Mkdir(nextDir, 0777); err != nil { log.Fatalf("error creating directory %q: %v", nextDir, err)