00001 #include <sys/stat.h>
00002 #include <sys/types.h>
00003 #include <unistd.h>
00004 #include "dbmi.h"
00005 #include "dbstubs.h"
00006
00007 static char *rfind();
00008 static int make_parent_dir();
00009 static int make_dir();
00010
00017 db_driver_mkdir (path, mode, parentdirs)
00018 char *path;
00019 int mode;
00020 int parentdirs;
00021 {
00022 if (parentdirs)
00023 {
00024 if (make_parent_dir (path, mode) != DB_OK)
00025 return DB_FAILED;
00026 }
00027 return make_dir (path, mode);
00028 }
00029
00030
00031
00032 static int
00033 make_dir (path, mode)
00034 char *path;
00035 int mode;
00036 {
00037 if (db_isdir(path) == DB_OK)
00038 return DB_OK;
00039 if (mkdir (path, mode) == 0)
00040 return DB_OK;
00041 db_syserror(path);
00042 return DB_FAILED;
00043 }
00044
00045 static
00046 make_parent_dir(path, mode)
00047 char *path;
00048 int mode;
00049 {
00050 char *slash;
00051 int stat;
00052
00053 slash = rfind(path,'/');
00054 if (slash == NULL || slash == path)
00055 return DB_OK;
00056
00057 *slash = 0;
00058 if (access(path,0) == 0)
00059 {
00060 stat = DB_OK;
00061 }
00062 else if (make_parent_dir (path, mode) != DB_OK)
00063 {
00064 stat = DB_FAILED;
00065 }
00066 else if(make_dir (path, mode) == DB_OK)
00067 {
00068 stat = DB_OK;
00069 }
00070 else
00071 {
00072 stat = DB_FAILED;
00073 }
00074 *slash = '/';
00075 return stat;
00076 }
00077
00078 static
00079 char *rfind(string, c)
00080 char *string;
00081 char c;
00082 {
00083 char *found;
00084
00085 found = NULL;
00086 while (*string)
00087 {
00088 if (*string == c)
00089 found = string;
00090 string++;
00091 }
00092 return found;
00093 }