Mirror of git://git.busybox.net/busybox with our patches on top
Source
/* vi: set sw=4 ts=4: */
/*
* Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
//config:config SHRED
//config: bool "shred"
//config: default y
//config: help
//config: Overwrite a file to hide its contents, and optionally delete it
//applet:IF_SHRED(APPLET(shred, BB_DIR_USR_BIN, BB_SUID_DROP))
//kbuild:lib-$(CONFIG_SHRED) += shred.o
//usage:#define shred_trivial_usage
//usage: "FILE..."
//usage:#define shred_full_usage "\n\n"
//usage: "Overwrite/delete FILEs\n"
//usage: "\n -f Chmod to ensure writability"
//usage: "\n -n N Overwrite N times (default 3)"
//usage: "\n -z Final overwrite with zeros"
//usage: "\n -u Remove file"
//-x and -v are accepted but have no effect
/* shred (GNU coreutils) 8.25:
-f, --force change permissions to allow writing if necessary
-u truncate and remove file after overwriting
-z, --zero add a final overwrite with zeros to hide shredding
-n, --iterations=N overwrite N times instead of the default (3)
-v, --verbose show progress
-x, --exact do not round file sizes up to the next full block; this is the default for non-regular files
--random-source=FILE get random bytes from FILE
-s, --size=N shred this many bytes (suffixes like K, M, G accepted)
--remove[=HOW] like -u but give control on HOW to delete; See below
*/
int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int shred_main(int argc UNUSED_PARAM, char **argv)
{
int rand_fd = rand_fd; /* for compiler */
int zero_fd;
unsigned num_iter = 3;
unsigned opt;
enum {
OPT_f = (1 << 0),
OPT_u = (1 << 1),
OPT_z = (1 << 2),
OPT_n = (1 << 3),
OPT_v = (1 << 4),
OPT_x = (1 << 5),
};
opt = getopt32(argv, "fuzn:+vx", &num_iter);
argv += optind;
zero_fd = xopen("/dev/zero", O_RDONLY);
if (num_iter != 0)
rand_fd = xopen("/dev/urandom", O_RDONLY);