1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
5 * Licensed under GPLv2, see file LICENSE in this source tree.
8 //config: bool "shred (5.5 kb)"
11 //config: Overwrite a file to hide its contents, and optionally delete it
13 //applet:IF_SHRED(APPLET(shred, BB_DIR_USR_BIN, BB_SUID_DROP))
15 //kbuild:lib-$(CONFIG_SHRED) += shred.o
17 //usage:#define shred_trivial_usage
18 //usage: "[-fuz] [-n N] [-s SIZE] FILE..."
19 //usage:#define shred_full_usage "\n\n"
20 //usage: "Overwrite/delete FILEs\n"
21 //usage: "\n -f Chmod to ensure writability"
22 //usage: "\n -s SIZE Size to write"
23 //usage: "\n -n N Overwrite N times (default 3)"
24 //usage: "\n -z Final overwrite with zeros"
25 //usage: "\n -u Remove file"
26 //-x (exact: don't round up to 4k) and -v (verbose) are accepted but have no effect
28 /* shred (GNU coreutils) 8.25:
29 -f, --force change permissions to allow writing if necessary
30 -u truncate and remove file after overwriting
31 -z, --zero add a final overwrite with zeros to hide shredding
32 -n, --iterations=N overwrite N times instead of the default (3)
33 -v, --verbose show progress
34 -x, --exact do not round file sizes up to the next full block; this is the default for non-regular files
35 --random-source=FILE get random bytes from FILE
36 -s, --size=N shred this many bytes (suffixes like K, M, G accepted)
37 --remove[=HOW] like -u but give control on HOW to delete; See below
42 int shred_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
43 int shred_main(int argc UNUSED_PARAM
, char **argv
)
46 int rand_fd
= rand_fd
; /* for compiler */
48 unsigned num_iter
= 3;
60 opt
= getopt32(argv
, "^" "fuzn:+vxs:" "\0" "-1"/*min 1 arg*/, &num_iter
, &opt_s
);
63 zero_fd
= xopen("/dev/zero", O_RDONLY
);
65 rand_fd
= xopen("/dev/urandom", O_RDONLY
);
78 fd
= open(fname
, O_WRONLY
);
83 fd
= xopen(fname
, O_WRONLY
);
85 if (fstat(fd
, &sb
) == 0 && sb
.st_size
> 0) {
86 off_t size
= sb
.st_size
;
89 size
= BB_STRTOOFF(opt_s
, NULL
, 0); /* accepts oct/hex */
90 if (errno
|| size
< 0) bb_show_usage();
93 for (i
= 0; i
< num_iter
; i
++) {
94 bb_copyfd_size(rand_fd
, fd
, size
);
96 xlseek(fd
, 0, SEEK_SET
);
99 bb_copyfd_size(zero_fd
, fd
, size
);