2011-09-19 12:18:17 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2016-09-07 04:43:40 -04:00
|
|
|
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-09-19 12:18:17 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-09-19 12:18:17 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2011-09-19 12:18:17 -04:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
# include <direct.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_dirhie.h"
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-19 12:18:17 -04:00
|
|
|
|
|
|
|
#ifdef NETWARE
|
|
|
|
# ifndef __NOVELL_LIBC__
|
|
|
|
# define mkdir mkdir_510
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
# define mkdir(x,y) (mkdir)((x))
|
|
|
|
# ifndef __POCC__
|
|
|
|
# define F_OK 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void show_dir_errno(FILE *errors, const char *name)
|
|
|
|
{
|
2017-06-19 00:52:38 -04:00
|
|
|
switch(errno) {
|
2011-09-19 12:18:17 -04:00
|
|
|
#ifdef EACCES
|
|
|
|
case EACCES:
|
|
|
|
fprintf(errors, "You don't have permission to create %s.\n", name);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef ENAMETOOLONG
|
|
|
|
case ENAMETOOLONG:
|
|
|
|
fprintf(errors, "The directory name %s is too long.\n", name);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef EROFS
|
|
|
|
case EROFS:
|
|
|
|
fprintf(errors, "%s resides on a read-only file system.\n", name);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef ENOSPC
|
|
|
|
case ENOSPC:
|
|
|
|
fprintf(errors, "No space left on the file system that will "
|
|
|
|
"contain the directory %s.\n", name);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef EDQUOT
|
|
|
|
case EDQUOT:
|
|
|
|
fprintf(errors, "Cannot create directory %s because you "
|
|
|
|
"exceeded your quota.\n", name);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default :
|
|
|
|
fprintf(errors, "Error creating directory %s.\n", name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the needed directory hierarchy recursively in order to save
|
|
|
|
* multi-GETs in file output, ie:
|
|
|
|
* curl "http://my.site/dir[1-5]/file[1-5].txt" -o "dir#1/file#2.txt"
|
|
|
|
* should create all the dir* automagically
|
|
|
|
*/
|
|
|
|
|
2016-09-13 09:20:05 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
/* systems that may use either or when specifying a path */
|
|
|
|
#define PATH_DELIMITERS "\\/"
|
|
|
|
#else
|
|
|
|
#define PATH_DELIMITERS DIR_CHAR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-09-19 12:18:17 -04:00
|
|
|
CURLcode create_dir_hierarchy(const char *outfile, FILE *errors)
|
|
|
|
{
|
|
|
|
char *tempdir;
|
|
|
|
char *tempdir2;
|
|
|
|
char *outdup;
|
|
|
|
char *dirbuildup;
|
|
|
|
CURLcode result = CURLE_OK;
|
2013-03-06 07:27:51 -05:00
|
|
|
size_t outlen;
|
2011-09-19 12:18:17 -04:00
|
|
|
|
2013-03-06 07:27:51 -05:00
|
|
|
outlen = strlen(outfile);
|
2011-09-19 12:18:17 -04:00
|
|
|
outdup = strdup(outfile);
|
|
|
|
if(!outdup)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2013-03-06 07:27:51 -05:00
|
|
|
dirbuildup = malloc(outlen + 1);
|
2011-09-19 12:18:17 -04:00
|
|
|
if(!dirbuildup) {
|
|
|
|
Curl_safefree(outdup);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
dirbuildup[0] = '\0';
|
|
|
|
|
2016-09-07 04:43:40 -04:00
|
|
|
/* Allow strtok() here since this isn't used threaded */
|
|
|
|
/* !checksrc! disable BANNEDFUNC 2 */
|
2016-09-13 09:20:05 -04:00
|
|
|
tempdir = strtok(outdup, PATH_DELIMITERS);
|
2011-09-19 12:18:17 -04:00
|
|
|
|
|
|
|
while(tempdir != NULL) {
|
2016-09-13 09:20:05 -04:00
|
|
|
tempdir2 = strtok(NULL, PATH_DELIMITERS);
|
2011-09-19 12:18:17 -04:00
|
|
|
/* since strtok returns a token for the last word even
|
|
|
|
if not ending with DIR_CHAR, we need to prune it */
|
|
|
|
if(tempdir2 != NULL) {
|
|
|
|
size_t dlen = strlen(dirbuildup);
|
|
|
|
if(dlen)
|
2013-03-06 07:27:51 -05:00
|
|
|
snprintf(&dirbuildup[dlen], outlen - dlen, "%s%s", DIR_CHAR, tempdir);
|
2011-09-19 12:18:17 -04:00
|
|
|
else {
|
2016-09-13 09:20:05 -04:00
|
|
|
if(outdup == tempdir)
|
|
|
|
/* the output string doesn't start with a separator */
|
2011-09-19 12:18:17 -04:00
|
|
|
strcpy(dirbuildup, tempdir);
|
|
|
|
else
|
2013-03-06 07:27:51 -05:00
|
|
|
snprintf(dirbuildup, outlen, "%s%s", DIR_CHAR, tempdir);
|
2011-09-19 12:18:17 -04:00
|
|
|
}
|
|
|
|
if(access(dirbuildup, F_OK) == -1) {
|
2015-03-17 08:41:49 -04:00
|
|
|
if(-1 == mkdir(dirbuildup, (mode_t)0000750)) {
|
2011-09-19 12:18:17 -04:00
|
|
|
show_dir_errno(errors, dirbuildup);
|
|
|
|
result = CURLE_WRITE_ERROR;
|
|
|
|
break; /* get out of loop */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tempdir = tempdir2;
|
|
|
|
}
|
|
|
|
|
|
|
|
Curl_safefree(dirbuildup);
|
|
|
|
Curl_safefree(outdup);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|