33
33
retvalue dirs_create(const char *dirname) {
36
ret = mkdir(dirname,0775);
39
printf("Created directory \"%s\"\n",dirname);
36
ret = mkdir(dirname, 0775);
39
printf("Created directory \"%s\"\n", dirname);
41
} else if( ret < 0 && ( e = errno ) != EEXIST ) {
41
} else if (ret < 0 && (e = errno) != EEXIST) {
42
42
fprintf(stderr, "Error %d creating directory \"%s\": %s\n",
43
43
e, dirname, strerror(e));
56
for( p = filename+1, i = 1 ; *p != '\0' ; p++,i++) {
58
h = strndup(filename,i);
56
for (p = filename+1, i = 1 ; *p != '\0' ; p++, i++) {
58
h = strndup(filename, i);
59
61
r = dirs_create(h);
60
if( RET_WAS_ERROR(r) ) {
62
if (RET_WAS_ERROR(r)) {
70
72
/* create dirname and any '/'-separated part of it */
71
73
retvalue dirs_make_recursive(const char *directory) {
75
77
return RET_ERROR_INTERRUPTED;
77
79
r = dirs_make_parent(directory);
78
80
result = dirs_create(directory);
81
RET_UPDATE(result, r);
93
95
return RET_ERROR_INTERRUPTED;
95
while( len > 0 && directory[len-1] == '/' )
97
while (len > 0 && directory[len-1] == '/')
98
100
this = strndup(directory, len);
101
if (FAILEDTOALLOC(this))
100
102
return RET_ERROR_OOM;
101
103
ret = mkdir(this, 0777);
105
107
printf("Created directory \"%s\"\n", this);
106
} else if( e == EEXIST ) {
108
} else if (e == EEXIST) {
109
/* normaly ENOENT should be the only problem, but check the others
110
* to be nice to annoying filesystems */
111
} else if( e != ENOENT && e != EACCES && e != EPERM ) {
112
fprintf(stderr, "Cannot create directory \"%s\": %s(%d)\n",
111
/* normaly ENOENT should be the only problem,
112
* but check the others to be nice to annoying filesystems */
113
} else if (e != ENOENT && e != EACCES && e != EPERM) {
115
"Cannot create directory \"%s\": %s(%d)\n",
113
116
this, strerror(e), e);
115
118
return RET_ERRNO(e);
119
while( len > 0 && directory[len-1] != '/' )
122
while (len > 0 && directory[len-1] != '/')
121
while( len > 0 && directory[len-1] == '/' )
124
while (len > 0 && directory[len-1] == '/')
125
while( directory[len] == '/' )
128
while (directory[len] == '/')
127
while( directory[len] != '\0' ) {
128
while( directory[len] != '\0' && directory[len] != '/' )
130
while (directory[len] != '\0') {
131
while (directory[len] != '\0' && directory[len] != '/')
130
133
this = strndup(directory, len);
134
if (FAILEDTOALLOC(this))
132
135
return RET_ERROR_OOM;
133
136
r = dirs_create(this);
135
if( RET_WAS_ERROR(r) )
138
if (RET_WAS_ERROR(r))
137
140
// TODO: if we get RET_NOTHING here, reduce depth?
139
while( directory[len] == '/' )
142
while (directory[len] == '/')
142
145
assert(check == 0);
152
while( len > 0 && directory[len-1] == '/' )
155
while (len > 0 && directory[len-1] == '/')
154
while( created > 0 && len > 0 ) {
157
while (created > 0 && len > 0) {
155
158
this = strndup(directory, len);
159
if (FAILEDTOALLOC(this))
158
161
ret = rmdir(this);
161
printf("Removed empty directory \"%s\"\n", this);
165
"Removed empty directory \"%s\"\n",
164
if( e != ENOTEMPTY ) {
165
fprintf(stderr, "Error removing directory \"%s\": %s(%d)\n",
169
if (e != ENOTEMPTY) {
171
"Error removing directory \"%s\": %s(%d)\n",
166
172
this, strerror(e), e);
173
while( len > 0 && directory[len-1] != '/' )
179
while (len > 0 && directory[len-1] != '/')
175
while( len > 0 && directory[len-1] == '/' )
181
while (len > 0 && directory[len-1] == '/')
181
retvalue dirs_getdirectory(const char *filename,char **directory) {
187
retvalue dirs_getdirectory(const char *filename, char **directory) {
184
assert( filename != NULL && *filename != '\0' );
190
assert (filename != NULL && *filename != '\0');
186
192
len = strlen(filename);
187
while( len > 1 && filename[len-1] == '/' ) {
190
while( len > 0 && filename[len-1] != '/' ) {
193
while (len > 1 && filename[len-1] == '/') {
196
while (len > 0 && filename[len-1] != '/') {
194
200
*directory = strdup(".");
197
203
*directory = strdup("/");
199
*directory = strndup(filename,len-1);
205
*directory = strndup(filename, len-1);
201
if( *directory == NULL )
207
if (FAILEDTOALLOC(*directory))
202
208
return RET_ERROR_OOM;
207
213
const char *dirs_basename(const char *filename) {
210
bn = strrchr(filename,'/');
216
bn = strrchr(filename, '/');
213
219
// not really suited for the basename of directories,
214
220
// things like /bla/blub/ will give emtpy string...