Go to the documentation of this file.00001
00015 #include <string.h>
00016 #include <unistd.h>
00017 #include <sys/stat.h>
00018 #include <sys/types.h>
00019 #include <grass/dbmi.h>
00020 #include "dbstubs.h"
00021
00022
00023 static char *rfind(char *string, char c);
00024 static int make_parent_dir(char *path, int mode);
00025 static int make_dir(const char *path, int mode);
00026
00027
00038 int db_driver_mkdir(const char *path, int mode, int parentdirs)
00039 {
00040 if (parentdirs) {
00041 char path2[GPATH_MAX];
00042
00043 strcpy(path2, path);
00044 if (make_parent_dir(path2, mode) != DB_OK)
00045 return DB_FAILED;
00046 }
00047
00048 return make_dir(path, mode);
00049 }
00050
00051
00052
00053
00054 static int make_dir(const char *path, int mode)
00055 {
00056 if (db_isdir(path) == DB_OK)
00057 return DB_OK;
00058
00059 if (G_mkdir(path) == 0)
00060 return DB_OK;
00061
00062 db_syserror(path);
00063
00064 return DB_FAILED;
00065 }
00066
00067
00068 static int make_parent_dir(char *path, int mode)
00069 {
00070 char *slash;
00071 int stat;
00072
00073 slash = rfind(path, '/');
00074 if (slash == NULL || slash == path)
00075 return DB_OK;
00076
00077 *slash = 0;
00078 if (access(path, 0) == 0) {
00079 stat = DB_OK;
00080 }
00081 else if (make_parent_dir(path, mode) != DB_OK) {
00082 stat = DB_FAILED;
00083 }
00084 else if (make_dir(path, mode) == DB_OK) {
00085 stat = DB_OK;
00086 }
00087 else {
00088 stat = DB_FAILED;
00089 }
00090 *slash = '/';
00091
00092 return stat;
00093 }
00094
00095
00096 static char *rfind(char *string, char c)
00097 {
00098 char *found;
00099
00100 found = NULL;
00101 while (*string) {
00102 if (*string == c)
00103 found = string;
00104 string++;
00105 }
00106
00107 return found;
00108 }