From: Kevin Day Date: Sat, 26 Apr 2025 03:03:10 +0000 (-0500) Subject: Bugfix: Correct the fl_directory_do() top-level path execution order. X-Git-Tag: 0.7.1~12 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=b1dbec111fd05d359b6f7bfccc7981d64726965f;p=fll Bugfix: Correct the fl_directory_do() top-level path execution order. The before must happen before the directory recurse. The action must happen after the directory recurse. The after must happen after the action. This is already done in the recursive paths and it is now done in the top level path. Update the documentation comments to more accurately and better explain this. Apply the directory path resetting following recurse at the top level as well. --- diff --git a/level_1/fl_directory/c/directory.c b/level_1/fl_directory/c/directory.c index 6731e87..f487171 100644 --- a/level_1/fl_directory/c/directory.c +++ b/level_1/fl_directory/c/directory.c @@ -114,22 +114,6 @@ extern "C" { if (!path.used) return; } - if (recurse->depth < recurse->depth_max) { - ++recurse->depth; - - private_fl_directory_do_recurse(recurse); - - --recurse->depth; - - if (F_status_is_error(recurse->state.status)) return; - - if (recurse->state.status == F_done) { - recurse->state.status = F_okay; - - return; - } - } - const uint32_t flag_actions[] = { recurse->flag & f_directory_recurse_do_flag_before_d ? f_directory_recurse_do_flag_before_d : 0, f_directory_recurse_do_flag_action_d, @@ -148,6 +132,30 @@ extern "C" { } if (flag_actions[action]) { + if (flag_actions[action] & f_directory_recurse_do_flag_action_d) { + if (recurse->depth < recurse->depth_max) { + recurse->state.status = F_okay; + + ++recurse->depth; + + private_fl_directory_do_recurse(recurse); + + --recurse->depth; + + if (F_status_is_error(recurse->state.status)) return; + + if (recurse->state.status == F_done) { + recurse->state.status = F_okay; + + return; + } + + // Reset the path after operating on child directories. + recurse->path.used = path.used; + recurse->path.string[recurse->path.used] = 0; + } + } + recurse->state.status = F_okay; recurse->action(recurse, path, flag_actions[action] | recurse->flag | f_directory_recurse_do_flag_directory_d); diff --git a/level_1/fl_directory/c/directory.h b/level_1/fl_directory/c/directory.h index 32128a0..b10978a 100644 --- a/level_1/fl_directory/c/directory.h +++ b/level_1/fl_directory/c/directory.h @@ -102,24 +102,28 @@ extern "C" { * The handle() gives the caller more power to do things like report errors or even ignore errors. * When ignoring errors, be careful about modifications as some may put the project in an unusual state. * + * The before action happens before the directory recurse or if not recursing then before the action. + * The directory recurse happens before the action. + * The action happens before the after. + * * The action() and handle() should check if the recurse is NULL (and any other appropraite NULL checks). * * General behavior flow: * 1. Check recurse.action existence (if not _di_level_1_parameter_checking_), flag is f_directory_recurse_do_flag_none_d. * 2. Check path.used and allocation recurse.path, flag is f_directory_recurse_do_flag_path_d (even if handle() changes error bit, if path.used is 0, then the function always returns here). - * 3. If recurse.depth < recurse.depth_max, then recurse into directory contents. - * 3.1 Open directory stream, flag is f_directory_recurse_do_flag_list_d. - * 3.2 Read directory stream, flag is f_directory_recurse_do_flag_path_list_d ('.' and '..' are skipped). - * 3.3 Allocate sub-path onto recurse.path, flag is f_directory_recurse_do_flag_path_d. - * 3.4 Process before state on path, flag is (type code of file/directory) | f_directory_recurse_do_flag_before_d. - * 3.5 Process action state on path, flag is (type code of file/directory) | f_directory_recurse_do_flag_action_d. - * 3.5.1 If type is directory (flag has f_directory_recurse_do_flag_directory_d), then increase recurse.depth and recurse (repeat step (3) for this sub-directory). - * 3.6 Process after state on path, flag is (type code of file/directory) | f_directory_recurse_do_flag_after_d. - * 3.7 Close directory stream, flag is f_directory_recurse_do_flag_clean_d | f_directory_recurse_do_flag_path_list_d. - * 4. Process top-level directory. - * 4.1 Process before state on path, flag is f_directory_recurse_do_flag_before_d | f_directory_recurse_do_flag_directory_d | recurse.flag. - * 4.2 Process action state on path, flag is f_directory_recurse_do_flag_action_d | f_directory_recurse_do_flag_directory_d | recurse.flag. - * 4.3 Process after state on path, flag is f_directory_recurse_do_flag_after_d | f_directory_recurse_do_flag_directory_d | recurse.flag. + * 3. Process before state on path, flag is f_directory_recurse_do_flag_before_d | f_directory_recurse_do_flag_directory_d | recurse.flag. + * 4. If recurse.depth < recurse.depth_max, then recurse into directory contents. + * 4.1. Open directory stream, flag is f_directory_recurse_do_flag_list_d. + * 4.2. Read directory stream, flag is f_directory_recurse_do_flag_path_list_d ('.' and '..' are skipped). + * 4.3. Allocate sub-path onto recurse.path, flag is f_directory_recurse_do_flag_path_d. + * 4.4. Process before state on path, flag is (type code of file/directory) | f_directory_recurse_do_flag_before_d. + * 4.5. If type is directory (flag has f_directory_recurse_do_flag_directory_d), then increase recurse.depth and recurse (repeat step (4) for this sub-directory). + * 4.6. Process action state on path, flag is (type code of file/directory) | f_directory_recurse_do_flag_action_d. + * 4.7. Process after state on path, flag is (type code of file/directory) | f_directory_recurse_do_flag_after_d. + * 4.8. Close directory stream, flag is f_directory_recurse_do_flag_clean_d | f_directory_recurse_do_flag_path_list_d. + * 5. Process top-level directory. + * 5.1. Process action state on path, flag is f_directory_recurse_do_flag_action_d | f_directory_recurse_do_flag_directory_d | recurse.flag. + * 5.2. Process after state on path, flag is f_directory_recurse_do_flag_after_d | f_directory_recurse_do_flag_directory_d | recurse.flag. * * @param path * The directory file path.