tool_dirhie.c 4.7 KB

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