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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
// SPDX-License-Identifier: GPL-3.0-or-later
// Package engine is what every krino front end calls: it loads and compiles
// the configuration, matches files against rules, and (from plan 3) plans
// and applies actions. It returns data; front ends only render it.
package engine
import (
"fmt"
"os"
"strings"
"time"
"krino/internal/cond"
"krino/internal/config"
"krino/internal/extract"
"krino/internal/ignore"
"krino/internal/plan"
)
// Engine holds a loaded, compiled configuration: everything a front end
// needs to check, match and (from plan 3) act.
type Engine struct {
Config *config.Config
Dirs []*Dir
Extract *extract.Extractor
Now func() time.Time // time.Now; tests replace it
MainFile string
}
// Dir is one configured directory, with its ignore matcher and rules
// compiled.
type Dir struct {
Name string
Root string // absolute
Conf *config.Dir
Settings config.Resolved // built-in, then defaults, then the directory
Ignore *ignore.Matcher
Rules []*Rule
// ContentVariants is the distinct (IgnoreCase, Fold) pairs any of
// Rules' content tests evaluate under, in first-seen order. B2: when
// this holds exactly one variant, facts.Content releases a file's raw
// extracted text once that variant's normalised copy exists, since no
// other variant will ever be asked for; with more than one, both must
// stay memoised, as before.
ContentVariants []cond.Options
// DupScopes is every distinct directory list the duplicate tests of
// Rules use, in first-seen order, the plain (duplicate) as an empty
// list. Spec §5.5 rule 2 looks a file up under each of them before any
// rule may delete it.
DupScopes [][]string
}
// Rule is one directory's rule, with its condition compiled.
type Rule struct {
Name string
Conf *config.Rule
Settings config.Resolved // the rule's own settings over its directory's
Cond *cond.Cond
}
// Load reads and compiles everything. Any problem anywhere returns a nil
// Engine and every diagnostic: krino never acts on a configuration it only
// partly understood. Duplicate names are ignored after their first use.
func Load(mainFile string, names ...string) (*Engine, []*config.Diag) {
cfg, errs := config.Load(mainFile, dedupeNames(names)...)
if cfg == nil {
return nil, errs
}
var dirs []*Dir
for _, d := range cfg.Dirs {
dir := &Dir{
Name: d.Name,
Root: d.Path,
Conf: d,
Settings: cfg.Resolved(d),
}
if m, err := ignore.New(d.Ignore); err != nil {
errs = append(errs, &config.Diag{File: d.File, Msg: err.Error()})
} else {
dir.Ignore = m
}
for _, r := range d.Rules {
rs := r.Settings.Over(dir.Settings)
c, cerrs := cond.Compile(d.File, r.When, cond.Options{
IgnoreCase: rs.Case == config.CaseIgnore,
Fold: rs.Fold,
})
if len(cerrs) > 0 {
errs = append(errs, cerrs...)
continue
}
if diag := checkCaptures(d.File, r, c); diag != nil {
errs = append(errs, diag)
continue
}
if diag := checkDuplicateDelete(d.File, r, c); diag != nil {
errs = append(errs, diag)
continue
}
dir.Rules = append(dir.Rules, &Rule{Name: r.Name, Conf: r, Settings: rs, Cond: c})
}
dir.ContentVariants = contentVariants(dir.Rules)
dir.DupScopes = dupScopes(dir.Rules)
dirs = append(dirs, dir)
}
if len(errs) > 0 {
return nil, errs
}
return &Engine{
Config: cfg,
Dirs: dirs,
Extract: extract.New(),
Now: time.Now,
MainFile: mainFile,
}, nil
}
// checkCaptures validates a compiled rule's actions against the capture
// groups its own name tests can supply (spec 7.3): a rule using {N} needs a
// name test at all, and every name test in it needs at least N groups. It
// reports only the first offending action, so one config mistake yields one
// diagnostic.
func checkCaptures(file string, r *config.Rule, c *cond.Cond) *config.Diag {
groups := c.NameGroups()
for _, a := range r.Actions {
n, err := plan.MaxIndex(a.Arg)
if err != nil || n == 0 {
continue
}
if len(groups) == 0 {
return &config.Diag{File: file, Pos: a.Pos, Msg: fmt.Sprintf("rule %q: {%d} needs a name test to capture from", r.Name, n)}
}
for _, g := range groups {
if g < n {
return &config.Diag{File: file, Pos: a.Pos, Msg: fmt.Sprintf("rule %q: {%d} but a name test has only %s", r.Name, n, captureGroups(g))}
}
}
}
return nil
}
// checkDuplicateDelete refuses a rule that combines a duplicate test with a
// delete action (spec §4.5, §5.5): duplicates are found, never deleted.
// Cond.DupDirs records every duplicate test compiled, inside or and not
// too, so a test anywhere in the condition counts. It reports only the
// first delete action, so one config mistake yields one diagnostic.
func checkDuplicateDelete(file string, r *config.Rule, c *cond.Cond) *config.Diag {
if len(c.DupDirs) == 0 {
return nil
}
for _, a := range r.Actions {
if a.Kind == config.Delete || a.Kind == config.DeletePermanent {
return &config.Diag{File: file, Pos: a.Pos, Msg: fmt.Sprintf("rule %q: (duplicate) cannot be combined with (%s): duplicates are never deleted, move them aside instead", r.Name, a.Kind)}
}
}
return nil
}
// captureGroups renders a capture-group count with correct singular/plural.
func captureGroups(n int) string {
if n == 1 {
return "1 capture group"
}
return fmt.Sprintf("%d capture groups", n)
}
// dedupeNames returns names with every repeat after its first occurrence
// removed, order preserved.
func dedupeNames(names []string) []string {
var out []string
seen := map[string]bool{}
for _, n := range names {
if seen[n] {
continue
}
seen[n] = true
out = append(out, n)
}
return out
}
// contentVariants returns the distinct (IgnoreCase, Fold) pairs any of
// rules' content tests evaluate under, in first-seen order — B2's per-Dir
// ContentVariants. A rule whose condition has no content test at all
// (Cond.UsesContent false) never calls facts.Content, so its resolved
// case/fold settings contribute no variant here.
func contentVariants(rules []*Rule) []cond.Options {
var out []cond.Options
seen := map[cond.Options]bool{}
for _, r := range rules {
if !r.Cond.UsesContent {
continue
}
opt := cond.Options{IgnoreCase: r.Settings.Case == config.CaseIgnore, Fold: r.Settings.Fold}
if seen[opt] {
continue
}
seen[opt] = true
out = append(out, opt)
}
return out
}
// dupScopes returns the distinct Cond.DupDirs lists of rules, in first-seen
// order. Lists are compared as written, with their length in the key so
// (duplicate) and (duplicate "") stay apart; two spellings of one directory
// stay two entries, which costs a second lookup but never a wrong answer,
// since facts.Duplicate resolves and shares the index itself.
func dupScopes(rules []*Rule) [][]string {
var out [][]string
seen := map[string]bool{}
for _, r := range rules {
for _, dirs := range r.Cond.DupDirs {
key := fmt.Sprintf("%d\x00%s", len(dirs), strings.Join(dirs, "\x00"))
if seen[key] {
continue
}
seen[key] = true
out = append(out, dirs)
}
}
return out
}
// Report is what Check reports: the files involved and each directory's
// state.
type Report struct {
MainFile string
LogFile string
Dirs []DirReport
Tools []extract.Tool
}
// DirReport is one directory's state in a Report.
type DirReport struct {
Dir *Dir
Missing bool // the root is not a directory right now
}
// Check reports the engine's configuration files, each directory's current
// state and the external tools found for content extraction.
func (e *Engine) Check() Report {
r := Report{
MainFile: e.MainFile,
LogFile: e.Config.LogFile(),
Tools: e.Extract.Tools(),
}
for _, d := range e.Dirs {
fi, err := os.Stat(d.Root)
r.Dirs = append(r.Dirs, DirReport{Dir: d, Missing: err != nil || !fi.IsDir()})
}
return r
}
|