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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package main
import (
"fmt"
"os"
"path/filepath"
"porg/fileop"
"porg/pipe"
)
type SHAInfo struct {
path string
sha256 string
}
type File struct {
Path string
SHA256 string
Time string
}
type MoveCommand struct {
src string
dest string
}
func Calculate(file string) *SHAInfo {
f, err := os.Open(file)
if err != nil {
fmt.Println(err)
return nil
}
defer f.Close()
sha, err := fileop.CalculateSHA256(f)
if err != nil {
fmt.Println(err)
return nil
} else {
return &SHAInfo{path: file, sha256: sha}
}
}
func Move(base string) func(*SHAInfo) *MoveCommand {
return func(info *SHAInfo) *MoveCommand {
ext := filepath.Ext(info.path)
head := info.sha256[0:2]
tail := info.sha256[2:]
newPath := fmt.Sprintf("%s/%s/%s%s", base, head, tail, ext)
return &MoveCommand{src: info.path, dest: newPath}
}
}
func Delete(path string) {
empty, err := fileop.IsEmpty(path)
if err != nil {
fmt.Printf("Error reading %s: %s\n", path, err.Error())
}
if empty {
err := os.Remove(path)
if err != nil {
fmt.Printf("Error deleting %s: %s\n", path, err.Error())
} else {
fmt.Printf("Folder deleted %s\n", path)
}
}
}
func Apply(move *MoveCommand) {
dir := filepath.Dir(move.dest)
os.Mkdir(dir, 0755)
if err := os.Rename(move.src, move.dest); err != nil {
fmt.Println(err)
}
}
func main() {
for _, path := range os.Args[1:] {
info, err := os.Stat(path)
if err != nil || !info.IsDir() {
continue
}
fmt.Println("Processing folder")
fmt.Println(path)
files := fileop.WalkFolder(path, fileop.File)
shas := pipe.Proc(files, 4, Calculate)
cmds := pipe.Proc(shas, 1, Move(path))
pipe.TailProc(cmds, 4, Apply)
fmt.Println("Deleting empty folders")
files = fileop.WalkFolder(path, fileop.Folder)
pipe.TailProc(files, 2, Delete)
}
}
|