aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNRK <nrk@disroot.org>2022-06-22 05:05:03 +0200
committerNRK <nrk@disroot.org>2022-06-22 05:05:03 +0200
commit9812d601c16fb243aac788abfeb9ea01a7870466 (patch)
tree815c5237a5790304dac29a7978932f834a3ffdb7
parenta67665a1c1569cdc16366cf635f76eb00c96048a (diff)
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. Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/322 Reviewed-by: Berke Kocaoğlu <berke.kocaoglu@metu.edu.tr> Reviewed-by: TAAPArthur <taaparthur@noreply.codeberg.org>
-rw-r--r--thumbs.c4
-rw-r--r--util.c14
2 files changed, 10 insertions, 8 deletions
diff --git a/thumbs.c b/thumbs.c
index 8ca0c58..d6b97ea 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -92,10 +92,8 @@ static void tns_cache_write(Imlib_Image im, const char *filepath, bool force)
{
if ((dirend = strrchr(cfile, '/')) != NULL) {
*dirend = '\0';
- if (r_mkdir(cfile) == -1) {
- error(0, errno, "%s", cfile);
+ if (r_mkdir(cfile) < 0)
goto end;
- }
*dirend = '/';
}
imlib_context_set_image(im);
diff --git a/util.c b/util.c
index d580839..e9c899b 100644
--- a/util.c
+++ b/util.c
@@ -193,10 +193,11 @@ char* r_readdir(r_dir_t *rdir, bool skip_dotfiles)
int r_mkdir(char *path)
{
+ int rc = 0;
char c, *s = path;
struct stat st;
- while (*s != '\0') {
+ while (*s != '\0' && rc == 0) {
if (*s == '/') {
s++;
continue;
@@ -204,12 +205,15 @@ int r_mkdir(char *path)
for (; *s != '\0' && *s != '/'; s++);
c = *s;
*s = '\0';
- if (mkdir(path, 0755) == -1)
- if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode))
- return -1;
+ if (mkdir(path, 0755) == -1) {
+ if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
+ error(0, errno, "%s", path);
+ rc = -1;
+ }
+ }
*s = c;
}
- return 0;
+ return rc;
}
void construct_argv(char **argv, unsigned int len, ...)