logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git

rm.c (5207B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. // NetBSD <10 hides fdopendir behind _NETBSD_SOURCE
  6. #if __NetBSD_Version__ < 1000000000
  7. #define _NETBSD_SOURCE
  8. #endif
  9. #include "../lib/consent.h"
  10. #include <assert.h>
  11. #include <ctype.h> // isprint
  12. #include <dirent.h> // fdopendir, readdir, closedir
  13. #include <errno.h> // errno
  14. #include <fcntl.h> // AT_FDCWD
  15. #include <limits.h> // PATH_MAX
  16. #include <locale.h> // setlocale
  17. #include <stdarg.h> // va_list
  18. #include <stdbool.h>
  19. #include <stdio.h> // fprintf, getline
  20. #include <stdlib.h> // free
  21. #include <string.h> // strerror
  22. #include <sys/stat.h> // chmod, fstatat, S_ISDIR
  23. #include <unistd.h> // unlink, isatty
  24. bool opt_d = false, force = false, recurse = false, verbose = false, opt_i = false;
  25. const char *argv0 = "rm";
  26. static int
  27. do_unlinkat(int fd, char *name, char *acc_path)
  28. {
  29. struct stat stats;
  30. int err = 0;
  31. assert(errno == 0);
  32. if(fstatat(fd, name, &stats, AT_SYMLINK_NOFOLLOW) != 0)
  33. {
  34. if(force && errno == ENOENT)
  35. {
  36. errno = 0;
  37. return 0;
  38. }
  39. fprintf(stderr, "rm: error: Failed getting status for '%s': %s\n", acc_path, strerror(errno));
  40. errno = 0;
  41. return 1;
  42. }
  43. bool is_dir = S_ISDIR(stats.st_mode);
  44. if(is_dir && !opt_d)
  45. {
  46. if(!recurse)
  47. {
  48. fprintf(stderr, "rm: error: Is a directory, pass -r or -d to remove: %s\n", acc_path);
  49. return 1;
  50. }
  51. if(!force && opt_i)
  52. if(!consentf("rm: Recurse into '%s' ? [y/N] ", acc_path)) return 0;
  53. assert(errno == 0);
  54. int dir = openat(fd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
  55. if(dir == -1)
  56. {
  57. fprintf(
  58. stderr, "rm: error: Couldn't open '%s' as directory: %s\n", acc_path, strerror(errno));
  59. errno = 0;
  60. return 1;
  61. }
  62. assert(errno == 0);
  63. DIR *dirp = fdopendir(dir);
  64. if(dirp == NULL)
  65. {
  66. fprintf(stderr,
  67. "rm: error: Couldn't get DIR entry for opened '%s': %s\n",
  68. acc_path,
  69. strerror(errno));
  70. errno = 0;
  71. return 1;
  72. }
  73. while(true)
  74. {
  75. assert(errno == 0);
  76. struct dirent *dp = readdir(dirp);
  77. if(dp == NULL)
  78. {
  79. if(errno == 0) break;
  80. fprintf(
  81. stderr, "rm: error: Failed reading directory '%s': %s\n", acc_path, strerror(errno));
  82. closedir(dirp); // error ignored
  83. errno = 0;
  84. return 1;
  85. }
  86. if(strcmp(dp->d_name, ".") == 0) continue;
  87. if(strcmp(dp->d_name, "..") == 0) continue;
  88. char new_path[PATH_MAX] = "";
  89. assert(errno == 0);
  90. if(snprintf(new_path, PATH_MAX, "%s/%s", acc_path, dp->d_name) < 0)
  91. {
  92. fprintf(stderr,
  93. "rm: error: Couldn't concatenate '%s' into parent '%s', skipping to next entry: %s",
  94. name,
  95. acc_path,
  96. strerror(errno));
  97. err = 1;
  98. errno = 0;
  99. continue;
  100. }
  101. // No depth counter for now, unlikely to be a problem
  102. int ret = do_unlinkat(dir, dp->d_name, new_path);
  103. if(ret != 0) err = 1;
  104. }
  105. // fdopendir allocates memory for DIR, needs closedir
  106. assert(errno == 0);
  107. if(closedir(dirp) != 0)
  108. {
  109. fprintf(stderr,
  110. "rm: error: Deallocating directory entry for '%s' failed: %s\n",
  111. acc_path,
  112. strerror(errno));
  113. errno = 0;
  114. return 1;
  115. }
  116. }
  117. if(!force)
  118. {
  119. if(opt_i)
  120. {
  121. if(!consentf("rm: Remove '%s' ? [y/N] ", acc_path)) return 0;
  122. }
  123. else
  124. {
  125. // Don't check symbolic links, would need AT_SYMLINK_NOFOLLOW on faccessat which isn't portable
  126. // Can assume symbolic links are 0777 anyway
  127. if(!S_ISLNK(stats.st_mode) && faccessat(fd, name, W_OK, 0) != 0)
  128. {
  129. errno = 0;
  130. if(!consentf("rm: Remove non-writable '%s' ? [y/N] ", acc_path)) return 0;
  131. }
  132. }
  133. }
  134. assert(errno == 0);
  135. if(unlinkat(fd, name, is_dir ? AT_REMOVEDIR : 0) != 0)
  136. {
  137. fprintf(stderr, "rm: error: Couldn't remove '%s': %s\n", acc_path, strerror(errno));
  138. errno = 0;
  139. return 1;
  140. }
  141. else if(verbose)
  142. {
  143. fprintf(stderr, "rm: Removed: %s\n", acc_path);
  144. }
  145. return err;
  146. }
  147. static void
  148. usage(void)
  149. {
  150. fprintf(stderr, "Usage: rm [-firRv] [files ...]\n");
  151. }
  152. int
  153. main(int argc, char *argv[])
  154. {
  155. int c = -1;
  156. while((c = getopt(argc, argv, ":dfirRv")) != -1)
  157. {
  158. switch(c)
  159. {
  160. case 'd':
  161. opt_d = true;
  162. break;
  163. case 'f':
  164. force = true;
  165. break;
  166. case 'i':
  167. opt_i = true;
  168. break;
  169. case 'r':
  170. recurse = true;
  171. break;
  172. case 'R':
  173. recurse = true;
  174. break;
  175. case 'v':
  176. verbose = true;
  177. break;
  178. case ':':
  179. fprintf(stderr, "rm: error: Missing operand for option: '-%c'\n", optopt);
  180. usage();
  181. return 1;
  182. case '?':
  183. fprintf(stderr, "rm: error: Unrecognised option: '-%c'\n", optopt);
  184. usage();
  185. return 1;
  186. default:
  187. abort();
  188. }
  189. }
  190. argc -= optind;
  191. argv += optind;
  192. errno = 0;
  193. setlocale(LC_ALL, "");
  194. if(errno != 0)
  195. {
  196. fprintf(stderr, "%s: warning: Failed to initialize locales: %s\n", argv0, strerror(errno));
  197. errno = 0;
  198. }
  199. consent_init();
  200. if(argc == 0 && !force)
  201. {
  202. fprintf(stderr, "rm: error: missing operand\n");
  203. usage();
  204. return 1;
  205. }
  206. int err = 0;
  207. for(int i = 0; i < argc; i++)
  208. {
  209. int ret = do_unlinkat(AT_FDCWD, argv[i], argv[i]);
  210. if(ret != 0) err = 1;
  211. }
  212. consent_finish();
  213. return err;
  214. }