d_mkdir.c

Go to the documentation of this file.
00001 #include <unistd.h>
00002 #include <sys/stat.h>
00003 #include <sys/types.h>
00004 #include <grass/dbmi.h>
00005 #include "dbstubs.h"
00006 
00007 
00008 static char *rfind(char *string, char c);
00009 static int make_parent_dir(char *path, int mode);
00010 static int make_dir(char *path, int mode);
00011 
00012 
00019 int
00020 db_driver_mkdir (char *path, int mode, int parentdirs)
00021 {
00022     if (parentdirs)
00023     {
00024         if (make_parent_dir (path, mode) != DB_OK)
00025             return DB_FAILED;
00026     }
00027 
00028     return make_dir (path, mode);
00029 }
00030 
00031 
00032 /* make a directory if it doesn't exist */
00033 /* this routine could be made more intelligent as to why it failed */
00034 static int
00035 make_dir (char *path, int mode)
00036 {
00037     if (db_isdir(path) == DB_OK)
00038         return DB_OK;
00039 
00040 #ifdef __MINGW32__
00041     if (mkdir (path) == 0)
00042         return DB_OK;
00043 #else
00044     if (mkdir (path, mode) == 0)
00045         return DB_OK;
00046 #endif
00047 
00048     db_syserror(path);
00049 
00050     return DB_FAILED;
00051 }
00052 
00053 
00054 static int
00055 make_parent_dir(char *path, int mode)
00056 {
00057     char *slash;
00058     int stat;
00059 
00060     slash = rfind(path,'/');
00061     if (slash == NULL || slash == path)
00062         return DB_OK; /* no parent dir to make. return ok */
00063 
00064     *slash = 0; /* add NULL to terminate parentdir string */
00065     if (access(path,0) == 0) /* path exists, good enough */
00066     {
00067         stat = DB_OK;
00068     }
00069     else if (make_parent_dir (path, mode) != DB_OK)
00070     {
00071         stat = DB_FAILED;
00072     }
00073     else if(make_dir (path, mode) == DB_OK)
00074     {
00075         stat = DB_OK;
00076     }
00077     else
00078     {
00079         stat = DB_FAILED;
00080     }
00081     *slash = '/';  /* put the slash back into the path */
00082 
00083     return stat;
00084 }
00085 
00086 
00087 static
00088 char *rfind(char *string, char c)
00089 {
00090     char *found;
00091 
00092     found = NULL;
00093     while (*string)
00094     {
00095         if (*string == c)
00096             found = string;
00097         string++;
00098     }
00099 
00100     return found;
00101 }

Generated on Sun Apr 6 17:31:38 2008 for GRASS by  doxygen 1.5.5