-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.go
300 lines (238 loc) · 6.75 KB
/
files.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"fmt"
"io"
"math"
"math/rand"
"os"
)
// FileExists: Check the given file exists
func FileExists(_file string) bool {
fileExists := false
if len(_file) > 0 {
// Try to get file info
_, err := os.Stat(_file)
// If any errors occur on getting file info, file does not exist
if os.IsNotExist(err) || err != nil {
if DEBUG == true {
fmt.Println(UI_FileNotFound, _file)
}
fileExists = false
} else {
// File info found - file exists
if DEBUG == true {
fmt.Println(UI_FileFound, _file)
}
fileExists = true
}
} else {
fmt.Print(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()))
fmt.Println(fmt.Sprintf(UI_Parameter, "_file:"+_file))
}
return fileExists
}
// GetFileSize: Gets the size of the given file in bytes
func GetFileSize(_file string) int64 {
var fileSize int64 = -1
if len(_file) > 0 {
// Get file info
fileInfo, err := os.Stat(_file)
if err == nil {
// Get file size
fileSize = fileInfo.Size()
if fileSize == 0 {
fmt.Println(UI_EmptyFile)
fileSize = -1
}
if fileSize < 0 {
fmt.Println(UI_InvalidFileSize, _file)
fileSize = -1
}
if fileSize > math.MaxInt64 {
fmt.Println(UI_FileTooBig)
fileSize = -1
}
} else {
fmt.Println(fmt.Sprintf(UI_NoFileSize, _file, err))
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()))
fmt.Println(fmt.Sprintf(UI_Parameter, "_file:"+_file))
}
return fileSize
}
// DeleteFile: Delete the given file
func DeleteFile(_file string) bool {
var deleteError bool = false
var err error
if len(_file) > 0 {
err = os.Remove(_file)
if err != nil {
fmt.Println(UI_DeleteError, _file, err)
deleteError = true
} else {
// File deleted
if DEBUG == true {
fmt.Println(UI_FileDeleted, _file)
}
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_file:", _file)
deleteError = true
}
return deleteError
}
// WipeFile: Wipes the given file
func WipeFile(_file string) bool {
var file *os.File
var fileSize int64 = 0
var currentWipePass int = 0
const WIPE_PASSES int = 5
const BUFFER_SIZE int64 = 1000
var randomDataCount int = 0
var buffer []byte
var bytesWritten int = 0
var totalBytesWritten int64 = 0
var wipeError bool = false
var fileWiped bool = false
var err error
if len(_file) > 0 {
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_WipingFile, _file))
}
// Open file
file, err = os.OpenFile(_file, os.O_WRONLY, 0)
if err == nil {
// Get file size
fileSize = GetFileSize(_file)
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_FileSize, _file, fileSize))
}
if fileSize != -1 {
// Wipe the file multiple times
for currentWipePass = 0; currentWipePass != WIPE_PASSES; currentWipePass++ {
// Ovewrite file with data in buffer
for totalBytesWritten = 0; totalBytesWritten != fileSize; totalBytesWritten++ {
if DEBUG == true {
// Update UI
fmt.Printf("\r" + fmt.Sprintf(UI_WipingFile, _file) + ". ")
}
// Create write buffer
if (totalBytesWritten + BUFFER_SIZE) > fileSize {
// If data left to write out is smaller than the buffer, set the buffer size to the size of data left
buffer = make([]byte, (fileSize - totalBytesWritten))
if int64(len(buffer)) != (fileSize - totalBytesWritten) {
// Failed to allocate buffer memory
fmt.Println(UI_NoBufferMemory)
wipeError = true
break
}
} else {
buffer = make([]byte, BUFFER_SIZE)
if int64(len(buffer)) != BUFFER_SIZE {
// Failed to allocate buffer memory
fmt.Println(UI_NoBufferMemory)
wipeError = true
break
}
}
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_BufferSize, len(buffer)))
}
// Fill buffer with random data
randomDataCount, err = rand.Read(buffer)
if DEBUG == true {
// Update UI
fmt.Printf("\r" + fmt.Sprintf(UI_WipingFile, _file) + ".. ")
}
if err == nil {
if randomDataCount == len(buffer) {
// Overwrite file with buffer contents
bytesWritten, err = file.WriteAt(buffer, totalBytesWritten)
if err != nil {
if err == io.EOF {
// If end of file found, display an error and stop writing to it and display end of file
fmt.Println(fmt.Sprintf(UI_EOF, _file))
wipeError = true
break
} else {
// Error writing to file - display an error and stop writing data
fmt.Println(fmt.Sprintf(UI_FileWriteError, _file, err))
wipeError = true
break
}
} else {
// If the buffer has not been written to the file - display an error and stop writing data
if bytesWritten != len(buffer) {
fmt.Println(fmt.Sprintf(UI_FileWriteError, _file, ""))
wipeError = true
break
} else {
// Keep track of amount of data written to the file
totalBytesWritten = totalBytesWritten + int64(bytesWritten)
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_BytesWritten, totalBytesWritten))
}
// If amount of data written to the file is the same as the file size, all data has been written to the file - stop writing data
if totalBytesWritten == fileSize {
if DEBUG == true {
fmt.Println(UI_DataWriteComplete)
fmt.Println(fmt.Sprintf(UI_FileSize, _file, fileSize))
}
break
}
}
}
buffer = nil
} else {
fmt.Println(UI_RandomDataError)
wipeError = true
break
}
} else {
fmt.Println(UI_RandomDataError)
wipeError = true
break
}
if DEBUG == true {
// Update UI
fmt.Printf("\r" + fmt.Sprintf(UI_WipingFile, _file) + "...")
}
} // end buffer loop
// If error has occured stop wiping file
if wipeError == true {
break
}
} // end wipe pass loop
} else {
fmt.Println(UI_InvalidFileSize, _file)
}
} else {
fmt.Println(UI_FileOpenError, err)
}
// Close file handle down and clear buffer
file.Sync()
file.Close()
// If all wipe passes have completed without error, delete file
if wipeError == false {
if DeleteFile(_file) == false {
fileWiped = true
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_WipedFile, _file))
}
} else {
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_WipeFileError, _file))
}
}
} else {
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_WipeFileError, _file))
}
}
} else {
fmt.Print(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()))
fmt.Println(fmt.Sprintf(UI_Parameter, "_file: "+_file))
}
return fileWiped
}