/
file.go
144 lines (136 loc) · 3.06 KB
/
file.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
package index
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
// File is a file that has been indexed
type File struct {
Status string `toml:"status"`
StatusOriginal string `toml:"status-original,omitempty"`
BadCount int `toml:"bad-count,omitzero"`
Dependencies []string `toml:"dependencies"`
fileName string
currLocation string
isInTemp bool
}
// Init checks that the file is correct, and stores the file name
func (file *File) Init(fileName string) error {
file.fileName = fileName
switch file.Status {
case "":
file.Status = "unknown"
// Valid statuses
case "unknown":
case "good":
case "bad":
case "ignored":
case "errored":
// Not one of the above:
default:
return errors.New("invalid file status for " + fileName)
}
return nil
}
// Check checks that the file exists and is valid
func (file *File) Check(tempDir string) bool {
_, err1 := os.Stat(file.fileName)
if err1 != nil {
if len(tempDir) > 0 && (file.Status == "" || file.Status == "unknown" || file.Status == "bad") {
_, err2 := os.Stat(filepath.Join(tempDir, file.fileName))
if err2 != nil {
fmt.Printf("Error reading file: %v\n", err1)
return false
}
currLoc, err := filepath.Abs(filepath.Join(tempDir, file.fileName))
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return false
}
file.currLocation = currLoc
file.isInTemp = true
} else {
fmt.Printf("Error reading file: %v\n", err1)
return false
}
} else {
currLoc, err := filepath.Abs(file.fileName)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return false
}
file.currLocation = currLoc
file.isInTemp = false
}
return true
}
// MoveToTemp moves the file to the temporary directory
func (file *File) MoveToTemp(tempDir string) error {
newPath := filepath.Join(tempDir, file.fileName)
if !file.isInTemp {
err := os.Rename(file.currLocation, newPath)
if err != nil {
err = moveFile(file.currLocation, newPath)
if err != nil {
return err
}
}
file.currLocation = newPath
file.isInTemp = true
}
return nil
}
// MoveToTest moves the file to the testing directory
func (file *File) MoveToTest() error {
if file.isInTemp {
err := os.Rename(file.currLocation, file.fileName)
if err != nil {
err = moveFile(file.currLocation, file.fileName)
if err != nil {
return err
}
}
file.currLocation = file.fileName
file.isInTemp = false
}
return nil
}
func moveFile(source, destination string) (err error) {
src, err := os.Open(source)
if err != nil {
return err
}
defer src.Close()
fi, err := src.Stat()
if err != nil {
return err
}
flag := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
perm := fi.Mode() & os.ModePerm
dst, err := os.OpenFile(destination, flag, perm)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
if err != nil {
dst.Close()
os.Remove(destination)
return err
}
err = dst.Close()
if err != nil {
return err
}
err = src.Close()
if err != nil {
return err
}
err = os.Remove(source)
if err != nil {
return err
}
return nil
}