1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <wait.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include "db.h"
#ifndef PATH_MAX
# define PATH_MAX 4096
#endif
#ifndef NAME_MAX
# define NAME_MAX 255
#endif
#define DIR_NAME_BUF_SIZE (NAME_MAX+1)
static const char *
find_chr_or_zero(const char *s, char ch)
{
while (*s && *s != ch)
s++;
return s;
}
static const char *
find_chr_or_zero_n(const char *s, char ch, int n)
{
int i = 0;
while (i < n && *s && *s != ch) {
s++;
i++;
}
return s;
}
static struct db_value_int
does_dir_exist(const char *path)
{
struct db_value_int res = {0};
struct stat st = {0};
int code;
res.code = dbe_ok;
res.data = 1;
code = stat(path, &st);
if (code != 0) {
if (errno == ENOENT)
res.data = 0;
else if (errno == EACCES)
res.code = dbe_access;
else
res.code = dbe_unknown;
} else {
res.data = ((st.st_mode & S_IFMT) == S_IFDIR) ? 1 : 0;
}
return res;
}
static enum db_error
make_dir_ifne(const char *path, int rights)
{
struct db_value_int val = {0};
int code;
val = does_dir_exist(path);
if (val.code != dbe_ok)
return val.code;
else if (val.data)
return dbe_ok;
code = mkdir(path, rights);
if (code != 0) {
if (errno == EACCES)
return dbe_access;
else if (errno == EEXIST)
return dbe_notdir;
}
return dbe_ok;
}
static enum db_error
do_make_dir_path(const char *path, int rights)
{
char buf[DIR_NAME_BUF_SIZE] = {0};
const char *p = find_chr_or_zero(path, '/');
unsigned sz = p - path + 1, code;
if (sz > sizeof(buf))
return dbe_inval;
strncpy(buf, path, sz);
buf[sz-1] = '\0';
code = make_dir_ifne(buf, rights);
if (code != dbe_ok)
return code;
if (*p) {
code = chdir(buf);
if (0 != code)
return dbe_access;
do_make_dir_path(p+1, rights);
code = chdir("..");
if (0 != code)
return dbe_access;
}
return dbe_ok;
}
/* mkdir -p */
enum db_error
make_dir_path(const char *path, int rights)
{
return do_make_dir_path(path, rights);
}
/* ==================================================================== */
#ifdef BTASKTD_DB_TEST
#include <stdio.h>
#define TEST_CODE(code) \
do { \
const char *s = NULL; \
switch (code) { \
case dbe_ok: s = "NO_ERROR"; break; \
case dbe_notdir: s = "NOT_DIR"; break; \
case dbe_inval: s = "INV_INPUT"; break; \
case dbe_access: s = "ACCESS"; break; \
default: s = "UNKNOWN"; \
} \
fprintf(stderr, "%s\n", s); \
} while (0)
int
main(void)
{
int i, pid;
const char *path = "test_db/super_test/mtest";
for (i = 0; i < 10; i++)
TEST_CODE(make_dir_path(path, 0777));
pid = fork();
if (pid == -1) {
fprintf(stderr, "can't fork\n");
return 1;
} else if (pid == 0) {
execlp("ls", "ls", "-laR", NULL);
_exit(128);
} else {
wait(NULL);
}
pid = fork();
if (pid == -1) {
fprintf(stderr, "can't fork\n");
return 1;
} else if (pid == 0) {
execlp("rm", "rm", "-rf", "test_db", NULL);
_exit(128);
} else {
wait(NULL);
}
return 0;
}
#endif
|