tool_dirhie.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #include <sys/stat.h>
  26. #ifdef WIN32
  27. # include <direct.h>
  28. #endif
  29. #define ENABLE_CURLX_PRINTF
  30. /* use our own printf() functions */
  31. #include "curlx.h"
  32. #include "tool_dirhie.h"
  33. #include "memdebug.h" /* keep this as LAST include */
  34. #ifdef NETWARE
  35. # ifndef __NOVELL_LIBC__
  36. # define mkdir mkdir_510
  37. # endif
  38. #endif
  39. #if defined(WIN32) || (defined(MSDOS) && !defined(__DJGPP__))
  40. # define mkdir(x,y) (mkdir)((x))
  41. # ifndef F_OK
  42. # define F_OK 0
  43. # endif
  44. #endif
  45. static void show_dir_errno(FILE *errors, const char *name)
  46. {
  47. switch(errno) {
  48. #ifdef EACCES
  49. case EACCES:
  50. fprintf(errors, "You don't have permission to create %s.\n", name);
  51. break;
  52. #endif
  53. #ifdef ENAMETOOLONG
  54. case ENAMETOOLONG:
  55. fprintf(errors, "The directory name %s is too long.\n", name);
  56. break;
  57. #endif
  58. #ifdef EROFS
  59. case EROFS:
  60. fprintf(errors, "%s resides on a read-only file system.\n", name);
  61. break;
  62. #endif
  63. #ifdef ENOSPC
  64. case ENOSPC:
  65. fprintf(errors, "No space left on the file system that will "
  66. "contain the directory %s.\n", name);
  67. break;
  68. #endif
  69. #ifdef EDQUOT
  70. case EDQUOT:
  71. fprintf(errors, "Cannot create directory %s because you "
  72. "exceeded your quota.\n", name);
  73. break;
  74. #endif
  75. default :
  76. fprintf(errors, "Error creating directory %s.\n", name);
  77. break;
  78. }
  79. }
  80. /*
  81. * Create the needed directory hierarchy recursively in order to save
  82. * multi-GETs in file output, ie:
  83. * curl "http://my.site/dir[1-5]/file[1-5].txt" -o "dir#1/file#2.txt"
  84. * should create all the dir* automagically
  85. */
  86. #if defined(WIN32) || defined(__DJGPP__)
  87. /* systems that may use either or when specifying a path */
  88. #define PATH_DELIMITERS "\\/"
  89. #else
  90. #define PATH_DELIMITERS DIR_CHAR
  91. #endif
  92. CURLcode create_dir_hierarchy(const char *outfile, FILE *errors)
  93. {
  94. char *tempdir;
  95. char *tempdir2;
  96. char *outdup;
  97. char *dirbuildup;
  98. CURLcode result = CURLE_OK;
  99. size_t outlen;
  100. outlen = strlen(outfile);
  101. outdup = strdup(outfile);
  102. if(!outdup)
  103. return CURLE_OUT_OF_MEMORY;
  104. dirbuildup = malloc(outlen + 1);
  105. if(!dirbuildup) {
  106. Curl_safefree(outdup);
  107. return CURLE_OUT_OF_MEMORY;
  108. }
  109. dirbuildup[0] = '\0';
  110. /* Allow strtok() here since this isn't used threaded */
  111. /* !checksrc! disable BANNEDFUNC 2 */
  112. tempdir = strtok(outdup, PATH_DELIMITERS);
  113. while(tempdir) {
  114. bool skip = false;
  115. tempdir2 = strtok(NULL, PATH_DELIMITERS);
  116. /* since strtok returns a token for the last word even
  117. if not ending with DIR_CHAR, we need to prune it */
  118. if(tempdir2) {
  119. size_t dlen = strlen(dirbuildup);
  120. if(dlen)
  121. msnprintf(&dirbuildup[dlen], outlen - dlen, "%s%s", DIR_CHAR, tempdir);
  122. else {
  123. if(outdup == tempdir) {
  124. #if defined(MSDOS) || defined(WIN32)
  125. /* Skip creating a drive's current directory.
  126. It may seem as though that would harmlessly fail but it could be
  127. a corner case if X: did not exist, since we would be creating it
  128. erroneously.
  129. eg if outfile is X:\foo\bar\filename then don't mkdir X:
  130. This logic takes into account unsupported drives !:, 1:, etc. */
  131. char *p = strchr(tempdir, ':');
  132. if(p && !p[1])
  133. skip = true;
  134. #endif
  135. /* the output string doesn't start with a separator */
  136. strcpy(dirbuildup, tempdir);
  137. }
  138. else
  139. msnprintf(dirbuildup, outlen, "%s%s", DIR_CHAR, tempdir);
  140. }
  141. /* Create directory. Ignore access denied error to allow traversal. */
  142. if(!skip && (-1 == mkdir(dirbuildup, (mode_t)0000750)) &&
  143. (errno != EACCES) && (errno != EEXIST)) {
  144. show_dir_errno(errors, dirbuildup);
  145. result = CURLE_WRITE_ERROR;
  146. break; /* get out of loop */
  147. }
  148. }
  149. tempdir = tempdir2;
  150. }
  151. Curl_safefree(dirbuildup);
  152. Curl_safefree(outdup);
  153. return result;
  154. }