2016-05-18 21:11:18 -07:00
|
|
|
package arrange
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-22 13:59:09 -07:00
|
|
|
"errors"
|
2016-05-18 21:11:18 -07:00
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2026-10-22 13:59:09 -07:00
|
|
|
"io/fs"
|
2016-05-18 21:11:18 -07:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2016-05-19 21:29:17 -07:00
|
|
|
// Media is the high-level filetype that can be arranged.
|
2016-05-18 21:11:18 -07:00
|
|
|
type Media struct {
|
|
|
|
|
Path string
|
|
|
|
|
Hash string
|
|
|
|
|
Extension string
|
|
|
|
|
Time time.Time
|
2026-10-22 13:59:09 -07:00
|
|
|
Size int64
|
2016-05-18 21:11:18 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:29:17 -07:00
|
|
|
// Move is called to push Media into its final destination, by content address
|
|
|
|
|
// and by date.
|
2026-10-22 13:59:09 -07:00
|
|
|
//
|
|
|
|
|
// 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.
|
2016-05-18 21:11:18 -07:00
|
|
|
func (m Media) Move(root string) error {
|
2016-07-09 21:40:14 -07:00
|
|
|
content := m.Content(root)
|
2016-05-18 21:11:18 -07:00
|
|
|
|
|
|
|
|
if _, err := os.Stat(content); !os.IsNotExist(err) {
|
|
|
|
|
return Dup{content}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
if err := m.store(content); err != nil {
|
|
|
|
|
if errors.Is(err, fs.ErrExist) {
|
|
|
|
|
return Dup{content}
|
|
|
|
|
}
|
|
|
|
|
return err
|
2016-05-22 09:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 21:11:18 -07:00
|
|
|
year := fmt.Sprintf("%04d", m.Time.Year())
|
|
|
|
|
month := fmt.Sprintf("%02d", m.Time.Month())
|
|
|
|
|
ts := fmt.Sprintf("%d", m.Time.UnixNano())
|
|
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Join(root, "date", year, month), 0755); err != nil {
|
|
|
|
|
return fmt.Errorf("problem creating date directory: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
date := filepath.Join(root, "date", year, month, ts)
|
|
|
|
|
name := date + m.Extension
|
|
|
|
|
for i := 0; i < 10000; i++ {
|
2026-10-22 13:59:09 -07:00
|
|
|
err := os.Link(content, name)
|
|
|
|
|
if !errors.Is(err, fs.ErrExist) {
|
|
|
|
|
return err
|
2016-05-18 21:11:18 -07:00
|
|
|
}
|
|
|
|
|
name = fmt.Sprintf("%s_%04d%s", date, i, m.Extension)
|
|
|
|
|
}
|
2026-10-22 13:59:09 -07:00
|
|
|
return fmt.Errorf("too many files at %q", date)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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())
|
2016-05-18 21:11:18 -07:00
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
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)
|
2016-05-18 21:11:18 -07:00
|
|
|
}
|
2016-07-09 21:40:14 -07:00
|
|
|
|
|
|
|
|
// Content returns the content-address path starting at root.
|
|
|
|
|
func (m Media) Content(root string) string {
|
|
|
|
|
return filepath.Join(root, "content", m.Hash[:2], m.Hash[2:]+m.Extension)
|
|
|
|
|
}
|