remove_file.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini remove_file implementation for busybox
  4. *
  5. * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <stdio.h>
  22. #include <time.h>
  23. #include <utime.h>
  24. #include <dirent.h>
  25. #include <errno.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <getopt.h>
  30. #include "libbb.h"
  31. extern int remove_file(const char *path, int flags)
  32. {
  33. struct stat path_stat;
  34. int path_exists = 1;
  35. if (lstat(path, &path_stat) < 0) {
  36. if (errno != ENOENT) {
  37. bb_perror_msg("unable to stat `%s'", path);
  38. return -1;
  39. }
  40. path_exists = 0;
  41. }
  42. if (!path_exists) {
  43. if (!(flags & FILEUTILS_FORCE)) {
  44. bb_perror_msg("cannot remove `%s'", path);
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. if (S_ISDIR(path_stat.st_mode)) {
  50. DIR *dp;
  51. struct dirent *d;
  52. int status = 0;
  53. if (!(flags & FILEUTILS_RECUR)) {
  54. bb_error_msg("%s: is a directory", path);
  55. return -1;
  56. }
  57. if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
  58. isatty(0)) ||
  59. (flags & FILEUTILS_INTERACTIVE)) {
  60. fprintf(stderr, "%s: descend into directory `%s'? ", bb_applet_name,
  61. path);
  62. if (!bb_ask_confirmation())
  63. return 0;
  64. }
  65. if ((dp = opendir(path)) == NULL) {
  66. bb_perror_msg("unable to open `%s'", path);
  67. return -1;
  68. }
  69. while ((d = readdir(dp)) != NULL) {
  70. char *new_path;
  71. new_path = concat_subpath_file(path, d->d_name);
  72. if(new_path == NULL)
  73. continue;
  74. if (remove_file(new_path, flags) < 0)
  75. status = -1;
  76. free(new_path);
  77. }
  78. if (closedir(dp) < 0) {
  79. bb_perror_msg("unable to close `%s'", path);
  80. return -1;
  81. }
  82. if (flags & FILEUTILS_INTERACTIVE) {
  83. fprintf(stderr, "%s: remove directory `%s'? ", bb_applet_name, path);
  84. if (!bb_ask_confirmation())
  85. return status;
  86. }
  87. if (rmdir(path) < 0) {
  88. bb_perror_msg("unable to remove `%s'", path);
  89. return -1;
  90. }
  91. return status;
  92. } else {
  93. if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
  94. !S_ISLNK(path_stat.st_mode) &&
  95. isatty(0)) ||
  96. (flags & FILEUTILS_INTERACTIVE)) {
  97. fprintf(stderr, "%s: remove `%s'? ", bb_applet_name, path);
  98. if (!bb_ask_confirmation())
  99. return 0;
  100. }
  101. if (unlink(path) < 0) {
  102. bb_perror_msg("unable to remove `%s'", path);
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. }