From a2f089df4de42aacea73f94f188f61547d6c67c0 Mon Sep 17 00:00:00 2001 From: Gautham B A Date: Thu, 21 Oct 2021 22:54:33 +0530 Subject: [PATCH] HDFS-16278. Make HDFS snapshot tools cross platform (#3563) --- .../libhdfspp/tests/tools/CMakeLists.txt | 9 ++ .../tests/tools/hdfs-create-snapshot-mock.cc | 58 ++++++++ .../tests/tools/hdfs-create-snapshot-mock.h | 72 +++++++++ .../tools/hdfs-disallow-snapshot-mock.cc | 56 +++++++ .../tests/tools/hdfs-disallow-snapshot-mock.h | 70 +++++++++ .../tests/tools/hdfs-rename-snapshot-mock.cc | 58 ++++++++ .../tests/tools/hdfs-rename-snapshot-mock.h | 72 +++++++++ .../libhdfspp/tests/tools/hdfs-tool-tests.cc | 42 +++++- .../libhdfspp/tests/tools/hdfs-tool-tests.h | 32 +++- .../native/libhdfspp/tools/CMakeLists.txt | 9 +- .../tools/hdfs-create-snapshot/CMakeLists.txt | 27 ++++ .../hdfs-create-snapshot.cc | 138 +++++++++++++++++ .../hdfs-create-snapshot.h | 96 ++++++++++++ .../tools/hdfs-create-snapshot/main.cc | 54 +++++++ .../hdfs-disallow-snapshot/CMakeLists.txt | 27 ++++ .../hdfs-disallow-snapshot.cc | 117 +++++++++++++++ .../hdfs-disallow-snapshot.h | 92 ++++++++++++ .../tools/hdfs-disallow-snapshot/main.cc | 54 +++++++ .../tools/hdfs-rename-snapshot/CMakeLists.txt | 27 ++++ .../hdfs-rename-snapshot.cc | 139 ++++++++++++++++++ .../hdfs-rename-snapshot.h | 95 ++++++++++++ .../tools/hdfs-rename-snapshot/main.cc | 54 +++++++ .../libhdfspp/tools/hdfs_createSnapshot.cc | 95 ------------ .../libhdfspp/tools/hdfs_disallowSnapshot.cc | 86 ----------- .../libhdfspp/tools/hdfs_renameSnapshot.cc | 88 ----------- 25 files changed, 1383 insertions(+), 284 deletions(-) create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.cc create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.h create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.cc create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.h create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.cc create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.h create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/CMakeLists.txt create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.cc create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.h create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/main.cc create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/CMakeLists.txt create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.cc create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.h create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/main.cc create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/CMakeLists.txt create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.cc create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.h create mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/main.cc delete mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_createSnapshot.cc delete mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_disallowSnapshot.cc delete mode 100644 hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_renameSnapshot.cc diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/CMakeLists.txt index 48580a4ccab..b5c21c7915b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/CMakeLists.txt +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/CMakeLists.txt @@ -18,7 +18,10 @@ add_executable(hdfs_tool_tests hdfs-allow-snapshot-mock.cc + hdfs-disallow-snapshot-mock.cc + hdfs-rename-snapshot-mock.cc hdfs-delete-snapshot-mock.cc + hdfs-create-snapshot-mock.cc hdfs-cat-mock.cc hdfs-tool-test-fixtures.cc hdfs-tool-tests.cc @@ -29,12 +32,18 @@ target_include_directories(hdfs_tool_tests PRIVATE ../../tools ../../tools/hdfs-df ../../tools/hdfs-allow-snapshot + ../../tools/hdfs-disallow-snapshot ../../tools/hdfs-delete-snapshot + ../../tools/hdfs-create-snapshot + ../../tools/hdfs-rename-snapshot ../../tools/hdfs-cat) target_link_libraries(hdfs_tool_tests PRIVATE gmock_main hdfs_df_lib hdfs_allowSnapshot_lib + hdfs_disallowSnapshot_lib hdfs_deleteSnapshot_lib + hdfs_createSnapshot_lib + hdfs_renameSnapshot_lib hdfs_cat_lib) add_test(hdfs_tool_tests hdfs_tool_tests) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.cc new file mode 100644 index 00000000000..323963181fc --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.cc @@ -0,0 +1,58 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include +#include +#include + +#include +#include + +#include "hdfs-create-snapshot-mock.h" +#include "hdfs-tool-tests.h" + +namespace hdfs::tools::test { +CreateSnapshotMock::~CreateSnapshotMock() = default; + +void CreateSnapshotMock::SetExpectations( + std::function()> test_case, + const std::vector &args) const { + // Get the pointer to the function that defines the test case + const auto test_case_func = + test_case.target (*)()>(); + ASSERT_NE(test_case_func, nullptr); + + // Set the expected method calls and their corresponding arguments for each + // test case + if (*test_case_func == &CallHelp) { + EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true)); + return; + } + + if (*test_case_func == &PassNOptAndAPath) { + const auto arg1 = args[1]; + const auto arg2 = std::optional{args[0]}; + EXPECT_CALL(*this, HandleSnapshot(arg1, arg2)) + .Times(1) + .WillOnce(testing::Return(true)); + } +} +} // namespace hdfs::tools::test diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.h new file mode 100644 index 00000000000..e159ec96c53 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.h @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBHDFSPP_TOOLS_HDFS_CREATE_SNAPSHOT_MOCK +#define LIBHDFSPP_TOOLS_HDFS_CREATE_SNAPSHOT_MOCK + +#include +#include +#include +#include +#include + +#include + +#include "hdfs-create-snapshot.h" + +namespace hdfs::tools::test { +/** + * {@class CreateSnapshotMock} is an {@class CreateSnapshot} whereby it mocks + * the HandleHelp and HandleSnapshot methods for testing their functionality. + */ +class CreateSnapshotMock : public hdfs::tools::CreateSnapshot { +public: + /** + * {@inheritdoc} + */ + CreateSnapshotMock(const int argc, char **argv) + : CreateSnapshot(argc, argv) {} + + // Abiding to the Rule of 5 + CreateSnapshotMock(const CreateSnapshotMock &) = delete; + CreateSnapshotMock(CreateSnapshotMock &&) = delete; + CreateSnapshotMock &operator=(const CreateSnapshotMock &) = delete; + CreateSnapshotMock &operator=(CreateSnapshotMock &&) = delete; + ~CreateSnapshotMock() override; + + /** + * Defines the methods and the corresponding arguments that are expected + * to be called on this instance of {@link HdfsTool} for the given test case. + * + * @param test_case An {@link std::function} object that points to the + * function defining the test case + * @param args The arguments that are passed to this test case + */ + void SetExpectations( + std::function()> test_case, + const std::vector &args = {}) const; + + MOCK_METHOD(bool, HandleHelp, (), (const, override)); + + MOCK_METHOD(bool, HandleSnapshot, + (const std::string &, const std::optional &), + (const, override)); +}; +} // namespace hdfs::tools::test + +#endif diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.cc new file mode 100644 index 00000000000..445cb3851cc --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.cc @@ -0,0 +1,56 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include +#include + +#include +#include + +#include "hdfs-disallow-snapshot-mock.h" +#include "hdfs-tool-tests.h" + +namespace hdfs::tools::test { +DisallowSnapshotMock::~DisallowSnapshotMock() = default; + +void DisallowSnapshotMock::SetExpectations( + std::function()> test_case, + const std::vector &args) const { + // Get the pointer to the function that defines the test case + const auto test_case_func = + test_case.target (*)()>(); + ASSERT_NE(test_case_func, nullptr); + + // Set the expected method calls and their corresponding arguments for each + // test case + if (*test_case_func == &CallHelp) { + EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true)); + return; + } + + if (*test_case_func == &PassAPath) { + const auto arg1 = args[0]; + EXPECT_CALL(*this, HandleSnapshot(arg1)) + .Times(1) + .WillOnce(testing::Return(true)); + } +} +} // namespace hdfs::tools::test diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.h new file mode 100644 index 00000000000..a6e2c240781 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.h @@ -0,0 +1,70 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBHDFSPP_TOOLS_HDFS_DISALLOW_SNAPSHOT_MOCK +#define LIBHDFSPP_TOOLS_HDFS_DISALLOW_SNAPSHOT_MOCK + +#include +#include +#include +#include + +#include + +#include "hdfs-disallow-snapshot.h" + +namespace hdfs::tools::test { +/** + * {@class DisallowSnapshotMock} is an {@class DisallowSnapshot} whereby it + * mocks the HandleHelp and HandleSnapshot methods for testing their + * functionality. + */ +class DisallowSnapshotMock : public hdfs::tools::DisallowSnapshot { +public: + /** + * {@inheritdoc} + */ + DisallowSnapshotMock(const int argc, char **argv) + : DisallowSnapshot(argc, argv) {} + + // Abiding to the Rule of 5 + DisallowSnapshotMock(const DisallowSnapshotMock &) = delete; + DisallowSnapshotMock(DisallowSnapshotMock &&) = delete; + DisallowSnapshotMock &operator=(const DisallowSnapshotMock &) = delete; + DisallowSnapshotMock &operator=(DisallowSnapshotMock &&) = delete; + ~DisallowSnapshotMock() override; + + /** + * Defines the methods and the corresponding arguments that are expected + * to be called on this instance of {@link HdfsTool} for the given test case. + * + * @param test_case An {@link std::function} object that points to the + * function defining the test case + * @param args The arguments that are passed to this test case + */ + void SetExpectations( + std::function()> test_case, + const std::vector &args = {}) const; + + MOCK_METHOD(bool, HandleHelp, (), (const, override)); + + MOCK_METHOD(bool, HandleSnapshot, (const std::string &), (const, override)); +}; +} // namespace hdfs::tools::test + +#endif diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.cc new file mode 100644 index 00000000000..6ee7f173d84 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.cc @@ -0,0 +1,58 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include +#include + +#include +#include + +#include "hdfs-rename-snapshot-mock.h" +#include "hdfs-tool-tests.h" + +namespace hdfs::tools::test { +RenameSnapshotMock::~RenameSnapshotMock() = default; + +void RenameSnapshotMock::SetExpectations( + std::function()> test_case, + const std::vector &args) const { + // Get the pointer to the function that defines the test case + const auto test_case_func = + test_case.target (*)()>(); + ASSERT_NE(test_case_func, nullptr); + + // Set the expected method calls and their corresponding arguments for each + // test case + if (*test_case_func == &CallHelp) { + EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true)); + return; + } + + if (*test_case_func == &Pass3Paths) { + const auto arg1 = args[0]; + const auto arg2 = args[1]; + const auto arg3 = args[2]; + EXPECT_CALL(*this, HandleSnapshot(arg1, arg2, arg3)) + .Times(1) + .WillOnce(testing::Return(true)); + } +} +} // namespace hdfs::tools::test diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.h new file mode 100644 index 00000000000..657c2f17914 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.h @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBHDFSPP_TOOLS_HDFS_RENAME_SNAPSHOT_MOCK +#define LIBHDFSPP_TOOLS_HDFS_RENAME_SNAPSHOT_MOCK + +#include +#include +#include +#include + +#include + +#include "hdfs-rename-snapshot.h" + +namespace hdfs::tools::test { +/** + * {@class RenameSnapshotMock} is an {@class RenameSnapshot} whereby it + * mocks the HandleHelp and HandleSnapshot methods for testing their + * functionality. + */ +class RenameSnapshotMock : public hdfs::tools::RenameSnapshot { +public: + /** + * {@inheritdoc} + */ + RenameSnapshotMock(const int argc, char **argv) + : RenameSnapshot(argc, argv) {} + + // Abiding to the Rule of 5 + RenameSnapshotMock(const RenameSnapshotMock &) = delete; + RenameSnapshotMock(RenameSnapshotMock &&) = delete; + RenameSnapshotMock &operator=(const RenameSnapshotMock &) = delete; + RenameSnapshotMock &operator=(RenameSnapshotMock &&) = delete; + ~RenameSnapshotMock() override; + + /** + * Defines the methods and the corresponding arguments that are expected + * to be called on this instance of {@link HdfsTool} for the given test case. + * + * @param test_case An {@link std::function} object that points to the + * function defining the test case + * @param args The arguments that are passed to this test case + */ + void SetExpectations( + std::function()> test_case, + const std::vector &args = {}) const; + + MOCK_METHOD(bool, HandleHelp, (), (const, override)); + + MOCK_METHOD(bool, HandleSnapshot, + (const std::string &, const std::string &, const std::string &), + (const, override)); +}; +} // namespace hdfs::tools::test + +#endif diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.cc index 8ffce3b3f6a..33480a933df 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.cc +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.cc @@ -20,9 +20,12 @@ #include #include "hdfs-allow-snapshot-mock.h" -#include "hdfs-df-mock.h" #include "hdfs-cat-mock.h" +#include "hdfs-create-snapshot-mock.h" #include "hdfs-delete-snapshot-mock.h" +#include "hdfs-df-mock.h" +#include "hdfs-disallow-snapshot-mock.h" +#include "hdfs-rename-snapshot-mock.h" #include "hdfs-tool-test-fixtures.h" #include "hdfs-tool-tests.h" @@ -32,17 +35,27 @@ * hdfs-tool-test.h} to yield the test suite. */ -/** - * This file combines the test fixtures defined in {@file - * hdfs-tool-test-fixtures.h} and the test cases defined in {@file - * hdfs-tool-test.h} to yield the test structure. - */ - +// Basic tests INSTANTIATE_TEST_SUITE_P( HdfsAllowSnapshot, HdfsToolBasicTest, testing::Values(PassAPath, CallHelp)); +INSTANTIATE_TEST_SUITE_P( + HdfsDisallowSnapshot, HdfsToolBasicTest, + testing::Values(PassAPath, + CallHelp)); + +INSTANTIATE_TEST_SUITE_P( + HdfsRenameSnapshot, HdfsToolBasicTest, + testing::Values(Pass3Paths, + CallHelp)); + +INSTANTIATE_TEST_SUITE_P( + HdfsCreateSnapshot, HdfsToolBasicTest, + testing::Values(PassNOptAndAPath, + CallHelp)); + INSTANTIATE_TEST_SUITE_P(HdfsCat, HdfsToolBasicTest, testing::Values(PassAPath, CallHelp)); @@ -56,10 +69,25 @@ INSTANTIATE_TEST_SUITE_P( testing::Values(CallHelp, Pass2Paths)); +// Negative tests INSTANTIATE_TEST_SUITE_P( HdfsAllowSnapshot, HdfsToolNegativeTestThrows, testing::Values(Pass2Paths)); +INSTANTIATE_TEST_SUITE_P( + HdfsRenameSnapshot, HdfsToolNegativeTestNoThrow, + testing::Values(PassAPath, + Pass2Paths)); + +INSTANTIATE_TEST_SUITE_P( + HdfsCreateSnapshot, HdfsToolNegativeTestThrows, + testing::Values(Pass2Paths, + Pass3Paths)); + +INSTANTIATE_TEST_SUITE_P( + HdfsDisallowSnapshot, HdfsToolNegativeTestThrows, + testing::Values(Pass2Paths)); + INSTANTIATE_TEST_SUITE_P( HdfsDf, HdfsToolNegativeTestThrows, testing::Values(Pass2Paths)); diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.h index f7fd6a706a6..188a76c4394 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.h +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.h @@ -17,8 +17,8 @@ under the License. */ -#ifndef LIBHDFSPP_TOOLS_HDFS_TOOL_TEST -#define LIBHDFSPP_TOOLS_HDFS_TOOL_TEST +#ifndef LIBHDFSPP_TOOLS_HDFS_TOOL_TESTS +#define LIBHDFSPP_TOOLS_HDFS_TOOL_TESTS #include #include @@ -69,4 +69,32 @@ template std::unique_ptr Pass2Paths() { return hdfs_tool; } +template std::unique_ptr Pass3Paths() { + constexpr auto argc = 4; + static std::string exe("hdfs_tool_name"); + static std::string arg1("a/b/c"); + static std::string arg2("d/e/f"); + static std::string arg3("g/h/i"); + + static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data()}; + + auto hdfs_tool = std::make_unique(argc, argv); + hdfs_tool->SetExpectations(Pass3Paths, {arg1, arg2, arg3}); + return hdfs_tool; +} + +template std::unique_ptr PassNOptAndAPath() { + constexpr auto argc = 4; + static std::string exe("hdfs_tool_name"); + static std::string arg1("-n"); + static std::string arg2("some_name"); + static std::string arg3("g/h/i"); + + static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data()}; + + auto hdfs_tool = std::make_unique(argc, argv); + hdfs_tool->SetExpectations(PassNOptAndAPath, {arg2, arg3}); + return hdfs_tool; +} + #endif diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/CMakeLists.txt index c1222502547..c4aed54eeab 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/CMakeLists.txt +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/CMakeLists.txt @@ -79,14 +79,11 @@ target_link_libraries(hdfs_setrep tools_common hdfspp_static) add_subdirectory(hdfs-allow-snapshot) -add_executable(hdfs_disallowSnapshot hdfs_disallowSnapshot.cc) -target_link_libraries(hdfs_disallowSnapshot tools_common hdfspp_static) +add_subdirectory(hdfs-disallow-snapshot) -add_executable(hdfs_createSnapshot hdfs_createSnapshot.cc) -target_link_libraries(hdfs_createSnapshot tools_common hdfspp_static) +add_subdirectory(hdfs-create-snapshot) -add_executable(hdfs_renameSnapshot hdfs_renameSnapshot.cc) -target_link_libraries(hdfs_renameSnapshot tools_common hdfspp_static) +add_subdirectory(hdfs-rename-snapshot) add_subdirectory(hdfs-delete-snapshot) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/CMakeLists.txt new file mode 100644 index 00000000000..871fcd8d732 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/CMakeLists.txt @@ -0,0 +1,27 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +add_library(hdfs_createSnapshot_lib STATIC $ hdfs-create-snapshot.cc) +target_include_directories(hdfs_createSnapshot_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS}) +target_link_libraries(hdfs_createSnapshot_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static) + +add_executable(hdfs_createSnapshot main.cc) +target_include_directories(hdfs_createSnapshot PRIVATE ../../tools) +target_link_libraries(hdfs_createSnapshot PRIVATE hdfs_createSnapshot_lib) + +install(TARGETS hdfs_createSnapshot RUNTIME DESTINATION bin) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.cc new file mode 100644 index 00000000000..6036e3a76f9 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.cc @@ -0,0 +1,138 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include +#include +#include +#include + +#include "hdfs-create-snapshot.h" +#include "tools_common.h" + +namespace hdfs::tools { +CreateSnapshot::CreateSnapshot(const int argc, char **argv) + : HdfsTool(argc, argv) {} + +bool CreateSnapshot::Initialize() { + auto add_options = opt_desc_.add_options(); + add_options("help,h", "Show the help for hdfs_createSnapshot"); + add_options("path", po::value(), + "The path to the directory for creating the snapshot"); + add_options("name,n", po::value(), + "The snapshot name, a default name is selected if omitted"); + + // We allow only one argument to be passed to path option. An exception is + // thrown if multiple arguments are passed to this. + pos_opt_desc_.add("path", 1); + + po::store(po::command_line_parser(argc_, argv_) + .options(opt_desc_) + .positional(pos_opt_desc_) + .run(), + opt_val_); + po::notify(opt_val_); + return true; +} + +bool CreateSnapshot::ValidateConstraints() const { + // If the name option is specified, there will be 4 arguments in total + if (argc_ == 4) { + return opt_val_.count("name") > 0; + } + + // Rest of the cases must contain more than 1 argument on the command line + return argc_ > 1; +} + +std::string CreateSnapshot::GetDescription() const { + std::stringstream desc; + desc << "Usage: hdfs_createSnapshot [OPTION] PATH" << std::endl + << std::endl + << "Create a snapshot of a snapshot-able directory." << std::endl + << "This operation requires owner privilege of the snapshot-able " + "directory." + << std::endl + << std::endl + << " -n NAME The snapshot name. When it is omitted, a default name " + "is generated" + << std::endl + << " using a timestamp with the format:" << std::endl + << R"( "'s'yyyyMMdd-HHmmss.SSS", e.g. s20130412-151029.033)" + << std::endl + << " -h display this help and exit" << std::endl + << std::endl + << "Examples:" << std::endl + << "hdfs_createSnapshot hdfs://localhost.localdomain:8020/dir" + << std::endl + << "hdfs_createSnapshot -n MySnapshot /dir1/dir2" << std::endl; + return desc.str(); +} + +bool CreateSnapshot::Do() { + if (!Initialize()) { + std::cerr << "Unable to initialize HDFS create snapshot tool" << std::endl; + return false; + } + + if (!ValidateConstraints()) { + std::cout << GetDescription(); + return false; + } + + if (opt_val_.count("help") > 0) { + return HandleHelp(); + } + + if (opt_val_.count("path") > 0) { + const auto path = opt_val_["path"].as(); + const auto name = opt_val_.count("name") > 0 + ? std::optional{opt_val_["name"].as()} + : std::nullopt; + return HandleSnapshot(path, name); + } + + return true; +} + +bool CreateSnapshot::HandleHelp() const { + std::cout << GetDescription(); + return true; +} + +bool CreateSnapshot::HandleSnapshot( + const std::string &path, const std::optional &name) const { + // Building a URI object from the given uri_path + auto uri = hdfs::parse_path_or_exit(path); + + const auto fs = hdfs::doConnect(uri, false); + if (!fs) { + std::cerr << "Could not connect the file system. " << std::endl; + return false; + } + + const auto status = fs->CreateSnapshot(uri.get_path(), name.value_or("")); + if (!status.ok()) { + std::cerr << "Error: " << status.ToString() << std::endl; + return false; + } + return true; +} +} // namespace hdfs::tools diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.h new file mode 100644 index 00000000000..9f1fc09ff2c --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.h @@ -0,0 +1,96 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBHDFSPP_TOOLS_HDFS_CREATE_SNAPSHOT +#define LIBHDFSPP_TOOLS_HDFS_CREATE_SNAPSHOT + +#include +#include + +#include + +#include "hdfs-tool.h" + +namespace hdfs::tools { +/** + * {@class CreateSnapshot} is an {@class HdfsTool} that facilitates the creation + * of the snapshot of a snapshot-able directory located at PATH. + */ +class CreateSnapshot : public HdfsTool { +public: + /** + * {@inheritdoc} + */ + CreateSnapshot(int argc, char **argv); + + // Abiding to the Rule of 5 + CreateSnapshot(const CreateSnapshot &) = default; + CreateSnapshot(CreateSnapshot &&) = default; + CreateSnapshot &operator=(const CreateSnapshot &) = delete; + CreateSnapshot &operator=(CreateSnapshot &&) = delete; + ~CreateSnapshot() override = default; + + /** + * {@inheritdoc} + */ + [[nodiscard]] std::string GetDescription() const override; + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool Do() override; + +protected: + /** + * {@inheritdoc} + */ + [[nodiscard]] bool Initialize() override; + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool ValidateConstraints() const override; + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool HandleHelp() const override; + + /** + * Handle the path argument that's passed to this tool. + * + * @param path The path to the snapshot-able directory for which the snapshot + * needs to be created. + * @param name The name to assign to the snapshot after creating it. + * + * @return A boolean indicating the result of this operation. + */ + [[nodiscard]] virtual bool + HandleSnapshot(const std::string &path, + const std::optional &name = std::nullopt) const; + +private: + /** + * A boost data-structure containing the description of positional arguments + * passed to the command-line. + */ + po::positional_options_description pos_opt_desc_; +}; +} // namespace hdfs::tools + +#endif diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/main.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/main.cc new file mode 100644 index 00000000000..7801bb4c604 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/main.cc @@ -0,0 +1,54 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include + +#include + +#include "hdfs-create-snapshot.h" + +int main(int argc, char *argv[]) { + const auto result = std::atexit([]() -> void { + // Clean up static data on exit and prevent valgrind memory leaks + google::protobuf::ShutdownProtobufLibrary(); + }); + if (result != 0) { + std::cerr << "Error: Unable to schedule clean-up tasks for HDFS create " + "snapshot tool, exiting" + << std::endl; + std::exit(EXIT_FAILURE); + } + + hdfs::tools::CreateSnapshot create_snapshot(argc, argv); + auto success = false; + + try { + success = create_snapshot.Do(); + } catch (const std::exception &e) { + std::cerr << "Error: " << e.what() << std::endl; + success = false; + } + + if (!success) { + std::exit(EXIT_FAILURE); + } + return 0; +} diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/CMakeLists.txt new file mode 100644 index 00000000000..263fc3adb96 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/CMakeLists.txt @@ -0,0 +1,27 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +add_library(hdfs_disallowSnapshot_lib STATIC $ hdfs-disallow-snapshot.cc) +target_include_directories(hdfs_disallowSnapshot_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS}) +target_link_libraries(hdfs_disallowSnapshot_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static) + +add_executable(hdfs_disallowSnapshot main.cc) +target_include_directories(hdfs_disallowSnapshot PRIVATE ../../tools) +target_link_libraries(hdfs_disallowSnapshot PRIVATE hdfs_disallowSnapshot_lib) + +install(TARGETS hdfs_disallowSnapshot RUNTIME DESTINATION bin) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.cc new file mode 100644 index 00000000000..576c1bec30f --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.cc @@ -0,0 +1,117 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include +#include +#include + +#include "hdfs-disallow-snapshot.h" +#include "tools_common.h" + +namespace hdfs::tools { +DisallowSnapshot::DisallowSnapshot(const int argc, char **argv) + : HdfsTool(argc, argv) {} + +bool DisallowSnapshot::Initialize() { + auto add_options = opt_desc_.add_options(); + add_options("help,h", "Show the help for hdfs_disallowSnapshot"); + add_options( + "path", po::value(), + "The path to the directory for which snapshot-ing must be disallowed"); + + // We allow only one argument to be passed to this tool. An exception is + // thrown if multiple arguments are passed. + pos_opt_desc_.add("path", 1); + + po::store(po::command_line_parser(argc_, argv_) + .options(opt_desc_) + .positional(pos_opt_desc_) + .run(), + opt_val_); + po::notify(opt_val_); + return true; +} + +std::string DisallowSnapshot::GetDescription() const { + std::stringstream desc; + desc << "Usage: hdfs_disallowSnapshot [OPTION] PATH" << std::endl + << std::endl + << "Disallowing snapshots of a directory at PATH to be created." + << std::endl + << "All snapshots of the directory must be deleted before disallowing " + "snapshots." + << std::endl + << std::endl + << " -h display this help and exit" << std::endl + << std::endl + << "Examples:" << std::endl + << "hdfs_disallowSnapshot hdfs://localhost.localdomain:8020/dir" + << std::endl + << "hdfs_disallowSnapshot /dir1/dir2" << std::endl; + return desc.str(); +} + +bool DisallowSnapshot::Do() { + if (!Initialize()) { + std::cerr << "Unable to initialize HDFS allow snapshot tool" << std::endl; + return false; + } + + if (!ValidateConstraints()) { + std::cout << GetDescription(); + return false; + } + + if (opt_val_.count("help") > 0) { + return HandleHelp(); + } + + if (opt_val_.count("path") > 0) { + const auto path = opt_val_["path"].as(); + return HandleSnapshot(path); + } + + return true; +} + +bool DisallowSnapshot::HandleHelp() const { + std::cout << GetDescription(); + return true; +} + +bool DisallowSnapshot::HandleSnapshot(const std::string &path) const { + // Building a URI object from the given uri_path + auto uri = hdfs::parse_path_or_exit(path); + + const auto fs = hdfs::doConnect(uri, false); + if (!fs) { + std::cerr << "Could not connect the file system. " << std::endl; + return false; + } + + const auto status = fs->DisallowSnapshot(uri.get_path()); + if (!status.ok()) { + std::cerr << "Error: " << status.ToString() << std::endl; + return false; + } + return true; +} +} // namespace hdfs::tools diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.h new file mode 100644 index 00000000000..b4174873cff --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.h @@ -0,0 +1,92 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBHDFSPP_TOOLS_HDFS_DISALLOW_SNAPSHOT +#define LIBHDFSPP_TOOLS_HDFS_DISALLOW_SNAPSHOT + +#include + +#include + +#include "hdfs-tool.h" + +namespace hdfs::tools { +/** + * {@class DisallowSnapshot} is an {@class HdfsTool} that disallows the + * snapshots from being created for a directory. All the existing snapshots must + * be deleted first. + */ +class DisallowSnapshot : public HdfsTool { +public: + /** + * {@inheritdoc} + */ + DisallowSnapshot(int argc, char **argv); + + // Abiding to the Rule of 5 + DisallowSnapshot(const DisallowSnapshot &) = default; + DisallowSnapshot(DisallowSnapshot &&) = default; + DisallowSnapshot &operator=(const DisallowSnapshot &) = delete; + DisallowSnapshot &operator=(DisallowSnapshot &&) = delete; + ~DisallowSnapshot() override = default; + + /** + * {@inheritdoc} + */ + [[nodiscard]] std::string GetDescription() const override; + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool Do() override; + +protected: + /** + * {@inheritdoc} + */ + [[nodiscard]] bool Initialize() override; + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool ValidateConstraints() const override { return argc_ > 1; } + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool HandleHelp() const override; + + /** + * Handle the path argument that's passed to this tool. + * + * @param path The path to the directory for which snapshot-ing must be + * disallowed. + * + * @return A boolean indicating the result of this operation. + */ + [[nodiscard]] virtual bool HandleSnapshot(const std::string &path) const; + +private: + /** + * A boost data-structure containing the description of positional arguments + * passed to the command-line. + */ + po::positional_options_description pos_opt_desc_; +}; +} // namespace hdfs::tools +#endif diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/main.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/main.cc new file mode 100644 index 00000000000..07009200f39 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/main.cc @@ -0,0 +1,54 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include + +#include + +#include "hdfs-disallow-snapshot.h" + +int main(int argc, char *argv[]) { + const auto result = std::atexit([]() -> void { + // Clean up static data on exit and prevent valgrind memory leaks + google::protobuf::ShutdownProtobufLibrary(); + }); + if (result != 0) { + std::cerr << "Error: Unable to schedule clean-up tasks for HDFS disallow " + "snapshot tool, exiting" + << std::endl; + std::exit(EXIT_FAILURE); + } + + hdfs::tools::DisallowSnapshot disallow_snapshot(argc, argv); + auto success = false; + + try { + success = disallow_snapshot.Do(); + } catch (const std::exception &e) { + std::cerr << "Error: " << e.what() << std::endl; + success = false; + } + + if (!success) { + std::exit(EXIT_FAILURE); + } + return 0; +} diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/CMakeLists.txt new file mode 100644 index 00000000000..44d9447eac7 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/CMakeLists.txt @@ -0,0 +1,27 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +add_library(hdfs_renameSnapshot_lib STATIC $ hdfs-rename-snapshot.cc) +target_include_directories(hdfs_renameSnapshot_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS}) +target_link_libraries(hdfs_renameSnapshot_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static) + +add_executable(hdfs_renameSnapshot main.cc) +target_include_directories(hdfs_renameSnapshot PRIVATE ../../tools) +target_link_libraries(hdfs_renameSnapshot PRIVATE hdfs_renameSnapshot_lib) + +install(TARGETS hdfs_renameSnapshot RUNTIME DESTINATION bin) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.cc new file mode 100644 index 00000000000..80858c5471b --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.cc @@ -0,0 +1,139 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include +#include +#include + +#include "hdfs-rename-snapshot.h" +#include "tools_common.h" + +namespace hdfs::tools { +RenameSnapshot::RenameSnapshot(const int argc, char **argv) + : HdfsTool(argc, argv) {} + +bool RenameSnapshot::Initialize() { + auto add_options = opt_desc_.add_options(); + add_options("help,h", "Show the help for hdfs_renameSnapshot"); + add_options("path", po::value(), + "The path to the directory that is snapshot-able"); + add_options("old-name", po::value(), + "The old/current name of the snapshot"); + add_options("new-name", po::value(), + "The new name for the snapshot"); + + // Register "path", "old-name" and "new-name" as positional arguments. + // We allow only three arguments to be passed to this tool. An exception is + // thrown if more than three arguments are passed. + pos_opt_desc_.add("path", 1); + pos_opt_desc_.add("old-name", 1); + pos_opt_desc_.add("new-name", 1); + + po::store(po::command_line_parser(argc_, argv_) + .options(opt_desc_) + .positional(pos_opt_desc_) + .run(), + opt_val_); + po::notify(opt_val_); + return true; +} + +bool RenameSnapshot::ValidateConstraints() const { + // Only "help" is allowed as single argument + if (argc_ == 2) { + return opt_val_.count("help") > 0; + } + + // Rest of the cases must contain more than 2 argument on the command line + return argc_ > 3; +} + +std::string RenameSnapshot::GetDescription() const { + std::stringstream desc; + desc << "Usage: hdfs_renameSnapshot [OPTION] PATH OLD_NAME NEW_NAME" + << std::endl + << std::endl + << "Rename a snapshot from OLD_NAME to NEW_NAME." << std::endl + << "This operation requires owner privilege of the snapshot-able " + "directory." + << std::endl + << std::endl + << " -h display this help and exit" << std::endl + << std::endl + << "Examples:" << std::endl + << "hdfs_renameSnapshot hdfs://localhost.localdomain:8020/dir oldDir " + "newDir" + << std::endl + << "hdfs_renameSnapshot /dir1/dir2 oldSnap newSnap" << std::endl; + return desc.str(); +} + +bool RenameSnapshot::Do() { + if (!Initialize()) { + std::cerr << "Unable to initialize HDFS rename snapshot tool" << std::endl; + return false; + } + + if (!ValidateConstraints()) { + std::cout << GetDescription(); + return false; + } + + if (opt_val_.count("help") > 0) { + return HandleHelp(); + } + + if (opt_val_.count("path") > 0 && opt_val_.count("old-name") > 0 && + opt_val_.count("new-name") > 0) { + const auto path = opt_val_["path"].as(); + const auto old_name = opt_val_["old-name"].as(); + const auto new_name = opt_val_["new-name"].as(); + return HandleSnapshot(path, old_name, new_name); + } + + return true; +} + +bool RenameSnapshot::HandleHelp() const { + std::cout << GetDescription(); + return true; +} + +bool RenameSnapshot::HandleSnapshot(const std::string &path, + const std::string &old_name, + const std::string &new_name) const { + // Building a URI object from the given path + auto uri = hdfs::parse_path_or_exit(path); + + const auto fs = hdfs::doConnect(uri, false); + if (!fs) { + std::cerr << "Could not connect the file system. " << std::endl; + return false; + } + + const auto status = fs->RenameSnapshot(uri.get_path(), old_name, new_name); + if (!status.ok()) { + std::cerr << "Error: " << status.ToString() << std::endl; + return false; + } + return true; +} +} // namespace hdfs::tools diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.h new file mode 100644 index 00000000000..5133999b87f --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.h @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBHDFSPP_TOOLS_HDFS_RENAME_SNAPSHOT +#define LIBHDFSPP_TOOLS_HDFS_RENAME_SNAPSHOT + +#include + +#include + +#include "hdfs-tool.h" + +namespace hdfs::tools { +/** + * {@class RenameSnapshot} is an {@class HdfsTool} that facilitates the + * renaming of the snapshots. + */ +class RenameSnapshot : public HdfsTool { +public: + /** + * {@inheritdoc} + */ + RenameSnapshot(int argc, char **argv); + + // Abiding to the Rule of 5 + RenameSnapshot(const RenameSnapshot &) = default; + RenameSnapshot(RenameSnapshot &&) = default; + RenameSnapshot &operator=(const RenameSnapshot &) = delete; + RenameSnapshot &operator=(RenameSnapshot &&) = delete; + ~RenameSnapshot() override = default; + + /** + * {@inheritdoc} + */ + [[nodiscard]] std::string GetDescription() const override; + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool Do() override; + +protected: + /** + * {@inheritdoc} + */ + [[nodiscard]] bool Initialize() override; + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool ValidateConstraints() const override; + + /** + * {@inheritdoc} + */ + [[nodiscard]] bool HandleHelp() const override; + + /** + * Handle the arguments that are passed to this tool. + * + * @param path The path to the directory that is snapshot-able. + * @param old_name The name of the current/older snapshot. + * @param new_name The new name for the old snapshot. + * + * @return A boolean indicating the result of this operation. + */ + [[nodiscard]] virtual bool HandleSnapshot(const std::string &path, + const std::string &old_name, + const std::string &new_name) const; + +private: + /** + * A boost data-structure containing the description of positional arguments + * passed to the command-line. + */ + po::positional_options_description pos_opt_desc_; +}; +} // namespace hdfs::tools + +#endif diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/main.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/main.cc new file mode 100644 index 00000000000..c52425411d3 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/main.cc @@ -0,0 +1,54 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +#include +#include +#include + +#include + +#include "hdfs-rename-snapshot.h" + +int main(int argc, char *argv[]) { + const auto result = std::atexit([]() -> void { + // Clean up static data on exit and prevent valgrind memory leaks + google::protobuf::ShutdownProtobufLibrary(); + }); + if (result != 0) { + std::cerr << "Error: Unable to schedule clean-up tasks for HDFS rename " + "snapshot tool, exiting" + << std::endl; + std::exit(EXIT_FAILURE); + } + + hdfs::tools::RenameSnapshot rename_snapshot(argc, argv); + auto success = false; + + try { + success = rename_snapshot.Do(); + } catch (const std::exception &e) { + std::cerr << "Error: " << e.what() << std::endl; + success = false; + } + + if (!success) { + std::exit(EXIT_FAILURE); + } + return 0; +} diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_createSnapshot.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_createSnapshot.cc deleted file mode 100644 index 1236dd61130..00000000000 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_createSnapshot.cc +++ /dev/null @@ -1,95 +0,0 @@ -/* - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*/ - -#include -#include -#include "tools_common.h" - -void usage(){ - std::cout << "Usage: hdfs_createSnapshot [OPTION] PATH" - << std::endl - << std::endl << "Create a snapshot of a snapshottable directory." - << std::endl << "This operation requires owner privilege of the snapshottable directory." - << std::endl - << std::endl << " -n NAME The snapshot name. When it is omitted, a default name is generated" - << std::endl << " using a timestamp with the format:" - << std::endl << " \"'s'yyyyMMdd-HHmmss.SSS\", e.g. s20130412-151029.033" - << std::endl << " -h display this help and exit" - << std::endl - << std::endl << "Examples:" - << std::endl << "hdfs_createSnapshot hdfs://localhost.localdomain:8020/dir" - << std::endl << "hdfs_createSnapshot -n MySnapshot /dir1/dir2" - << std::endl; -} - -int main(int argc, char *argv[]) { - //We should have at least 2 arguments - if (argc < 2) { - usage(); - exit(EXIT_FAILURE); - } - - int input; - std::string name; - - //Using GetOpt to read in the values - opterr = 0; - while ((input = getopt(argc, argv, "hn:")) != -1) { - switch (input) - { - case 'h': - usage(); - exit(EXIT_SUCCESS); - case 'n': - name = optarg; - break; - case '?': - if (optopt == 'n') - std::cerr << "Option -" << (char) optopt << " requires an argument." << std::endl; - else if (isprint(optopt)) - std::cerr << "Unknown option `-" << (char) optopt << "'." << std::endl; - else - std::cerr << "Unknown option character `" << (char) optopt << "'." << std::endl; - usage(); - exit(EXIT_FAILURE); - default: - exit(EXIT_FAILURE); - } - } - std::string uri_path = argv[optind]; - - //Building a URI object from the given uri_path - hdfs::URI uri = hdfs::parse_path_or_exit(uri_path); - - std::shared_ptr fs = hdfs::doConnect(uri, false); - if (!fs) { - std::cerr << "Could not connect the file system. " << std::endl; - exit(EXIT_FAILURE); - } - - hdfs::Status status = fs->CreateSnapshot(uri.get_path(), name); - if (!status.ok()) { - std::cerr << "Error: " << status.ToString() << std::endl; - exit(EXIT_FAILURE); - } - - // Clean up static data and prevent valgrind memory leaks - google::protobuf::ShutdownProtobufLibrary(); - return 0; -} diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_disallowSnapshot.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_disallowSnapshot.cc deleted file mode 100644 index b5c97ab9033..00000000000 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_disallowSnapshot.cc +++ /dev/null @@ -1,86 +0,0 @@ -/* - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*/ - -#include -#include -#include "tools_common.h" - -void usage(){ - std::cout << "Usage: hdfs_disallowSnapshot [OPTION] PATH" - << std::endl - << std::endl << "Disallowing snapshots of a directory at PATH to be created." - << std::endl << "All snapshots of the directory must be deleted before disallowing snapshots." - << std::endl - << std::endl << " -h display this help and exit" - << std::endl - << std::endl << "Examples:" - << std::endl << "hdfs_disallowSnapshot hdfs://localhost.localdomain:8020/dir" - << std::endl << "hdfs_disallowSnapshot /dir1/dir2" - << std::endl; -} - -int main(int argc, char *argv[]) { - //We should have at least 2 arguments - if (argc < 2) { - usage(); - exit(EXIT_FAILURE); - } - - int input; - - //Using GetOpt to read in the values - opterr = 0; - while ((input = getopt(argc, argv, "Rh")) != -1) { - switch (input) - { - case 'h': - usage(); - exit(EXIT_SUCCESS); - case '?': - if (isprint(optopt)) - std::cerr << "Unknown option `-" << (char) optopt << "'." << std::endl; - else - std::cerr << "Unknown option character `" << (char) optopt << "'." << std::endl; - usage(); - exit(EXIT_FAILURE); - default: - exit(EXIT_FAILURE); - } - } - std::string uri_path = argv[optind]; - - //Building a URI object from the given uri_path - hdfs::URI uri = hdfs::parse_path_or_exit(uri_path); - - std::shared_ptr fs = hdfs::doConnect(uri, false); - if (!fs) { - std::cerr << "Could not connect the file system. " << std::endl; - exit(EXIT_FAILURE); - } - - hdfs::Status status = fs->DisallowSnapshot(uri.get_path()); - if (!status.ok()) { - std::cerr << "Error: " << status.ToString() << std::endl; - exit(EXIT_FAILURE); - } - - // Clean up static data and prevent valgrind memory leaks - google::protobuf::ShutdownProtobufLibrary(); - return 0; -} diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_renameSnapshot.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_renameSnapshot.cc deleted file mode 100644 index 8d38fc2292d..00000000000 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_renameSnapshot.cc +++ /dev/null @@ -1,88 +0,0 @@ -/* - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*/ - -#include -#include -#include "tools_common.h" - -void usage(){ - std::cout << "Usage: hdfs_renameSnapshot [OPTION] PATH OLD_NAME NEW_NAME" - << std::endl - << std::endl << "Rename a snapshot from OLD_NAME to NEW_NAME." - << std::endl << "This operation requires owner privilege of the snapshottable directory." - << std::endl - << std::endl << " -h display this help and exit" - << std::endl - << std::endl << "Examples:" - << std::endl << "hdfs_renameSnapshot hdfs://localhost.localdomain:8020/dir oldDir newDir" - << std::endl << "hdfs_renameSnapshot /dir1/dir2 oldSnap newSnap" - << std::endl; -} - -int main(int argc, char *argv[]) { - //We should have at least 2 arguments - if (argc < 2) { - usage(); - exit(EXIT_FAILURE); - } - - int input; - - //Using GetOpt to read in the values - opterr = 0; - while ((input = getopt(argc, argv, "h")) != -1) { - switch (input) - { - case 'h': - usage(); - exit(EXIT_SUCCESS); - case '?': - if (isprint(optopt)) - std::cerr << "Unknown option `-" << (char) optopt << "'." << std::endl; - else - std::cerr << "Unknown option character `" << (char) optopt << "'." << std::endl; - usage(); - exit(EXIT_FAILURE); - default: - exit(EXIT_FAILURE); - } - } - std::string uri_path = argv[optind]; - std::string old_name = argv[optind+1]; - std::string new_name = argv[optind+2]; - - //Building a URI object from the given uri_path - hdfs::URI uri = hdfs::parse_path_or_exit(uri_path); - - std::shared_ptr fs = hdfs::doConnect(uri, false); - if (!fs) { - std::cerr << "Could not connect the file system. " << std::endl; - exit(EXIT_FAILURE); - } - - hdfs::Status status = fs->RenameSnapshot(uri.get_path(), old_name, new_name); - if (!status.ok()) { - std::cerr << "Error: " << status.ToString() << std::endl; - exit(EXIT_FAILURE); - } - - // Clean up static data and prevent valgrind memory leaks - google::protobuf::ShutdownProtobufLibrary(); - return 0; -}