Volksdata 1.0b7
RDF library
Loading...
Searching...
No Matches
core.c
Go to the documentation of this file.
1#include <errno.h>
2#include <dirent.h>
3#include <sys/stat.h>
4#include <unistd.h>
5#include <string.h>
6
7#include "lmdb.h"
8#include "volksdata/core.h"
9
10
11bool VOLK_env_is_init = false;
12
18char *warning_msg[] = {
19 "VOLK_NOACTION: No action or change of state occurred.",
20 "VOLK_NORESULT: No result.",
21 "VOLK_END: End of the loop reached.",
22 "VOLK_CONFLICT: A conflict prevented a resource from being updated.",
23};
24
33char *err_msg[] = {
34 "VOLK_ERROR: Runtime error.",
35 "VOLK_PARSE_ERR: Error parsing input.",
36 "VOLK_VALUE_ERR: Invalid input.",
37 "VOLK_TXN_ERR: MDB transaction error.",
38 "VOLK_DB_ERR: Database error.",
39 "VOLK_NOT_IMPL_ERR: Feature is not implemented.",
40 "VOLK_IO_ERR: Input/Output error.",
41 "VOLK_MEM_ERR: Memory error.",
42 "VOLK_CONFLICT_ERR: A resource conflict interrupted the operation.",
43 "VOLK_ENV_ERR: Environment not initialized. Did you call VOLK_init()?",
44};
45
46char *VOLK_root_path = __FILE__; // This is trimmed to root path on init.
47
48
50mkdir_p (const char *_path, mode_t mode)
51{
52 char *path = strdup (_path);
53 char *p;
54
55 // Trim any trailing slash(es).
56 for (p = path + strlen (path) - 1; p > path; p--)
57 if (*p == '/') *p = '\0';
58 else break;
59
60 errno = 0;
62
63 /* Iterate the string */
64 for (p = path + 1; *p; p++) {
65 if (*p == '/') {
66 /* Temporarily truncate */
67 *p = '\0';
68
69 if (mkdir (path, mode) != 0 && errno != EEXIST) goto finally;
70
71 *p = '/';
72 }
73 }
74 if (mkdir (path, mode) != 0) {
75 if (errno != EEXIST) rc = errno;
76 } else {
77 rc = VOLK_OK;
78 }
79
80finally:
81
82 LOG_TRACE("Path: %s", path);
83 LOG_TRACE("errno: %d", errno);
84 LOG_TRACE("rc: %d", rc);
85 free (path);
86
87 return rc;
88}
89
90
91char *
92strndup (const char *src, size_t max)
93{
94 size_t len = strlen (src);
95 if (len > max) len = max;
96
97 char *dup;
98 dup = malloc (len + 1);
99 if (dup) {
100 memcpy (dup, src, len);
101 dup[len] = '\0';
102 }
103
104 return dup;
105}
106
107
108char *
109strdup (const char *src)
110{
111 char *dup;
112 dup = malloc (strlen (src) + 1);
113 if (dup) strcpy(dup, src);
114
115 return dup;
116}
117
118
124VOLK_rc rm_r (const char *path)
125{
126 size_t path_len;
127 char *full_path;
128 DIR *dir;
129 struct stat stat_path, stat_entry;
130 struct dirent *entry;
131
132 // stat for the path
133 stat(path, &stat_path);
134
135 // if path does not exists or is not dir - exit with status -1
136 if (S_ISDIR(stat_path.st_mode) == 0) {
137 log_error ("%s: %s\n", "Is not directory", path);
138 return VOLK_IO_ERR;
139 }
140
141 // if not possible to read the directory for this user
142 if ((dir = opendir(path)) == NULL) {
143 log_error ("%s: %s\n", "Can`t open directory", path);
144 return VOLK_IO_ERR;
145 }
146
147 // the length of the path
148 path_len = strlen(path);
149
150 // iteration through entries in the directory
151 while ((entry = readdir(dir)) != NULL) {
152
153 // skip entries "." and ".."
154 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
155 continue;
156
157 // determine a full path of an entry
158 full_path = calloc(
159 path_len + 1 + strlen(entry->d_name) + 1, sizeof(char));
160 strcpy(full_path, path);
161 strcat(full_path, "/");
162 strcat(full_path, entry->d_name);
163
164 // stat for the entry
165 stat(full_path, &stat_entry);
166
167 // recursively remove a nested directory
168 if (S_ISDIR(stat_entry.st_mode) != 0) {
169 rm_r (full_path);
170 free (full_path);
171 continue;
172 }
173
174 // remove a file object
175 if (unlink(full_path) == 0)
176 LOG_DEBUG ("Removed a file:\t%s\n", full_path);
177 else
178 log_error ("Can't remove a file:\t%s\n", full_path);
179 free(full_path);
180 }
181
182 // remove the devastated directory and close the object of it
183 if (rmdir(path) == 0)
184 LOG_DEBUG ("Removed a directory:\t%s\n", path);
185 else
186 log_error ("Can't remove a directory:\t%s\n", path);
187
188 closedir(dir);
189
190 return VOLK_OK;
191}
192
193
194const char *
196{
197 if (rc >= VOLK_MIN_ERROR && rc <= VOLK_MAX_ERROR)
198 return err_msg[rc - VOLK_MIN_ERROR];
199
200 if (rc >= VOLK_MIN_WARNING && rc <= VOLK_MAX_WARNING)
201 return warning_msg[rc - VOLK_MIN_WARNING];
202
203 return mdb_strerror (rc);
204}
205
206
207/* Inline extern functions. */
208
209int utf8_encode (const uint32_t utf, unsigned char *out);
char * err_msg[]
error messages.
Definition core.c:33
char * VOLK_root_path
Definition core.c:46
bool VOLK_env_is_init
Whether the environment is initialized.
Definition core.c:11
char * warning_msg[]
Warning messages.
Definition core.c:18
#define VOLK_MAX_ERROR
Definition core.h:254
#define VOLK_MAX_WARNING
Definition core.h:263
#define LOG_TRACE(...)
Definition core.h:276
char * strdup(const char *src)
Replacement for GNU strdup.
Definition core.c:109
#define LOG_DEBUG(...)
Definition core.h:275
int utf8_encode(const uint32_t utf, unsigned char *out)
Encode a code point using UTF-8.
Definition core.h:445
VOLK_rc rm_r(const char *path)
Remove a directory recursively (POSIX compatible).
Definition core.c:124
#define VOLK_MIN_ERROR
Minimum error value.
Definition core.h:249
char * strndup(const char *src, size_t max)
Replacement for GNU strndup.
Definition core.c:92
#define VOLK_MIN_WARNING
First warning value.
Definition core.h:257
VOLK_rc mkdir_p(const char *_path, mode_t mode)
Make recursive directories.
Definition core.c:50
#define VOLK_OK
Generic success return code.
Definition core.h:83
#define VOLK_NOACTION
No action taken.
Definition core.h:93
int VOLK_rc
Definition core.h:79
#define VOLK_IO_ERR
I/O error.
Definition core.h:141
const char * VOLK_strerror(VOLK_rc rc)
Return an error message for a return code.
Definition core.c:195