tool_dirhie.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #include <sys/stat.h>
00025 
00026 #ifdef WIN32
00027 #  include <direct.h>
00028 #endif
00029 
00030 #define ENABLE_CURLX_PRINTF
00031 /* use our own printf() functions */
00032 #include "curlx.h"
00033 
00034 #include "tool_dirhie.h"
00035 
00036 #include "memdebug.h" /* keep this as LAST include */
00037 
00038 #ifdef NETWARE
00039 #  ifndef __NOVELL_LIBC__
00040 #    define mkdir mkdir_510
00041 #  endif
00042 #endif
00043 
00044 #ifdef WIN32
00045 #  define mkdir(x,y) (mkdir)((x))
00046 #  ifndef __POCC__
00047 #    define F_OK 0
00048 #  endif
00049 #endif
00050 
00051 static void show_dir_errno(FILE *errors, const char *name)
00052 {
00053   switch(ERRNO) {
00054 #ifdef EACCES
00055   case EACCES:
00056     fprintf(errors, "You don't have permission to create %s.\n", name);
00057     break;
00058 #endif
00059 #ifdef ENAMETOOLONG
00060   case ENAMETOOLONG:
00061     fprintf(errors, "The directory name %s is too long.\n", name);
00062     break;
00063 #endif
00064 #ifdef EROFS
00065   case EROFS:
00066     fprintf(errors, "%s resides on a read-only file system.\n", name);
00067     break;
00068 #endif
00069 #ifdef ENOSPC
00070   case ENOSPC:
00071     fprintf(errors, "No space left on the file system that will "
00072             "contain the directory %s.\n", name);
00073     break;
00074 #endif
00075 #ifdef EDQUOT
00076   case EDQUOT:
00077     fprintf(errors, "Cannot create directory %s because you "
00078             "exceeded your quota.\n", name);
00079     break;
00080 #endif
00081   default :
00082     fprintf(errors, "Error creating directory %s.\n", name);
00083     break;
00084   }
00085 }
00086 
00087 /*
00088  * Create the needed directory hierarchy recursively in order to save
00089  *  multi-GETs in file output, ie:
00090  *  curl "http://my.site/dir[1-5]/file[1-5].txt" -o "dir#1/file#2.txt"
00091  *  should create all the dir* automagically
00092  */
00093 
00094 #ifdef WIN32
00095 /* systems that may use either or when specifying a path */
00096 #define PATH_DELIMITERS "\\/"
00097 #else
00098 #define PATH_DELIMITERS DIR_CHAR
00099 #endif
00100 
00101 
00102 CURLcode create_dir_hierarchy(const char *outfile, FILE *errors)
00103 {
00104   char *tempdir;
00105   char *tempdir2;
00106   char *outdup;
00107   char *dirbuildup;
00108   CURLcode result = CURLE_OK;
00109   size_t outlen;
00110 
00111   outlen = strlen(outfile);
00112   outdup = strdup(outfile);
00113   if(!outdup)
00114     return CURLE_OUT_OF_MEMORY;
00115 
00116   dirbuildup = malloc(outlen + 1);
00117   if(!dirbuildup) {
00118     Curl_safefree(outdup);
00119     return CURLE_OUT_OF_MEMORY;
00120   }
00121   dirbuildup[0] = '\0';
00122 
00123   /* Allow strtok() here since this isn't used threaded */
00124   /* !checksrc! disable BANNEDFUNC 2 */
00125   tempdir = strtok(outdup, PATH_DELIMITERS);
00126 
00127   while(tempdir != NULL) {
00128     tempdir2 = strtok(NULL, PATH_DELIMITERS);
00129     /* since strtok returns a token for the last word even
00130        if not ending with DIR_CHAR, we need to prune it */
00131     if(tempdir2 != NULL) {
00132       size_t dlen = strlen(dirbuildup);
00133       if(dlen)
00134         snprintf(&dirbuildup[dlen], outlen - dlen, "%s%s", DIR_CHAR, tempdir);
00135       else {
00136         if(outdup == tempdir)
00137           /* the output string doesn't start with a separator */
00138           strcpy(dirbuildup, tempdir);
00139         else
00140           snprintf(dirbuildup, outlen, "%s%s", DIR_CHAR, tempdir);
00141       }
00142       if(access(dirbuildup, F_OK) == -1) {
00143         if(-1 == mkdir(dirbuildup, (mode_t)0000750)) {
00144           show_dir_errno(errors, dirbuildup);
00145           result = CURLE_WRITE_ERROR;
00146           break; /* get out of loop */
00147         }
00148       }
00149     }
00150     tempdir = tempdir2;
00151   }
00152 
00153   Curl_safefree(dirbuildup);
00154   Curl_safefree(outdup);
00155 
00156   return result;
00157 }
00158 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07