rm.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Vector.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <dirent.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sys/stat.h>
  35. #include <unistd.h>
  36. static int remove(bool recursive, bool force, String path)
  37. {
  38. struct stat path_stat;
  39. if (lstat(path.characters(), &path_stat) < 0) {
  40. if (!force)
  41. perror("lstat");
  42. return 1;
  43. }
  44. if (S_ISDIR(path_stat.st_mode) && recursive) {
  45. auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
  46. if (di.has_error()) {
  47. if (!force)
  48. fprintf(stderr, "DirIterator: %s\n", di.error_string());
  49. return 1;
  50. }
  51. while (di.has_next()) {
  52. int s = remove(true, force, di.next_full_path());
  53. if (s != 0 && !force)
  54. return s;
  55. }
  56. int s = rmdir(path.characters());
  57. if (s < 0 && !force) {
  58. perror("rmdir");
  59. return 1;
  60. }
  61. } else {
  62. int rc = unlink(path.characters());
  63. if (rc < 0 && !force) {
  64. perror("unlink");
  65. return 1;
  66. }
  67. }
  68. return 0;
  69. }
  70. int main(int argc, char** argv)
  71. {
  72. if (pledge("stdio rpath cpath", nullptr) < 0) {
  73. perror("pledge");
  74. return 1;
  75. }
  76. bool recursive = false;
  77. bool force = false;
  78. Vector<const char*> paths;
  79. Core::ArgsParser args_parser;
  80. args_parser.add_option(recursive, "Delete directories recursively", "recursive", 'r');
  81. args_parser.add_option(force, "Force", "force", 'f');
  82. args_parser.add_positional_argument(paths, "Path(s) to remove", "path");
  83. args_parser.parse(argc, argv);
  84. int rc = 0;
  85. for (auto& path : paths) {
  86. rc |= remove(recursive, force, path);
  87. }
  88. return rc;
  89. }