-
Notifications
You must be signed in to change notification settings - Fork 1
/
vshred.v
232 lines (207 loc) · 5.81 KB
/
vshred.v
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
import rand
import os
import flag
// 1MB
const buffersize = 1048576
struct Options {
no_stop bool
no_output bool
yes bool
}
fn shred_dir(dir string, rounds int, options Options) bool {
println('Shredding directory...')
// get files to delete
files := make_files_list(os.real_path(dir), options)
if !options.no_output {
println('[file-list begin]')
for file in files {
println(file)
}
println('[file-list end]')
}
if !options.yes {
if os.input('Shredding files now. Okay? (y/n): ') != 'y' {
println('Abort!')
return false
}
}
mut enc_files := []string{}
for file in files {
new_fname := os.dir(file) + os.path_separator + rand.string(12)
os.mv(file, new_fname) or {
println('\tError while renaming file: ' + os.file_name(file))
println('\tError message: ' + err.msg())
return false
}
enc_files << new_fname
}
// remove file for file
for i, file in enc_files {
if !options.no_output {
println('Next file: ' + os.file_name(files[i]))
println('Shredding file... ' + files[i])
}
shred_file(file, rounds, options) or {
println('\tError while shredding file: ' + os.file_name(files[i]))
println(err.msg())
if !options.no_stop {
return false
}
if options.no_output {
println('')
}
}
if !options.no_output {
println('Completed!\n')
}
}
os.rmdir_all(os.real_path(dir)) or {
println('\tError while removing directory: ' + err.msg())
return false
}
println('Removed directory successfull')
return true
}
fn make_files_list(dir string, options Options) []string {
if !options.no_output {
println('Entering dir: ' + dir)
}
dir_content := os.ls(dir) or { [] }
mut files := []string{}
for content in dir_content {
fpath := os.join_path(dir, content)
if os.is_link(fpath) {
continue
}
if os.is_dir(fpath) {
files << make_files_list(fpath, options)
} else {
files << fpath
}
}
return files
}
fn shred_file(file_str string, rounds int, options Options) ! {
file := os.real_path(file_str)
file_len := os.file_size(file)
if os.is_link(file_str) {
return
}
if file_len > 0 {
mut i := 1
mut f := os.create(file) or { // binary write mode
return error('\tCould not open file: ${file}\n\tError message: ' + err.msg())
}
mut file_len_temp := u64(0)
if !options.no_output {
print('Shred rounds ${rounds} => Working round: ')
}
for i <= rounds {
if !options.no_output {
print('${i} ')
}
for {
// use buffersize for byte array length
if (file_len_temp + buffersize) <= file_len && file_len > buffersize {
if i != rounds {
mut random_bytes := []u8{}
// create new output as random byte array of buffer size
for _ in 0 .. buffersize {
random_bytes << rand.u8()
}
f.write_to(file_len_temp, random_bytes)!
} else {
mut nulls_bytes := []u8{}
// create new output as random byte array of buffer size
for _ in 0 .. buffersize {
nulls_bytes << `0`
}
f.write_to(file_len_temp, nulls_bytes)!
}
file_len_temp += buffersize
} else {
if i != rounds {
mut random_bytes := []u8{}
// create new output as random byte array of buffer size
for _ in 0 .. file_len - file_len_temp {
random_bytes << rand.u8()
}
f.write_to(file_len_temp, random_bytes)!
} else {
mut nulls_bytes := []u8{}
// create new output as random byte array of buffer size
for _ in 0 .. file_len - file_len_temp {
nulls_bytes << `0`
}
f.write_to(file_len_temp, nulls_bytes)!
}
file_len_temp = 0
break
}
}
i++
}
f.close()
if !options.no_output {
println('Done')
}
}
if !options.no_output {
print('Removing File... ')
}
// remove file
os.rm(file) or { return error('\tCould not remove file: ' + err.msg()) }
if !options.no_output {
println('Done!')
}
}
fn main() {
// set flags
mut fp := flag.new_flag_parser(os.args)
fp.application('VShred (Securely delete files)')
fp.version('v1.3.0')
fp.description('VShred securely delete files, you do not need anymore. Files will be written with random and zero bytes')
whole_dir := fp.bool('dir', `d`, false, 'secure delete whole directory')
dir_name := fp.string('dir_name', 0, '', 'name of directory, which should be shred. No empty directories!')
file_name := fp.string('file_name', 0, '', 'secure delete a file')
rounds := fp.int('rounds', 0, 5, 'define how often the file should be overridden (> 0)')
no_stop := fp.bool('continue', 0, false, 'continue if an error occurs')
no_output := fp.bool('no_output', 0, false, 'show less output')
yes := fp.bool('yes', `y`, false, 'no checkbacks')
_ := fp.finalize() or {
println(fp.usage())
return
}
options := Options{no_stop, no_output, yes}
println('VShred -- secure delete files!')
// check if flags correct set
if whole_dir && os.is_dir(dir_name) && !os.is_dir_empty(dir_name) && rounds > 0 {
if !shred_dir(dir_name, rounds, options) {
println('Something went wrong... => See error messages above')
return
}
println('Success! Deleted directory: ' + dir_name)
} else if !whole_dir && os.is_file(file_name) && rounds > 0 {
new_fname := os.dir(os.real_path(file_name)) + os.path_separator + rand.string(12)
os.mv(file_name, new_fname)!
if !options.no_output {
println('Shredding file... ' + os.file_name(file_name))
}
shred_file(new_fname, rounds, options) or {
println('Something went wrong...')
println('\tError message: ' + err.msg())
return
}
println('Success! Shredded file: ' + file_name)
} else {
println('Flags incorrect!')
if !whole_dir && dir_name == '' && os.is_dir(file_name) {
println('Hint: \'${file_name}\' is a directory.')
}
println("Maybe there is a typo, the file/dir does not exist or 'rounds' is lower 1\n")
if os.input('Show usage? (y/n) ') == 'y' {
println(fp.usage())
}
}
println('Bye!')
}