diff options
| author | 2023-04-28 01:36:44 +0800 | |
|---|---|---|
| committer | 2023-04-28 01:36:44 +0800 | |
| commit | dd84b9d64fb98746a230cd24233ff50a562c39c9 (patch) | |
| tree | b583261ef00b3afe72ec4d6dacb31e57779a6faf /cli/internal/cacheitem/restore_regular.go | |
| parent | 0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff) | |
| download | HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip | |
Diffstat (limited to 'cli/internal/cacheitem/restore_regular.go')
| -rw-r--r-- | cli/internal/cacheitem/restore_regular.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/cli/internal/cacheitem/restore_regular.go b/cli/internal/cacheitem/restore_regular.go new file mode 100644 index 0000000..ed8946e --- /dev/null +++ b/cli/internal/cacheitem/restore_regular.go @@ -0,0 +1,46 @@ +package cacheitem + +import ( + "archive/tar" + "io" + "os" + + "github.com/vercel/turbo/cli/internal/turbopath" +) + +// restoreRegular restores a file. +func restoreRegular(dirCache *cachedDirTree, anchor turbopath.AbsoluteSystemPath, header *tar.Header, reader *tar.Reader) (turbopath.AnchoredSystemPath, error) { + // Assuming this was a `turbo`-created input, we currently have an AnchoredUnixPath. + // Assuming this is malicious input we don't really care if we do the wrong thing. + processedName, err := canonicalizeName(header.Name) + if err != nil { + return "", err + } + + // We need to traverse `processedName` from base to root split at + // `os.Separator` to make sure we don't end up following a symlink + // outside of the restore path. + if err := safeMkdirFile(dirCache, anchor, processedName, header.Mode); err != nil { + return "", err + } + + // Create the file. + if f, err := processedName.RestoreAnchor(anchor).OpenFile(os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(header.Mode)); err != nil { + return "", err + } else if _, err := io.Copy(f, reader); err != nil { + return "", err + } else if err := f.Close(); err != nil { + return "", err + } + return processedName, nil +} + +// safeMkdirAll creates all directories, assuming that the leaf node is a file. +func safeMkdirFile(dirCache *cachedDirTree, anchor turbopath.AbsoluteSystemPath, processedName turbopath.AnchoredSystemPath, mode int64) error { + isRootFile := processedName.Dir() == "." + if !isRootFile { + return safeMkdirAll(dirCache, anchor, processedName.Dir(), 0755) + } + + return nil +} |
