From 90402ed55bff8f881e53e5a8c6b486ab0c5adb2b Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 20 Oct 2026 10:41:41 +0300 Subject: [PATCH] Fix vitastor-cli ls --tree hiding layers whose parent is filtered out ls --tree built the tree starting from the roots of the selected layers, so a layer whose parent was left out by a glob, -p or -n was never reached and not printed at all: ls --tree testimg printed nothing when testimg had a parent. Show such a layer as a root; its parent is still shown in the PARENT column. --- src/cmd/cli_ls.cpp | 8 +++++++- tests/run_tests.sh | 1 + tests/test_ls_tree.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100755 tests/test_ls_tree.sh diff --git a/src/cmd/cli_ls.cpp b/src/cmd/cli_ls.cpp index affbf1e9..b75fdd88 100644 --- a/src/cmd/cli_ls.cpp +++ b/src/cmd/cli_ls.cpp @@ -250,11 +250,17 @@ resume_1: json11::Json::array to_tree(const json11::Json::array & array) { + std::set listed; + for (const auto & item: array) + { + listed.insert(item["inode_id"].uint64_value()); + } std::map children; for (const auto & item: array) { uint64_t parent_id = item["parent_inode_id"].uint64_value(); - children[parent_id].push_back(item); + // Layers with a filtered out parent are shown as roots + children[listed.find(parent_id) != listed.end() ? parent_id : 0].push_back(item); } json11::Json::array tree; std::function add_children = [&](uint64_t parent_id, const std::string & prefix) diff --git a/tests/run_tests.sh b/tests/run_tests.sh index d72fcf78..76a5c2ac 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -122,6 +122,7 @@ OLD=1 ./test_corrupt_all.sh ./test_reweight_half.sh ./test_snapshot_pool2.sh +./test_ls_tree.sh ./test_snapshot_read_bitmap.sh TEST_NAME=csum_32k_dmj OSD_ARGS="--data_csum_type crc32c --csum_block_size 32k --inmemory_metadata false --inmemory_journal false" OFFSET_ARGS=$OSD_ARGS ./test_heal.sh diff --git a/tests/test_ls_tree.sh b/tests/test_ls_tree.sh new file mode 100755 index 00000000..2e6a624a --- /dev/null +++ b/tests/test_ls_tree.sh @@ -0,0 +1,40 @@ +#!/bin/bash -ex + +# Test vitastor-cli ls --tree with filters which leave out parent layers + +. `dirname $0`/run_3osds.sh + +$VITASTOR_CLI create -s 128M testimg +$VITASTOR_CLI snap-create testimg@snap1 +$VITASTOR_CLI create --parent testimg@snap1 testclone + +$VITASTOR_CLI create-pool testpool2 -s 3 -n 4 --failure_domain osd +wait_pool_up 30 2 3 4 +$VITASTOR_CLI create --parent testimg@snap1 -p testpool2 testclone2 + +# First column of each row without the header +check_tree() +{ + local expected=$1 + shift + local rows=$($VITASTOR_CLI ls --tree "$@" | tail -n +2 | sed 's/ .*//' | tr '\n' ';') + if [[ "$rows" != "$expected" ]]; then + format_error "ls --tree $* shows '$rows', expected '$expected'" + fi +} + +$VITASTOR_CLI ls --tree + +check_tree 'testimg@snap1;├─ testclone;├─ testclone2;└─ testimg;' +check_tree 'testimg@snap1;└─ testimg;' 'testimg*' +# Layers whose parent is filtered out are shown as roots +check_tree 'testimg;' testimg +check_tree 'testclone;testimg;' testclone testimg +check_tree 'testclone;' -n 1 +check_tree 'testclone2;' -p testpool2 + +if ! $VITASTOR_CLI ls --tree testimg | grep -q ' testimg@snap1 *$'; then + format_error "ls --tree testimg should show testimg@snap1 as the parent" +fi + +format_green OK -- 2.47.3