1
0

hdfs_chown.cc 7.3 KB

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