If btrfs_search_slot() returns a value >= 0, then we can be sure that path->nodes[i] is not NULL for each i between 0 to tree height - 1. The function btrfs_next_leaf() also ensures any path->nodes[i] is not NULL as long as it returns 0.
Signed-off-by: Filipe David Borba Manana <fdman...@gmail.com> --- cmds-restore.c | 118 ++++++++++++++++++-------------------------------------- 1 file changed, 37 insertions(+), 81 deletions(-) diff --git a/cmds-restore.c b/cmds-restore.c index ed4815a..baa9cab 100644 --- a/cmds-restore.c +++ b/cmds-restore.c @@ -394,21 +394,6 @@ static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key, } leaf = path->nodes[0]; - while (!leaf) { - ret = btrfs_next_leaf(root, path); - if (ret < 0) { - fprintf(stderr, "Error getting next leaf %d\n", - ret); - btrfs_free_path(path); - return ret; - } else if (ret > 0) { - /* No more leaves to search */ - btrfs_free_path(path); - return 0; - } - leaf = path->nodes[0]; - } - while (1) { if (loops++ >= 1024) { ret = ask_to_continue(file); @@ -417,19 +402,17 @@ static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key, loops = 0; } if (path->slots[0] >= btrfs_header_nritems(leaf)) { - do { - ret = btrfs_next_leaf(root, path); - if (ret < 0) { - fprintf(stderr, "Error searching %d\n", ret); - btrfs_free_path(path); - return ret; - } else if (ret) { - /* No more leaves to search */ - btrfs_free_path(path); - goto set_size; - } - leaf = path->nodes[0]; - } while (!leaf); + ret = btrfs_next_leaf(root, path); + if (ret < 0) { + fprintf(stderr, "Error searching %d\n", ret); + btrfs_free_path(path); + return ret; + } else if (ret) { + /* No more leaves to search */ + btrfs_free_path(path); + goto set_size; + } + leaf = path->nodes[0]; continue; } btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); @@ -513,27 +496,6 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, } leaf = path->nodes[0]; - while (!leaf) { - if (verbose > 1) - printf("No leaf after search, looking for the next " - "leaf\n"); - ret = btrfs_next_leaf(root, path); - if (ret < 0) { - fprintf(stderr, "Error getting next leaf %d\n", - ret); - btrfs_free_path(path); - return ret; - } else if (ret > 0) { - /* No more leaves to search */ - if (verbose) - printf("Reached the end of the tree looking " - "for the directory\n"); - btrfs_free_path(path); - return 0; - } - leaf = path->nodes[0]; - } - while (leaf) { if (loops++ >= 1024) { printf("We have looped trying to restore files in %s " @@ -543,24 +505,22 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, } if (path->slots[0] >= btrfs_header_nritems(leaf)) { - do { - ret = btrfs_next_leaf(root, path); - if (ret < 0) { - fprintf(stderr, "Error searching %d\n", - ret); - btrfs_free_path(path); - return ret; - } else if (ret > 0) { - /* No more leaves to search */ - if (verbose) - printf("Reached the end of " - "the tree searching the" - " directory\n"); - btrfs_free_path(path); - return 0; - } - leaf = path->nodes[0]; - } while (!leaf); + ret = btrfs_next_leaf(root, path); + if (ret < 0) { + fprintf(stderr, "Error searching %d\n", + ret); + btrfs_free_path(path); + return ret; + } else if (ret > 0) { + /* No more leaves to search */ + if (verbose) + printf("Reached the end of " + "the tree searching the" + " directory\n"); + btrfs_free_path(path); + return 0; + } + leaf = path->nodes[0]; continue; } btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); @@ -884,20 +844,16 @@ again: ret = 0; goto out; } - do { - ret = btrfs_next_leaf(root, path); - if (ret < 0) { - fprintf(stderr, "Error getting next leaf %d\n", - ret); - goto out; - } else if (ret > 0) { - fprintf(stderr, "No more leaves\n"); - goto out; - } - } while (!path->nodes[0]); - if (path->nodes[0]) - goto again; - printf("Couldn't find a dir index item\n"); + ret = btrfs_next_leaf(root, path); + if (ret < 0) { + fprintf(stderr, "Error getting next leaf %d\n", + ret); + goto out; + } else if (ret > 0) { + fprintf(stderr, "No more leaves\n"); + goto out; + } + goto again; out: btrfs_free_path(path); return ret; -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html