| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base32" |
| 5 |
"encoding/binary" |
| 6 |
"flag" |
| 7 |
"fmt" |
| 8 |
"hash/fnv" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"github.com/kisom/goutils/fileutil" |
| 15 |
"github.com/kisom/goutils/lib" |
| 16 |
) |
| 17 |
|
| 18 |
func hashName(path, encodedHash string) string { |
| 19 |
basename := filepath.Base(path) |
| 20 |
location := filepath.Dir(path) |
| 21 |
ext := filepath.Ext(basename) |
| 22 |
return filepath.Join(location, encodedHash+ext) |
| 23 |
} |
| 24 |
|
| 25 |
func newName(path string) (string, error) { |
| 26 |
h := fnv.New32a() |
| 27 |
|
| 28 |
f, err := os.Open(path) |
| 29 |
if err != nil { |
| 30 |
return "", err |
| 31 |
} |
| 32 |
defer f.Close() |
| 33 |
|
| 34 |
_, err = io.Copy(h, f) |
| 35 |
if err != nil { |
| 36 |
return "", err |
| 37 |
} |
| 38 |
|
| 39 |
var buf [8]byte |
| 40 |
binary.BigEndian.PutUint32(buf[:], h.Sum32()) |
| 41 |
encodedHash := base32.StdEncoding.EncodeToString(h.Sum(nil)) |
| 42 |
encodedHash = strings.TrimRight(encodedHash, "=") |
| 43 |
return hashName(path, encodedHash), nil |
| 44 |
} |
| 45 |
|
| 46 |
func move(dst, src string, force bool) (err error) { |
| 47 |
if fileutil.FileDoesExist(dst) && !force { |
| 48 |
return fmt.Errorf("%s exists (pass the -f flag to overwrite)", dst) |
| 49 |
return nil |
| 50 |
} |
| 51 |
dstFile, err := os.Create(dst) |
| 52 |
if err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
|
| 56 |
defer func(e error) { |
| 57 |
dstFile.Close() |
| 58 |
if e != nil { |
| 59 |
os.Remove(dst) |
| 60 |
} |
| 61 |
}(err) |
| 62 |
|
| 63 |
srcFile, err := os.Open(src) |
| 64 |
if err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
defer srcFile.Close() |
| 68 |
|
| 69 |
_, err = io.Copy(dstFile, srcFile) |
| 70 |
if err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
|
| 74 |
os.Remove(src) |
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
func usage(w io.Writer) { |
| 79 |
fmt.Fprintf(w, `Usage: renfnv [-fhlnv] files... |
| 80 |
|
| 81 |
renfnv renames files to the base32-encoded 32-bit FNV-1a hash of their |
| 82 |
contents, preserving the dirname and extension. |
| 83 |
|
| 84 |
Options: |
| 85 |
-f force overwriting of files when there is a collision. |
| 86 |
-h print this help message. |
| 87 |
-l list changed files. |
| 88 |
-n Perform a dry run: don't actually move files. |
| 89 |
-v Print all files as they are processed. If both -v and -l |
| 90 |
are specified, it will behave as if only -v was specified. |
| 91 |
`) |
| 92 |
} |
| 93 |
|
| 94 |
func init() { |
| 95 |
flag.Usage = func () { usage(os.Stdout) } |
| 96 |
} |
| 97 |
|
| 98 |
func main() { |
| 99 |
var dryRun, force, printChanged, verbose bool |
| 100 |
flag.BoolVar(&force, "f", false, "force overwriting of files if there is a collision") |
| 101 |
flag.BoolVar(&printChanged, "l", false, "list changed files") |
| 102 |
flag.BoolVar(&dryRun, "n", false, "dry run --- don't perform moves") |
| 103 |
flag.BoolVar(&verbose, "v", false, "list all processed files") |
| 104 |
|
| 105 |
flag.Parse() |
| 106 |
|
| 107 |
if verbose && printChanged { |
| 108 |
printChanged = false |
| 109 |
} |
| 110 |
|
| 111 |
for _, file := range flag.Args() { |
| 112 |
renamed, err := newName(file) |
| 113 |
if err != nil { |
| 114 |
lib.Warn(err, "failed to get new file name") |
| 115 |
continue |
| 116 |
} |
| 117 |
|
| 118 |
if verbose && !printChanged { |
| 119 |
fmt.Println(file) |
| 120 |
} |
| 121 |
|
| 122 |
if renamed != file { |
| 123 |
if !dryRun { |
| 124 |
err = move(renamed, file, force) |
| 125 |
if err != nil { |
| 126 |
lib.Warn(err, "failed to rename file from %s to %s", file, renamed) |
| 127 |
continue |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if printChanged && !verbose { |
| 132 |
fmt.Println(file, "->", renamed) |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |