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