shred.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 (4.9 kb)"
  9. //config: default y
  10. //config: help
  11. //config: Overwrite a file to hide its contents, and optionally delete it
  12. //applet:IF_SHRED(APPLET(shred, BB_DIR_USR_BIN, BB_SUID_DROP))
  13. //kbuild:lib-$(CONFIG_SHRED) += shred.o
  14. //usage:#define shred_trivial_usage
  15. //usage: "[-fuz] [-n N] [-s SIZE] FILE..."
  16. //usage:#define shred_full_usage "\n\n"
  17. //usage: "Overwrite/delete FILEs\n"
  18. //usage: "\n -f Chmod to ensure writability"
  19. //usage: "\n -s SIZE Size to write"
  20. //usage: "\n -n N Overwrite N times (default 3)"
  21. //usage: "\n -z Final overwrite with zeros"
  22. //usage: "\n -u Remove file"
  23. //-x (exact: don't round up to 4k) and -v (verbose) are accepted but have no effect
  24. /* shred (GNU coreutils) 8.25:
  25. -f, --force change permissions to allow writing if necessary
  26. -u truncate and remove file after overwriting
  27. -z, --zero add a final overwrite with zeros to hide shredding
  28. -n, --iterations=N overwrite N times instead of the default (3)
  29. -v, --verbose show progress
  30. -x, --exact do not round file sizes up to the next full block; this is the default for non-regular files
  31. --random-source=FILE get random bytes from FILE
  32. -s, --size=N shred this many bytes (suffixes like K, M, G accepted)
  33. --remove[=HOW] like -u but give control on HOW to delete; See below
  34. */
  35. #include "libbb.h"
  36. int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  37. int shred_main(int argc UNUSED_PARAM, char **argv)
  38. {
  39. char *opt_s;
  40. int rand_fd = rand_fd; /* for compiler */
  41. int zero_fd;
  42. unsigned num_iter = 3;
  43. unsigned opt;
  44. enum {
  45. OPT_f = (1 << 0),
  46. OPT_u = (1 << 1),
  47. OPT_z = (1 << 2),
  48. OPT_n = (1 << 3),
  49. OPT_v = (1 << 4),
  50. OPT_x = (1 << 5),
  51. OPT_s = (1 << 6),
  52. };
  53. opt = getopt32(argv, "^" "fuzn:+vxs:" "\0" "-1"/*min 1 arg*/, &num_iter, &opt_s);
  54. argv += optind;
  55. zero_fd = xopen("/dev/zero", O_RDONLY);
  56. if (num_iter != 0)
  57. rand_fd = xopen("/dev/urandom", O_RDONLY);
  58. for (;;) {
  59. struct stat sb;
  60. const char *fname;
  61. unsigned i;
  62. int fd;
  63. fname = *argv++;
  64. if (!fname)
  65. break;
  66. fd = -1;
  67. if (opt & OPT_f) {
  68. fd = open(fname, O_WRONLY);
  69. if (fd < 0)
  70. chmod(fname, 0666);
  71. }
  72. if (fd < 0)
  73. fd = xopen(fname, O_WRONLY);
  74. if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
  75. off_t size = sb.st_size;
  76. if (opt & OPT_s) {
  77. size = BB_STRTOOFF(opt_s, NULL, 0); /* accepts oct/hex */
  78. if (errno || size < 0) bb_show_usage();
  79. }
  80. for (i = 0; i < num_iter; i++) {
  81. bb_copyfd_size(rand_fd, fd, size);
  82. fdatasync(fd);
  83. xlseek(fd, 0, SEEK_SET);
  84. }
  85. if (opt & OPT_z) {
  86. bb_copyfd_size(zero_fd, fd, size);
  87. fdatasync(fd);
  88. }
  89. }
  90. if (opt & OPT_u) {
  91. ftruncate(fd, 0);
  92. xunlink(fname);
  93. }
  94. xclose(fd);
  95. }
  96. return EXIT_SUCCESS;
  97. }