Skip to content

Commit

Permalink
Implement file action remove for wildcards
Browse files Browse the repository at this point in the history
Signed-off-by: Edgar Lee <[email protected]>
  • Loading branch information
hinshun committed Oct 30, 2019
1 parent 50a406a commit dc64d2a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
58 changes: 58 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestIntegration(t *testing.T) {
testRelativeWorkDir,
testFileOpMkdirMkfile,
testFileOpCopyRm,
testFileOpRmWildcard,
testCallDiskUsage,
testBuildMultiMount,
testBuildHTTPSource,
Expand Down Expand Up @@ -1046,6 +1047,63 @@ func testFileOpCopyRm(t *testing.T, sb integration.Sandbox) {

}

func testFileOpRmWildcard(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)
c, err := New(context.TODO(), sb.Address())
require.NoError(t, err)
defer c.Close()

dir, err := tmpdir(
fstest.CreateDir("foo", 0700),
fstest.CreateDir("bar", 0700),
fstest.CreateFile("foo/target", []byte("foo0"), 0600),
fstest.CreateFile("bar/target", []byte("bar0"), 0600),
fstest.CreateFile("bar/remaining", []byte("bar1"), 0600),
)
require.NoError(t, err)
defer os.RemoveAll(dir)

st := llb.Scratch().File(
llb.Copy(llb.Local("mylocal"), "foo", "foo").
Copy(llb.Local("mylocal"), "bar", "bar"),
).File(
llb.Rm("*/target", llb.WithAllowWildcard(true)),
)
def, err := st.Marshal()
require.NoError(t, err)

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

_, err = c.Solve(context.TODO(), def, SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterLocal,
OutputDir: destDir,
},
},
LocalDirs: map[string]string{
"mylocal": dir,
},
}, nil)
require.NoError(t, err)

dt, err := ioutil.ReadFile(filepath.Join(destDir, "bar/remaining"))
require.NoError(t, err)
require.Equal(t, []byte("bar1"), dt)

fi, err := os.Stat(filepath.Join(destDir, "foo"))
require.NoError(t, err)
require.Equal(t, true, fi.IsDir())

_, err = os.Stat(filepath.Join(destDir, "foo/target"))
require.Equal(t, true, os.IsNotExist(err))

_, err = os.Stat(filepath.Join(destDir, "bar/target"))
require.Equal(t, true, os.IsNotExist(err))
}

func testCallDiskUsage(t *testing.T, sb integration.Sandbox) {
c, err := New(context.TODO(), sb.Address())
require.NoError(t, err)
Expand Down
33 changes: 29 additions & 4 deletions solver/llbsolver/file/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func mapUser(user *copy.ChownOpt, idmap *idtools.IdentityMapping) (*copy.ChownOp
}

func mkdir(ctx context.Context, d string, action pb.FileActionMkDir, user *copy.ChownOpt, idmap *idtools.IdentityMapping) error {
p, err := fs.RootPath(d, filepath.Join(filepath.Join("/", action.Path)))
p, err := rootPath(d, action.Path)
if err != nil {
return err
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func mkdir(ctx context.Context, d string, action pb.FileActionMkDir, user *copy.
}

func mkfile(ctx context.Context, d string, action pb.FileActionMkFile, user *copy.ChownOpt, idmap *idtools.IdentityMapping) error {
p, err := fs.RootPath(d, filepath.Join(filepath.Join("/", action.Path)))
p, err := rootPath(d, action.Path)
if err != nil {
return err
}
Expand All @@ -100,7 +100,28 @@ func mkfile(ctx context.Context, d string, action pb.FileActionMkFile, user *cop
}

func rm(ctx context.Context, d string, action pb.FileActionRm) error {
p, err := fs.RootPath(d, filepath.Join(filepath.Join("/", action.Path)))
if action.AllowWildcard {
src := cleanPath(action.Path)
m, err := copy.ResolveWildcards(d, src, false)
if err != nil {
return err
}

for _, s := range m {
p, err := rootPath(d, s)
if err != nil {
return err
}

if err := os.RemoveAll(p); err != nil {
return err
}
}

return nil
}

p, err := rootPath(d, action.Path)
if err != nil {
return err
}
Expand All @@ -120,7 +141,7 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
destPath := cleanPath(action.Dest)

if !action.CreateDestPath {
p, err := fs.RootPath(dest, filepath.Join(filepath.Join("/", action.Dest)))
p, err := rootPath(dest, action.Dest)
if err != nil {
return err
}
Expand Down Expand Up @@ -204,6 +225,10 @@ func cleanPath(s string) string {
return s2
}

func rootPath(root, src string) (string, error) {
return fs.RootPath(root, filepath.Join(filepath.Join("/", src)))
}

type Backend struct {
}

Expand Down
9 changes: 8 additions & 1 deletion solver/pb/caps.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const (
CapExecMountSSH apicaps.CapID = "exec.mount.ssh"
CapExecCgroupsMounted apicaps.CapID = "exec.cgroup"

CapFileBase apicaps.CapID = "file.base"
CapFileBase apicaps.CapID = "file.base"
CapFileRmWildcard apicaps.CapID = "file.rm.wildcard"

CapConstraints apicaps.CapID = "constraints"
CapPlatform apicaps.CapID = "platform"
Expand Down Expand Up @@ -252,6 +253,12 @@ func init() {
},
})

Caps.Init(apicaps.Cap{
ID: CapFileRmWildcard,
Enabled: true,
Status: apicaps.CapStatusPrerelease,
})

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

0 comments on commit dc64d2a

Please sign in to comment.