Преглед изворни кода

HDFS-16278. Make HDFS snapshot tools cross platform (#3563)

Gautham B A пре 3 година
родитељ
комит
a2f089df4d
25 измењених фајлова са 1383 додато и 284 уклоњено
  1. 9 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/CMakeLists.txt
  2. 58 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.cc
  3. 72 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-create-snapshot-mock.h
  4. 56 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.cc
  5. 70 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-disallow-snapshot-mock.h
  6. 58 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.cc
  7. 72 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-rename-snapshot-mock.h
  8. 35 7
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.cc
  9. 30 2
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.h
  10. 3 6
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/CMakeLists.txt
  11. 27 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/CMakeLists.txt
  12. 138 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.cc
  13. 96 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/hdfs-create-snapshot.h
  14. 54 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-create-snapshot/main.cc
  15. 27 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/CMakeLists.txt
  16. 117 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.cc
  17. 92 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/hdfs-disallow-snapshot.h
  18. 54 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-disallow-snapshot/main.cc
  19. 27 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/CMakeLists.txt
  20. 139 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.cc
  21. 95 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/hdfs-rename-snapshot.h
  22. 54 0
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs-rename-snapshot/main.cc
  23. 0 95
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_createSnapshot.cc
  24. 0 86
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_disallowSnapshot.cc
  25. 0 88
      hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_renameSnapshot.cc

+ 9 - 0
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)

+ 58 - 0
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 <functional>
+#include <memory>
+#include <optional>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "hdfs-create-snapshot-mock.h"
+#include "hdfs-tool-tests.h"
+
+namespace hdfs::tools::test {
+CreateSnapshotMock::~CreateSnapshotMock() = default;
+
+void CreateSnapshotMock::SetExpectations(
+    std::function<std::unique_ptr<CreateSnapshotMock>()> test_case,
+    const std::vector<std::string> &args) const {
+  // Get the pointer to the function that defines the test case
+  const auto test_case_func =
+      test_case.target<std::unique_ptr<CreateSnapshotMock> (*)()>();
+  ASSERT_NE(test_case_func, nullptr);
+
+  // Set the expected method calls and their corresponding arguments for each
+  // test case
+  if (*test_case_func == &CallHelp<CreateSnapshotMock>) {
+    EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
+    return;
+  }
+
+  if (*test_case_func == &PassNOptAndAPath<CreateSnapshotMock>) {
+    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

+ 72 - 0
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 <functional>
+#include <memory>
+#include <optional>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+
+#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<std::unique_ptr<CreateSnapshotMock>()> test_case,
+      const std::vector<std::string> &args = {}) const;
+
+  MOCK_METHOD(bool, HandleHelp, (), (const, override));
+
+  MOCK_METHOD(bool, HandleSnapshot,
+              (const std::string &, const std::optional<std::string> &),
+              (const, override));
+};
+} // namespace hdfs::tools::test
+
+#endif

+ 56 - 0
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 <functional>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "hdfs-disallow-snapshot-mock.h"
+#include "hdfs-tool-tests.h"
+
+namespace hdfs::tools::test {
+DisallowSnapshotMock::~DisallowSnapshotMock() = default;
+
+void DisallowSnapshotMock::SetExpectations(
+    std::function<std::unique_ptr<DisallowSnapshotMock>()> test_case,
+    const std::vector<std::string> &args) const {
+  // Get the pointer to the function that defines the test case
+  const auto test_case_func =
+      test_case.target<std::unique_ptr<DisallowSnapshotMock> (*)()>();
+  ASSERT_NE(test_case_func, nullptr);
+
+  // Set the expected method calls and their corresponding arguments for each
+  // test case
+  if (*test_case_func == &CallHelp<DisallowSnapshotMock>) {
+    EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
+    return;
+  }
+
+  if (*test_case_func == &PassAPath<DisallowSnapshotMock>) {
+    const auto arg1 = args[0];
+    EXPECT_CALL(*this, HandleSnapshot(arg1))
+        .Times(1)
+        .WillOnce(testing::Return(true));
+  }
+}
+} // namespace hdfs::tools::test

+ 70 - 0
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 <functional>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+
+#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<std::unique_ptr<DisallowSnapshotMock>()> test_case,
+      const std::vector<std::string> &args = {}) const;
+
+  MOCK_METHOD(bool, HandleHelp, (), (const, override));
+
+  MOCK_METHOD(bool, HandleSnapshot, (const std::string &), (const, override));
+};
+} // namespace hdfs::tools::test
+
+#endif

+ 58 - 0
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 <functional>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "hdfs-rename-snapshot-mock.h"
+#include "hdfs-tool-tests.h"
+
+namespace hdfs::tools::test {
+RenameSnapshotMock::~RenameSnapshotMock() = default;
+
+void RenameSnapshotMock::SetExpectations(
+    std::function<std::unique_ptr<RenameSnapshotMock>()> test_case,
+    const std::vector<std::string> &args) const {
+  // Get the pointer to the function that defines the test case
+  const auto test_case_func =
+      test_case.target<std::unique_ptr<RenameSnapshotMock> (*)()>();
+  ASSERT_NE(test_case_func, nullptr);
+
+  // Set the expected method calls and their corresponding arguments for each
+  // test case
+  if (*test_case_func == &CallHelp<RenameSnapshotMock>) {
+    EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
+    return;
+  }
+
+  if (*test_case_func == &Pass3Paths<RenameSnapshotMock>) {
+    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

+ 72 - 0
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 <functional>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+
+#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<std::unique_ptr<RenameSnapshotMock>()> test_case,
+      const std::vector<std::string> &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

+ 35 - 7
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.cc

@@ -20,9 +20,12 @@
 #include <gtest/gtest.h>
 
 #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<hdfs::tools::test::AllowSnapshotMock>,
                     CallHelp<hdfs::tools::test::AllowSnapshotMock>));
 
+INSTANTIATE_TEST_SUITE_P(
+    HdfsDisallowSnapshot, HdfsToolBasicTest,
+    testing::Values(PassAPath<hdfs::tools::test::DisallowSnapshotMock>,
+                    CallHelp<hdfs::tools::test::DisallowSnapshotMock>));
+
+INSTANTIATE_TEST_SUITE_P(
+    HdfsRenameSnapshot, HdfsToolBasicTest,
+    testing::Values(Pass3Paths<hdfs::tools::test::RenameSnapshotMock>,
+                    CallHelp<hdfs::tools::test::RenameSnapshotMock>));
+
+INSTANTIATE_TEST_SUITE_P(
+    HdfsCreateSnapshot, HdfsToolBasicTest,
+    testing::Values(PassNOptAndAPath<hdfs::tools::test::CreateSnapshotMock>,
+                    CallHelp<hdfs::tools::test::CreateSnapshotMock>));
+
 INSTANTIATE_TEST_SUITE_P(HdfsCat, HdfsToolBasicTest,
                          testing::Values(PassAPath<hdfs::tools::test::CatMock>,
                                          CallHelp<hdfs::tools::test::CatMock>));
@@ -56,10 +69,25 @@ INSTANTIATE_TEST_SUITE_P(
     testing::Values(CallHelp<hdfs::tools::test::DeleteSnapshotMock>,
                     Pass2Paths<hdfs::tools::test::DeleteSnapshotMock>));
 
+// Negative tests
 INSTANTIATE_TEST_SUITE_P(
     HdfsAllowSnapshot, HdfsToolNegativeTestThrows,
     testing::Values(Pass2Paths<hdfs::tools::test::AllowSnapshotMock>));
 
+INSTANTIATE_TEST_SUITE_P(
+    HdfsRenameSnapshot, HdfsToolNegativeTestNoThrow,
+    testing::Values(PassAPath<hdfs::tools::test::RenameSnapshotMock>,
+                    Pass2Paths<hdfs::tools::test::RenameSnapshotMock>));
+
+INSTANTIATE_TEST_SUITE_P(
+    HdfsCreateSnapshot, HdfsToolNegativeTestThrows,
+    testing::Values(Pass2Paths<hdfs::tools::test::CreateSnapshotMock>,
+                    Pass3Paths<hdfs::tools::test::CreateSnapshotMock>));
+
+INSTANTIATE_TEST_SUITE_P(
+    HdfsDisallowSnapshot, HdfsToolNegativeTestThrows,
+    testing::Values(Pass2Paths<hdfs::tools::test::DisallowSnapshotMock>));
+
 INSTANTIATE_TEST_SUITE_P(
     HdfsDf, HdfsToolNegativeTestThrows,
     testing::Values(Pass2Paths<hdfs::tools::test::DfMock>));

+ 30 - 2
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 <memory>
 #include <string>
@@ -69,4 +69,32 @@ template <class T> std::unique_ptr<T> Pass2Paths() {
   return hdfs_tool;
 }
 
+template <class T> std::unique_ptr<T> 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<T>(argc, argv);
+  hdfs_tool->SetExpectations(Pass3Paths<T>, {arg1, arg2, arg3});
+  return hdfs_tool;
+}
+
+template <class T> std::unique_ptr<T> 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<T>(argc, argv);
+  hdfs_tool->SetExpectations(PassNOptAndAPath<T>, {arg2, arg3});
+  return hdfs_tool;
+}
+
 #endif

+ 3 - 6
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)
 

+ 27 - 0
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  $<TARGET_OBJECTS:hdfs_tool_obj> 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)

+ 138 - 0
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 <iostream>
+#include <memory>
+#include <optional>
+#include <ostream>
+#include <sstream>
+#include <string>
+
+#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<std::string>(),
+              "The path to the directory for creating the snapshot");
+  add_options("name,n", po::value<std::string>(),
+              "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<std::string>();
+    const auto name = opt_val_.count("name") > 0
+                          ? std::optional{opt_val_["name"].as<std::string>()}
+                          : 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<std::string> &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

+ 96 - 0
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 <optional>
+#include <string>
+
+#include <boost/program_options.hpp>
+
+#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<std::string> &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

+ 54 - 0
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 <cstdlib>
+#include <exception>
+#include <iostream>
+
+#include <google/protobuf/stubs/common.h>
+
+#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;
+}

+ 27 - 0
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  $<TARGET_OBJECTS:hdfs_tool_obj> 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)

+ 117 - 0
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 <iostream>
+#include <memory>
+#include <ostream>
+#include <sstream>
+#include <string>
+
+#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<std::string>(),
+      "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<std::string>();
+    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

+ 92 - 0
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 <string>
+
+#include <boost/program_options.hpp>
+
+#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

+ 54 - 0
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 <cstdlib>
+#include <exception>
+#include <iostream>
+
+#include <google/protobuf/stubs/common.h>
+
+#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;
+}

+ 27 - 0
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 $<TARGET_OBJECTS:hdfs_tool_obj> 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)

+ 139 - 0
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 <iostream>
+#include <memory>
+#include <ostream>
+#include <sstream>
+#include <string>
+
+#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<std::string>(),
+              "The path to the directory that is snapshot-able");
+  add_options("old-name", po::value<std::string>(),
+              "The old/current name of the snapshot");
+  add_options("new-name", po::value<std::string>(),
+              "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<std::string>();
+    const auto old_name = opt_val_["old-name"].as<std::string>();
+    const auto new_name = opt_val_["new-name"].as<std::string>();
+    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

+ 95 - 0
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 <string>
+
+#include <boost/program_options.hpp>
+
+#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

+ 54 - 0
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 <cstdlib>
+#include <exception>
+#include <iostream>
+
+#include <google/protobuf/stubs/common.h>
+
+#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;
+}

+ 0 - 95
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_createSnapshot.cc

@@ -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 <google/protobuf/stubs/common.h>
-#include <unistd.h>
-#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<hdfs::FileSystem> 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;
-}

+ 0 - 86
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_disallowSnapshot.cc

@@ -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 <google/protobuf/stubs/common.h>
-#include <unistd.h>
-#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<hdfs::FileSystem> 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;
-}

+ 0 - 88
hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/hdfs_renameSnapshot.cc

@@ -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 <google/protobuf/stubs/common.h>
-#include <unistd.h>
-#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<hdfs::FileSystem> 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;
-}