/
blob_access_file_fetcher.go
48 lines (41 loc) · 1.21 KB
/
blob_access_file_fetcher.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
package cas
import (
"context"
"os"
"github.com/buildbarn/bb-storage/pkg/blobstore"
"github.com/buildbarn/bb-storage/pkg/digest"
"github.com/buildbarn/bb-storage/pkg/filesystem"
"github.com/buildbarn/bb-storage/pkg/filesystem/path"
)
type blobAccessFileFetcher struct {
blobAccess blobstore.BlobAccess
}
// NewBlobAccessFileFetcher creates a FileFetcher that reads files fom a
// BlobAccess based store.
func NewBlobAccessFileFetcher(blobAccess blobstore.BlobAccess) FileFetcher {
return &blobAccessFileFetcher{
blobAccess: blobAccess,
}
}
func (ff *blobAccessFileFetcher) GetFile(ctx context.Context, digest digest.Digest, directory filesystem.Directory, name path.Component, isExecutable bool) error {
var mode os.FileMode = 0o444
if isExecutable {
mode = 0o555
}
w, err := directory.OpenAppend(name, filesystem.CreateExcl(mode))
if err != nil {
return err
}
defer w.Close()
if err := ff.blobAccess.Get(ctx, digest).IntoWriter(w); err != nil {
// Ensure no traces are left behind upon failure.
directory.Remove(name)
return err
}
time := filesystem.DeterministicFileModificationTimestamp
if err := directory.Chtimes(name, time, time); err != nil {
directory.Remove(name)
return err
}
return nil
}