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>
291 lines
7.4 KiB
Go
291 lines
7.4 KiB
Go
package arrange
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// atom builds a QuickTime atom with a 32-bit size.
|
|
func atom(typ string, body ...[]byte) []byte {
|
|
b := bytes.Join(body, nil)
|
|
out := make([]byte, 8, 8+len(b))
|
|
binary.BigEndian.PutUint32(out, uint32(8+len(b)))
|
|
copy(out[4:], typ)
|
|
return append(out, b...)
|
|
}
|
|
|
|
// mvhd builds a movie header body of the given version with creation time t.
|
|
func mvhd(version byte, t time.Time) []byte {
|
|
secs := uint64(t.Sub(epoch1904) / time.Second)
|
|
if version == 0 {
|
|
b := make([]byte, 100)
|
|
binary.BigEndian.PutUint32(b[4:], uint32(secs))
|
|
return b
|
|
}
|
|
b := make([]byte, 112)
|
|
b[0] = 1
|
|
binary.BigEndian.PutUint64(b[4:], secs)
|
|
return b
|
|
}
|
|
|
|
func TestQuickTimeCreated(t *testing.T) {
|
|
when := time.Date(2019, 7, 4, 20, 15, 0, 0, time.UTC)
|
|
|
|
// 64-bit size atom: size field 1, then the real size
|
|
big := make([]byte, 16+4)
|
|
binary.BigEndian.PutUint32(big, 1)
|
|
copy(big[4:], "free")
|
|
binary.BigEndian.PutUint64(big[8:], uint64(len(big)))
|
|
|
|
tests := []struct {
|
|
name string
|
|
file []byte
|
|
want time.Time
|
|
ok bool
|
|
}{
|
|
{"v0", bytes.Join([][]byte{atom("ftyp", []byte("qt ")), atom("moov", atom("mvhd", mvhd(0, when)))}, nil), when, true},
|
|
{"v1", bytes.Join([][]byte{atom("ftyp", []byte("isom")), atom("mdat", make([]byte, 64)), atom("moov", atom("trak"), atom("mvhd", mvhd(1, when)))}, nil), when, true},
|
|
{"largesize", bytes.Join([][]byte{big, atom("moov", atom("mvhd", mvhd(0, when)))}, nil), when, true},
|
|
{"zero time", atom("moov", atom("mvhd", mvhd(0, epoch1904))), time.Time{}, false},
|
|
{"no moov", atom("ftyp", []byte("isom")), time.Time{}, false},
|
|
{"garbage", []byte("a\n"), time.Time{}, false},
|
|
{"lying size", append(atom("moov", atom("mvhd", mvhd(0, when)))[:4], []byte("moov")...), time.Time{}, false},
|
|
}
|
|
for _, test := range tests {
|
|
got, err := quickTimeCreated(bytes.NewReader(test.file))
|
|
if (err == nil) != test.ok {
|
|
t.Errorf("%s: err = %v, want ok=%v", test.name, err, test.ok)
|
|
continue
|
|
}
|
|
if test.ok && !got.Equal(test.want) {
|
|
t.Errorf("%s: got %v, want %v", test.name, got, test.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseQuickTimeDate(t *testing.T) {
|
|
when := time.Date(2019, 7, 4, 20, 15, 0, 0, time.UTC)
|
|
p := filepath.Join(t.TempDir(), "clip.MP4")
|
|
if err := os.WriteFile(p, atom("moov", atom("mvhd", mvhd(0, when))), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m, err := ParseFile(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !m.Time.Equal(when) {
|
|
t.Errorf("got %v, want %v from mvhd", m.Time, when)
|
|
}
|
|
if m.Extension != ".mp4" {
|
|
t.Errorf("extension %q, want .mp4", m.Extension)
|
|
}
|
|
}
|
|
|
|
func TestParseHEIC(t *testing.T) {
|
|
m, err := ParseFile(filepath.Join("testdata", "exif.heic"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := time.Date(2006, 3, 27, 17, 19, 27, 0, time.Local)
|
|
if !m.Time.Equal(want) {
|
|
t.Errorf("got %v, want %v", m.Time, want)
|
|
}
|
|
if len(m.Hash) != 64 {
|
|
t.Errorf("hash %q isn't sha256", m.Hash)
|
|
}
|
|
}
|
|
|
|
func TestSourceSkips(t *testing.T) {
|
|
root := t.TempDir()
|
|
for _, p := range []string{
|
|
"keep.jpg",
|
|
"sub/KEEP.HEIC",
|
|
"._resource-fork.jpg",
|
|
".@__thumb/thumb.jpg",
|
|
"notes.txt",
|
|
} {
|
|
full := filepath.Join(root, p)
|
|
if err := os.MkdirAll(filepath.Dir(full), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(full, nil, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
unreadable := filepath.Join(root, "locked")
|
|
if err := os.Mkdir(unreadable, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Chmod(unreadable, 0755)
|
|
|
|
var got []string
|
|
for p := range Source(root) {
|
|
rel, _ := filepath.Rel(root, p)
|
|
got = append(got, rel)
|
|
}
|
|
if want := "keep.jpg sub/KEEP.HEIC"; strings.Join(got, " ") != want {
|
|
t.Errorf("got %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
// arranged returns a store with one file arranged into it, and that Media.
|
|
func arranged(t *testing.T) (string, Media) {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
if err := PrepOutput(root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m, err := ParseFile(filepath.Join("testdata", "lenna.png"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.Move(root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return root, m
|
|
}
|
|
|
|
func dated(t *testing.T, root string) []string {
|
|
t.Helper()
|
|
var out []string
|
|
for p := range Source(filepath.Join(root, "date")) {
|
|
out = append(out, p)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// The old clean deleted date files whose content was missing from the store,
|
|
// then failed to relink them, losing the file.
|
|
func TestCleanKeepsFilesMissingFromStore(t *testing.T) {
|
|
root, m := arranged(t)
|
|
d := dated(t, root)
|
|
if len(d) != 1 {
|
|
t.Fatalf("expected one dated file, got %v", d)
|
|
}
|
|
if err := os.Remove(m.Content(root)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
pm, err := ParseFile(d[0])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
in := make(chan Media, 1)
|
|
in <- pm
|
|
close(in)
|
|
out, errs := MissingLink(in, root)
|
|
var nerr int
|
|
go func() {
|
|
for range errs {
|
|
nerr++
|
|
}
|
|
}()
|
|
for m := range out {
|
|
t.Errorf("MissingLink sent %q on for relinking with no content", m.Path)
|
|
}
|
|
if err := Relink(pm, root); err == nil {
|
|
t.Error("Relink should refuse when content is missing")
|
|
}
|
|
if _, err := os.Stat(d[0]); err != nil {
|
|
t.Errorf("dated file is gone: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRelinkReplacesCopy(t *testing.T) {
|
|
root, m := arranged(t)
|
|
d := dated(t, root)[0]
|
|
|
|
// break the hardlink: replace the dated file with an identical copy
|
|
b, err := os.ReadFile(d)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Remove(d); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(d, b, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
pm, err := ParseFile(d)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Relink(pm, root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a, _ := os.Stat(d)
|
|
c, _ := os.Stat(m.Content(root))
|
|
if !os.SameFile(a, c) {
|
|
t.Error("dated file isn't a hardlink to content after Relink")
|
|
}
|
|
if left, _ := filepath.Glob(filepath.Join(filepath.Dir(d), ".arrange-*")); len(left) > 0 {
|
|
t.Errorf("temp files left behind: %v", left)
|
|
}
|
|
}
|
|
|
|
func TestMoveVerifiesHash(t *testing.T) {
|
|
root := t.TempDir()
|
|
if err := PrepOutput(root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m, err := ParseFile(filepath.Join("testdata", "lenna.png"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// as if the file changed between hashing and copying
|
|
m.Hash = strings.Repeat("ab", 32)
|
|
if err := m.Move(root); err == nil {
|
|
t.Fatal("Move should fail when the copy doesn't match the hash")
|
|
}
|
|
left, _ := filepath.Glob(filepath.Join(root, "content", "ab", "*"))
|
|
hidden, _ := filepath.Glob(filepath.Join(root, "content", "ab", ".*"))
|
|
if len(left)+len(hidden) > 0 {
|
|
t.Errorf("store not clean after failed move: %v %v", left, hidden)
|
|
}
|
|
if err := m.Move(root); err == nil {
|
|
t.Fatal("a second try should fail too, not be called a dup")
|
|
}
|
|
}
|
|
|
|
func TestVerify(t *testing.T) {
|
|
root, m := arranged(t)
|
|
|
|
problems := func() map[string]int {
|
|
out := map[string]int{}
|
|
ps, checked := Verify(root, 2)
|
|
for p := range ps {
|
|
out[p.Kind]++
|
|
}
|
|
out["checked"] = checked()
|
|
return out
|
|
}
|
|
|
|
if got := problems(); got["checked"] != 1 || len(got) != 1 {
|
|
t.Fatalf("healthy store: %v", got)
|
|
}
|
|
|
|
// orphan: content nothing links to
|
|
for _, d := range dated(t, root) {
|
|
os.Remove(d)
|
|
}
|
|
// stale: a temp file from a crashed run
|
|
os.WriteFile(filepath.Join(root, "content", "00", ".tmp-123"), []byte("x"), 0644)
|
|
if got := problems(); got["orphan"] != 1 || got["stale"] != 1 {
|
|
t.Errorf("want one orphan and one stale, got %v", got)
|
|
}
|
|
|
|
// corrupt: bytes no longer match the name
|
|
if err := os.WriteFile(m.Content(root), []byte("bitrot"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := problems(); got["corrupt"] != 1 {
|
|
t.Errorf("want one corrupt, got %v", got)
|
|
}
|
|
}
|