1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
}
|