-
Notifications
You must be signed in to change notification settings - Fork 9
/
workspace.go
146 lines (122 loc) · 3.26 KB
/
workspace.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fnfs
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
"io/fs"
"path/filepath"
"namespacelabs.dev/foundation/internal/bytestream"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/schema"
)
type ReadWriteFS interface {
fs.ReadDirFS
WriteFS
}
func WriteWorkspaceFile(ctx context.Context, log io.Writer, vfs ReadWriteFS, filePath string, h func(io.Writer) error) error {
return WriteFileExtended(ctx, vfs, filePath, 0644, WriteFileExtendedOpts{
CompareContents: true,
AnnounceWrite: log,
EnsureFileMode: false,
}, h)
}
func WriteFSToWorkspace(ctx context.Context, log io.Writer, vfs ReadWriteFS, src fs.FS) error {
return VisitFiles(ctx, src, func(path string, contents bytestream.ByteStream, dirent fs.DirEntry) error {
return WriteWorkspaceFile(ctx, log, vfs, path, func(w io.Writer) error {
return bytestream.WriteTo(w, contents)
})
})
}
type WriteFileExtendedOpts struct {
ContentsDigest schema.Digest
CompareContents bool
FailOverwrite bool
EnsureFileMode bool
AnnounceWrite io.Writer
}
func WriteFileExtended(ctx context.Context, dst ReadWriteFS, filePath string, mode fs.FileMode, opts WriteFileExtendedOpts, writeContents func(io.Writer) error) error {
if opts.ContentsDigest.IsSet() || opts.CompareContents {
f, err := dst.Open(filePath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
goto write
}
return fnerrors.New("failed to write a workspace file %q: %w", filePath, err)
}
defer f.Close()
if opts.CompareContents {
// XXX use a compare writer, instead of buffering contents.
contents, err := io.ReadAll(f)
if err != nil {
return err
}
var b bytes.Buffer
if err := writeContents(&b); err != nil {
return err
}
if bytes.Equal(contents, b.Bytes()) {
// Nothing to do.
if opts.EnsureFileMode {
return chmod(dst, filePath, mode)
}
return nil
}
} else {
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return err
}
if schema.FromHash("sha256", h) == opts.ContentsDigest {
// Contents are good.
if opts.EnsureFileMode {
return chmod(dst, filePath, mode)
}
return nil
}
}
}
write:
if opts.FailOverwrite {
return fmt.Errorf("%s: would have been rewritten", filePath)
}
if mkfs, ok := dst.(MkdirFS); ok {
if err := mkfs.MkdirAll(filepath.Dir(filePath), addExecToRead(mode)); err != nil {
return err
}
}
f, err := dst.OpenWrite(filePath, mode)
if err != nil {
return err
}
err = writeContents(f)
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
}
if err != nil {
return err
}
if opts.AnnounceWrite != nil {
fmt.Fprintf(opts.AnnounceWrite, "Wrote %s\n", filePath)
}
if opts.EnsureFileMode {
return chmod(dst, filePath, mode)
}
return nil
}
func chmod(fsys fs.FS, filePath string, mode fs.FileMode) error {
if chm, ok := fsys.(ChmodFS); ok {
if st, err := fs.Stat(fsys, filePath); err != nil {
return err
} else if st.Mode().Perm() == mode.Perm() {
return nil
}
return chm.Chmod(filePath, mode.Perm())
}
return nil
}