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:
@@ -1,8 +1,10 @@
|
||||
package arrange
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@@ -14,35 +16,27 @@ type Media struct {
|
||||
Hash string
|
||||
Extension string
|
||||
Time time.Time
|
||||
Size int64
|
||||
}
|
||||
|
||||
// Move is called to push Media into its final destination, by content address
|
||||
// and by date.
|
||||
//
|
||||
// The content file is written under a temporary name, checked against m.Hash,
|
||||
// synced, and only then linked into place, so an interrupted run never leaves
|
||||
// a truncated file in the store looking like a dup.
|
||||
func (m Media) Move(root string) error {
|
||||
f, err := os.Open(m.Path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("problem opening file %q: %v", m.Path, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
content := m.Content(root)
|
||||
|
||||
if _, err := os.Stat(content); !os.IsNotExist(err) {
|
||||
return Dup{content}
|
||||
}
|
||||
|
||||
out, err := os.Create(content)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create output file: %v", err)
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
if _, err := io.Copy(out, f); err != nil {
|
||||
return fmt.Errorf("trouble copying file: %v", err)
|
||||
}
|
||||
|
||||
if err := os.Chtimes(content, time.Now(), m.Time); err != nil {
|
||||
return fmt.Errorf("couldn't chtimes for %q: %v", content, err)
|
||||
if err := m.store(content); err != nil {
|
||||
if errors.Is(err, fs.ErrExist) {
|
||||
return Dup{content}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
year := fmt.Sprintf("%04d", m.Time.Year())
|
||||
@@ -56,16 +50,54 @@ func (m Media) Move(root string) error {
|
||||
date := filepath.Join(root, "date", year, month, ts)
|
||||
name := date + m.Extension
|
||||
for i := 0; i < 10000; i++ {
|
||||
if _, err := os.Stat(name); os.IsNotExist(err) {
|
||||
break
|
||||
err := os.Link(content, name)
|
||||
if !errors.Is(err, fs.ErrExist) {
|
||||
return err
|
||||
}
|
||||
name = fmt.Sprintf("%s_%04d%s", date, i, m.Extension)
|
||||
}
|
||||
return fmt.Errorf("too many files at %q", date)
|
||||
}
|
||||
|
||||
// TODO: or maybe symlinking? (issue #2)
|
||||
// rel := filepath.Join("..", "..", "..", "content", j.hash[:2], j.hash[2:]+m.Extension)
|
||||
// return os.Symlink(rel, name)
|
||||
return os.Link(content, name)
|
||||
// store copies m.Path to content via a verified temporary file.
|
||||
func (m Media) store(content string) error {
|
||||
f, err := os.Open(m.Path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("problem opening file %q: %v", m.Path, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
tmp, err := os.CreateTemp(filepath.Dir(content), ".tmp-")
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create output file: %v", err)
|
||||
}
|
||||
defer os.Remove(tmp.Name())
|
||||
|
||||
hash := NewHash()
|
||||
if _, err := io.Copy(io.MultiWriter(tmp, hash), f); err != nil {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("trouble copying file: %v", err)
|
||||
}
|
||||
if got := fmt.Sprintf("%x", hash.Sum(nil)); got != m.Hash {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("%q changed while arranging: hashed %s, copied %s", m.Path, m.Hash, got)
|
||||
}
|
||||
if err := tmp.Sync(); err != nil {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("couldn't sync %q: %v", tmp.Name(), err)
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return fmt.Errorf("couldn't close %q: %v", tmp.Name(), err)
|
||||
}
|
||||
if err := os.Chmod(tmp.Name(), 0644); err != nil {
|
||||
return fmt.Errorf("couldn't chmod %q: %v", tmp.Name(), err)
|
||||
}
|
||||
if err := os.Chtimes(tmp.Name(), time.Now(), m.Time); err != nil {
|
||||
return fmt.Errorf("couldn't chtimes for %q: %v", tmp.Name(), err)
|
||||
}
|
||||
// link, not rename: it fails rather than clobbers if content appeared
|
||||
// in the meantime.
|
||||
return os.Link(tmp.Name(), content)
|
||||
}
|
||||
|
||||
// Content returns the content-address path starting at root.
|
||||
|
||||
Reference in New Issue
Block a user