Skip to content

Commit a84f713

Browse files
committed
ContentValue root must be absolute.
Fixes #8.
1 parent 30148f3 commit a84f713

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎internal/scripts/scripts.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ const (
8686
)
8787

8888
func (c *ContentValue) RealPath(path string, what Check) (string, error) {
89+
if !filepath.IsAbs(c.RootDir) {
90+
return "", fmt.Errorf("internal error: content defined with relative root: %s", c.RootDir)
91+
}
8992
if !filepath.IsAbs(path) {
9093
return "", fmt.Errorf("content path must be absolute, got: %s", path)
9194
}

‎internal/scripts/scripts_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,9 @@ func (s *S) TestScripts(c *C) {
233233
c.Assert(testutil.TreeDump(rootDir), DeepEquals, test.result)
234234
}
235235
}
236+
237+
func (s *S) TestContentRelative(c *C) {
238+
content := scripts.ContentValue{RootDir: "foo"}
239+
_, err := content.RealPath("/bar", scripts.CheckNone)
240+
c.Assert(err, ErrorMatches, "internal error: content defined with relative root: foo")
241+
}

‎internal/slicer/slicer.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ func Run(options *RunOptions) error {
2929
extract := make(map[string]map[string][]deb.ExtractInfo)
3030
pathInfos := make(map[string]setup.PathInfo)
3131

32-
targetDir := filepath.Clean(options.TargetDir)
3332
release := options.Selection.Release
33+
targetDir := filepath.Clean(options.TargetDir)
34+
targetDirAbs := targetDir
35+
if !filepath.IsAbs(targetDirAbs) {
36+
dir, err := os.Getwd()
37+
if err != nil {
38+
return fmt.Errorf("cannot obtain current directory: %w", err)
39+
}
40+
targetDirAbs = filepath.Join(dir, targetDir)
41+
}
3442

3543
// Build information to process the selection.
3644
for _, slice := range options.Selection.Slices {
@@ -176,7 +184,7 @@ func Run(options *RunOptions) error {
176184
return nil
177185
}
178186
content := &scripts.ContentValue{
179-
RootDir: targetDir,
187+
RootDir: targetDirAbs,
180188
CheckWrite: checkWrite,
181189
CheckRead: checkRead,
182190
}

‎internal/slicer/slicer_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type slicerTest struct {
2020
summary string
2121
release map[string]string
2222
slices []setup.SliceKey
23+
hackopt func(c *C, opts *slicer.RunOptions)
2324
result map[string]string
2425
error string
2526
}
@@ -263,6 +264,26 @@ var slicerTests = []slicerTest{{
263264
content.read("/usr/bin/hello")
264265
`,
265266
},
267+
}, {
268+
summary: "Relative content root directory must not error",
269+
slices: []setup.SliceKey{{"base-files", "myslice"}},
270+
release: map[string]string{
271+
"slices/mydir/base-files.yaml": `
272+
package: base-files
273+
slices:
274+
myslice:
275+
contents:
276+
/tmp/file1: {text: data1}
277+
mutate: |
278+
content.read("/tmp/file1")
279+
`,
280+
},
281+
hackopt: func(c *C, opts *slicer.RunOptions) {
282+
dir, err := os.Getwd()
283+
c.Assert(err, IsNil)
284+
opts.TargetDir, err = filepath.Rel(dir, opts.TargetDir)
285+
c.Assert(err, IsNil)
286+
},
266287
}}
267288

268289
const defaultChiselYaml = `
@@ -326,6 +347,9 @@ func (s *S) TestRun(c *C) {
326347
Archives: archives,
327348
TargetDir: targetDir,
328349
}
350+
if test.hackopt != nil {
351+
test.hackopt(c, &options)
352+
}
329353
err = slicer.Run(&options)
330354
if test.error == "" {
331355
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)