3
0

make_directory.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * parse_mode implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* Mar 5, 2003 Manuel Novoa III
  10. *
  11. * This is the main work function for the 'mkdir' applet. As such, it
  12. * strives to be SUSv3 compliant in it's behaviour when recursively
  13. * making missing parent dirs, and in it's mode setting of the final
  14. * directory 'path'.
  15. *
  16. * To recursively build all missing intermediate directories, make
  17. * sure that (flags & FILEUTILS_RECUR) is non-zero. Newly created
  18. * intermediate directories will have at least u+wx perms.
  19. *
  20. * To set specific permissions on 'path', pass the appropriate 'mode'
  21. * val. Otherwise, pass -1 to get default permissions.
  22. */
  23. #include "libbb.h"
  24. /* This function is used from NOFORK applets. It must not allocate anything */
  25. int bb_make_directory (char *path, long mode, int flags)
  26. {
  27. mode_t mask;
  28. const char *fail_msg;
  29. char *s = path;
  30. char c;
  31. struct stat st;
  32. mask = umask(0);
  33. if (mode == -1) {
  34. umask(mask);
  35. mode = (S_IXUSR | S_IXGRP | S_IXOTH |
  36. S_IWUSR | S_IWGRP | S_IWOTH |
  37. S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
  38. } else {
  39. umask(mask & ~0300);
  40. }
  41. do {
  42. c = 0;
  43. if (flags & FILEUTILS_RECUR) { /* Get the parent. */
  44. /* Bypass leading non-'/'s and then subsequent '/'s. */
  45. while (*s) {
  46. if (*s == '/') {
  47. do {
  48. ++s;
  49. } while (*s == '/');
  50. c = *s; /* Save the current char */
  51. *s = 0; /* and replace it with nul. */
  52. break;
  53. }
  54. ++s;
  55. }
  56. }
  57. if (mkdir(path, 0777) < 0) {
  58. /* If we failed for any other reason than the directory
  59. * already exists, output a diagnostic and return -1.*/
  60. if (errno != EEXIST
  61. || !(flags & FILEUTILS_RECUR)
  62. || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
  63. fail_msg = "create";
  64. umask(mask);
  65. break;
  66. }
  67. /* Since the directory exists, don't attempt to change
  68. * permissions if it was the full target. Note that
  69. * this is not an error conditon. */
  70. if (!c) {
  71. umask(mask);
  72. return 0;
  73. }
  74. }
  75. if (!c) {
  76. /* Done. If necessary, updated perms on the newly
  77. * created directory. Failure to update here _is_
  78. * an error.*/
  79. umask(mask);
  80. if ((mode != -1) && (chmod(path, mode) < 0)){
  81. fail_msg = "set permissions of";
  82. break;
  83. }
  84. return 0;
  85. }
  86. /* Remove any inserted nul from the path (recursive mode). */
  87. *s = c;
  88. } while (1);
  89. bb_perror_msg("cannot %s directory '%s'", fail_msg, path);
  90. return -1;
  91. }