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:
+38
-16
@@ -3,14 +3,18 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"mcquay.me/arrange"
|
||||
)
|
||||
|
||||
func arr(indir, outdir string) error {
|
||||
if err := arrange.PrepOutput(outdir); err != nil {
|
||||
return fmt.Errorf("problem creating directory structure: %v", err)
|
||||
if !*dryRun {
|
||||
if err := arrange.PrepOutput(outdir); err != nil {
|
||||
return fmt.Errorf("problem creating directory structure: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
work := arrange.Source(indir)
|
||||
@@ -25,23 +29,41 @@ func arr(indir, outdir string) error {
|
||||
streams = append(streams, arrange.Parse(work))
|
||||
}
|
||||
|
||||
st := stats{}
|
||||
for err := range arrange.Move(arrange.Merge(streams), outdir) {
|
||||
st.total++
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case arrange.Dup:
|
||||
st.dupes++
|
||||
default:
|
||||
log.Printf("%+v", err)
|
||||
st := newStats()
|
||||
// in a dry run nothing lands in the store, so remember what would have
|
||||
seen := map[string]bool{}
|
||||
for m := range arrange.Merge(streams) {
|
||||
if *dryRun {
|
||||
content := m.Content(outdir)
|
||||
_, err := os.Stat(content)
|
||||
dup := seen[m.Hash] || err == nil
|
||||
seen[m.Hash] = true
|
||||
action := "new"
|
||||
if dup {
|
||||
action = "dup"
|
||||
}
|
||||
} else {
|
||||
st.moved++
|
||||
date := filepath.Join(outdir, "date", fmt.Sprintf("%04d", m.Time.Year()), fmt.Sprintf("%02d", m.Time.Month()))
|
||||
fmt.Printf("%s\t%s\t%s\t%s\n", action, m.Path, content, date)
|
||||
st.add(m, dup)
|
||||
continue
|
||||
}
|
||||
|
||||
err := m.Move(outdir)
|
||||
switch err.(type) {
|
||||
case nil:
|
||||
st.add(m, false)
|
||||
case arrange.Dup:
|
||||
st.add(m, true)
|
||||
default:
|
||||
st.errors++
|
||||
log.Printf("%+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("dupes: %+v", st.dupes)
|
||||
log.Printf("moved: %+v", st.moved)
|
||||
log.Printf("total: %+v", st.total)
|
||||
verb := "moved"
|
||||
if *dryRun {
|
||||
verb = "would move"
|
||||
}
|
||||
st.write(os.Stderr, verb)
|
||||
return nil
|
||||
}
|
||||
|
||||
+24
-7
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
@@ -32,25 +33,41 @@ func clean(dir string) error {
|
||||
errs = append(errs, e)
|
||||
}
|
||||
|
||||
var err error
|
||||
var problems int
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
for e := range eMerge(errs) {
|
||||
log.Printf("%+v", e)
|
||||
err = fmt.Errorf("%v, %v", err, e)
|
||||
problems++
|
||||
}
|
||||
close(done)
|
||||
}()
|
||||
|
||||
var relinked, failed int
|
||||
for m := range arrange.Merge(streams) {
|
||||
log.Printf("%q > %q", m.Path, m.Content(dir))
|
||||
if err := os.Remove(m.Path); err != nil {
|
||||
log.Printf("%+v", err)
|
||||
if *dryRun {
|
||||
fmt.Printf("relink\t%s\t%s\n", m.Path, m.Content(dir))
|
||||
relinked++
|
||||
continue
|
||||
}
|
||||
if err := os.Link(m.Content(dir), m.Path); err != nil {
|
||||
if err := arrange.Relink(m, dir); err != nil {
|
||||
log.Printf("%+v", err)
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
relinked++
|
||||
}
|
||||
<-done
|
||||
|
||||
return err
|
||||
verb := "relinked"
|
||||
if *dryRun {
|
||||
verb = "would relink"
|
||||
}
|
||||
log.Printf("%s: %d, failed: %d, problems: %d", verb, relinked, failed, problems)
|
||||
if problems+failed > 0 {
|
||||
return errors.New("some files could not be checked or relinked; see log")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func eMerge(cs []<-chan error) <-chan error {
|
||||
|
||||
+30
-9
@@ -1,24 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"mcquay.me/arrange"
|
||||
)
|
||||
|
||||
const usage = "am <arr|clean|meta> [flags]"
|
||||
const arrUsage = "am arr [-h|-cores=N] <in> <out>"
|
||||
const cleanUsage = "am clean [-h|-cores=N] <directory>"
|
||||
const usage = "am <arr|clean|verify|meta> [flags]"
|
||||
const arrUsage = "am arr [-h|-n|-hash=sha256|md5|-cores=N] <in> <out>"
|
||||
const cleanUsage = "am clean [-h|-n|-cores=N] <directory>"
|
||||
const verifyUsage = "am verify [-h|-cores=N] <directory>"
|
||||
const metaUsage = "am meta [-h|-cores=N] <file0> <file1> ... <fileN>"
|
||||
|
||||
type stats struct {
|
||||
total int
|
||||
dupes int
|
||||
moved int
|
||||
}
|
||||
|
||||
var cores = flag.Int("cores", 0, "how many threads to use")
|
||||
var dryRun = flag.Bool("n", false, "dry run: print the plan as tab-separated lines, change nothing")
|
||||
var hashName = flag.String("hash", "sha256", "content-address hash: sha256, or md5 to add to a store built by older versions")
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
@@ -31,6 +32,16 @@ func main() {
|
||||
flag.Parse()
|
||||
log.SetFlags(log.Lshortfile)
|
||||
|
||||
switch *hashName {
|
||||
case "sha256":
|
||||
arrange.NewHash = sha256.New
|
||||
case "md5":
|
||||
arrange.NewHash = md5.New
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unsupported hash %q (supported: sha256, md5)\n", *hashName)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
switch sub {
|
||||
case "ar", "arr", "arrange":
|
||||
args := flag.Args()
|
||||
@@ -54,6 +65,16 @@ func main() {
|
||||
fmt.Fprintf(os.Stderr, "problem cleaning: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
case "v", "verify":
|
||||
args := flag.Args()
|
||||
if len(args) != 1 {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", verifyUsage)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := verify(args[0]); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "problem verifying: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
case "m", "meta":
|
||||
args := flag.Args()
|
||||
if len(args) < 1 {
|
||||
|
||||
+2
-2
@@ -26,9 +26,9 @@ func meta(files []string) {
|
||||
pf, err := arrange.ParseFile(pth)
|
||||
if err != nil {
|
||||
log.Printf("%+v", err)
|
||||
} else {
|
||||
fc <- pf
|
||||
}
|
||||
|
||||
fc <- pf
|
||||
<-s
|
||||
wg.Done()
|
||||
}(f)
|
||||
|
||||
+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])
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"mcquay.me/arrange"
|
||||
)
|
||||
|
||||
// verify rehashes the content store. It only reads.
|
||||
func verify(dir string) error {
|
||||
if _, err := os.Stat(filepath.Join(dir, "content")); err != nil {
|
||||
return fmt.Errorf("couldn't find 'content' dir in %q: %v", dir, err)
|
||||
}
|
||||
|
||||
workers := runtime.NumCPU()
|
||||
if *cores != 0 {
|
||||
workers = *cores
|
||||
}
|
||||
|
||||
counts := map[string]int{}
|
||||
problems, checked := arrange.Verify(dir, workers)
|
||||
for p := range problems {
|
||||
counts[p.Kind]++
|
||||
log.Printf("%v", p)
|
||||
}
|
||||
|
||||
log.Printf("checked: %d, corrupt: %d, orphan: %d, stale: %d, errors: %d",
|
||||
checked(), counts["corrupt"], counts["orphan"], counts["stale"], counts["error"])
|
||||
if counts["corrupt"]+counts["error"] > 0 {
|
||||
return fmt.Errorf("store has problems")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user