-
Notifications
You must be signed in to change notification settings - Fork 1
/
flaccat.go
73 lines (68 loc) · 1.65 KB
/
flaccat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// flaccat
//
// Usage:
//
// flaccat <path>
//
// Scans all directories under the specified path for FLAC files, and outputs
// a catalog file to standard output consisting of the MD5 checksum for each
// FLAC file, followed by the path to the file.
//
// By comparing successive versions of the output, you can determine which
// flac files have been added, removed, or moved. The flacdiff utility will
// perform that function.
package main
import (
"encoding/hex"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/mewkiz/flac"
)
func checksum(fspc string) (string, error) {
stream, err := flac.ParseFile(fspc)
if err != nil {
return "", err
}
defer stream.Close()
info := stream.Info
md5 := hex.EncodeToString(info.MD5sum[:])
return md5, nil
}
func examine(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if strings.HasSuffix(path, "/#recycle") || strings.HasSuffix(path, "/@eaDir") {
return filepath.SkipDir
}
} else {
lcpath := strings.ToLower(path)
if strings.HasSuffix(lcpath, ".flac") {
md5, ferr := checksum(path)
if ferr != nil {
fmt.Fprintf(os.Stderr, "error reading %s: %s\n", path, ferr)
} else {
fmt.Printf("%s %s\n", md5, path)
}
}
}
return err
}
func main() {
flag.Usage = func() {
cmdname := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "%s - catalog FLAC files by checksum for use with flacdiff\nusage: %s path ...\n", cmdname, cmdname)
flag.PrintDefaults()
}
flag.Parse()
for _, fname := range flag.Args() {
if err := filepath.WalkDir(fname, examine); err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", fname, err)
}
}
}