/
exportcache.go
111 lines (92 loc) · 2.44 KB
/
exportcache.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package bass
import (
"archive/tar"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/adrg/xdg"
)
// CacheThunkPath exports a thunk path to a local cache under $XDG_CACHE_HOME.
//
// If the path is a file, a path to the cached file will be returned.
//
// If the path is a directory, a path to a cached directory containing its
// immediate files will be returned. Note that sub-directories are not
// recursively exported.
//
// A cached directory will be marked as cached by placing a .cached file in it
// to distinguish from a directory created in order to export a child path.
//
// It does not preserve things like file permissions and timestamps. It is only
// for accessing the content of files.
func CacheThunkPath(ctx context.Context, tp ThunkPath) (string, error) {
sha, err := tp.SHA256() // sha of the path, not just its thunk
if err != nil {
return "", err
}
cachePath, err := xdg.CacheFile(filepath.Join("bass", "export", sha))
if err != nil {
return "", err
}
if _, err := os.Stat(cachePath); err == nil {
return cachePath, nil
}
pool, err := RuntimePoolFromContext(ctx)
if err != nil {
return "", err
}
runt, err := pool.Select(tp.Thunk.Platform())
if err != nil {
return "", err
}
src := new(bytes.Buffer)
err = runt.ExportPath(ctx, src, tp)
if err != nil {
return "", fmt.Errorf("export thunk path: %w", err)
}
tr := tar.NewReader(src)
for {
hdr, err := tr.Next()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return "", fmt.Errorf("export %s: %w", tp, err)
}
if tp.Path.FilesystemPath().IsDir() && (strings.Contains(hdr.Name, "/") || hdr.Typeflag == tar.TypeDir) {
// NB: do not recurse into sub-directories; we only cache directory
// exports when we're looking for a particular file, so it's better to be
// explicit rather than needlessly hoist around things like .git/...
continue
}
// TODO: handle symlinks? might be necessary for bass.lock
var dest string
if tp.Path.FilesystemPath().IsDir() {
dest = filepath.Join(cachePath, hdr.Name)
} else {
dest = cachePath
}
err = os.MkdirAll(filepath.Dir(dest), 0755)
if err != nil {
return "", err
}
cacheFile, err := os.Create(dest)
if err != nil {
return "", fmt.Errorf("create cache: %w", err)
}
_, err = io.Copy(cacheFile, tr)
if err != nil {
return "", err
}
err = cacheFile.Close()
if err != nil {
return "", err
}
}
return cachePath, nil
}