-
Notifications
You must be signed in to change notification settings - Fork 111
/
repo.go
137 lines (110 loc) · 2.9 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
package file
import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/rilldata/rill/runtime/drivers"
)
var limit = 500
// Driver implements drivers.RepoStore.
func (c *connection) Driver() string {
return "file"
}
// Root implements drivers.RepoStore.
func (c *connection) Root() string {
return c.root
}
// ListRecursive implements drivers.RepoStore.
func (c *connection) ListRecursive(ctx context.Context, instID, glob string) ([]string, error) {
// Check that folder hasn't been moved
if err := c.checkRoot(); err != nil {
return nil, err
}
fsRoot := os.DirFS(c.root)
glob = path.Clean(path.Join("./", glob))
var paths []string
err := doublestar.GlobWalk(fsRoot, glob, func(p string, d fs.DirEntry) error {
// Don't track directories
if d.IsDir() {
return nil
}
// Exit if we reached the limit
if len(paths) == 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)
paths = append(paths, p)
return nil
})
if err != nil {
return nil, err
}
return paths, nil
}
// Get implements drivers.RepoStore.
func (c *connection) Get(ctx context.Context, instID, 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, instID, 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(),
}, nil
}
// Put implements drivers.RepoStore.
func (c *connection) Put(ctx context.Context, instID, 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
}
// Rename implements drivers.RepoStore.
func (c *connection) Rename(ctx context.Context, instID, fromPath, toPath string) error {
toPath = path.Join(c.root, toPath)
fromPath = path.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, instID, filePath string) error {
filePath = filepath.Join(c.root, filePath)
return os.Remove(filePath)
}
func (c *connection) Sync(ctx context.Context, instID string) error {
return nil
}