Repository navigation
index --prune: remove deleted photos nightly, only when every indexed folder is there #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dce1815
index --prune: remove deleted photos after the nightly index, only wh…
claude b6f90b7
index --prune: prune after the copy clean-ups, and only after a run t…
claude f8a5436
index --prune: refuse --force, don't trust symlinked folders, hold af…
claude 9c042b6
Index folders named by a link; count lookup failures and files gone m…
claude 0c6cd9a
index --prune: a catalogued photo moved to a file under --min-bytes h…
claude 8ee7311
index --prune spares missing app copies with hand-given names, rating…
claude d796b10
prune hands a removed photo's names, ratings and stars to an identica…
claude 3b83d57
prune: move labels only on a full-content fingerprint; roll back on a…
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/auteursoft/archivis/internal/store" | ||
| ) | ||
|
|
||
| // folderProblem says why a folder to index can't be trusted to show which | ||
| // of its files were deleted, or "" if it can. A drive that isn't mounted | ||
| // leaves its folder gone, or an empty mount point; one the service may not | ||
| // read (no Full Disk Access) can't be listed. A folder that is a link is | ||
| // judged by the folder it points to, which the indexer walks. | ||
| func folderProblem(dir string) string { | ||
| st, err := os.Stat(dir) | ||
| switch { | ||
| case err != nil && os.IsNotExist(err): | ||
| return "is not there (drive not mounted?)" | ||
| case err != nil: | ||
| return fmt.Sprintf("can't be read: %v", err) | ||
| case !st.IsDir(): | ||
| return "is not a folder" | ||
| } | ||
| f, err := os.Open(dir) | ||
| if err != nil { | ||
| return fmt.Sprintf("can't be read: %v (Full Disk Access?)", err) | ||
| } | ||
| defer f.Close() | ||
| names, err := f.Readdirnames(1) | ||
| if len(names) == 0 { | ||
| if err != nil && err != io.EOF { | ||
| return fmt.Sprintf("can't be read: %v (Full Disk Access?)", err) | ||
| } | ||
| return "is empty (drive not mounted?)" | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // pruneAfterIndex (index --prune) removes the catalogue entries of files | ||
| // deleted from the folders just indexed. It runs after a complete index and | ||
| // its clean-ups, so moved and renamed files already have their new paths | ||
| // and copies' names and ratings are on their originals, and only when the | ||
| // run saw everything: every folder the job names was ready when it started | ||
| // (atStart holds those that weren't, with why) and still is, and the run | ||
| // read and recorded every file (gaps is 0). Otherwise it logs an error with | ||
| // each reason and prunes nothing, keeping those photos until a run that | ||
| // sees everything (after a restart, several drives can be late). Prune | ||
| // itself also keeps any folder whose files are all gone and that is itself | ||
| // gone or empty. | ||
| func pruneAfterIndex(st *store.Store, folders []string, atStart map[string]string, gaps int64, w io.Writer, also ...string) (store.PruneResult, bool) { | ||
| reasons := append([]string{}, also...) | ||
| for _, f := range folders { | ||
| if p, ok := atStart[f]; ok { | ||
| reasons = append(reasons, fmt.Sprintf("%s: when indexing started, it %s", f, p)) | ||
| } else if p := folderProblem(f); p != "" { | ||
| reasons = append(reasons, fmt.Sprintf("%s %s", f, p)) | ||
| } | ||
| } | ||
| if gaps > 0 { | ||
| reasons = append(reasons, fmt.Sprintf("this run could not read or record %d folders or files (see the warnings and errors above)", gaps)) | ||
| } | ||
| if len(reasons) > 0 { | ||
| fmt.Fprintf(w, "error: not removing deleted photos, because this run may not have seen them all:\n %s\n Their photos stay in the catalogue; deleted ones are removed after a run that finds every folder and reads every file.\n", | ||
| strings.Join(reasons, "\n ")) | ||
|
sgoggins marked this conversation as resolved.
Resolved
|
||
| return store.PruneResult{}, false | ||
| } | ||
| res, err := st.PruneSparingCopies(folders) | ||
| if err != nil { | ||
| fmt.Fprintf(w, "error: removing deleted photos: %v (retried on the next run)\n", err) | ||
| return res, false | ||
| } | ||
| fmt.Fprintf(w, "pruned: removed %d deleted photos from the catalogue", len(res.Gone)) | ||
| if m := movedToTwins(res); m != "" { | ||
| fmt.Fprintf(w, "; %s", m) | ||
| } | ||
| if len(res.Spared) > 0 { | ||
| fmt.Fprintf(w, "; kept %d copies photo apps made, whose hand-given names, ratings or stars would go with them (archivis prune removes them)", len(res.Spared)) | ||
| } | ||
| if len(res.Held) > 0 { | ||
| fmt.Fprintf(w, "; kept %d folders whose files are all missing and that are gone or empty, as an offline drive looks (archivis prune --all-missing removes them): %s", | ||
| len(res.Held), strings.Join(res.Held, ", ")) | ||
| } | ||
| fmt.Fprintln(w) | ||
| return res, true | ||
| } | ||
|
|
||
| // pruneJob is what index --prune needs once the run is over. | ||
| type pruneJob struct { | ||
| folders []string // the folders the job names | ||
| notReady map[string]string // those not ready as indexing started, and why | ||
| gaps int64 // what the run could not read or record | ||
| } | ||
|
|
||
| // cleanUpAfterIndex removes the copies photo apps keep (moving their face | ||
| // names and ratings to the photos they copy), then, for index --prune | ||
| // (pj not nil), the photos deleted from the folders indexed. In that | ||
| // order: a copy whose file is gone, among others still there, would lose | ||
| // its names and ratings if pruned first. Before auto-labelling, so moved | ||
| // names count as examples and deleted photos' faces don't. | ||
| func cleanUpAfterIndex(ctx context.Context, st *store.Store, pj *pruneJob, w io.Writer) { | ||
| // Apple's copies catalogued from inside Photos libraries by older | ||
| // versions: removed once their originals are in (names moved first) | ||
| var failed []string // a copy whose names and ratings weren't moved must not be pruned | ||
| res, err := st.CleanPhotosLibraries(ctx) | ||
| if err != nil { | ||
| fmt.Fprintf(w, "warning: cleaning up Photos library copies: %v (retried on the next run)\n", err) | ||
| failed = append(failed, "the clean-up of Photos library copies failed (see the warning above); their names and ratings must be moved before pruning") | ||
| } | ||
| if res.Removed > 0 || res.Kept > 0 { | ||
| fmt.Fprintf(w, "Photos libraries: removed %d of Apple's copies (moved %d face names, %d ratings to the originals); kept %d with no original catalogued\n", | ||
| res.Removed, res.LabelsMoved, res.FeedbackMoved, res.Kept) | ||
| } | ||
| // then any app copies left: Photos copies with a twin elsewhere, | ||
| // iPhoto, Aperture and Capture One caches | ||
| cres, err := st.CleanAppCaches(ctx) | ||
| if err != nil { | ||
| fmt.Fprintf(w, "warning: cleaning up photo apps' copies: %v (retried on the next run)\n", err) | ||
| failed = append(failed, "the clean-up of photo apps' copies failed (see the warning above); their names and ratings must be moved before pruning") | ||
| } | ||
| if cres.Removed > 0 || cres.Kept > 0 { | ||
| fmt.Fprintf(w, "app copies: removed %d thumbnails and previews of photo apps (moved %d face names, %d ratings to the photos they copy); kept %d (no other copy, or hand-named faces or ratings on them)\n", | ||
| cres.Removed, cres.LabelsMoved, cres.FeedbackMoved, cres.Kept) | ||
| } | ||
| if pj != nil && ctx.Err() == nil { | ||
| pruneAfterIndex(st, pj.folders, pj.notReady, pj.gaps, w, failed...) | ||
|
sgoggins marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
| // movedToTwins says what prune handed from removed photos to identical | ||
| // photos still there, or "" if nothing. | ||
| func movedToTwins(res store.PruneResult) string { | ||
| if res.LabelsMoved+res.RatingsMoved+res.StarsMoved == 0 { | ||
| return "" | ||
| } | ||
| return fmt.Sprintf("moved %d face names, %d ratings and %d stars to identical photos still there", | ||
| res.LabelsMoved, res.RatingsMoved, res.StarsMoved) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.