Add a sync option to exclude paths by regexp

This commit is contained in:
Přemysl Eric Janouch 2024-12-21 10:49:20 +01:00
parent 595db869e5
commit 615af97043
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 27 additions and 3 deletions

23
main.go
View File

@ -1286,6 +1286,9 @@ type syncContext struct {
stmtDisposeSub *sql.Stmt stmtDisposeSub *sql.Stmt
stmtDisposeAll *sql.Stmt stmtDisposeAll *sql.Stmt
// exclude specifies filesystem paths that should be seen as missing.
exclude *regexp.Regexp
// linked tracks which image hashes we've checked so far in the run. // linked tracks which image hashes we've checked so far in the run.
linked map[string]struct{} linked map[string]struct{}
} }
@ -1694,6 +1697,12 @@ func syncDirectory(c *syncContext, dbParent int64, fsPath string) error {
fs = nil fs = nil
} }
if c.exclude != nil {
fs = slices.DeleteFunc(fs, func(f syncFile) bool {
return c.exclude.MatchString(filepath.Join(fsPath, f.fsName))
})
}
// Convert differences to a form more convenient for processing. // Convert differences to a form more convenient for processing.
iDB, iFS, pairs := 0, 0, []syncPair{} iDB, iFS, pairs := 0, 0, []syncPair{}
for iDB < len(db) && iFS < len(fs) { for iDB < len(db) && iFS < len(fs) {
@ -1869,9 +1878,21 @@ const disposeCTE = `WITH RECURSIVE
HAVING count = total HAVING count = total
)` )`
type excludeRE struct{ re *regexp.Regexp }
func (re *excludeRE) String() string { return fmt.Sprintf("%v", re.re) }
func (re *excludeRE) Set(value string) error {
var err error
re.re, err = regexp.Compile(value)
return err
}
// cmdSync ensures the given (sub)roots are accurately reflected // cmdSync ensures the given (sub)roots are accurately reflected
// in the database. // in the database.
func cmdSync(fs *flag.FlagSet, args []string) error { func cmdSync(fs *flag.FlagSet, args []string) error {
var exclude excludeRE
fs.Var(&exclude, "exclude", "exclude paths matching regular expression")
fullpaths := fs.Bool("fullpaths", false, "don't basename arguments") fullpaths := fs.Bool("fullpaths", false, "don't basename arguments")
if err := fs.Parse(args); err != nil { if err := fs.Parse(args); err != nil {
return err return err
@ -1909,7 +1930,7 @@ func cmdSync(fs *flag.FlagSet, args []string) error {
} }
c := syncContext{ctx: ctx, tx: tx, pb: newProgressBar(-1), c := syncContext{ctx: ctx, tx: tx, pb: newProgressBar(-1),
linked: make(map[string]struct{})} exclude: exclude.re, linked: make(map[string]struct{})}
defer c.pb.Stop() defer c.pb.Stop()
if c.stmtOrphan, err = c.tx.Prepare(disposeCTE + ` if c.stmtOrphan, err = c.tx.Prepare(disposeCTE + `

View File

@ -16,6 +16,9 @@ sha1duplicate=$sha1
cp $input/Test/dhash.png \ cp $input/Test/dhash.png \
$input/Test/multiple-paths.png $input/Test/multiple-paths.png
gen -seed 15 -size 256x256 plasma:fractal \
$input/Test/excluded.png
gen -seed 20 -size 160x128 plasma:fractal \ gen -seed 20 -size 160x128 plasma:fractal \
-bordercolor transparent -border 64 \ -bordercolor transparent -border 64 \
$input/Test/transparent-wide.png $input/Test/transparent-wide.png
@ -36,7 +39,7 @@ gen $input/Test/animation-small.gif \
$input/Test/video.mp4 $input/Test/video.mp4
./gallery init $target ./gallery init $target
./gallery sync $target $input "$@" ./gallery sync -exclude '/excluded[.]' $target $input "$@"
./gallery thumbnail $target ./gallery thumbnail $target
./gallery dhash $target ./gallery dhash $target
./gallery tag $target test "Test space" <<-END ./gallery tag $target test "Test space" <<-END
@ -47,7 +50,7 @@ END
# TODO: Test all the various possible sync transitions. # TODO: Test all the various possible sync transitions.
mv $input/Test $input/Plasma mv $input/Test $input/Plasma
./gallery sync $target $input ./gallery sync -exclude '/excluded[.]' $target $input
./gallery web $target :8080 & ./gallery web $target :8080 &
web=$! web=$!