safer moves, verify, dry run, heic/raw and video dates
Safety: - clean no longer deletes a dated file whose content is missing from the store. os.SameFile(nil, ...) returned false, so clean removed the file and then failed to relink it. MissingLink now reports and skips those files, and Relink swaps the link in with link+rename, so the path exists the whole time. - Move writes the content file under a temporary name, checks it against the hash, fsyncs it, and links it into place. An interrupted run can no longer leave a truncated file that later runs treat as a dup. - The crawl logs and skips unreadable paths instead of stopping, and it ignores AppleDouble ._ files and QNAP .@__thumb directories. Features: - sha256 content addresses by default; -hash=md5 adds to older stores. - am verify rehashes the store and reports corrupt, orphaned, and stale temp files. It only reads. - -n dry run for arr and clean prints the plan as TSV and changes nothing. - arr ends with a summary of counts and sizes by extension and year, with no file names. - heic/heif/avif, tiff, dng, cr2/cr3, nef, and arw are dated by exif via imagemeta, with goexif kept as the jpeg fallback. Exif times without an offset are read as local time, as before. - mov/mp4/m4v/3gp are dated from the moov/mvhd creation time. mkv, mts, webm, and friends are now included and dated by mtime. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"mcquay.me/arrange"
|
||||
)
|
||||
|
||||
// stats tallies a run without recording any file names, so the summary is
|
||||
// safe to share.
|
||||
type stats struct {
|
||||
total int
|
||||
dupes int
|
||||
moved int
|
||||
errors int
|
||||
bytes int64
|
||||
|
||||
ext map[string]*tally
|
||||
year map[int]*tally
|
||||
}
|
||||
|
||||
type tally struct {
|
||||
files, dupes int
|
||||
bytes int64
|
||||
}
|
||||
|
||||
func newStats() *stats {
|
||||
return &stats{ext: map[string]*tally{}, year: map[int]*tally{}}
|
||||
}
|
||||
|
||||
func (s *stats) add(m arrange.Media, dup bool) {
|
||||
s.total++
|
||||
s.bytes += m.Size
|
||||
e := s.ext[m.Extension]
|
||||
if e == nil {
|
||||
e = &tally{}
|
||||
s.ext[m.Extension] = e
|
||||
}
|
||||
y := s.year[m.Time.Year()]
|
||||
if y == nil {
|
||||
y = &tally{}
|
||||
s.year[m.Time.Year()] = y
|
||||
}
|
||||
e.files++
|
||||
e.bytes += m.Size
|
||||
y.files++
|
||||
y.bytes += m.Size
|
||||
if dup {
|
||||
s.dupes++
|
||||
e.dupes++
|
||||
y.dupes++
|
||||
} else {
|
||||
s.moved++
|
||||
}
|
||||
}
|
||||
|
||||
func (s *stats) write(w io.Writer, verb string) {
|
||||
fmt.Fprintf(w, "total: %d files, %s\n", s.total, human(s.bytes))
|
||||
fmt.Fprintf(w, "%s: %d\n", verb, s.moved)
|
||||
fmt.Fprintf(w, "dupes: %d\n", s.dupes)
|
||||
fmt.Fprintf(w, "errors: %d\n", s.errors)
|
||||
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.AlignRight)
|
||||
fmt.Fprintln(tw, "\text\tfiles\tdupes\tsize\t")
|
||||
exts := make([]string, 0, len(s.ext))
|
||||
for k := range s.ext {
|
||||
exts = append(exts, k)
|
||||
}
|
||||
sort.Strings(exts)
|
||||
for _, k := range exts {
|
||||
t := s.ext[k]
|
||||
fmt.Fprintf(tw, "\t%s\t%d\t%d\t%s\t\n", strings.TrimPrefix(k, "."), t.files, t.dupes, human(t.bytes))
|
||||
}
|
||||
tw.Flush()
|
||||
|
||||
tw = tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.AlignRight)
|
||||
fmt.Fprintln(tw, "\tyear\tfiles\tdupes\tsize\t")
|
||||
years := make([]int, 0, len(s.year))
|
||||
for k := range s.year {
|
||||
years = append(years, k)
|
||||
}
|
||||
sort.Ints(years)
|
||||
for _, k := range years {
|
||||
t := s.year[k]
|
||||
fmt.Fprintf(tw, "\t%d\t%d\t%d\t%s\t\n", k, t.files, t.dupes, human(t.bytes))
|
||||
}
|
||||
tw.Flush()
|
||||
}
|
||||
|
||||
func human(n int64) string {
|
||||
const unit = 1024
|
||||
if n < unit {
|
||||
return fmt.Sprintf("%dB", n)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for m := n / unit; m >= unit; m /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f%ciB", float64(n)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
Reference in New Issue
Block a user