hdfs_deleteSnapshot.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #include <google/protobuf/stubs/common.h>
  18. #include <unistd.h>
  19. #include "tools_common.h"
  20. void usage(){
  21. std::cout << "Usage: hdfs_deleteSnapshot [OPTION] PATH NAME"
  22. << std::endl
  23. << std::endl << "Delete a snapshot NAME from a snapshottable directory."
  24. << std::endl << "This operation requires owner privilege of the snapshottable directory."
  25. << std::endl
  26. << std::endl << " -h display this help and exit"
  27. << std::endl
  28. << std::endl << "Examples:"
  29. << std::endl << "hdfs_deleteSnapshot hdfs://localhost.localdomain:8020/dir mySnapshot"
  30. << std::endl << "hdfs_deleteSnapshot /dir1/dir2 mySnapshot"
  31. << std::endl;
  32. }
  33. int main(int argc, char *argv[]) {
  34. //We should have at least 2 arguments
  35. if (argc < 2) {
  36. usage();
  37. exit(EXIT_FAILURE);
  38. }
  39. int input;
  40. //Using GetOpt to read in the values
  41. opterr = 0;
  42. while ((input = getopt(argc, argv, "h")) != -1) {
  43. switch (input)
  44. {
  45. case 'h':
  46. usage();
  47. exit(EXIT_SUCCESS);
  48. case '?':
  49. if (isprint(optopt))
  50. std::cerr << "Unknown option `-" << (char) optopt << "'." << std::endl;
  51. else
  52. std::cerr << "Unknown option character `" << (char) optopt << "'." << std::endl;
  53. usage();
  54. exit(EXIT_FAILURE);
  55. default:
  56. exit(EXIT_FAILURE);
  57. }
  58. }
  59. std::string uri_path = argv[optind];
  60. std::string name = argv[optind+1];
  61. //Building a URI object from the given uri_path
  62. hdfs::URI uri = hdfs::parse_path_or_exit(uri_path);
  63. std::shared_ptr<hdfs::FileSystem> fs = hdfs::doConnect(uri, false);
  64. if (!fs) {
  65. std::cerr << "Could not connect the file system. " << std::endl;
  66. exit(EXIT_FAILURE);
  67. }
  68. hdfs::Status status = fs->DeleteSnapshot(uri.get_path(), name);
  69. if (!status.ok()) {
  70. std::cerr << "Error: " << status.ToString() << std::endl;
  71. exit(EXIT_FAILURE);
  72. }
  73. // Clean up static data and prevent valgrind memory leaks
  74. google::protobuf::ShutdownProtobufLibrary();
  75. return 0;
  76. }