GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
d_mkdir.c
Go to the documentation of this file.
1 
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <grass/dbmi.h>
20 #include "dbstubs.h"
21 
22 
23 static char *rfind(char *string, char c);
24 static int make_parent_dir(char *path, int mode);
25 static int make_dir(const char *path, int mode);
26 
27 
38 int db_driver_mkdir(const char *path, int mode, int parentdirs)
39 {
40  if (parentdirs) {
41  char path2[GPATH_MAX];
42 
43  strcpy(path2, path);
44  if (make_parent_dir(path2, mode) != DB_OK)
45  return DB_FAILED;
46  }
47 
48  return make_dir(path, mode);
49 }
50 
51 
52 /* make a directory if it doesn't exist */
53 /* this routine could be made more intelligent as to why it failed */
54 static int make_dir(const char *path, int mode)
55 {
56  if (db_isdir(path) == DB_OK)
57  return DB_OK;
58 
59  if (G_mkdir(path) == 0)
60  return DB_OK;
61 
62  db_syserror(path);
63 
64  return DB_FAILED;
65 }
66 
67 
68 static int make_parent_dir(char *path, int mode)
69 {
70  char *slash;
71  int stat;
72 
73  slash = rfind(path, '/');
74  if (slash == NULL || slash == path)
75  return DB_OK; /* no parent dir to make. return ok */
76 
77  *slash = 0; /* add NULL to terminate parentdir string */
78  if (access(path, 0) == 0) { /* path exists, good enough */
79  stat = DB_OK;
80  }
81  else if (make_parent_dir(path, mode) != DB_OK) {
82  stat = DB_FAILED;
83  }
84  else if (make_dir(path, mode) == DB_OK) {
85  stat = DB_OK;
86  }
87  else {
88  stat = DB_FAILED;
89  }
90  *slash = '/'; /* put the slash back into the path */
91 
92  return stat;
93 }
94 
95 
96 static char *rfind(char *string, char c)
97 {
98  char *found;
99 
100  found = NULL;
101  while (*string) {
102  if (*string == c)
103  found = string;
104  string++;
105  }
106 
107  return found;
108 }
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:17
int stat
Definition: g3dcolor.c:369
void db_syserror(const char *s)
return NULL
Definition: dbfopen.c:1394
int db_driver_mkdir(const char *path, int mode, int parentdirs)
Create db directory.
Definition: d_mkdir.c:38
int db_isdir(const char *path)
Definition: isdir.c:12
tuple mode
Definition: tools.py:1481