/
info.go
143 lines (120 loc) · 3.02 KB
/
info.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"time"
base58 "github.com/jbenet/go-base58"
mh "github.com/jbenet/go-multihash"
attrs "github.com/mildred/doc/attrs"
repo "github.com/mildred/doc/repo"
)
const infoUsage string = `doc info [OPTIONS...] FILE...
Show information about each file presented, including its status, hash and
conflict status. It can also run integrity check on the files. It is a more
detailed version of doc status.
Options:
`
func mainInfo(args []string) int {
f := flag.NewFlagSet("info", flag.ExitOnError)
opt_check := f.Bool("c", false, "Run integrity check")
f.Usage = func() {
fmt.Print(infoUsage)
f.PrintDefaults()
}
f.Parse(args)
dir := f.Arg(0)
if dir == "" {
dir = "."
}
rep := repo.GetRepo(dir)
status := 0
first := true
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err.Error())
status = 1
return err
}
// Skip .dirstore/ at root
if filepath.Base(path) == attrs.DirStoreName && filepath.Dir(path) == dir && info.IsDir() {
return filepath.SkipDir
} else if !info.Mode().IsRegular() {
return nil
}
if first {
first = false
} else {
fmt.Println()
}
fmt.Printf("File: %s\n", path)
if conflict := repo.ConflictFile(path); conflict != "" {
fmt.Printf("Conflict With: %s\n", conflict)
}
for _, alt := range repo.ConflictFileAlternatives(path) {
fmt.Printf("Conflict Alternatives: %s\n", alt)
}
var realHash mh.Multihash
if *opt_check {
realHash, err = repo.HashFile(path, info)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
return nil
}
}
hashTime, err := repo.GetHashTime(path)
if repo.IsNoData(err) {
if *opt_check {
fmt.Printf("Actual Hash: %s\n", base58.Encode(realHash))
}
fmt.Printf("Status: New\n")
} else {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err.Error())
return nil
}
fmt.Printf("Hash Time: %v\n", hashTime.Format(time.RFC3339Nano))
hash, err := attrs.Get(path, repo.XattrHash)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
return nil
}
var par2exists = false
if rep != nil {
par2exists, _ = rep.Par2Exists(hash)
}
fmt.Printf("Recorded Hash: %s (reduncency %s)\n", base58.Encode(hash), boolToAvailableStr(par2exists))
if *opt_check {
par2exists = false
if rep != nil {
par2exists, _ = rep.Par2Exists(realHash)
}
fmt.Printf("Actual Hash: %s (redundency %s)\n", base58.Encode(realHash), boolToAvailableStr(par2exists))
}
if hashTime != info.ModTime() {
fmt.Printf("Status: Dirty\n")
} else {
if *opt_check && !bytes.Equal(realHash, hash) {
fmt.Printf("Status: Corrupted\n")
} else {
fmt.Printf("Status: Clean\n")
}
}
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
return status
}
func boolToAvailableStr(b bool) string {
if b {
return "available"
} else {
return "unavailable"
}
}