make_directory.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 FAST_FUNC 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. umask(mask & ~0300); /* Ensure intermediate dirs are wx */
  34. while (1) {
  35. c = '\0';
  36. if (flags & FILEUTILS_RECUR) { /* Get the parent. */
  37. /* Bypass leading non-'/'s and then subsequent '/'s. */
  38. while (*s) {
  39. if (*s == '/') {
  40. do {
  41. ++s;
  42. } while (*s == '/');
  43. c = *s; /* Save the current char */
  44. *s = '\0'; /* and replace it with nul. */
  45. break;
  46. }
  47. ++s;
  48. }
  49. }
  50. if (!c) /* Last component uses orig umask */
  51. umask(mask);
  52. if (mkdir(path, 0777) < 0) {
  53. /* If we failed for any other reason than the directory
  54. * already exists, output a diagnostic and return -1. */
  55. if (errno != EEXIST
  56. || !(flags & FILEUTILS_RECUR)
  57. || ((stat(path, &st) < 0) || !S_ISDIR(st.st_mode))
  58. ) {
  59. fail_msg = "create";
  60. umask(mask);
  61. break;
  62. }
  63. /* Since the directory exists, don't attempt to change
  64. * permissions if it was the full target. Note that
  65. * this is not an error condition. */
  66. if (!c) {
  67. umask(mask);
  68. return 0;
  69. }
  70. }
  71. if (!c) {
  72. /* Done. If necessary, update perms on the newly
  73. * created directory. Failure to update here _is_
  74. * an error. */
  75. if ((mode != -1) && (chmod(path, mode) < 0)) {
  76. fail_msg = "set permissions of";
  77. break;
  78. }
  79. return 0;
  80. }
  81. /* Remove any inserted nul from the path (recursive mode). */
  82. *s = c;
  83. } /* while (1) */
  84. bb_perror_msg("cannot %s directory '%s'", fail_msg, path);
  85. return -1;
  86. }