Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove symlink, not target, in FileOp.Rm. #2474

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestIntegration(t *testing.T) {
testPullZstdImage,
testMergeOp,
testMergeOpCache,
testRmSymlink,
}, mirrors)

integration.Run(t, []integration.Test{
Expand Down Expand Up @@ -3824,6 +3825,41 @@ func testSourceMapFromRef(t *testing.T, sb integration.Sandbox) {
require.Equal(t, int32(1), srcs[0].Ranges[0].Start.Character)
}

func testRmSymlink(t *testing.T, sb integration.Sandbox) {
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

// Test that if FileOp.Rm is called on a symlink, then
// the symlink is removed rather than the target
mnt := llb.Image("alpine").
Run(llb.Shlex("touch /mnt/target")).
AddMount("/mnt", llb.Scratch())

mnt = llb.Image("alpine").
Run(llb.Shlex("ln -s target /mnt/link")).
AddMount("/mnt", mnt)

def, err := mnt.File(llb.Rm("link")).Marshal(sb.Context())
require.NoError(t, err)

destDir, err := ioutil.TempDir("", "buildkit")
require.NoError(t, err)
defer os.RemoveAll(destDir)

_, err = c.Solve(sb.Context(), def, SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterLocal,
OutputDir: destDir,
},
},
}, nil)
require.NoError(t, err)

require.NoError(t, fstest.CheckDirectoryEqualWithApplier(destDir, fstest.CreateFile("target", nil, 0644)))
}

func testProxyEnv(t *testing.T, sb integration.Sandbox) {
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
Expand Down
8 changes: 7 additions & 1 deletion solver/llbsolver/file/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,16 @@ func rm(ctx context.Context, d string, action pb.FileActionRm) error {
}

func rmPath(root, src string, allowNotFound bool) error {
p, err := fs.RootPath(root, filepath.Join("/", src))
src = filepath.Clean(src)
dir, base := filepath.Split(src)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean() the src for safety before. And validate base not empty?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if base == "" {
return errors.New("rmPath: invalid empty path")
}
dir, err := fs.RootPath(root, filepath.Join("/", dir))
if err != nil {
return err
}
p := filepath.Join(dir, base)

if err := os.RemoveAll(p); err != nil {
if errors.Is(err, os.ErrNotExist) && allowNotFound {
Expand Down
7 changes: 7 additions & 0 deletions solver/pb/caps.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const (
CapFileBase apicaps.CapID = "file.base"
CapFileRmWildcard apicaps.CapID = "file.rm.wildcard"
CapFileCopyIncludeExcludePatterns apicaps.CapID = "file.copy.includeexcludepatterns"
CapFileRmNoFollowSymlink apicaps.CapID = "file.rm.nofollowsymlink"

CapConstraints apicaps.CapID = "constraints"
CapPlatform apicaps.CapID = "platform"
Expand Down Expand Up @@ -328,6 +329,12 @@ func init() {
Status: apicaps.CapStatusExperimental,
})

Caps.Init(apicaps.Cap{
ID: CapFileRmNoFollowSymlink,
Enabled: true,
Status: apicaps.CapStatusExperimental,
})

Caps.Init(apicaps.Cap{
ID: CapFileCopyIncludeExcludePatterns,
Enabled: true,
Expand Down