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:
+141
-79
@@ -2,11 +2,14 @@ package arrange
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"hash"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -15,22 +18,64 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var exts map[string]bool
|
||||
// kind is how a file extension gets parsed.
|
||||
type kind int
|
||||
|
||||
func init() {
|
||||
exts = map[string]bool{
|
||||
// images
|
||||
".jpg": true,
|
||||
".jpeg": true,
|
||||
".png": true,
|
||||
".gif": true,
|
||||
const (
|
||||
kindJPEG kind = iota
|
||||
kindPNG
|
||||
kindGIF
|
||||
kindStill // HEIC, camera RAW, etc.: dated by exif, no decode check
|
||||
kindQuickTime // mov/mp4 family: dated by the mvhd atom
|
||||
kindVideo // everything else: dated by mtime
|
||||
)
|
||||
|
||||
// videos
|
||||
".mov": true,
|
||||
".mp4": true,
|
||||
".m4v": true,
|
||||
".avi": true,
|
||||
var exts = map[string]kind{
|
||||
// images
|
||||
".jpg": kindJPEG,
|
||||
".jpeg": kindJPEG,
|
||||
".png": kindPNG,
|
||||
".gif": kindGIF,
|
||||
".heic": kindStill,
|
||||
".heif": kindStill,
|
||||
".avif": kindStill,
|
||||
".tif": kindStill,
|
||||
".tiff": kindStill,
|
||||
".dng": kindStill,
|
||||
".cr2": kindStill,
|
||||
".cr3": kindStill,
|
||||
".nef": kindStill,
|
||||
".arw": kindStill,
|
||||
|
||||
// videos
|
||||
".mov": kindQuickTime,
|
||||
".mp4": kindQuickTime,
|
||||
".m4v": kindQuickTime,
|
||||
".3gp": kindQuickTime,
|
||||
".avi": kindVideo,
|
||||
".mkv": kindVideo,
|
||||
".mts": kindVideo,
|
||||
".m2ts": kindVideo,
|
||||
".mpg": kindVideo,
|
||||
".mpeg": kindVideo,
|
||||
".wmv": kindVideo,
|
||||
".webm": kindVideo,
|
||||
}
|
||||
|
||||
// NewHash makes the hash used for content addresses. Stores built by older
|
||||
// versions used md5; set this to md5.New to keep adding to one of those.
|
||||
var NewHash func() hash.Hash = sha256.New
|
||||
|
||||
// HashByName returns the hash constructor matching the length of a hex
|
||||
// content address, or nil if it is not one we know.
|
||||
func HashByName(hexname string) func() hash.Hash {
|
||||
switch len(hexname) {
|
||||
case 2 * md5.Size:
|
||||
return md5.New
|
||||
case 2 * sha256.Size:
|
||||
return sha256.New
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mtime(path string) (time.Time, error) {
|
||||
@@ -56,21 +101,34 @@ func PrepOutput(root string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Source returns sends all files that match known extensions.
|
||||
// Source sends all files that match known extensions. Unreadable paths are
|
||||
// logged and skipped rather than ending the crawl.
|
||||
func Source(root string) <-chan string {
|
||||
out := make(chan string)
|
||||
go func() {
|
||||
err := filepath.Walk(
|
||||
err := filepath.WalkDir(
|
||||
root,
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
if path == root {
|
||||
return err
|
||||
}
|
||||
log.Printf("skipping %q: %v", path, err)
|
||||
return nil
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(path))
|
||||
if _, ok := exts[ext]; ok {
|
||||
name := d.Name()
|
||||
if d.IsDir() {
|
||||
// QNAP thumbnail caches
|
||||
if name == ".@__thumb" {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// AppleDouble resource forks
|
||||
if strings.HasPrefix(name, "._") {
|
||||
return nil
|
||||
}
|
||||
if _, ok := exts[strings.ToLower(filepath.Ext(path))]; ok {
|
||||
out <- path
|
||||
}
|
||||
return nil
|
||||
@@ -112,19 +170,23 @@ func Parse(in <-chan string) <-chan Media {
|
||||
}
|
||||
|
||||
// MissingLink detects if the values coming from medias is a duplicate file
|
||||
// rather than a hardlink to the content store.
|
||||
// rather than a hardlink to the content store. Files whose content is not in
|
||||
// the store are reported on errs and never sent on, since there is nothing to
|
||||
// link them to.
|
||||
func MissingLink(medias <-chan Media, root string) (<-chan Media, <-chan error) {
|
||||
out := make(chan Media)
|
||||
errs := make(chan error)
|
||||
go func() {
|
||||
for m := range medias {
|
||||
var d, c os.FileInfo
|
||||
var err error
|
||||
if d, err = os.Stat(m.Path); err != nil {
|
||||
d, err := os.Stat(m.Path)
|
||||
if err != nil {
|
||||
errs <- err
|
||||
continue
|
||||
}
|
||||
if c, err = os.Stat(m.Content(root)); err != nil {
|
||||
errs <- err
|
||||
c, err := os.Stat(m.Content(root))
|
||||
if err != nil {
|
||||
errs <- fmt.Errorf("%q has no content in store: %v", m.Path, err)
|
||||
continue
|
||||
}
|
||||
if !os.SameFile(d, c) {
|
||||
out <- m
|
||||
@@ -136,6 +198,24 @@ func MissingLink(medias <-chan Media, root string) (<-chan Media, <-chan error)
|
||||
return out, errs
|
||||
}
|
||||
|
||||
// Relink atomically replaces m.Path with a hardlink to its content in the
|
||||
// store, so m.Path exists at every moment.
|
||||
func Relink(m Media, root string) error {
|
||||
content := m.Content(root)
|
||||
if _, err := os.Stat(content); err != nil {
|
||||
return fmt.Errorf("refusing to relink %q: %v", m.Path, err)
|
||||
}
|
||||
tmp := filepath.Join(filepath.Dir(m.Path), fmt.Sprintf(".arrange-%d-%s", os.Getpid(), filepath.Base(m.Path)))
|
||||
if err := os.Link(content, tmp); err != nil {
|
||||
return fmt.Errorf("couldn't link %q: %v", tmp, err)
|
||||
}
|
||||
if err := os.Rename(tmp, m.Path); err != nil {
|
||||
os.Remove(tmp)
|
||||
return fmt.Errorf("couldn't replace %q: %v", m.Path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Move calls Move on each Media on input chan. It is the first step in the
|
||||
// pipeline after fan-in.
|
||||
func Move(in <-chan Media, root string) <-chan error {
|
||||
@@ -153,8 +233,11 @@ func Move(in <-chan Media, root string) <-chan error {
|
||||
func ParseFile(path string) (Media, error) {
|
||||
ext := strings.ToLower(filepath.Ext(path))
|
||||
var r Media
|
||||
hash := md5.New()
|
||||
var t time.Time
|
||||
|
||||
k, ok := exts[ext]
|
||||
if !ok {
|
||||
return r, NotMedia{path}
|
||||
}
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
@@ -162,65 +245,43 @@ func ParseFile(path string) (Media, error) {
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
switch ext {
|
||||
default:
|
||||
// make sure it is what it says it is
|
||||
switch k {
|
||||
case kindJPEG:
|
||||
_, err = jpeg.DecodeConfig(f)
|
||||
case kindPNG:
|
||||
_, err = png.DecodeConfig(f)
|
||||
case kindGIF:
|
||||
_, err = gif.DecodeConfig(f)
|
||||
}
|
||||
if err != nil {
|
||||
return r, NotMedia{path}
|
||||
case ".jpg", ".jpeg":
|
||||
if _, err := jpeg.DecodeConfig(f); err != nil {
|
||||
return r, NotMedia{path}
|
||||
}
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
return r, fmt.Errorf("couldn't seek back in file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// try a few things for a time value
|
||||
{
|
||||
success := false
|
||||
if t, err = parseExif(f); err == nil {
|
||||
success = true
|
||||
}
|
||||
if !success {
|
||||
t, err = mtime(path)
|
||||
}
|
||||
if err != nil {
|
||||
return r, fmt.Errorf("unable to calculate reasonble time for jpg %q: %v", path, err)
|
||||
// try a few things for a time value, falling back to mtime
|
||||
var t time.Time
|
||||
switch k {
|
||||
case kindJPEG, kindPNG, kindStill:
|
||||
t = exifTime(f, k == kindJPEG)
|
||||
case kindQuickTime:
|
||||
if _, err := f.Seek(0, io.SeekStart); err == nil {
|
||||
if qt, err := quickTimeCreated(f); err == nil {
|
||||
t = qt
|
||||
}
|
||||
}
|
||||
case ".png":
|
||||
if _, err := png.DecodeConfig(f); err != nil {
|
||||
return r, NotMedia{path}
|
||||
}
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
return r, fmt.Errorf("couldn't seek back in file: %v", err)
|
||||
}
|
||||
|
||||
t, err = mtime(path)
|
||||
if err != nil {
|
||||
return r, fmt.Errorf("unable to calculate reasonble time for media %q: %v", path, err)
|
||||
}
|
||||
case ".gif":
|
||||
if _, err := gif.DecodeConfig(f); err != nil {
|
||||
return r, NotMedia{path}
|
||||
}
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
return r, fmt.Errorf("couldn't seek back in file: %v", err)
|
||||
}
|
||||
|
||||
t, err = mtime(path)
|
||||
if err != nil {
|
||||
return r, fmt.Errorf("unable to calculate reasonble time for media %q: %v", path, err)
|
||||
}
|
||||
case ".mov", ".mp4", ".m4v", ".avi":
|
||||
t, err = mtime(path)
|
||||
if err != nil {
|
||||
return r, fmt.Errorf("unable to calculate reasonble time for media %q: %v", path, err)
|
||||
}
|
||||
if t.IsZero() {
|
||||
if t, err = mtime(path); err != nil {
|
||||
return r, fmt.Errorf("unable to calculate reasonable time for media %q: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
||||
return r, fmt.Errorf("couldn't seek back in file: %v", err)
|
||||
}
|
||||
if _, err := io.Copy(hash, f); err != nil {
|
||||
hash := NewHash()
|
||||
n, err := io.Copy(hash, f)
|
||||
if err != nil {
|
||||
return r, fmt.Errorf("problem calculating checksum on %q: %v", path, err)
|
||||
}
|
||||
r = Media{
|
||||
@@ -228,6 +289,7 @@ func ParseFile(path string) (Media, error) {
|
||||
Hash: fmt.Sprintf("%x", hash.Sum(nil)),
|
||||
Extension: ext,
|
||||
Time: t,
|
||||
Size: n,
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user