-
Notifications
You must be signed in to change notification settings - Fork 9
/
wscontents.go
178 lines (148 loc) · 3.9 KB
/
wscontents.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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 wscontents
import (
"context"
"encoding/hex"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"time"
"github.com/cespare/xxhash/v2"
"github.com/fsnotify/fsnotify"
"github.com/moby/patternmatcher"
"namespacelabs.dev/foundation/internal/filewatcher"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs/memfs"
"namespacelabs.dev/foundation/internal/workspace/dirs"
)
func SnapshotDirectory(ctx context.Context, absPath string, excludeMatcher *patternmatcher.PatternMatcher, digest bool) (*memfs.FS, error) {
fsys, err := snapshotDirectory(ctx, absPath, excludeMatcher, digest)
if err != nil {
return nil, fnerrors.InternalError("snapshot failed: %v", err)
}
return fsys, nil
}
func snapshotDirectory(ctx context.Context, absPath string, excludeMatcher *patternmatcher.PatternMatcher, digest bool) (*memfs.FS, error) {
if err := verifyDir(absPath); err != nil {
return nil, err
}
var inmem memfs.FS
if err := filepath.WalkDir(absPath, func(osPathname string, de fs.DirEntry, err error) error {
if err != nil {
return err
}
if excludeMatcher != nil {
if matches, err := excludeMatcher.MatchesOrParentMatches(osPathname); err != nil {
return err
} else if matches {
if de.IsDir() {
return fs.SkipDir
}
return nil
}
}
// TODO: use the exclude/include patterns passed as a config instead.
if dirs.IsExcludedAsSource(osPathname) {
if de.IsDir() {
return filepath.SkipDir
}
return nil
}
if de.IsDir() {
return nil
} else if !de.Type().IsRegular() {
return nil
}
rel, err := filepath.Rel(absPath, osPathname) // XXX expensive?
if err != nil {
return err
}
contents, err := os.Open(osPathname)
if err != nil {
return err
}
defer contents.Close()
st, err := contents.Stat()
if err != nil {
return err
}
// XXX symlinks.
if !st.Mode().IsRegular() {
return nil
}
target, err := inmem.OpenWrite(rel, st.Mode().Perm())
if err != nil {
return err
}
// In digest mode, we store a digest of the file as the file contents.
if digest {
var digest []byte
digest, err = digestfile(contents)
if err == nil {
_, err = target.Write(digest)
}
} else {
_, err = io.Copy(target, contents)
}
err2 := target.Close()
if err != nil {
return err
}
return err2
}); err != nil {
return nil, err
}
return &inmem, nil
}
func verifyDir(path string) error {
if st, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return err
}
return fnerrors.New("%s: accessing the path failed: %v", path, err)
} else if !st.IsDir() {
return fnerrors.New("%s: expected it to be a directory", path)
}
return nil
}
func AggregateFSEvents(watcher filewatcher.EventsAndErrors, debugLogger, errLogger io.Writer, bufferCh chan []fsnotify.Event) {
// Usually the return callback would be sole responsible to stop the watcher,
// but we want to free resources as early as we know that we can longer listen
// to events.
defer watcher.Close()
defer close(bufferCh)
t := time.NewTicker(250 * time.Millisecond)
defer func() {
t.Stop()
}()
var buffer []fsnotify.Event
for {
select {
case ev, ok := <-watcher.Events():
if !ok {
return
}
fmt.Fprintf(debugLogger, "Received filesystem event (%s): %v\n", ev.Name, ev.Op)
buffer = append(buffer, ev)
case _, ok := <-t.C:
if ok && len(buffer) > 0 {
bufferCh <- buffer
buffer = nil
}
case err, ok := <-watcher.Errors():
if !ok {
return
}
fmt.Fprintf(errLogger, "Received filesystem event error: %v\n", err)
}
}
}
func digestfile(contents io.Reader) ([]byte, error) {
h := xxhash.New()
_, err := io.Copy(h, contents)
return []byte(hex.EncodeToString(h.Sum(nil))), err
}