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 |
// Package folder watches a directory of content files and feeds changes to
// the engine.
package folder
import (
"context"
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
"golang.org/x/text/unicode/norm"
"git.pablomurad.com/pablo/patchwork/internal/engine"
)
// File is one content file found by a scan.
type File struct {
Rel string // relative to the root, '/'-separated, NFC
Abs string
Size int64
Mtime time.Time
}
// Matcher decides which files are content.
type Matcher struct {
Include []string // "*.md" top level only, "**/*.md" any depth
Exclude []string // matched against the file or directory name
}
func (m Matcher) excluded(name string) bool {
for _, p := range m.Exclude {
if ok, _ := path.Match(p, name); ok {
return true
}
}
return false
}
func (m Matcher) included(rel string) bool {
for _, p := range m.Include {
target := rel
if rest, deep := strings.CutPrefix(p, "**/"); deep {
p, target = rest, path.Base(rel)
}
if ok, _ := path.Match(p, target); ok {
return true
}
}
return false
}
// List walks root and returns the content files, sorted by path.
func List(root string, m Matcher) ([]File, error) {
var out []File
err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
if err != nil {
if p == root {
return err
}
return nil // an unreadable entry shouldn't stop the scan
}
if p == root {
return nil
}
if m.excluded(d.Name()) {
if d.IsDir() {
return filepath.SkipDir
}
return nil
}
if d.IsDir() || !d.Type().IsRegular() {
return nil
}
rel, err := filepath.Rel(root, p)
if err != nil {
return err
}
rel = norm.NFC.String(filepath.ToSlash(rel))
if !m.included(rel) {
return nil
}
info, err := d.Info()
if err != nil {
return nil
}
out = append(out, File{Rel: rel, Abs: p, Size: info.Size(), Mtime: info.ModTime()})
return nil
})
sort.Slice(out, func(i, j int) bool { return out[i].Rel < out[j].Rel })
return out, err
}
// Report summarizes one scan.
type Report struct {
Results []engine.Result
Pending []string // files still being written; scanned again later
Errors []error
}
// Runner scans a folder and hands changed files to the engine.
type Runner struct {
Root string
Matcher Matcher
Engine *engine.Engine
Log *slog.Logger
// Settle is how long a file's mtime must be in the past before it is
// read, so a half-written upload isn't snapshotted.
Settle time.Duration
Now func() time.Time
// PollOnly skips fsnotify, for mounts where file events never arrive.
PollOnly bool
seen map[string]File
}
// NewRunner builds a runner with the usual defaults.
func NewRunner(root string, m Matcher, e *engine.Engine, log *slog.Logger) *Runner {
return &Runner{Root: root, Matcher: m, Engine: e, Log: log, Settle: time.Second, Now: time.Now}
}
// Scan runs one full pass. Unchanged files (same size and mtime as last
// time) are not read again.
func (r *Runner) Scan(ctx context.Context, dryRun bool) (Report, error) {
var rep Report
files, err := List(r.Root, r.Matcher)
if err != nil {
return rep, fmt.Errorf("scan %s: %w", r.Root, err)
}
if r.seen == nil {
r.seen = map[string]File{}
}
present := map[string]bool{}
now := r.Now()
for _, f := range files {
present[f.Rel] = true
if old, ok := r.seen[f.Rel]; ok && old.Size == f.Size && old.Mtime.Equal(f.Mtime) {
continue
}
if now.Sub(f.Mtime) < r.Settle {
rep.Pending = append(rep.Pending, f.Rel)
continue
}
raw, err := os.ReadFile(f.Abs)
if err != nil {
rep.Errors = append(rep.Errors, fmt.Errorf("%s: %w", f.Rel, err))
continue
}
res, err := r.Engine.Ingest(ctx, engine.Observation{Path: f.Rel, Raw: raw, Mtime: f.Mtime}, dryRun)
if err != nil {
rep.Errors = append(rep.Errors, fmt.Errorf("%s: %w", f.Rel, err))
continue
}
if !dryRun {
r.seen[f.Rel] = f
}
rep.Results = append(rep.Results, res)
}
for rel := range r.seen {
if !present[rel] {
delete(r.seen, rel)
}
}
// A scan that finds nothing while the store still has pages is far more
// likely a dropped mount than a deleted site: don't remove anything.
if len(files) == 0 {
if n, err := r.Engine.ActivePages(ctx); err == nil && n > 0 {
rep.Errors = append(rep.Errors, errors.New("no content files found but the store has active pages; not marking anything removed"))
return rep, nil
}
}
removed, err := r.Engine.Reconcile(ctx, present, dryRun)
if err != nil {
return rep, err
}
rep.Results = append(rep.Results, removed...)
return rep, nil
}
|