make_directory.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* Mar 5, 2003 Manuel Novoa III
  23. *
  24. * This is the main work function for the 'mkdir' applet. As such, it
  25. * strives to be SUSv3 compliant in it's behaviour when recursively
  26. * making missing parent dirs, and in it's mode setting of the final
  27. * directory 'path'.
  28. *
  29. * To recursively build all missing intermediate directories, make
  30. * sure that (flags & FILEUTILS_RECUR) is non-zero. Newly created
  31. * intermediate directories will have at least u+wx perms.
  32. *
  33. * To set specific permissions on 'path', pass the appropriate 'mode'
  34. * val. Otherwise, pass -1 to get default permissions.
  35. */
  36. #include <errno.h>
  37. #include <unistd.h>
  38. #include <sys/stat.h>
  39. #include "libbb.h"
  40. int bb_make_directory (char *path, long mode, int flags)
  41. {
  42. mode_t mask;
  43. const char *fail_msg;
  44. char *s = path;
  45. char c;
  46. struct stat st;
  47. mask = umask(0);
  48. if (mode == -1) {
  49. umask(mask);
  50. mode = (S_IXUSR | S_IXGRP | S_IXOTH |
  51. S_IWUSR | S_IWGRP | S_IWOTH |
  52. S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
  53. } else {
  54. umask(mask & ~0300);
  55. }
  56. do {
  57. c = 0;
  58. if (flags & FILEUTILS_RECUR) { /* Get the parent. */
  59. /* Bypass leading non-'/'s and then subsequent '/'s. */
  60. while (*s) {
  61. if (*s == '/') {
  62. do {
  63. ++s;
  64. } while (*s == '/');
  65. c = *s; /* Save the current char */
  66. *s = 0; /* and replace it with nul. */
  67. break;
  68. }
  69. ++s;
  70. }
  71. }
  72. if (mkdir(path, 0777) < 0) {
  73. /* If we failed for any other reason than the directory
  74. * already exists, output a diagnostic and return -1.*/
  75. if (errno != EEXIST
  76. || !(flags & FILEUTILS_RECUR)
  77. || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
  78. fail_msg = "create";
  79. umask(mask);
  80. break;
  81. }
  82. /* Since the directory exists, don't attempt to change
  83. * permissions if it was the full target. Note that
  84. * this is not an error conditon. */
  85. if (!c) {
  86. umask(mask);
  87. return 0;
  88. }
  89. }
  90. if (!c) {
  91. /* Done. If necessary, updated perms on the newly
  92. * created directory. Failure to update here _is_
  93. * an error.*/
  94. umask(mask);
  95. if ((mode != -1) && (chmod(path, mode) < 0)){
  96. fail_msg = "set permissions of";
  97. break;
  98. }
  99. return 0;
  100. }
  101. /* Remove any inserted nul from the path (recursive mode). */
  102. *s = c;
  103. } while (1);
  104. bb_perror_msg ("Cannot %s directory `%s'", fail_msg, path);
  105. return -1;
  106. }