GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 1999-2008 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Joel Jones (CERL/UIUC), Radim Blazek
10 */
11
12#include <string.h>
13#include <unistd.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16#include <grass/dbmi.h>
17#include "dbstubs.h"
18
19static char *rfind(char *string, char c);
20static int make_parent_dir(char *path, int mode);
21static int make_dir(const char *path, int mode);
22
23/*!
24 \brief Create db directory
25
26 \param path full path
27 \param mode mode (unused, defaults to chmod 0777 on non-Windows systems)
28 \param parentdirs parent directories
29
30 \return DB_OK on success
31 \return DB_FAILED on failure
32 */
33int db_driver_mkdir(const char *path, int mode, int parentdirs)
34{
35 if (parentdirs) {
36 char path2[GPATH_MAX];
37
38 strcpy(path2, path);
39 if (make_parent_dir(path2, mode) != DB_OK)
40 return DB_FAILED;
41 }
42
43 return make_dir(path, mode);
44}
45
46/* make a directory if it doesn't exist */
47/* this routine could be made more intelligent as to why it failed */
48static int make_dir(const char *path, int mode G_UNUSED)
49{
50 if (db_isdir(path) == DB_OK)
51 return DB_OK;
52
53 if (G_mkdir(path) == 0)
54 return DB_OK;
55
57
58 return DB_FAILED;
59}
60
61static int make_parent_dir(char *path, int mode)
62{
63 char *slash;
64 int stat;
65
66 slash = rfind(path, '/');
67 if (slash == NULL || slash == path)
68 return DB_OK; /* no parent dir to make. return ok */
69
70 *slash = 0; /* add NULL to terminate parentdir string */
71 if (access(path, 0) == 0) { /* path exists, good enough */
72 stat = DB_OK;
73 }
74 else if (make_parent_dir(path, mode) != DB_OK) {
76 }
77 else if (make_dir(path, mode) == DB_OK) {
78 stat = DB_OK;
79 }
80 else {
82 }
83 *slash = '/'; /* put the slash back into the path */
84
85 return stat;
86}
87
88static char *rfind(char *string, char c)
89{
90 char *found;
91
92 found = NULL;
93 while (*string) {
94 if (*string == c)
95 found = string;
96 string++;
97 }
98
99 return found;
100}
#define NULL
Definition ccmath.h:32
int db_driver_mkdir(const char *path, int mode, int parentdirs)
Create db directory.
Definition d_mkdir.c:33
Main header of GRASS DataBase Management Interface.
#define DB_FAILED
Definition dbmi.h:70
#define DB_OK
Definition dbmi.h:69
int db_isdir(const char *)
Test if path is a directory.
Definition isdir.c:27
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:196
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
#define strcpy
Definition parson.c:66
Definition path.h:15
#define access
Definition unistd.h:7