hdfs_disallowSnapshot.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_disallowSnapshot [OPTION] PATH"
  22. << std::endl
  23. << std::endl << "Disallowing snapshots of a directory at PATH to be created."
  24. << std::endl << "All snapshots of the directory must be deleted before disallowing snapshots."
  25. << std::endl
  26. << std::endl << " -h display this help and exit"
  27. << std::endl
  28. << std::endl << "Examples:"
  29. << std::endl << "hdfs_disallowSnapshot hdfs://localhost.localdomain:8020/dir"
  30. << std::endl << "hdfs_disallowSnapshot /dir1/dir2"
  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, "Rh")) != -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. //Building a URI object from the given uri_path
  61. hdfs::URI uri = hdfs::parse_path_or_exit(uri_path);
  62. std::shared_ptr<hdfs::FileSystem> fs = hdfs::doConnect(uri, false);
  63. if (!fs) {
  64. std::cerr << "Could not connect the file system. " << std::endl;
  65. exit(EXIT_FAILURE);
  66. }
  67. hdfs::Status status = fs->DisallowSnapshot(uri.get_path());
  68. if (!status.ok()) {
  69. std::cerr << "Error: " << status.ToString() << std::endl;
  70. exit(EXIT_FAILURE);
  71. }
  72. // Clean up static data and prevent valgrind memory leaks
  73. google::protobuf::ShutdownProtobufLibrary();
  74. return 0;
  75. }