Skip to content

Commit 6c80e65

Browse files
authored
Merge pull request #6875 from mtrmac/TempDirForURL-API
Restore the previous TempDirForURL API
2 parents d724f1d + 015c1c7 commit 6c80e65

5 files changed

Lines changed: 28 additions & 16 deletions

File tree

add.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
599599
go func() {
600600
defer wg.Done()
601601
defer pipeWriter.Close()
602-
var repositoryDir string
603-
// TODO: the returned tempDir is never cleaned up, leaking disk space.
604-
_, repositoryDir, getErr = define.TempDirForURL(tmpdir.GetTempDir(), "", src)
602+
// TODO: the returned cloneDir is never cleaned up, leaking disk space.
603+
var cloneDir, subdir string
604+
cloneDir, subdir, getErr = define.TempDirForURL(tmpdir.GetTempDir(), "", src)
605605
if getErr != nil {
606606
return
607607
}
@@ -621,6 +621,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
621621
Timestamp: options.Timestamp,
622622
}
623623
writer := io.WriteCloser(pipeWriter)
624+
repositoryDir := filepath.Join(cloneDir, subdir)
624625
getErr = copier.Get(repositoryDir, repositoryDir, getOptions, []string{"."}, writer)
625626
}()
626627
} else {

define/types.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ type SBOMScanOptions struct {
189189
// TempDirForURL checks if the passed-in string looks like a URL or "-". If it
190190
// is, TempDirForURL creates a temporary directory, arranges for its contents
191191
// to be the contents of that URL, and returns the temporary directory's path
192-
// (for cleanup) and the absolute path to the build context within it.
192+
// (for cleanup) and a relative subdirectory to the build context within it.
193193
// Removal of the temporary directory is the responsibility of the caller.
194194
// If the string doesn't look like a URL or "-", TempDirForURL returns empty
195195
// strings and a nil error code.
196-
func TempDirForURL(dir, prefix, url string) (tempDir string, contextDir string, err error) {
196+
func TempDirForURL(dir, prefix, url string) (tempDir string, relativeContextDir string, err error) {
197197
if !urlsource.IsHTTPOrHTTPS(url) &&
198198
!strings.HasPrefix(url, "git://") &&
199199
!strings.HasPrefix(url, "github.com/") &&
@@ -250,13 +250,17 @@ func TempDirForURL(dir, prefix, url string) (tempDir string, contextDir string,
250250
}
251251
}
252252

253-
contextDir, err = securejoin.SecureJoin(downloadDir, contentSubdir)
253+
contextDir, err := securejoin.SecureJoin(downloadDir, contentSubdir)
254254
if err != nil {
255255
return "", "", fmt.Errorf("resolving subdirectory %q in %q: %w", contentSubdir, downloadDir, err)
256256
}
257+
relativeContextDir, err = filepath.Rel(tempDir, contextDir)
258+
if err != nil {
259+
return "", "", err
260+
}
257261
logrus.Debugf("Build context is at %q", contextDir)
258262
succeeded = true
259-
return tempDir, contextDir, nil
263+
return tempDir, relativeContextDir, nil
260264
}
261265

262266
// parseGitBuildContext parses git build context to `repo`, `sub-dir`
@@ -372,7 +376,12 @@ func stdinToDirectory(dir string) error {
372376

373377
// writeFileInRoot safely writes data to a file inside root, without following
374378
// symlinks that escape the root directory.
375-
func writeFileInRoot(root, name string, data []byte, perm os.FileMode) error {
379+
func writeFileInRoot(root, name string, data []byte, perm os.FileMode) error { //nolint:unparam,nolintlint
380+
// Above:
381+
// unparam: 'name' currently only receives "Dockerfile" but will potentially support other files later
382+
// nolintlint: the unparam linter only triggers if there are ≥ 4 instances; we do have that
383+
// with --tests defaulting to true, but not with --tests=false.
384+
376385
rootHandle, err := os.OpenRoot(root)
377386
if err != nil {
378387
return err

imagebuildah/stage_executor.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,13 @@ func (s *stageExecutor) performCopy(excludes []string, copies ...imagebuilder.Co
512512
// additional context contains a tar file
513513
// so download and explode tar to buildah
514514
// temp and point context to that.
515-
// TODO: the returned tempDir is never cleaned up, leaking disk space.
516-
_, contextPath, err := define.TempDirForURL(tmpdir.GetTempDir(), internal.BuildahExternalArtifactsDir, additionalBuildContext.Value)
515+
// TODO: the returned path is never cleaned up, leaking disk space.
516+
path, subdir, err := define.TempDirForURL(tmpdir.GetTempDir(), internal.BuildahExternalArtifactsDir, additionalBuildContext.Value)
517517
if err != nil {
518518
return fmt.Errorf("unable to download context from external source %q: %w", additionalBuildContext.Value, err)
519519
}
520520
// point context dir to the extracted path
521-
contextDir = contextPath
521+
contextDir = filepath.Join(path, subdir)
522522
// populate cache for next RUN step
523523
additionalBuildContext.DownloadedCache = contextDir
524524
} else {
@@ -733,13 +733,13 @@ func (s *stageExecutor) runStageMountPoints(mountList []string) (map[string]inte
733733
// additional context contains a tar file
734734
// so download and explode tar to buildah
735735
// temp and point context to that.
736-
// TODO: the returned tempDir is never cleaned up, leaking disk space.
737-
_, contextPath, err := define.TempDirForURL(tmpdir.GetTempDir(), internal.BuildahExternalArtifactsDir, additionalBuildContext.Value)
736+
// TODO: the returned path is never cleaned up, leaking disk space.
737+
path, subdir, err := define.TempDirForURL(tmpdir.GetTempDir(), internal.BuildahExternalArtifactsDir, additionalBuildContext.Value)
738738
if err != nil {
739739
return nil, fmt.Errorf("unable to download context from external source %q: %w", additionalBuildContext.Value, err)
740740
}
741741
// point context dir to the extracted path
742-
mountPoint = contextPath
742+
mountPoint = filepath.Join(path, subdir)
743743
// populate cache for next RUN step
744744
additionalBuildContext.DownloadedCache = mountPoint
745745
} else {

pkg/cli/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
151151
}
152152
} else {
153153
// The context directory could be a URL. Try to handle that.
154-
tempDir, tempContextDir, err := define.TempDirForURL("", "buildah", cliArgs[0])
154+
tempDir, subDir, err := define.TempDirForURL("", "buildah", cliArgs[0])
155155
if err != nil {
156156
return options, nil, nil, fmt.Errorf("prepping temporary context directory: %w", err)
157157
}
158158
if tempDir != "" {
159159
// We had to download it to a temporary directory.
160160
// Delete it later.
161161
removeAll = append(removeAll, tempDir)
162-
contextDir = tempContextDir
162+
contextDir = filepath.Join(tempDir, subDir)
163163
} else {
164164
// Nope, it was local. Use it as is.
165165
absDir, err := filepath.Abs(cliArgs[0])

tests/bud.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8184,6 +8184,8 @@ _EOF
81848184
}
81858185

81868186
@test "bud with ADD with git repository source escape directory" {
8187+
skip 'TEMPORARY: needs a (git config user.{email,name})'
8188+
81878189
_prefetch alpine
81888190

81898191
local secretdir=${TEST_SCRATCH_DIR}/secretdir

0 commit comments

Comments
 (0)