tool_dirhie.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 "tool_msgs.h"
  34. #include "memdebug.h" /* keep this as LAST include */
  35. #if defined(_WIN32) || (defined(MSDOS) && !defined(__DJGPP__))
  36. # define mkdir(x,y) (mkdir)((x))
  37. # ifndef F_OK
  38. # define F_OK 0
  39. # endif
  40. #endif
  41. static void show_dir_errno(struct GlobalConfig *global, const char *name)
  42. {
  43. switch(errno) {
  44. #ifdef EACCES
  45. case EACCES:
  46. errorf(global, "You don't have permission to create %s", name);
  47. break;
  48. #endif
  49. #ifdef ENAMETOOLONG
  50. case ENAMETOOLONG:
  51. errorf(global, "The directory name %s is too long", name);
  52. break;
  53. #endif
  54. #ifdef EROFS
  55. case EROFS:
  56. errorf(global, "%s resides on a read-only file system", name);
  57. break;
  58. #endif
  59. #ifdef ENOSPC
  60. case ENOSPC:
  61. errorf(global, "No space left on the file system that will "
  62. "contain the directory %s", name);
  63. break;
  64. #endif
  65. #ifdef EDQUOT
  66. case EDQUOT:
  67. errorf(global, "Cannot create directory %s because you "
  68. "exceeded your quota", name);
  69. break;
  70. #endif
  71. default:
  72. errorf(global, "Error creating directory %s", name);
  73. break;
  74. }
  75. }
  76. /*
  77. * Create the needed directory hierarchy recursively in order to save
  78. * multi-GETs in file output, ie:
  79. * curl "http://example.org/dir[1-5]/file[1-5].txt" -o "dir#1/file#2.txt"
  80. * should create all the dir* automagically
  81. */
  82. #if defined(_WIN32) || defined(__DJGPP__)
  83. /* systems that may use either or when specifying a path */
  84. #define PATH_DELIMITERS "\\/"
  85. #else
  86. #define PATH_DELIMITERS DIR_CHAR
  87. #endif
  88. CURLcode create_dir_hierarchy(const char *outfile, struct GlobalConfig *global)
  89. {
  90. char *tempdir;
  91. char *tempdir2;
  92. char *outdup;
  93. char *dirbuildup;
  94. CURLcode result = CURLE_OK;
  95. size_t outlen;
  96. outlen = strlen(outfile);
  97. outdup = strdup(outfile);
  98. if(!outdup)
  99. return CURLE_OUT_OF_MEMORY;
  100. dirbuildup = malloc(outlen + 1);
  101. if(!dirbuildup) {
  102. Curl_safefree(outdup);
  103. return CURLE_OUT_OF_MEMORY;
  104. }
  105. dirbuildup[0] = '\0';
  106. /* Allow strtok() here since this isn't used threaded */
  107. /* !checksrc! disable BANNEDFUNC 2 */
  108. tempdir = strtok(outdup, PATH_DELIMITERS);
  109. while(tempdir) {
  110. bool skip = false;
  111. tempdir2 = strtok(NULL, PATH_DELIMITERS);
  112. /* since strtok returns a token for the last word even
  113. if not ending with DIR_CHAR, we need to prune it */
  114. if(tempdir2) {
  115. size_t dlen = strlen(dirbuildup);
  116. if(dlen)
  117. msnprintf(&dirbuildup[dlen], outlen - dlen, "%s%s", DIR_CHAR, tempdir);
  118. else {
  119. if(outdup == tempdir) {
  120. #if defined(_WIN32) || defined(MSDOS)
  121. /* Skip creating a drive's current directory.
  122. It may seem as though that would harmlessly fail but it could be
  123. a corner case if X: did not exist, since we would be creating it
  124. erroneously.
  125. eg if outfile is X:\foo\bar\filename then don't mkdir X:
  126. This logic takes into account unsupported drives !:, 1:, etc. */
  127. char *p = strchr(tempdir, ':');
  128. if(p && !p[1])
  129. skip = true;
  130. #endif
  131. /* the output string doesn't start with a separator */
  132. strcpy(dirbuildup, tempdir);
  133. }
  134. else
  135. msnprintf(dirbuildup, outlen, "%s%s", DIR_CHAR, tempdir);
  136. }
  137. /* Create directory. Ignore access denied error to allow traversal. */
  138. if(!skip && (-1 == mkdir(dirbuildup, (mode_t)0000750)) &&
  139. (errno != EACCES) && (errno != EEXIST)) {
  140. show_dir_errno(global, dirbuildup);
  141. result = CURLE_WRITE_ERROR;
  142. break; /* get out of loop */
  143. }
  144. }
  145. tempdir = tempdir2;
  146. }
  147. Curl_safefree(dirbuildup);
  148. Curl_safefree(outdup);
  149. return result;
  150. }