Skip to content

Commit

Permalink
v1.4.0
Browse files Browse the repository at this point in the history
-Performance improvements
-Improved handling of flags (--dir_name + --file_name is no longer needed)
-Improved flag checks
  • Loading branch information
bstnbuck committed Dec 29, 2024
1 parent 9416846 commit 57d5e7d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
## Usage
* Clone the repository: ```git clone https://github.com/bstnbuck/VShred.git```
* Compile the source code with: ```v -cc [tcc][msvc][gcc] -prod vshred.v ```
* Run it: ```vshred [--dir] [-d] [--dir_name "dir-name"] [--file_name "file-name"] [--rounds some-int] [--continue] [--no_output] [--yes] [-y]```
* Run it: ```vshred [--dir] [-d] [--rounds int] [-r int] [--continue] [--no_output] [-s] [--yes] [-y] <file or directory>```

## What it is and how to use it
VShred is a simple tool to safely delete files and entire directories.
Expand All @@ -20,19 +20,15 @@ This tool writes random content to the file and that several times. After that t

### Usage
Options:
* --dir -d
* `--dir` `-d`
* secure delete whole directory
* --dir_name "string"
* name of the directory, which should be recursively shredded. No empty directories! Needs `--dir -d` flag.
* --file_name "string"
* secure delete a file
* --rounds \<some-int>
* `--rounds` `-r` \<some-int>
* define how often the file should be overridden
* --continue
* `--continue`
* continue if an error occurs
* --no_output
* `--no_output` `-s`
* show less output
* --yes -y
* `--yes` `-y`
* no checkbacks
* --help -h
* `--help` `-h`
* show help
Binary file added vshred.exe
Binary file not shown.
55 changes: 35 additions & 20 deletions vshred.v
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ fn shred_file(file_str string, rounds int, options Options) ! {

if !options.no_output {
print('Shred rounds ${rounds} => Working round: ')
flush_stdout()
}
for i <= rounds {
if !options.no_output {
print('${i} ')
flush_stdout()
}
for {
// use buffersize for byte array length
Expand All @@ -129,7 +131,7 @@ fn shred_file(file_str string, rounds int, options Options) ! {
} else {
mut nulls_bytes := []u8{}

// create new output as random byte array of buffer size
// create new output as null byte array of buffer size
for _ in 0 .. buffersize {
nulls_bytes << `0`
}
Expand All @@ -140,15 +142,15 @@ fn shred_file(file_str string, rounds int, options Options) ! {
if i != rounds {
mut random_bytes := []u8{}

// create new output as random byte array of buffer size
// create new output as random byte array of remaining 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
// create new output as null byte array of remaining size
for _ in 0 .. file_len - file_len_temp {
nulls_bytes << `0`
}
Expand All @@ -163,65 +165,78 @@ fn shred_file(file_str string, rounds int, options Options) ! {
f.close()
if !options.no_output {
println('Done')
flush_stdout()
}
}
if !options.no_output {
print('Removing File... ')
flush_stdout()
}

// remove file
os.rm(file) or { return error('\tCould not remove file: ' + err.msg()) }
if !options.no_output {
println('Done!')
flush_stdout()
}
}

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')
fp.version('v1.4.0')
fp.description('VShred securely delete files, you do not need anymore. Files will be written with random and zero bytes.')
fp.skip_executable()
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)')
rounds := fp.int('rounds', `r`, 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')
no_output := fp.bool('no_output', `s`, false, 'show less output')
yes := fp.bool('yes', `y`, false, 'no checkbacks')

_ := fp.finalize() or {
fp.arguments_description('<file or directory>')
fp.usage_example('./vshred -d -r <rounds> <dir or file>')

elem := fp.finalize() or {
println(fp.usage())
return
}
dir_or_file := if elem.len != 0 { elem[0] } else { '' }

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) {
if whole_dir && os.is_dir(dir_or_file) && !os.is_dir_empty(dir_or_file) && rounds > 0 {
if !shred_dir(dir_or_file, rounds, options) {
println('Something went wrong... => See error messages above')
return
} else {
println('Success! Deleted directory: ' + dir_or_file)
}
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)!
} else if !whole_dir && os.is_file(dir_or_file) && rounds > 0 {
new_fname := os.dir(os.real_path(dir_or_file)) + os.path_separator + rand.string(12)
os.mv(dir_or_file, new_fname)!
if !options.no_output {
println('Shredding file... ' + os.file_name(file_name))
println('Shredding file... ' + os.file_name(dir_or_file))
}
shred_file(new_fname, rounds, options) or {
println('Something went wrong...')
println('\tError message: ' + err.msg())
return
}
println('Success! Shredded file: ' + file_name)
println('Success! Shredded file: ' + dir_or_file)
} else {
println('Flags incorrect!')
if !whole_dir && dir_name == '' && os.is_dir(file_name) {
println('Hint: \'${file_name}\' is a directory.')
if !whole_dir && os.is_dir(dir_or_file) {
println('Hint: \'${dir_or_file}\' is a directory.')
}
if whole_dir && os.is_file(dir_or_file) {
println('Hint: \'${dir_or_file}\' is a file not a directory (remove -d).')
}
if dir_or_file == '' {
println('Hint: no file or directory specified.')
}
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' {
Expand Down

0 comments on commit 57d5e7d

Please sign in to comment.