3
0

make_directory.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <errno.h>
  24. #include <unistd.h>
  25. #include <sys/stat.h>
  26. #include "libbb.h"
  27. int bb_make_directory (char *path, long mode, int flags)
  28. {
  29. mode_t mask;
  30. const char *fail_msg;
  31. char *s = path;
  32. char c;
  33. struct stat st;
  34. mask = umask(0);
  35. if (mode == -1) {
  36. umask(mask);
  37. mode = (S_IXUSR | S_IXGRP | S_IXOTH |
  38. S_IWUSR | S_IWGRP | S_IWOTH |
  39. S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
  40. } else {
  41. umask(mask & ~0300);
  42. }
  43. do {
  44. c = 0;
  45. if (flags & FILEUTILS_RECUR) { /* Get the parent. */
  46. /* Bypass leading non-'/'s and then subsequent '/'s. */
  47. while (*s) {
  48. if (*s == '/') {
  49. do {
  50. ++s;
  51. } while (*s == '/');
  52. c = *s; /* Save the current char */
  53. *s = 0; /* and replace it with nul. */
  54. break;
  55. }
  56. ++s;
  57. }
  58. }
  59. if (mkdir(path, 0777) < 0) {
  60. /* If we failed for any other reason than the directory
  61. * already exists, output a diagnostic and return -1.*/
  62. if (errno != EEXIST
  63. || !(flags & FILEUTILS_RECUR)
  64. || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
  65. fail_msg = "create";
  66. umask(mask);
  67. break;
  68. }
  69. /* Since the directory exists, don't attempt to change
  70. * permissions if it was the full target. Note that
  71. * this is not an error conditon. */
  72. if (!c) {
  73. umask(mask);
  74. return 0;
  75. }
  76. }
  77. if (!c) {
  78. /* Done. If necessary, updated perms on the newly
  79. * created directory. Failure to update here _is_
  80. * an error.*/
  81. umask(mask);
  82. if ((mode != -1) && (chmod(path, mode) < 0)){
  83. fail_msg = "set permissions of";
  84. break;
  85. }
  86. return 0;
  87. }
  88. /* Remove any inserted nul from the path (recursive mode). */
  89. *s = c;
  90. } while (1);
  91. bb_perror_msg ("cannot %s directory '%s'", fail_msg, path);
  92. return -1;
  93. }