tool_dirhie.c 4.8 KB

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