GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
d_mkdir.c
Go to the documentation of this file.
1 /*!
2  * \file db/dbmi_driver/d_mkdir.c
3  *
4  * \brief DBMI Library (driver) - creare directories
5  *
6  * (C) 1999-2008 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author Joel Jones (CERL/UIUC), Radim Blazek
13  */
14 
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 static char *rfind(char *string, char c);
23 static int make_parent_dir(char *path, int mode);
24 static int make_dir(const char *path, int mode);
25 
26 /*!
27  \brief Create db directory
28 
29  \param path full path
30  \param mode mode (unused, defaults to chmod 0777 on non-Windows systems)
31  \param parentdirs parent directories
32 
33  \return DB_OK on success
34  \return DB_FAILED on failure
35  */
36 int db_driver_mkdir(const char *path, int mode, int parentdirs)
37 {
38  if (parentdirs) {
39  char path2[GPATH_MAX];
40 
41  strcpy(path2, path);
42  if (make_parent_dir(path2, mode) != DB_OK)
43  return DB_FAILED;
44  }
45 
46  return make_dir(path, mode);
47 }
48 
49 /* make a directory if it doesn't exist */
50 /* this routine could be made more intelligent as to why it failed */
51 static int make_dir(const char *path, int mode UNUSED)
52 {
53  if (db_isdir(path) == DB_OK)
54  return DB_OK;
55 
56  if (G_mkdir(path) == 0)
57  return DB_OK;
58 
60 
61  return DB_FAILED;
62 }
63 
64 static int make_parent_dir(char *path, int mode)
65 {
66  char *slash;
67  int stat;
68 
69  slash = rfind(path, '/');
70  if (slash == NULL || slash == path)
71  return DB_OK; /* no parent dir to make. return ok */
72 
73  *slash = 0; /* add NULL to terminate parentdir string */
74  if (access(path, 0) == 0) { /* path exists, good enough */
75  stat = DB_OK;
76  }
77  else if (make_parent_dir(path, mode) != DB_OK) {
78  stat = DB_FAILED;
79  }
80  else if (make_dir(path, mode) == DB_OK) {
81  stat = DB_OK;
82  }
83  else {
84  stat = DB_FAILED;
85  }
86  *slash = '/'; /* put the slash back into the path */
87 
88  return stat;
89 }
90 
91 static char *rfind(char *string, char c)
92 {
93  char *found;
94 
95  found = NULL;
96  while (*string) {
97  if (*string == c)
98  found = string;
99  string++;
100  }
101 
102  return found;
103 }
#define NULL
Definition: ccmath.h:32
int db_driver_mkdir(const char *path, int mode, int parentdirs)
Create db directory.
Definition: d_mkdir.c:36
#define DB_FAILED
Definition: dbmi.h:72
#define DB_OK
Definition: dbmi.h:71
int db_isdir(const char *)
Test if path is a directory.
Definition: isdir.c:29
void db_syserror(const char *)
Report system error.
int G_mkdir(const char *)
Creates a new directory.
Definition: paths.c:27
#define GPATH_MAX
Definition: gis.h:194
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
#define strcpy
Definition: parson.c:62
Definition: path.h:15