-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile_state.go
75 lines (60 loc) · 1.54 KB
/
file_state.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
package driveext
import (
"fmt"
"os"
"github.com/dustin/go-humanize"
"github.com/pkg/errors"
"github.com/mtojek/gdriver/internal/osext"
)
type FileState struct {
File *DriveFile
LocalPath string
missing bool
md5Checksum string
size int64
}
func (fs *FileState) Offset() int64 {
if fs.size > fs.File.Size {
return 0 // file is corrupted (too long)
}
if fs.size == fs.File.Size && fs.md5Checksum != fs.File.Md5Checksum {
return 0 // file is corrupted
}
return fs.size
}
func (fs *FileState) Valid() (bool, error) {
if fs.missing {
return false, errors.New("missing file")
}
if fs.size == 0 {
return false, errors.New("empty file")
}
if fs.size < fs.File.Size {
return false, fmt.Errorf("incomplete file (%s / %s)",
humanize.Bytes(uint64(fs.size)), humanize.Bytes(uint64(fs.File.Size)))
}
if fs.md5Checksum != fs.File.Md5Checksum {
return false, fmt.Errorf("corrupted file (bad MD5 checksum: %s)", fs.md5Checksum)
}
return true, nil
}
func EvaluateFileState(file *DriveFile, localPath string) (*FileState, error) {
state := &FileState{
File: file,
LocalPath: localPath,
}
fi, err := os.Stat(localPath)
if os.IsNotExist(err) {
state.missing = true
return state, nil // file hasn't been downloaded yet
}
if err != nil {
return nil, errors.Wrapf(err, "stat file failed (path: %s)", state.LocalPath)
}
state.md5Checksum, err = osext.Md5Checksum(localPath)
if err != nil {
return nil, errors.Wrap(err, "calculating MD5 checksum failed")
}
state.size = fi.Size()
return state, nil
}