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