From f66a812055c38d5614afd4b93a0e7eb04afc3a8e Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Mon, 10 Jun 2024 19:05:21 -0500 Subject: [PATCH] Bugfix: The fl_directory_create() needs to also handle F_file_found_not. Creating an entire directory tree is not working as expected when creating non-existent directories that are two levels or greater deep. For example take "a/b/c", if "a" exists but neither "a/b" nor "a/b/c" then the create fails. For example take "a/b", if "a exists but not "a/b" then the create succeeds (or appears to because I never noticed the bug before). The ENOENT (aka: F_file_found_not) is sometimes returned rather than ENOTDIR (aka: F_false) from f_directory_exists(). Process the ENOENT F_file_found_not. I noticed some problems in the logic of the fl_directory_create() function as well. The memcpy() needs to start from the same offset as the source copy offset. Otherwise, the copy is overwriting the string. Make sure to place the NULL at the "at_path" rather at "at_path - at_tree". The initial assignment of "tree.used" is not necessary. --- level_1/fl_directory/c/directory.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/level_1/fl_directory/c/directory.c b/level_1/fl_directory/c/directory.c index f1646a2..23cb7b2 100644 --- a/level_1/fl_directory/c/directory.c +++ b/level_1/fl_directory/c/directory.c @@ -26,19 +26,19 @@ extern "C" { f_char_t tree_string[path.used + 1]; tree.string = tree_string; - tree.used = path.used; tree_string[path.used] = 0; for (; at_path < path.used; ++at_path) { if (at_path && path.string[at_path] == f_path_separator_s.string[0]) { - memcpy(tree.string, path.string + at_tree, sizeof(f_char_t) * (at_path - at_tree)); - tree.string[at_path - at_tree] = 0; + memcpy(tree.string + at_tree, path.string + at_tree, sizeof(f_char_t) * (at_path - at_tree)); + tree.string[at_path] = 0; + tree.used = at_path; status = f_directory_exists(tree); if (F_status_is_error(status)) return status; - if (status == F_false) { + if (status == F_false || status == F_file_found_not) { status = f_directory_create(tree, mode); if (F_status_is_error(status)) return status; } -- 1.8.3.1