fix leaks in add_dir_on_disk(), and ensure all parents get created
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
diff --git a/lib/worktree.c b/lib/worktree.c
index 62fe285..b0d9794 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -520,11 +520,13 @@ make_parent_dirs(const char *abspath)
err = make_parent_dirs(parent);
if (err)
return err;
+ if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
+ return got_error_from_errno();
} else
- return got_error_from_errno();
+ err = got_error_from_errno();
}
- return NULL;
+ return err;
}
static const struct got_error *
@@ -548,22 +550,19 @@ add_dir_on_disk(struct got_worktree *worktree, const char *path)
if (!S_ISDIR(sb.st_mode)) {
/* TODO directory is obstructed; do something */
- return got_error(GOT_ERR_FILE_OBSTRUCTED);
+ err = got_error(GOT_ERR_FILE_OBSTRUCTED);
+ goto done;
}
return NULL;
- }
-
- if (errno == ENOENT) {
+ } else if (errno == ENOENT) {
err = make_parent_dirs(abspath);
if (err)
- return err;
- if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == 0)
- return NULL;
- } else {
+ goto done;
+ if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
+ err = got_error_from_errno();
+ } else
err = got_error_from_errno();
- goto done;
- }
}
done: