# HG changeset patch # User Paul Boddie # Date 1645032061 -3600 # Node ID 88d2d7364bea729d62bb0337b6a33a63c2c42c9f # Parent 5c301fd604182260c4ddc67bcee98b1f823863ba Simplified directory listing traversal, eliminating superfluous state structure. diff -r 5c301fd60418 -r 88d2d7364bea libe2access/lib/src/image.c --- a/libe2access/lib/src/image.c Wed Feb 16 17:55:47 2022 +0100 +++ b/libe2access/lib/src/image.c Wed Feb 16 18:21:01 2022 +0100 @@ -60,28 +60,21 @@ return 0; } -/* Common parent entry state. */ - -struct _image_parent_entry_state -{ - ext2_ino_t ino; - int nonempty; -}; - /* Get entry details for a directory to be removed. */ static int _image_get_parent_entry(struct ext2_dir_entry *dir_entry, int offset, int blocksize, char *buf, void *priv_data) { - struct _image_parent_entry_state *state = (struct _image_parent_entry_state *) priv_data; + ext2_ino_t *ino = (ext2_ino_t *) priv_data; (void) offset; (void) blocksize; (void) buf; if (!strcmp(dir_entry->name, "..")) - state->ino = dir_entry->inode; - else if (strcmp(dir_entry->name, ".")) - state->nonempty = 1; + { + *ino = dir_entry->inode; + return DIRENT_ABORT; + } return 0; } @@ -92,13 +85,13 @@ int offset, int blocksize, char *buf, void *priv_data) { - struct _image_parent_entry_state *state = (struct _image_parent_entry_state *) priv_data; + int *nonempty = (int *) priv_data; (void) offset; (void) blocksize; (void) buf; if (strcmp(dir_entry->name, ".") && strcmp(dir_entry->name, "..")) { - state->nonempty = 1; + *nonempty = 1; return DIRENT_ABORT; } @@ -111,13 +104,13 @@ int offset, int blocksize, char *buf, void *priv_data) { - struct _image_parent_entry_state *state = (struct _image_parent_entry_state *) priv_data; + ext2_ino_t *ino = (ext2_ino_t *) priv_data; (void) offset; (void) blocksize; (void) buf; if (!strcmp(dir_entry->name, "..")) { - dir_entry->inode = state->ino; + dir_entry->inode = *ino; return DIRENT_CHANGED | DIRENT_ABORT; } else @@ -178,16 +171,16 @@ { /* Initialise the directory listing processing state. */ - struct _image_parent_entry_state state = {.nonempty = 0}; + int nonempty = 0; errcode_t retval = ext2fs_dir_iterate(fs, ino, 0, NULL, - _image_test_directory, &state); + _image_test_directory, &nonempty); if (retval) return retval; /* NOTE: Need a proper error here. */ - return state.nonempty; + return nonempty; } /* Test for an empty directory using its path. */ @@ -204,18 +197,13 @@ { /* Initialise the directory listing processing state. */ - struct _image_parent_entry_state state = {.ino = 0}; - - /* A directory needs to be inspected for files and for the .. entry - to be queried to obtain the parent directory. */ + *ino_parent = 0; - errcode_t retval = ext2fs_dir_iterate(fs, ino, 0, NULL, - _image_get_parent_entry, &state); + /* A directory needs to be inspected for the .. entry in order to obtain the + parent directory. */ - if (!retval) - *ino_parent = state.ino; - - return retval; + return ext2fs_dir_iterate(fs, ino, 0, NULL, _image_get_parent_entry, + ino_parent); } /* Return the appropriate ext2 file type value for the given mode value. */ @@ -571,11 +559,6 @@ errcode_t retval; struct ext2_inode source_inode, source_parent_inode, target_parent_inode; - /* Initialise the directory listing processing state to refer to the target - parent. */ - - struct _image_parent_entry_state state = {.ino = target_parent}; - /* NOTE: Should check for space. */ /* Obtain the source object. */ @@ -613,7 +596,7 @@ parent. */ retval = ext2fs_dir_iterate(fs, source, 0, NULL, - _image_update_parent_entry, &state); + _image_update_parent_entry, &target_parent); /* Update the link count for the source. */