From 3a265978c329692de68fa9553ca6161eff62e0b8 Mon Sep 17 00:00:00 2001 From: goodale Date: Tue, 2 May 2000 21:13:17 +0000 Subject: Added CCTK_MkDirs to make a directory and all necessary parents. CCTK_mkdir now calls this. Tom git-svn-id: http://svn.cactuscode.org/flesh/trunk@1622 17b73243-c579-4c4c-a9d2-2d5706c11dac --- src/util/File.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/util/File.c b/src/util/File.c index 67aaa2fa..5396a47a 100644 --- a/src/util/File.c +++ b/src/util/File.c @@ -17,15 +17,33 @@ #include "cctk_WarnLevel.h" #include "cctk_FortranString.h" +#include "util_String.h" + static char *rcsid = "$Header$"; CCTK_FILEVERSION(util_File_c) /* some systems (eg. Windows NT) don't define this macro */ #ifndef S_ISDIR -#define S_ISDIR(mode) ((mode) & S_IFMT) == S_IFDIR) +#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) #endif +/* Some systems don't have mode_t and only pass one argument to mkdir. */ +#ifdef HAVE_MODE_T +#define mkdir(a,b) mkdir(a,b) +#else + +/* FIXME: Temporary kludge until everyone has reconfigured */ +#ifdef WIN32 +#define mkdir(a,b) mkdir(a) +#else +#define mkdir(a,b) mkdir(a,b) +/* Should just be the clause with one argument, this one should go. */ +#endif /* WIN32 */ + +#endif /* HAVE_MODE_T */ + +int CCTK_MkDirs(const char *pathname, int mode); /*@@ @routine CCTK_mkdir @@ -51,7 +69,11 @@ CCTK_FILEVERSION(util_File_c) int CCTK_mkdir(const char *dir) { + int retval; + +#if 0 + struct stat statbuf; char *command; @@ -87,6 +109,9 @@ int CCTK_mkdir(const char *dir) free(command); retval = 0; +#endif + + retval = CCTK_MkDirs(dir, 0755); return retval; @@ -98,3 +123,116 @@ void FMODIFIER FORTRAN_NAME(CCTK_mkdir)(int *ierr, ONE_FORTSTRING_ARG) *ierr = CCTK_mkdir(arg1); free(arg1); } + + /*@@ + @routine CCTK_MkDirs + @date Tue May 2 21:38:48 2000 + @author Tom Goodale + @desc + Makes all directories necessary for the path to exist. + @enddesc + @calls + @calledby + @history + + @endhistory + +@@*/ +int CCTK_MkDirs(const char *pathname, int mode) +{ + int retval; + const char *path; + char *current; + const char *token; + struct stat statbuf; + + path = pathname; + + retval = 0; + + current = (char *)malloc((strlen(pathname)+1)*sizeof(char)); + + if(current) + { + current[0] = '\0'; + + while((token = Util_StrSep(&path, "/"))) + { + /* Treat first token carefully. */ + if(*current == '\0') + { + if(*token == '\0') + { + strcpy(current, "/"); + } + else + { + strcpy(current,token); + } + } + else + { + sprintf(current,"%s/%s", current, token); + } + + if(stat(current, &statbuf)) + { + CCTK_VInfo("Cactus","Creating directory: \"%s\"", current); + if(mkdir(current, mode) == -1) + { + CCTK_VWarn(0,__LINE__,__FILE__,"Cactus","Failed to create directory \"%s\"", current); + retval = -2; + } + } + else + { + if(! S_ISDIR(statbuf.st_mode)) + { + CCTK_VWarn(0,__LINE__,__FILE__,"Cactus","\"%s\" exists but is not a directory", current); + retval = -3; + } + } + + if(retval) + { + break; + } + } + + if(! retval) + { + /* Deal with last component of path */ + if(path - pathname < strlen(pathname)) + { + if(stat(pathname, &statbuf)) + { + CCTK_VInfo("Cactus","Creating directory: \"%s\"", pathname); + if(mkdir(pathname, mode) == -1) + { + CCTK_VWarn(0,__LINE__,__FILE__,"Cactus","Failed to create directory \"%s\"", pathname); + retval = -2; + } + } + else + { + if(! S_ISDIR(statbuf.st_mode)) + { + CCTK_VWarn(0,__LINE__,__FILE__,"Cactus", "\"%s\" exists but is not a directory", pathname); + retval = -3; + } + } + } + } + + free(current); + + } + else + { + CCTK_Warn(0,__LINE__,__FILE__,"Cactus", "Failed to allocate some temporary memory"); + retval = -1; + } + + return retval; + +} -- cgit v1.2.3