-
Notifications
You must be signed in to change notification settings - Fork 0
/
msfile.go
174 lines (148 loc) · 4.15 KB
/
msfile.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
// msfile.go - A utility to get and compare Mass Spectrometry file metadata
// msfile is similar to the Linux file command, but is designed to work with Mass Spectrometry files
// Output of msfile is a JSON string, which can be used by other programs
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/524D/msfile/fcompare"
"github.com/djherbis/atime"
)
// For files less than minPartialChecksumSize, we use the full checksum as the partial checksum
// because the speed benefit of reading 1M three times is probably less than reading the entire file once
const minPartialChecksumSize = 16 * 1024 * 1024
type params struct {
compare bool
json bool
method string
}
type FileInfo struct {
Filename string
Size int64
Atime int64
Mtime int64
PartialChecksum string
FullChecksum string
Properties map[string]string
}
// flags:
// -compare: compare two files
// -json: produce output in JSON format
// -comparemethod: partial, size, full (default: partial)
var par params
// parse flags
func handleCommandLine() {
flag.BoolVar(&par.compare, "compare", false, "compare files, instead of printing results")
flag.BoolVar(&par.json, "json", false, "produce output in JSON format")
flag.StringVar(&par.method, "comparemethod", "partial", "method to use when comparing files (partial, size, full))")
flag.Parse()
}
func processFile(filename string) (FileInfo, error) {
var fileinfo FileInfo
fileinfo.Properties = make(map[string]string)
fileinfo.Filename = filename
// Get file times
atime, err := atime.Stat(filename)
if err != nil {
log.Fatal(err.Error())
}
fi, err := os.Stat(filename)
if err != nil {
return fileinfo, err
}
mtime := fi.ModTime()
// Convert times to Unix time
fileinfo.Atime = atime.Unix()
fileinfo.Mtime = mtime.Unix()
// Restore file times before we return
defer os.Chtimes(filename, atime, mtime)
fileinfo.Size = fi.Size()
if par.compare {
// Compare files
// Use appropriate method to compare files
switch par.method {
case "partial":
// Get partial checksum
isFull := false
fileinfo.PartialChecksum, isFull, err = fcompare.GetPartialChecksum(filename)
if err != nil {
return fileinfo, err
}
if isFull {
fileinfo.FullChecksum = fileinfo.PartialChecksum
}
case "size":
// Compare file sizes
case "full":
// Get full checksum
fileinfo.FullChecksum, err = fcompare.GetChecksum(filename)
if err != nil {
return fileinfo, err
}
default:
log.Fatal("Invalid compare method")
}
}
return fileinfo, nil
}
func main() {
handleCommandLine()
// Print usage if no arguments are provided
if flag.NArg() == 0 {
fmt.Println("Usage: msfile [options] file1 [file2]")
flag.PrintDefaults()
os.Exit(1)
}
for _, fn := range flag.Args() {
canKeep, _ := fcompare.TestKeepAtime(fn)
if !canKeep {
log.Fatalln("Warning: unable to preserve file times for", fn)
}
}
// Check if we are comparing files
if par.compare {
// This only works with 2 files
if flag.NArg() != 2 {
log.Fatal("Compare option only works with 2 files")
} else {
inf1, err := processFile(flag.Args()[0])
if err != nil {
log.Fatal(err)
}
inf2, err := processFile(flag.Args()[1])
if err != nil {
log.Fatal(err)
}
if (par.method == "partial" && inf1.PartialChecksum == inf2.PartialChecksum) ||
(par.method == "size" && inf1.Size == inf2.Size) ||
(par.method == "full" && inf1.FullChecksum == inf2.FullChecksum) {
fmt.Println("Files are the same")
} else {
fmt.Println("Files are different")
}
}
} else {
// for all remaining arguments
for _, arg := range flag.Args() {
// process each file
inf, err := processFile(arg)
if err != nil {
log.Fatal(err)
}
// Output in JSON format if requested
if par.json {
// Convert inf to a JSON string
j, err := json.Marshal(inf)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(j))
} else {
fmt.Printf("%+v\n", inf)
}
}
}
}