nsxiv9812d60
r_mkdir: don't truncate the path on error (#322)
currently, in case of error, r_mkdir will leave the path at a truncated state. a7d39b0ab8 is the commit that introduced this change, and in it the error printing is moved from r_mkdir to the caller, which makes me think it was probably intentional. make it so that the function itself prints the error/warning message and returns the path back to the caller unharmed.
+10 −8
2 files changed
| @@ -92,10 +92,8 @@ static void tns_cache_write(Imlib_Image im, const char *filepath, bool force) | |||
| 92 | 92 | { | |
| 93 | 93 | if ((dirend = strrchr(cfile, '/')) != NULL) { | |
| 94 | 94 | *dirend = '\0'; | |
| 95 | − | if (r_mkdir(cfile) == -1) { | |
| 96 | − | error(0, errno, "%s", cfile); | |
| 95 | + | if (r_mkdir(cfile) < 0) | |
| 97 | 96 | goto end; | |
| 98 | − | } | |
| 99 | 97 | *dirend = '/'; | |
| 100 | 98 | } | |
| 101 | 99 | imlib_context_set_image(im); | |
| @@ -193,10 +193,11 @@ char* r_readdir(r_dir_t *rdir, bool skip_dotfiles) | |||
| 193 | 193 | ||
| 194 | 194 | int r_mkdir(char *path) | |
| 195 | 195 | { | |
| 196 | + | int rc = 0; | |
| 196 | 197 | char c, *s = path; | |
| 197 | 198 | struct stat st; | |
| 198 | 199 | ||
| 199 | − | while (*s != '\0') { | |
| 200 | + | while (*s != '\0' && rc == 0) { | |
| 200 | 201 | if (*s == '/') { | |
| 201 | 202 | s++; | |
| 202 | 203 | continue; | |
| @@ -204,12 +205,15 @@ int r_mkdir(char *path) | |||
| 204 | 205 | for (; *s != '\0' && *s != '/'; s++); | |
| 205 | 206 | c = *s; | |
| 206 | 207 | *s = '\0'; | |
| 207 | − | if (mkdir(path, 0755) == -1) | |
| 208 | − | if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) | |
| 209 | − | return -1; | |
| 208 | + | if (mkdir(path, 0755) == -1) { | |
| 209 | + | if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { | |
| 210 | + | error(0, errno, "%s", path); | |
| 211 | + | rc = -1; | |
| 212 | + | } | |
| 213 | + | } | |
| 210 | 214 | *s = c; | |
| 211 | 215 | } | |
| 212 | − | return 0; | |
| 216 | + | return rc; | |
| 213 | 217 | } | |
| 214 | 218 | ||
| 215 | 219 | void construct_argv(char **argv, unsigned int len, ...) | |