-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileinfo.go
62 lines (53 loc) · 1.08 KB
/
fileinfo.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
package main
import (
"encoding/json"
"os"
)
const fileInfoExt = ".info"
type FileInfo struct {
Offset int64 `json:"offset"`
Digest Digest `json:"digest"`
}
type Digest struct {
Sha1 *sha1digest `json:"sha1"`
CRC32 *crc32digest `json:"crc32"`
}
func newFileInfo() *FileInfo {
return &FileInfo{
Digest: Digest{
Sha1: NewSha1(),
CRC32: NewCRC32IEEE(),
},
}
}
func ReadFileInfo(path string) (fi *FileInfo, err error) {
fi, err = ReadExistingFileInfo(path)
if os.IsNotExist(err) {
return newFileInfo(), nil
}
return fi, err
}
func ReadExistingFileInfo(path string) (fi *FileInfo, err error) {
f, err := os.Open(path + fileInfoExt)
if err != nil {
return
}
defer func() { _ = f.Close() }()
err = json.NewDecoder(f).Decode(&fi)
return
}
func SaveFileInfo(path string, fi *FileInfo) error {
f, err := os.Create(path + fileInfoExt)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
err = json.NewEncoder(f).Encode(fi)
if err != nil {
return err
}
return f.Close()
}
func DeleteFileInfo(path string) error {
return os.Remove(path + fileInfoExt)
}