hdfs_chmod.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <future>
  20. #include "tools_common.h"
  21. void usage(){
  22. std::cout << "Usage: hdfs_chmod [OPTION] <MODE[,MODE]... | OCTALMODE> FILE"
  23. << std::endl
  24. << std::endl << "Change the permissions of each FILE to MODE."
  25. << std::endl << "The user must be the owner of the file, or else a super-user."
  26. << std::endl << "Additional information is in the Permissions Guide:"
  27. << std::endl << "https://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-hdfs/HdfsPermissionsGuide.html"
  28. << std::endl
  29. << std::endl << " -R operate on files and directories recursively"
  30. << std::endl << " -h display this help and exit"
  31. << std::endl
  32. << std::endl << "Examples:"
  33. << std::endl << "hdfs_chmod -R 755 hdfs://localhost.localdomain:9433/dir/file"
  34. << std::endl << "hdfs_chmod 777 /dir/file"
  35. << std::endl;
  36. }
  37. struct SetPermissionState {
  38. const uint16_t permissions;
  39. const std::function<void(const hdfs::Status &)> handler;
  40. //The request counter is incremented once every time SetOwner async call is made
  41. uint64_t request_counter;
  42. //This boolean will be set when find returns the last result
  43. bool find_is_done;
  44. //Final status to be returned
  45. hdfs::Status status;
  46. //Shared variables will need protection with a lock
  47. std::mutex lock;
  48. SetPermissionState(const uint16_t permissions_, const std::function<void(const hdfs::Status &)> & handler_,
  49. uint64_t request_counter_, bool find_is_done_)
  50. : permissions(permissions_),
  51. handler(handler_),
  52. request_counter(request_counter_),
  53. find_is_done(find_is_done_),
  54. status(),
  55. lock() {
  56. }
  57. };
  58. int main(int argc, char *argv[]) {
  59. //We should have 3 or 4 parameters
  60. if (argc != 3 && argc != 4) {
  61. usage();
  62. exit(EXIT_FAILURE);
  63. }
  64. bool recursive = false;
  65. int input;
  66. //Using GetOpt to read in the values
  67. opterr = 0;
  68. while ((input = getopt(argc, argv, "Rh")) != -1) {
  69. switch (input)
  70. {
  71. case 'R':
  72. recursive = 1;
  73. break;
  74. case 'h':
  75. usage();
  76. exit(EXIT_SUCCESS);
  77. break;
  78. case '?':
  79. if (isprint(optopt))
  80. std::cerr << "Unknown option `-" << (char) optopt << "'." << std::endl;
  81. else
  82. std::cerr << "Unknown option character `" << (char) optopt << "'." << std::endl;
  83. usage();
  84. exit(EXIT_FAILURE);
  85. default:
  86. exit(EXIT_FAILURE);
  87. }
  88. }
  89. std::string permissions = argv[optind];
  90. std::string uri_path = argv[optind + 1];
  91. //Building a URI object from the given uri_path
  92. hdfs::optional<hdfs::URI> uri = hdfs::URI::parse_from_string(uri_path);
  93. if (!uri) {
  94. std::cerr << "Malformed URI: " << uri_path << std::endl;
  95. exit(EXIT_FAILURE);
  96. }
  97. //TODO: HDFS-9539 Currently options can be returned empty
  98. hdfs::Options options = *hdfs::getOptions();
  99. //TODO: HDFS-9539 - until then we increase the time-out to allow all recursive async calls to finish
  100. options.rpc_timeout = std::numeric_limits<int>::max();
  101. std::shared_ptr<hdfs::FileSystem> fs = hdfs::doConnect(uri.value(), options);
  102. if (!fs) {
  103. std::cerr << "Could not connect the file system. " << std::endl;
  104. exit(EXIT_FAILURE);
  105. }
  106. /* wrap async FileSystem::SetPermission with promise to make it a blocking call */
  107. std::shared_ptr<std::promise<hdfs::Status>> promise = std::make_shared<std::promise<hdfs::Status>>();
  108. std::future<hdfs::Status> future(promise->get_future());
  109. auto handler = [promise](const hdfs::Status &s) {
  110. promise->set_value(s);
  111. };
  112. //strtol() is reading the value with base 8, NULL because we are reading in just one value.
  113. uint16_t perm = strtol(permissions.c_str(), NULL, 8);
  114. if(!recursive){
  115. fs->SetPermission(uri->get_path(), perm, handler);
  116. }
  117. else {
  118. //Allocating shared state, which includes:
  119. //username and groupname to be set, handler to be called, request counter, and a boolean to keep track if find is done
  120. std::shared_ptr<SetPermissionState> state = std::make_shared<SetPermissionState>(perm, handler, 0, false);
  121. // Keep requesting more from Find until we process the entire listing. Call handler when Find is done and reques counter is 0.
  122. // Find guarantees that the handler will only be called once at a time so we do not need locking in handlerFind.
  123. auto handlerFind = [fs, state](const hdfs::Status &status_find, const std::vector<hdfs::StatInfo> & stat_infos, bool has_more_results) -> bool {
  124. //For each result returned by Find we call async SetOwner with the handler below.
  125. //SetOwner DOES NOT guarantee that the handler will only be called once at a time, so we DO need locking in handlerSetOwner.
  126. auto handlerSetOwner = [state](const hdfs::Status &status_set_owner) {
  127. std::lock_guard<std::mutex> guard(state->lock);
  128. //Decrement the counter once since we are done with this async call
  129. if (!status_set_owner.ok() && state->status.ok()){
  130. //We make sure we set state->status only on the first error.
  131. state->status = status_set_owner;
  132. }
  133. state->request_counter--;
  134. if(state->request_counter == 0 && state->find_is_done){
  135. state->handler(state->status); //exit
  136. }
  137. };
  138. if(!stat_infos.empty() && state->status.ok()) {
  139. for (hdfs::StatInfo const& s : stat_infos) {
  140. //Launch an asynchronous call to SetOwner for every returned result
  141. state->request_counter++;
  142. fs->SetPermission(s.full_path, state->permissions, handlerSetOwner);
  143. }
  144. }
  145. //Lock this section because handlerSetOwner might be accessing the same
  146. //shared variables simultaneously
  147. std::lock_guard<std::mutex> guard(state->lock);
  148. if (!status_find.ok() && state->status.ok()){
  149. //We make sure we set state->status only on the first error.
  150. state->status = status_find;
  151. }
  152. if(!has_more_results){
  153. state->find_is_done = true;
  154. if(state->request_counter == 0){
  155. state->handler(state->status); //exit
  156. }
  157. return false;
  158. }
  159. return true;
  160. };
  161. //Asynchronous call to Find
  162. fs->Find(uri->get_path(), "*", hdfs::FileSystem::GetDefaultFindMaxDepth(), handlerFind);
  163. }
  164. /* block until promise is set */
  165. hdfs::Status status = future.get();
  166. if (!status.ok()) {
  167. std::cerr << "Error: " << status.ToString() << std::endl;
  168. exit(EXIT_FAILURE);
  169. }
  170. // Clean up static data and prevent valgrind memory leaks
  171. google::protobuf::ShutdownProtobufLibrary();
  172. return 0;
  173. }