aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-04-07 20:45:08 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-04-07 20:45:08 +0200
commit2f59bf39e2ea6fb4c3ed3e74ea113a521e1a3558 (patch)
treeb42d8b7bfe0684a40ef669e09aa2a0cdfba8684f /coreutils
parent42776708f21b8f7293627f88d8d5d3df00d8be1c (diff)
downloadbusybox-w32-2f59bf39e2ea6fb4c3ed3e74ea113a521e1a3558.tar.gz
busybox-w32-2f59bf39e2ea6fb4c3ed3e74ea113a521e1a3558.tar.bz2
busybox-w32-2f59bf39e2ea6fb4c3ed3e74ea113a521e1a3558.zip
shred: new applet
function old new delta shred_main - 361 +361 packed_usage 31427 31467 +40 applet_names 2578 2584 +6 applet_main 1492 1496 +4 run_applet_and_exit 679 682 +3 ------------------------------------------------------------------------------ (add/remove: 2/0 grow/shrink: 4/0 up/down: 414/0) Total: 414 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/cksum.c4
-rw-r--r--coreutils/shred.c104
2 files changed, 106 insertions, 2 deletions
diff --git a/coreutils/cksum.c b/coreutils/cksum.c
index aeec0188d..9034fc19a 100644
--- a/coreutils/cksum.c
+++ b/coreutils/cksum.c
@@ -17,9 +17,9 @@
17//kbuild:lib-$(CONFIG_CKSUM) += cksum.o 17//kbuild:lib-$(CONFIG_CKSUM) += cksum.o
18 18
19//usage:#define cksum_trivial_usage 19//usage:#define cksum_trivial_usage
20//usage: "FILES..." 20//usage: "FILE..."
21//usage:#define cksum_full_usage "\n\n" 21//usage:#define cksum_full_usage "\n\n"
22//usage: "Calculate the CRC32 checksums of FILES" 22//usage: "Calculate the CRC32 checksums of FILEs"
23 23
24#include "libbb.h" 24#include "libbb.h"
25#include "common_bufsiz.h" 25#include "common_bufsiz.h"
diff --git a/coreutils/shred.c b/coreutils/shred.c
new file mode 100644
index 000000000..9cd39b79c
--- /dev/null
+++ b/coreutils/shred.c
@@ -0,0 +1,104 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
4 *
5 * Licensed under GPLv2, see file LICENSE in this source tree.
6 */
7//config:config SHRED
8//config: bool "shred"
9//config: default y
10//config: help
11//config: Overwrite a file to hide its contents, and optionally delete it
12
13//applet:IF_SHRED(APPLET(shred, BB_DIR_USR_BIN, BB_SUID_DROP))
14
15//kbuild:lib-$(CONFIG_SHRED) += shred.o
16
17//usage:#define shred_trivial_usage
18//usage: "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 -n N Overwrite N times (default 3)"
23//usage: "\n -z Final overwrite with zeros"
24//usage: "\n -u Remove file"
25//-x and -v are accepted but have no effect
26
27/* shred (GNU coreutils) 8.25:
28-f, --force change permissions to allow writing if necessary
29-u truncate and remove file after overwriting
30-z, --zero add a final overwrite with zeros to hide shredding
31-n, --iterations=N overwrite N times instead of the default (3)
32-v, --verbose show progress
33-x, --exact do not round file sizes up to the next full block; this is the default for non-regular files
34--random-source=FILE get random bytes from FILE
35-s, --size=N shred this many bytes (suffixes like K, M, G accepted)
36--remove[=HOW] like -u but give control on HOW to delete; See below
37*/
38
39#include "libbb.h"
40
41int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42int shred_main(int argc UNUSED_PARAM, char **argv)
43{
44 int rand_fd = rand_fd; /* for compiler */
45 int zero_fd;
46 unsigned num_iter = 3;
47 unsigned opt;
48 enum {
49 OPT_f = (1 << 0),
50 OPT_u = (1 << 1),
51 OPT_z = (1 << 2),
52 OPT_n = (1 << 3),
53 OPT_v = (1 << 4),
54 OPT_x = (1 << 5),
55 };
56
57 opt = getopt32(argv, "fuzn:+vx", &num_iter);
58 argv += optind;
59
60 zero_fd = xopen("/dev/zero", O_RDONLY);
61 if (num_iter != 0)
62 rand_fd = xopen("/dev/urandom", O_RDONLY);
63
64 if (!*argv)
65 bb_show_usage();
66
67 for (;;) {
68 struct stat sb;
69 unsigned i;
70 int fd = -1;
71
72 if (opt & OPT_f) {
73 fd = open(*argv, O_WRONLY);
74 if (fd < 0)
75 chmod(*argv, 0666);
76 }
77 if (fd < 0)
78 fd = xopen(*argv, O_WRONLY);
79
80 if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
81 off_t size = sb.st_size;
82
83 for (i = 0; i < num_iter; i++) {
84 bb_copyfd_size(rand_fd, fd, size);
85 fdatasync(fd);
86 xlseek(fd, 0, SEEK_SET);
87 }
88 if (opt & OPT_z) {
89 bb_copyfd_size(zero_fd, fd, size);
90 fdatasync(fd);
91 }
92 if (opt & OPT_u) {
93 ftruncate(fd, 0);
94 xunlink(*argv);
95 }
96 xclose(fd);
97 }
98 argv++;
99 if (!*argv)
100 break;
101 }
102
103 return EXIT_SUCCESS;
104}