Skip to content

Commit

Permalink
[Filestore] add SetNodeAttr command to filestore client (#1469)
Browse files Browse the repository at this point in the history
Issue: #1474
  • Loading branch information
antonmyagkov committed Jul 22, 2024
1 parent b70b452 commit f1c822b
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cloud/filestore/apps/client/lib/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ TCommandPtr NewExecuteActionCommand();
TCommandPtr NewCreateSessionCommand();
TCommandPtr NewResetSessionCommand();
TCommandPtr NewStatCommand();
TCommandPtr NewSetNodeAttrCommand();

////////////////////////////////////////////////////////////////////////////////

Expand All @@ -41,8 +42,10 @@ using TFactoryMap = TMap<TString, TFactoryFunc>;
static const TMap<TString, TFactoryFunc> Commands = {
{ "addclusternode", NewAddClusterNodeCommand },
{ "create", NewCreateCommand },
{ "createsession", NewCreateSessionCommand },
{ "describe", NewDescribeCommand },
{ "destroy", NewDestroyCommand },
{ "executeaction", NewExecuteActionCommand },
{ "kickendpoint", NewKickEndpointCommand },
{ "listclusternodes", NewListClusterNodesCommand },
{ "listendpoints", NewListEndpointsCommand },
Expand All @@ -52,16 +55,15 @@ static const TMap<TString, TFactoryFunc> Commands = {
{ "mount", NewMountCommand },
{ "read", NewReadCommand },
{ "removeclusternode", NewRemoveClusterNodeCommand },
{ "resetsession", NewResetSessionCommand },
{ "resize", NewResizeCommand },
{ "rm", NewRmCommand },
{ "setnodeattr", NewSetNodeAttrCommand },
{ "startendpoint", NewStartEndpointCommand },
{ "stat", NewStatCommand },
{ "stopendpoint", NewStopEndpointCommand },
{ "touch", NewTouchCommand },
{ "write", NewWriteCommand },
{ "executeaction", NewExecuteActionCommand },
{ "createsession", NewCreateSessionCommand },
{ "resetsession", NewResetSessionCommand },
{ "stat", NewStatCommand },
};

////////////////////////////////////////////////////////////////////////////////
Expand Down
131 changes: 131 additions & 0 deletions cloud/filestore/apps/client/lib/set_node_attr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "command.h"

#include <optional>

#include <cloud/filestore/public/api/protos/fs.pb.h>

#include <util/datetime/base.h>
#include <util/stream/file.h>
#include <util/system/sysstat.h>

namespace NCloud::NFileStore::NClient {

namespace {

////////////////////////////////////////////////////////////////////////////////

class TSetNodeAttrCommand final: public TFileStoreCommand
{
private:
ui32 NodeId = 0;
std::optional<ui32> ModeAttr;
std::optional<ui32> UidAttr;
std::optional<ui32> GidAttr;
std::optional<ui64> SizeAttr;
std::optional<ui64> ATimeAttr;
std::optional<ui64> MTimeAttr;
std::optional<ui64> CTimeAttr;

public:
TSetNodeAttrCommand()
{
Opts.AddLongOption("node-id")
.Required()
.StoreResult(&NodeId);

Opts.AddLongOption("mode")
.Optional()
.StoreResult(&ModeAttr);

Opts.AddLongOption("uid")
.Optional()
.StoreResult(&UidAttr);

Opts.AddLongOption("gid")
.Optional()
.StoreResult(&GidAttr);

Opts.AddLongOption("size")
.Optional()
.StoreResult(&SizeAttr);

Opts.AddLongOption("atime")
.Optional()
.StoreResult(&ATimeAttr);

Opts.AddLongOption("mtime")
.Optional()
.StoreResult(&MTimeAttr);

Opts.AddLongOption("ctime")
.Optional()
.StoreResult(&CTimeAttr);
}

bool Execute() override
{
auto sessionGuard = CreateSession();

auto request = CreateRequest<NProto::TSetNodeAttrRequest>();
request->SetNodeId(NodeId);

auto addFlag = [&request](NProto::TSetNodeAttrRequest::EFlags flag)
{
request->SetFlags(
request->GetFlags() | NCloud::NFileStore::ProtoFlag(flag));
};

if (ModeAttr)
{
addFlag(NProto::TSetNodeAttrRequest::F_SET_ATTR_MODE);
request->MutableUpdate()->SetMode(*ModeAttr);
}

if (UidAttr) {
addFlag(NProto::TSetNodeAttrRequest::F_SET_ATTR_UID);
request->MutableUpdate()->SetUid(*UidAttr);
}

if (GidAttr) {
addFlag(NProto::TSetNodeAttrRequest::F_SET_ATTR_GID);
request->MutableUpdate()->SetGid(*GidAttr);
}

if (SizeAttr) {
addFlag(NProto::TSetNodeAttrRequest::F_SET_ATTR_SIZE);
request->MutableUpdate()->SetSize(*SizeAttr);
}

if (ATimeAttr) {
addFlag(NProto::TSetNodeAttrRequest::F_SET_ATTR_ATIME);
request->MutableUpdate()->SetATime(*ATimeAttr);
}

if (MTimeAttr) {
addFlag(NProto::TSetNodeAttrRequest::F_SET_ATTR_MTIME);
request->MutableUpdate()->SetMTime(*MTimeAttr);
}

if (CTimeAttr) {
addFlag(NProto::TSetNodeAttrRequest::F_SET_ATTR_CTIME);
request->MutableUpdate()->SetCTime(*CTimeAttr);
}

auto response = WaitFor(
Client->SetNodeAttr(PrepareCallContext(), std::move(request)));

CheckResponse(response);
return true;
}
};

} // namespace

////////////////////////////////////////////////////////////////////////////////

TCommandPtr NewSetNodeAttrCommand()
{
return std::make_shared<TSetNodeAttrCommand>();
}

} // namespace NCloud::NFileStore::NClient
1 change: 1 addition & 0 deletions cloud/filestore/apps/client/lib/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SRCS(
reset_session.cpp
resize.cpp
rm.cpp
set_node_attr.cpp
start_endpoint.cpp
stat.cpp
stop_endpoint.cpp
Expand Down
73 changes: 73 additions & 0 deletions cloud/filestore/tests/client/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,76 @@ def test_write_ls_rm_ls():

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


def test_set_node_attr():
client, results_path = __init_test()
client.create("fs0", "test_cloud", "test_folder", BLOCK_SIZE, BLOCKS_COUNT)
client.mkdir("fs0", "/aaa")

out = client.stat("fs0", "/aaa")
stat = json.loads(out)
node_id = stat["Id"]
uid = 1
gid = 1
size = 123
mode = 221
atime = stat["ATime"] - 1
mtime = stat["MTime"] - 1
ctime = stat["CTime"] - 1

client.set_node_attr(
"fs0", node_id,
"--uid", uid,
"--gid", gid,
"--size", size,
"--mode", mode,
"--atime", atime,
"--mtime", mtime,
"--ctime", ctime)

out = client.stat("fs0", "/aaa")
stat = json.loads(out)

assert uid == stat["Uid"]
assert gid == stat["Gid"]
assert size == stat["Size"]
assert mode == stat["Mode"]
assert atime == stat["ATime"]
assert mtime == stat["MTime"]
assert ctime == stat["CTime"]


def test_partial_set_node_attr():
client, results_path = __init_test()
client.create("fs0", "test_cloud", "test_folder", BLOCK_SIZE, BLOCKS_COUNT)
client.mkdir("fs0", "/aaa")
client.touch("fs0", "/aaa/bbb")

out = client.stat("fs0", "/aaa/bbb")
stat = json.loads(out)
node_id = stat["Id"]
uid = 1
gid = 1

client.set_node_attr(
"fs0", node_id,
"--uid", uid,
"--gid", gid,
"--size", 123)

out = client.stat("fs0", "/aaa/bbb")
stat = json.loads(out)

assert uid == stat["Uid"]
assert gid == stat["Gid"]
gid = 2
client.set_node_attr(
"fs0", node_id,
"--gid", gid)
out = client.stat("fs0", "/aaa/bbb")
new_stat = json.loads(out)
assert gid == new_stat["Gid"]
assert stat["Uid"] == new_stat["Uid"]
assert stat["Size"] == new_stat["Size"]
assert stat["Mode"] == new_stat["Mode"]
10 changes: 10 additions & 0 deletions cloud/filestore/tests/python/lib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ def stat(self, fs, path):

return common.execute(cmd).stdout

def set_node_attr(self, fs, node_id, *argv):
list_args = [str(x) for x in argv]
cmd = [
self.__binary_path, "setnodeattr",
"--filesystem", fs,
"--node-id", str(node_id),
] + list_args + self.__cmd_opts()

return common.execute(cmd).stdout

def execute_action(self, action, request):
request_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
json.dump(request, request_file)
Expand Down

0 comments on commit f1c822b

Please sign in to comment.