Skip to content

Commit

Permalink
[Filestore]: support remove by parentId and name in filestore-client
Browse files Browse the repository at this point in the history
  • Loading branch information
debnatkh committed Jan 16, 2025
1 parent 10c250f commit 2dfcb4e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cloud/filestore/apps/client/lib/rm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TRmCommand final
private:
TString Path;
bool RemoveDir = false;
ui64 NodeId = 0;

public:
TRmCommand()
Expand All @@ -25,6 +26,11 @@ class TRmCommand final
.RequiredArgument("PATH")
.StoreResult(&Path);

Opts.AddLongOption("node")
.DefaultValue(0)
.RequiredArgument("ID")
.StoreResult(&NodeId);

Opts.AddLongOption('r', "recursive")
.NoArgument()
.SetFlag(&RemoveDir);
Expand All @@ -35,14 +41,20 @@ class TRmCommand final
auto sessionGuard = CreateSession();
auto& session = sessionGuard.AccessSession();

const auto resolved = ResolvePath(session, Path, false);
// If nodeId is set, treat it as a nodeId and path as a name. Otherwise,
// resolve path to get nodeId and name.
if (!NodeId) {
const auto resolved = ResolvePath(session, Path, false);
Y_ENSURE(resolved.size() >= 2, "can't rm root node");

Y_ENSURE(resolved.size() >= 2, "can't rm root node");
NodeId = resolved[resolved.size() - 2].Node.GetId();
Path = ToString(resolved.back().Name);
}

auto request = CreateRequest<NProto::TUnlinkNodeRequest>();

request->SetNodeId(resolved[resolved.size() - 2].Node.GetId());
request->SetName(ToString(resolved.back().Name));
request->SetNodeId(NodeId);
request->SetName(Path);
request->SetUnlinkDirectory(RemoveDir);

auto response = WaitFor(session.UnlinkNode(
Expand Down
3 changes: 3 additions & 0 deletions cloud/filestore/tests/client/canondata/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"test.test_resize": {
"uri": "file://test.test_resize/results.txt"
},
"test.test_rm": {
"uri": "file://test.test_rm/results.txt"
},
"test.test_stat": {
"uri": "file://test.test_stat/results.txt"
},
Expand Down
32 changes: 32 additions & 0 deletions cloud/filestore/tests/client/canondata/test.test_rm/results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"Type": 1,
"Links": 1,
"Name": "c.txt",
"Mode": 420,
"Id": 4
},
{
"Type": 1,
"Links": 1,
"Name": "d.txt",
"Mode": 420,
"Id": 5
}
][
{
"Type": 1,
"Links": 1,
"Name": "d.txt",
"Mode": 420,
"Id": 5
}
][][
{
"Type": 2,
"Links": 1,
"Name": "b",
"Mode": 511,
"Id": 3
}
][]
32 changes: 32 additions & 0 deletions cloud/filestore/tests/client/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,38 @@ def test_diff():
return ret


def test_rm():
client, results_path = __init_test()
client.create("fs", "test_cloud", "test_folder", BLOCK_SIZE, BLOCKS_COUNT)

client.mkdir("fs", "/a")
client.mkdir("fs", "/a/b")
client.touch("fs", "/a/b/c.txt")
client.touch("fs", "/a/b/d.txt")

# remove by path
out = __exec_ls(client, "fs", "/a/b")
out += client.rm("fs", "/a/b/c.txt")

# remove by parentId + name
out += __exec_ls(client, "fs", "/a/b")
node_id = json.loads(client.stat("fs", "/a/b"))["Id"]
out += client.rm("fs", "d.txt", "--node", str(node_id))
out += __exec_ls(client, "fs", "/a/b")

# remove a directory
out += __exec_ls(client, "fs", "/a")
out += client.rm("fs", "/a/b", "-r")
out += __exec_ls(client, "fs", "/a")

client.destroy("fs")
with open(results_path, "wb") as results_file:
results_file.write(out)

ret = common.canonical_file(results_path, local=True)
return ret


def test_write_ls_rm_ls():
client, results_path = __init_test()

Expand Down

0 comments on commit 2dfcb4e

Please sign in to comment.