mkdir.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/LexicalPath.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. int main(int argc, char** argv)
  33. {
  34. if (pledge("stdio cpath rpath", nullptr) < 0) {
  35. perror("pledge");
  36. return 1;
  37. }
  38. bool create_parents = false;
  39. Vector<const char*> directories;
  40. Core::ArgsParser args_parser;
  41. args_parser.add_option(create_parents, "Create parent directories if they don't exist", "parents", 'p');
  42. args_parser.add_positional_argument(directories, "Directories to create", "directories");
  43. args_parser.parse(argc, argv);
  44. // FIXME: Support -m/--mode option
  45. mode_t mode = 0755;
  46. bool has_errors = false;
  47. for (auto& directory : directories) {
  48. LexicalPath lexical_path(directory);
  49. if (!create_parents) {
  50. if (mkdir(lexical_path.string().characters(), mode) < 0) {
  51. perror("mkdir");
  52. has_errors = true;
  53. }
  54. continue;
  55. }
  56. StringBuilder path_builder;
  57. if (lexical_path.is_absolute())
  58. path_builder.append("/");
  59. for (auto& part : lexical_path.parts()) {
  60. path_builder.append(part);
  61. auto path = path_builder.build();
  62. struct stat st;
  63. if (stat(path.characters(), &st) < 0) {
  64. if (errno != ENOENT) {
  65. perror("stat");
  66. has_errors = true;
  67. break;
  68. }
  69. if (mkdir(path.characters(), mode) < 0) {
  70. perror("mkdir");
  71. has_errors = true;
  72. break;
  73. }
  74. } else {
  75. if (!S_ISDIR(st.st_mode)) {
  76. fprintf(stderr, "mkdir: cannot create directory '%s': not a directory\n", path.characters());
  77. has_errors = true;
  78. break;
  79. }
  80. }
  81. path_builder.append("/");
  82. }
  83. }
  84. return has_errors ? 1 : 0;
  85. }