-
Notifications
You must be signed in to change notification settings - Fork 111
/
repo.go
190 lines (156 loc) · 4.01 KB
/
repo.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
179
180
181
182
183
184
185
186
187
188
189
190
package file
import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/rilldata/rill/runtime/drivers"
)
var limit = 1000
// Driver implements drivers.RepoStore.
func (c *connection) Driver() string {
return "file"
}
// Root implements drivers.RepoStore.
func (c *connection) Root() string {
return c.root
}
// CommitHash implements drivers.RepoStore.
func (c *connection) CommitHash(ctx context.Context) (string, error) {
return "", nil
}
// ListRecursive implements drivers.RepoStore.
func (c *connection) ListRecursive(ctx context.Context, glob string, skipDirs bool) ([]drivers.DirEntry, error) {
// Check that folder hasn't been moved
if err := c.checkRoot(); err != nil {
return nil, err
}
fsRoot := os.DirFS(c.root)
glob = filepath.Clean(filepath.Join("./", glob))
var entries []drivers.DirEntry
err := doublestar.GlobWalk(fsRoot, glob, func(p string, d fs.DirEntry) error {
if skipDirs && d.IsDir() {
return nil
}
// Exit if we reached the limit
if len(entries) == limit {
return fmt.Errorf("glob exceeded limit of %d matched files", limit)
}
// Track file (p is already relative to the FS root)
p = filepath.Join("/", p)
// Do not send files for ignored paths
if drivers.IsIgnored(p, c.ignorePaths) {
return nil
}
entries = append(entries, drivers.DirEntry{
Path: p,
IsDir: d.IsDir(),
})
return nil
})
if err != nil {
return nil, err
}
return entries, nil
}
// Get implements drivers.RepoStore.
func (c *connection) Get(ctx context.Context, filePath string) (string, error) {
filePath = filepath.Join(c.root, filePath)
b, err := os.ReadFile(filePath)
if err != nil {
return "", err
}
return string(b), nil
}
// Stat implements drivers.RepoStore.
func (c *connection) Stat(ctx context.Context, filePath string) (*drivers.RepoObjectStat, error) {
filePath = filepath.Join(c.root, filePath)
info, err := os.Stat(filePath)
if err != nil {
return nil, err
}
return &drivers.RepoObjectStat{
LastUpdated: info.ModTime(),
IsDir: info.IsDir(),
}, nil
}
// Put implements drivers.RepoStore.
func (c *connection) Put(ctx context.Context, filePath string, reader io.Reader) error {
filePath = filepath.Join(c.root, filePath)
err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
if err != nil {
return err
}
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, reader)
if err != nil {
return err
}
return nil
}
func (c *connection) MakeDir(ctx context.Context, dirPath string) error {
dirPath = filepath.Join(c.root, dirPath)
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return err
}
return nil
}
// Rename implements drivers.RepoStore.
func (c *connection) Rename(ctx context.Context, fromPath, toPath string) error {
toPath = filepath.Join(c.root, toPath)
fromPath = filepath.Join(c.root, fromPath)
if _, err := os.Stat(toPath); !strings.EqualFold(fromPath, toPath) && err == nil {
return drivers.ErrFileAlreadyExists
}
err := os.Rename(fromPath, toPath)
if err != nil {
return err
}
return os.Chtimes(toPath, time.Now(), time.Now())
}
// Delete implements drivers.RepoStore.
func (c *connection) Delete(ctx context.Context, filePath string, force bool) error {
filePath = filepath.Join(c.root, filePath)
if force {
return os.RemoveAll(filePath)
}
return os.Remove(filePath)
}
// Sync implements drivers.RepoStore.
func (c *connection) Sync(ctx context.Context) error {
return nil
}
// Watch implements drivers.RepoStore.
func (c *connection) Watch(ctx context.Context, cb drivers.WatchCallback) error {
c.watcherMu.Lock()
if c.watcher == nil {
w, err := newWatcher(c.root, c.ignorePaths, c.logger)
if err != nil {
c.watcherMu.Unlock()
return err
}
c.watcher = w
}
c.watcherCount++
c.watcherMu.Unlock()
defer func() {
c.watcherMu.Lock()
c.watcherCount--
if c.watcherCount == 0 {
c.watcher.close()
c.watcher = nil
}
c.watcherMu.Unlock()
}()
return c.watcher.subscribe(ctx, cb)
}