Krolyxon
Commit a7d39b0

Branches

Refs →

nsxiva7d39b0

Simplified r_mkdir()

Author
Bert Münnich
Date
2015-10-28 23:21 +0100
Parent
d3a70a2
Commit
a7d39b0ab8fabb4f6417bb69a4e70d9da598d1aa

+18 −31

3 files changed

  1. M thumbs.c +2 −1
  2. M util.c +15 −29
  3. M util.h +1 −1
M thumbs.c View
@@ -92,7 +92,8 @@ void tns_cache_write(Imlib_Image im, const char *filepath, bool force)
9292 {
9393 if ((dirend = strrchr(cfile, '/')) != NULL) {
9494 *dirend = '\0';
95− err = r_mkdir(cfile);
95+ if ((err = r_mkdir(cfile)) == -1)
96+ error(0, errno, "%s", cfile);
9697 *dirend = '/';
9798 }
9899 if (err == 0) {
M util.c View
@@ -181,37 +181,23 @@ char* r_readdir(r_dir_t *rdir)
181181 return NULL;
182182 }
183183
184−int r_mkdir(const char *path)
184+int r_mkdir(char *path)
185185 {
186− char *dir, *d;
187− struct stat stats;
188− int err = 0;
186+ char c, *s = path;
187+ struct stat st;
189188
190− if (*path == '\0')
191− return -1;
192−
193− if (stat(path, &stats) == 0)
194− return S_ISDIR(stats.st_mode) ? 0 : -1;
195−
196− d = dir = (char*) emalloc(strlen(path) + 1);
197− strcpy(dir, path);
198−
199− while (d != NULL && err == 0) {
200− d = strchr(d + 1, '/');
201− if (d != NULL)
202− *d = '\0';
203− if (access(dir, F_OK) < 0 && errno == ENOENT) {
204− if (mkdir(dir, 0755) < 0) {
205− error(0, errno, "%s", dir);
206− err = -1;
207− }
208− } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) {
209− err = -1;
189+ while (*s != '\0') {
190+ if (*s == '/') {
191+ s++;
192+ continue;
210193 }
211− if (d != NULL)
212− *d = '/';
194+ for (; *s != '\0' && *s != '/'; s++);
195+ c = *s;
196+ *s = '\0';
197+ if (mkdir(path, 0755) == -1)
198+ if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode))
199+ return -1;
200+ *s = c;
213201 }
214− free(dir);
215−
216− return err;
202+ return 0;
217203 }
M util.h View
@@ -74,6 +74,6 @@ void size_readable(float*, const char**);
7474 int r_opendir(r_dir_t*, const char*);
7575 int r_closedir(r_dir_t*);
7676 char* r_readdir(r_dir_t*);
77−int r_mkdir(const char *);
77+int r_mkdir(char*);
7878
7979 #endif /* UTIL_H */