3
0

shred.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 (5 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: "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 -n N Overwrite N times (default 3)"
  20. //usage: "\n -z Final overwrite with zeros"
  21. //usage: "\n -u Remove file"
  22. //-x and -v are accepted but have no effect
  23. /* shred (GNU coreutils) 8.25:
  24. -f, --force change permissions to allow writing if necessary
  25. -u truncate and remove file after overwriting
  26. -z, --zero add a final overwrite with zeros to hide shredding
  27. -n, --iterations=N overwrite N times instead of the default (3)
  28. -v, --verbose show progress
  29. -x, --exact do not round file sizes up to the next full block; this is the default for non-regular files
  30. --random-source=FILE get random bytes from FILE
  31. -s, --size=N shred this many bytes (suffixes like K, M, G accepted)
  32. --remove[=HOW] like -u but give control on HOW to delete; See below
  33. */
  34. #include "libbb.h"
  35. int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  36. int shred_main(int argc UNUSED_PARAM, char **argv)
  37. {
  38. int rand_fd = rand_fd; /* for compiler */
  39. int zero_fd;
  40. unsigned num_iter = 3;
  41. unsigned opt;
  42. enum {
  43. OPT_f = (1 << 0),
  44. OPT_u = (1 << 1),
  45. OPT_z = (1 << 2),
  46. OPT_n = (1 << 3),
  47. OPT_v = (1 << 4),
  48. OPT_x = (1 << 5),
  49. };
  50. opt = getopt32(argv, "fuzn:+vx", &num_iter);
  51. argv += optind;
  52. zero_fd = xopen("/dev/zero", O_RDONLY);
  53. if (num_iter != 0)
  54. rand_fd = xopen("/dev/urandom", O_RDONLY);
  55. if (!*argv)
  56. bb_show_usage();
  57. for (;;) {
  58. struct stat sb;
  59. const char *fname;
  60. unsigned i;
  61. int fd;
  62. fname = *argv++;
  63. if (!fname)
  64. break;
  65. fd = -1;
  66. if (opt & OPT_f) {
  67. fd = open(fname, O_WRONLY);
  68. if (fd < 0)
  69. chmod(fname, 0666);
  70. }
  71. if (fd < 0)
  72. fd = xopen(fname, O_WRONLY);
  73. if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
  74. off_t size = sb.st_size;
  75. for (i = 0; i < num_iter; i++) {
  76. bb_copyfd_size(rand_fd, fd, size);
  77. fdatasync(fd);
  78. xlseek(fd, 0, SEEK_SET);
  79. }
  80. if (opt & OPT_z) {
  81. bb_copyfd_size(zero_fd, fd, size);
  82. fdatasync(fd);
  83. }
  84. if (opt & OPT_u) {
  85. ftruncate(fd, 0);
  86. xunlink(fname);
  87. }
  88. xclose(fd);
  89. }
  90. }
  91. return EXIT_SUCCESS;
  92. }