filesystem.cc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "filesystem.h"
  19. #include "common/namenode_info.h"
  20. #include <functional>
  21. #include <limits>
  22. #include <future>
  23. #include <tuple>
  24. #include <iostream>
  25. #include <pwd.h>
  26. #include <fnmatch.h>
  27. #define FMT_THIS_ADDR "this=" << (void*)this
  28. namespace hdfs {
  29. static const char kNamenodeProtocol[] =
  30. "org.apache.hadoop.hdfs.protocol.ClientProtocol";
  31. static const int kNamenodeProtocolVersion = 1;
  32. using ::asio::ip::tcp;
  33. static constexpr uint16_t kDefaultPort = 8020;
  34. // forward declarations
  35. const std::string get_effective_user_name(const std::string &);
  36. uint32_t FileSystem::GetDefaultFindMaxDepth() {
  37. return std::numeric_limits<uint32_t>::max();
  38. }
  39. uint16_t FileSystem::GetDefaultPermissionMask() {
  40. return 0755;
  41. }
  42. Status FileSystem::CheckValidPermissionMask(uint16_t permissions) {
  43. if (permissions > 01777) {
  44. std::stringstream errormsg;
  45. errormsg << "CheckValidPermissionMask: argument 'permissions' is " << std::oct
  46. << std::showbase << permissions << " (should be between 0 and 01777)";
  47. return Status::InvalidArgument(errormsg.str().c_str());
  48. }
  49. return Status::OK();
  50. }
  51. Status FileSystem::CheckValidReplication(uint16_t replication) {
  52. if (replication < 1 || replication > 512) {
  53. std::stringstream errormsg;
  54. errormsg << "CheckValidReplication: argument 'replication' is "
  55. << replication << " (should be between 1 and 512)";
  56. return Status::InvalidArgument(errormsg.str().c_str());
  57. }
  58. return Status::OK();
  59. }
  60. /*****************************************************************************
  61. * FILESYSTEM BASE CLASS
  62. ****************************************************************************/
  63. FileSystem *FileSystem::New(
  64. IoService *&io_service, const std::string &user_name, const Options &options) {
  65. return new FileSystemImpl(io_service, user_name, options);
  66. }
  67. FileSystem *FileSystem::New() {
  68. // No, this pointer won't be leaked. The FileSystem takes ownership.
  69. IoService *io_service = IoService::New();
  70. if(!io_service)
  71. return nullptr;
  72. std::string user_name = get_effective_user_name("");
  73. Options options;
  74. return new FileSystemImpl(io_service, user_name, options);
  75. }
  76. /*****************************************************************************
  77. * FILESYSTEM IMPLEMENTATION
  78. ****************************************************************************/
  79. const std::string get_effective_user_name(const std::string &user_name) {
  80. if (!user_name.empty())
  81. return user_name;
  82. // If no user name was provided, try the HADOOP_USER_NAME and USER environment
  83. // variables
  84. const char * env = getenv("HADOOP_USER_NAME");
  85. if (env) {
  86. return env;
  87. }
  88. env = getenv("USER");
  89. if (env) {
  90. return env;
  91. }
  92. // If running on POSIX, use the currently logged in user
  93. #if defined(_POSIX_VERSION)
  94. uid_t uid = geteuid();
  95. struct passwd *pw = getpwuid(uid);
  96. if (pw && pw->pw_name)
  97. {
  98. return pw->pw_name;
  99. }
  100. #endif
  101. return "unknown_user";
  102. }
  103. FileSystemImpl::FileSystemImpl(IoService *&io_service, const std::string &user_name, const Options &options) :
  104. options_(options), client_name_(GetRandomClientName()), io_service_(
  105. static_cast<IoServiceImpl *>(io_service)),
  106. nn_(
  107. &io_service_->io_service(), options, client_name_,
  108. get_effective_user_name(user_name), kNamenodeProtocol,
  109. kNamenodeProtocolVersion
  110. ), bad_node_tracker_(std::make_shared<BadDataNodeTracker>()),
  111. event_handlers_(std::make_shared<LibhdfsEvents>()) {
  112. LOG_TRACE(kFileSystem, << "FileSystemImpl::FileSystemImpl("
  113. << FMT_THIS_ADDR << ") called");
  114. // Poor man's move
  115. io_service = nullptr;
  116. /* spawn background threads for asio delegation */
  117. unsigned int threads = 1 /* options.io_threads_, pending HDFS-9117 */;
  118. for (unsigned int i = 0; i < threads; i++) {
  119. AddWorkerThread();
  120. }
  121. }
  122. FileSystemImpl::~FileSystemImpl() {
  123. LOG_TRACE(kFileSystem, << "FileSystemImpl::~FileSystemImpl("
  124. << FMT_THIS_ADDR << ") called");
  125. /**
  126. * Note: IoService must be stopped before getting rid of worker threads.
  127. * Once worker threads are joined and deleted the service can be deleted.
  128. **/
  129. io_service_->Stop();
  130. worker_threads_.clear();
  131. }
  132. void FileSystemImpl::Connect(const std::string &server,
  133. const std::string &service,
  134. const std::function<void(const Status &, FileSystem * fs)> &handler) {
  135. LOG_INFO(kFileSystem, << "FileSystemImpl::Connect(" << FMT_THIS_ADDR
  136. << ", server=" << server << ", service="
  137. << service << ") called");
  138. /* IoService::New can return nullptr */
  139. if (!io_service_) {
  140. handler (Status::Error("Null IoService"), this);
  141. }
  142. // DNS lookup here for namenode(s)
  143. std::vector<ResolvedNamenodeInfo> resolved_namenodes;
  144. auto name_service = options_.services.find(server);
  145. if(name_service != options_.services.end()) {
  146. cluster_name_ = name_service->first;
  147. resolved_namenodes = BulkResolve(&io_service_->io_service(), name_service->second);
  148. } else {
  149. cluster_name_ = server + ":" + service;
  150. // tmp namenode info just to get this in the right format for BulkResolve
  151. NamenodeInfo tmp_info;
  152. optional<URI> uri = URI::parse_from_string("hdfs://" + cluster_name_);
  153. if(!uri) {
  154. LOG_ERROR(kFileSystem, << "Unable to use URI for cluster " << cluster_name_);
  155. handler(Status::Error(("Invalid namenode " + cluster_name_ + " in config").c_str()), this);
  156. }
  157. tmp_info.uri = uri.value();
  158. resolved_namenodes = BulkResolve(&io_service_->io_service(), {tmp_info});
  159. }
  160. for(unsigned int i=0;i<resolved_namenodes.size();i++) {
  161. LOG_DEBUG(kFileSystem, << "Resolved Namenode");
  162. LOG_DEBUG(kFileSystem, << resolved_namenodes[i].str());
  163. }
  164. nn_.Connect(cluster_name_, /*server, service*/ resolved_namenodes, [this, handler](const Status & s) {
  165. handler(s, this);
  166. });
  167. }
  168. Status FileSystemImpl::Connect(const std::string &server, const std::string &service) {
  169. LOG_INFO(kFileSystem, << "FileSystemImpl::[sync]Connect(" << FMT_THIS_ADDR
  170. << ", server=" << server << ", service=" << service << ") called");
  171. /* synchronized */
  172. auto stat = std::make_shared<std::promise<Status>>();
  173. std::future<Status> future = stat->get_future();
  174. auto callback = [stat](const Status &s, FileSystem *fs) {
  175. (void)fs;
  176. stat->set_value(s);
  177. };
  178. Connect(server, service, callback);
  179. /* block until promise is set */
  180. auto s = future.get();
  181. return s;
  182. }
  183. void FileSystemImpl::ConnectToDefaultFs(const std::function<void(const Status &, FileSystem *)> &handler) {
  184. std::string scheme = options_.defaultFS.get_scheme();
  185. if (strcasecmp(scheme.c_str(), "hdfs") != 0) {
  186. std::string error_message;
  187. error_message += "defaultFS of [" + options_.defaultFS.str() + "] is not supported";
  188. handler(Status::InvalidArgument(error_message.c_str()), nullptr);
  189. return;
  190. }
  191. std::string host = options_.defaultFS.get_host();
  192. if (host.empty()) {
  193. handler(Status::InvalidArgument("defaultFS must specify a hostname"), nullptr);
  194. return;
  195. }
  196. optional<uint16_t> port = options_.defaultFS.get_port();
  197. if (!port) {
  198. port = kDefaultPort;
  199. }
  200. std::string port_as_string = std::to_string(*port);
  201. Connect(host, port_as_string, handler);
  202. }
  203. Status FileSystemImpl::ConnectToDefaultFs() {
  204. auto stat = std::make_shared<std::promise<Status>>();
  205. std::future<Status> future = stat->get_future();
  206. auto callback = [stat](const Status &s, FileSystem *fs) {
  207. (void)fs;
  208. stat->set_value(s);
  209. };
  210. ConnectToDefaultFs(callback);
  211. /* block until promise is set */
  212. auto s = future.get();
  213. return s;
  214. }
  215. int FileSystemImpl::AddWorkerThread() {
  216. LOG_DEBUG(kFileSystem, << "FileSystemImpl::AddWorkerThread("
  217. << FMT_THIS_ADDR << ") called."
  218. << " Existing thread count = " << worker_threads_.size());
  219. auto service_task = [](IoService *service) { service->Run(); };
  220. worker_threads_.push_back(
  221. WorkerPtr(new std::thread(service_task, io_service_.get())));
  222. return worker_threads_.size();
  223. }
  224. void FileSystemImpl::Open(
  225. const std::string &path,
  226. const std::function<void(const Status &, FileHandle *)> &handler) {
  227. LOG_DEBUG(kFileSystem, << "FileSystemImpl::Open("
  228. << FMT_THIS_ADDR << ", path="
  229. << path << ") called");
  230. nn_.GetBlockLocations(path, 0, std::numeric_limits<int64_t>::max(), [this, path, handler](const Status &stat, std::shared_ptr<const struct FileInfo> file_info) {
  231. if(!stat.ok()) {
  232. LOG_DEBUG(kFileSystem, << "FileSystemImpl::Open failed to get block locations. status=" << stat.ToString());
  233. if(stat.get_server_exception_type() == Status::kStandbyException) {
  234. LOG_DEBUG(kFileSystem, << "Operation not allowed on standby datanode");
  235. }
  236. }
  237. handler(stat, stat.ok() ? new FileHandleImpl(cluster_name_, path, &io_service_->io_service(), client_name_, file_info, bad_node_tracker_, event_handlers_)
  238. : nullptr);
  239. });
  240. }
  241. Status FileSystemImpl::Open(const std::string &path,
  242. FileHandle **handle) {
  243. LOG_DEBUG(kFileSystem, << "FileSystemImpl::[sync]Open("
  244. << FMT_THIS_ADDR << ", path="
  245. << path << ") called");
  246. auto callstate = std::make_shared<std::promise<std::tuple<Status, FileHandle*>>>();
  247. std::future<std::tuple<Status, FileHandle*>> future(callstate->get_future());
  248. /* wrap async FileSystem::Open with promise to make it a blocking call */
  249. auto h = [callstate](const Status &s, FileHandle *is) {
  250. callstate->set_value(std::make_tuple(s, is));
  251. };
  252. Open(path, h);
  253. /* block until promise is set */
  254. auto returnstate = future.get();
  255. Status stat = std::get<0>(returnstate);
  256. FileHandle *file_handle = std::get<1>(returnstate);
  257. if (!stat.ok()) {
  258. delete file_handle;
  259. return stat;
  260. }
  261. if (!file_handle) {
  262. return stat;
  263. }
  264. *handle = file_handle;
  265. return stat;
  266. }
  267. BlockLocation LocatedBlockToBlockLocation(const hadoop::hdfs::LocatedBlockProto & locatedBlock)
  268. {
  269. BlockLocation result;
  270. result.setCorrupt(locatedBlock.corrupt());
  271. result.setOffset(locatedBlock.offset());
  272. std::vector<DNInfo> dn_info;
  273. dn_info.reserve(locatedBlock.locs_size());
  274. for (const hadoop::hdfs::DatanodeInfoProto & datanode_info: locatedBlock.locs()) {
  275. const hadoop::hdfs::DatanodeIDProto &id = datanode_info.id();
  276. DNInfo newInfo;
  277. if (id.has_ipaddr())
  278. newInfo.setIPAddr(id.ipaddr());
  279. if (id.has_hostname())
  280. newInfo.setHostname(id.hostname());
  281. if (id.has_xferport())
  282. newInfo.setXferPort(id.xferport());
  283. if (id.has_infoport())
  284. newInfo.setInfoPort(id.infoport());
  285. if (id.has_ipcport())
  286. newInfo.setIPCPort(id.ipcport());
  287. if (id.has_infosecureport())
  288. newInfo.setInfoSecurePort(id.infosecureport());
  289. dn_info.push_back(newInfo);
  290. }
  291. result.setDataNodes(dn_info);
  292. if (locatedBlock.has_b()) {
  293. const hadoop::hdfs::ExtendedBlockProto & b=locatedBlock.b();
  294. result.setLength(b.numbytes());
  295. }
  296. return result;
  297. }
  298. void FileSystemImpl::GetBlockLocations(const std::string & path, uint64_t offset, uint64_t length,
  299. const std::function<void(const Status &, std::shared_ptr<FileBlockLocation> locations)> handler)
  300. {
  301. LOG_DEBUG(kFileSystem, << "FileSystemImpl::GetBlockLocations("
  302. << FMT_THIS_ADDR << ", path="
  303. << path << ") called");
  304. //Protobuf gives an error 'Negative value is not supported'
  305. //if the high bit is set in uint64 in GetBlockLocations
  306. if (IsHighBitSet(offset)) {
  307. handler(Status::InvalidArgument("GetBlockLocations: argument 'offset' cannot have high bit set"), nullptr);
  308. return;
  309. }
  310. if (IsHighBitSet(length)) {
  311. handler(Status::InvalidArgument("GetBlockLocations: argument 'length' cannot have high bit set"), nullptr);
  312. return;
  313. }
  314. auto conversion = [handler](const Status & status, std::shared_ptr<const struct FileInfo> fileInfo) {
  315. if (status.ok()) {
  316. auto result = std::make_shared<FileBlockLocation>();
  317. result->setFileLength(fileInfo->file_length_);
  318. result->setLastBlockComplete(fileInfo->last_block_complete_);
  319. result->setUnderConstruction(fileInfo->under_construction_);
  320. std::vector<BlockLocation> blocks;
  321. for (const hadoop::hdfs::LocatedBlockProto & locatedBlock: fileInfo->blocks_) {
  322. auto newLocation = LocatedBlockToBlockLocation(locatedBlock);
  323. blocks.push_back(newLocation);
  324. }
  325. result->setBlockLocations(blocks);
  326. handler(status, result);
  327. } else {
  328. handler(status, std::shared_ptr<FileBlockLocation>());
  329. }
  330. };
  331. nn_.GetBlockLocations(path, offset, length, conversion);
  332. }
  333. Status FileSystemImpl::GetBlockLocations(const std::string & path, uint64_t offset, uint64_t length,
  334. std::shared_ptr<FileBlockLocation> * fileBlockLocations)
  335. {
  336. LOG_DEBUG(kFileSystem, << "FileSystemImpl::[sync]GetBlockLocations("
  337. << FMT_THIS_ADDR << ", path="
  338. << path << ") called");
  339. if (!fileBlockLocations)
  340. return Status::InvalidArgument("Null pointer passed to GetBlockLocations");
  341. auto callstate = std::make_shared<std::promise<std::tuple<Status, std::shared_ptr<FileBlockLocation>>>>();
  342. std::future<std::tuple<Status, std::shared_ptr<FileBlockLocation>>> future(callstate->get_future());
  343. /* wrap async call with promise/future to make it blocking */
  344. auto callback = [callstate](const Status &s, std::shared_ptr<FileBlockLocation> blockInfo) {
  345. callstate->set_value(std::make_tuple(s,blockInfo));
  346. };
  347. GetBlockLocations(path, offset, length, callback);
  348. /* wait for async to finish */
  349. auto returnstate = future.get();
  350. auto stat = std::get<0>(returnstate);
  351. if (!stat.ok()) {
  352. return stat;
  353. }
  354. *fileBlockLocations = std::get<1>(returnstate);
  355. return stat;
  356. }
  357. void FileSystemImpl::GetPreferredBlockSize(const std::string &path,
  358. const std::function<void(const Status &, const uint64_t &)> &handler) {
  359. LOG_DEBUG(kFileSystem, << "FileSystemImpl::GetPreferredBlockSize("
  360. << FMT_THIS_ADDR << ", path="
  361. << path << ") called");
  362. nn_.GetPreferredBlockSize(path, handler);
  363. }
  364. Status FileSystemImpl::GetPreferredBlockSize(const std::string &path, uint64_t & block_size) {
  365. LOG_DEBUG(kFileSystem, << "FileSystemImpl::[sync]GetPreferredBlockSize("
  366. << FMT_THIS_ADDR << ", path="
  367. << path << ") called");
  368. auto callstate = std::make_shared<std::promise<std::tuple<Status, uint64_t>>>();
  369. std::future<std::tuple<Status, uint64_t>> future(callstate->get_future());
  370. /* wrap async FileSystem::GetPreferredBlockSize with promise to make it a blocking call */
  371. auto h = [callstate](const Status &s, const uint64_t & bsize) {
  372. callstate->set_value(std::make_tuple(s, bsize));
  373. };
  374. GetPreferredBlockSize(path, h);
  375. /* block until promise is set */
  376. auto returnstate = future.get();
  377. Status stat = std::get<0>(returnstate);
  378. uint64_t size = std::get<1>(returnstate);
  379. if (!stat.ok()) {
  380. return stat;
  381. }
  382. block_size = size;
  383. return stat;
  384. }
  385. void FileSystemImpl::SetReplication(const std::string & path, int16_t replication, std::function<void(const Status &)> handler) {
  386. LOG_DEBUG(kFileSystem,
  387. << "FileSystemImpl::SetReplication(" << FMT_THIS_ADDR << ", path=" << path <<
  388. ", replication=" << replication << ") called");
  389. if (path.empty()) {
  390. handler(Status::InvalidArgument("SetReplication: argument 'path' cannot be empty"));
  391. return;
  392. }
  393. Status replStatus = FileSystem::CheckValidReplication(replication);
  394. if (!replStatus.ok()) {
  395. handler(replStatus);
  396. return;
  397. }
  398. nn_.SetReplication(path, replication, handler);
  399. }
  400. Status FileSystemImpl::SetReplication(const std::string & path, int16_t replication) {
  401. LOG_DEBUG(kFileSystem,
  402. << "FileSystemImpl::[sync]SetReplication(" << FMT_THIS_ADDR << ", path=" << path <<
  403. ", replication=" << replication << ") called");
  404. auto callstate = std::make_shared<std::promise<Status>>();
  405. std::future<Status> future(callstate->get_future());
  406. /* wrap async FileSystem::SetReplication with promise to make it a blocking call */
  407. auto h = [callstate](const Status &s) {
  408. callstate->set_value(s);
  409. };
  410. SetReplication(path, replication, h);
  411. /* block until promise is set */
  412. auto returnstate = future.get();
  413. Status stat = returnstate;
  414. return stat;
  415. }
  416. void FileSystemImpl::SetTimes(const std::string & path, uint64_t mtime, uint64_t atime,
  417. std::function<void(const Status &)> handler) {
  418. LOG_DEBUG(kFileSystem,
  419. << "FileSystemImpl::SetTimes(" << FMT_THIS_ADDR << ", path=" << path <<
  420. ", mtime=" << mtime << ", atime=" << atime << ") called");
  421. if (path.empty()) {
  422. handler(Status::InvalidArgument("SetTimes: argument 'path' cannot be empty"));
  423. return;
  424. }
  425. nn_.SetTimes(path, mtime, atime, handler);
  426. }
  427. Status FileSystemImpl::SetTimes(const std::string & path, uint64_t mtime, uint64_t atime) {
  428. LOG_DEBUG(kFileSystem,
  429. << "FileSystemImpl::[sync]SetTimes(" << FMT_THIS_ADDR << ", path=" << path <<
  430. ", mtime=" << mtime << ", atime=" << atime << ") called");
  431. auto callstate = std::make_shared<std::promise<Status>>();
  432. std::future<Status> future(callstate->get_future());
  433. /* wrap async FileSystem::SetTimes with promise to make it a blocking call */
  434. auto h = [callstate](const Status &s) {
  435. callstate->set_value(s);
  436. };
  437. SetTimes(path, mtime, atime, h);
  438. /* block until promise is set */
  439. auto returnstate = future.get();
  440. Status stat = returnstate;
  441. return stat;
  442. }
  443. void FileSystemImpl::GetFileInfo(
  444. const std::string &path,
  445. const std::function<void(const Status &, const StatInfo &)> &handler) {
  446. LOG_DEBUG(kFileSystem, << "FileSystemImpl::GetFileInfo("
  447. << FMT_THIS_ADDR << ", path="
  448. << path << ") called");
  449. nn_.GetFileInfo(path, handler);
  450. }
  451. Status FileSystemImpl::GetFileInfo(const std::string &path,
  452. StatInfo & stat_info) {
  453. LOG_DEBUG(kFileSystem, << "FileSystemImpl::[sync]GetFileInfo("
  454. << FMT_THIS_ADDR << ", path="
  455. << path << ") called");
  456. auto callstate = std::make_shared<std::promise<std::tuple<Status, StatInfo>>>();
  457. std::future<std::tuple<Status, StatInfo>> future(callstate->get_future());
  458. /* wrap async FileSystem::GetFileInfo with promise to make it a blocking call */
  459. auto h = [callstate](const Status &s, const StatInfo &si) {
  460. callstate->set_value(std::make_tuple(s, si));
  461. };
  462. GetFileInfo(path, h);
  463. /* block until promise is set */
  464. auto returnstate = future.get();
  465. Status stat = std::get<0>(returnstate);
  466. StatInfo info = std::get<1>(returnstate);
  467. if (!stat.ok()) {
  468. return stat;
  469. }
  470. stat_info = info;
  471. return stat;
  472. }
  473. void FileSystemImpl::GetFsStats(
  474. const std::function<void(const Status &, const FsInfo &)> &handler) {
  475. LOG_DEBUG(kFileSystem,
  476. << "FileSystemImpl::GetFsStats(" << FMT_THIS_ADDR << ") called");
  477. nn_.GetFsStats(handler);
  478. }
  479. Status FileSystemImpl::GetFsStats(FsInfo & fs_info) {
  480. LOG_DEBUG(kFileSystem,
  481. << "FileSystemImpl::[sync]GetFsStats(" << FMT_THIS_ADDR << ") called");
  482. auto callstate = std::make_shared<std::promise<std::tuple<Status, FsInfo>>>();
  483. std::future<std::tuple<Status, FsInfo>> future(callstate->get_future());
  484. /* wrap async FileSystem::GetFsStats with promise to make it a blocking call */
  485. auto h = [callstate](const Status &s, const FsInfo &si) {
  486. callstate->set_value(std::make_tuple(s, si));
  487. };
  488. GetFsStats(h);
  489. /* block until promise is set */
  490. auto returnstate = future.get();
  491. Status stat = std::get<0>(returnstate);
  492. FsInfo info = std::get<1>(returnstate);
  493. if (!stat.ok()) {
  494. return stat;
  495. }
  496. fs_info = info;
  497. return stat;
  498. }
  499. /**
  500. * Helper function for recursive GetListing calls.
  501. *
  502. * Some compilers don't like recursive lambdas, so we make the lambda call a
  503. * method, which in turn creates a lambda calling itself.
  504. */
  505. void FileSystemImpl::GetListingShim(const Status &stat, const std::vector<StatInfo> & stat_infos, bool has_more,
  506. std::string path, const std::function<bool(const Status &, const std::vector<StatInfo> &, bool)> &handler) {
  507. bool has_next = !stat_infos.empty();
  508. bool get_more = handler(stat, stat_infos, has_more && has_next);
  509. if (get_more && has_more && has_next ) {
  510. auto callback = [this, path, handler](const Status &stat, const std::vector<StatInfo> & stat_infos, bool has_more) {
  511. GetListingShim(stat, stat_infos, has_more, path, handler);
  512. };
  513. std::string last = stat_infos.back().path;
  514. nn_.GetListing(path, callback, last);
  515. }
  516. }
  517. void FileSystemImpl::GetListing(
  518. const std::string &path,
  519. const std::function<bool(const Status &, const std::vector<StatInfo> &, bool)> &handler) {
  520. LOG_DEBUG(kFileSystem, << "FileSystemImpl::GetListing("
  521. << FMT_THIS_ADDR << ", path="
  522. << path << ") called");
  523. // Caputure the state and push it into the shim
  524. auto callback = [this, path, handler](const Status &stat, const std::vector<StatInfo> & stat_infos, bool has_more) {
  525. GetListingShim(stat, stat_infos, has_more, path, handler);
  526. };
  527. nn_.GetListing(path, callback);
  528. }
  529. Status FileSystemImpl::GetListing(const std::string &path, std::vector<StatInfo> * stat_infos) {
  530. LOG_DEBUG(kFileSystem, << "FileSystemImpl::[sync]GetListing("
  531. << FMT_THIS_ADDR << ", path="
  532. << path << ") called");
  533. if (!stat_infos) {
  534. return Status::InvalidArgument("FileSystemImpl::GetListing: argument 'stat_infos' cannot be NULL");
  535. }
  536. auto callstate = std::make_shared<std::promise<Status>>();
  537. std::future<Status> future(callstate->get_future());
  538. /* wrap async FileSystem::GetListing with promise to make it a blocking call.
  539. *
  540. Keep requesting more until we get the entire listing, and don't set the promise
  541. * until we have the entire listing.
  542. */
  543. auto h = [callstate, stat_infos](const Status &s, const std::vector<StatInfo> & si, bool has_more) -> bool {
  544. if (!si.empty()) {
  545. stat_infos->insert(stat_infos->end(), si.begin(), si.end());
  546. }
  547. bool done = !s.ok() || !has_more;
  548. if (done) {
  549. callstate->set_value(s);
  550. return false;
  551. }
  552. return true;
  553. };
  554. GetListing(path, h);
  555. /* block until promise is set */
  556. Status stat = future.get();
  557. return stat;
  558. }
  559. void FileSystemImpl::Mkdirs(const std::string & path, uint16_t permissions, bool createparent,
  560. std::function<void(const Status &)> handler) {
  561. LOG_DEBUG(kFileSystem,
  562. << "FileSystemImpl::Mkdirs(" << FMT_THIS_ADDR << ", path=" << path <<
  563. ", permissions=" << permissions << ", createparent=" << createparent << ") called");
  564. if (path.empty()) {
  565. handler(Status::InvalidArgument("Mkdirs: argument 'path' cannot be empty"));
  566. return;
  567. }
  568. Status permStatus = FileSystem::CheckValidPermissionMask(permissions);
  569. if (!permStatus.ok()) {
  570. handler(permStatus);
  571. return;
  572. }
  573. nn_.Mkdirs(path, permissions, createparent, handler);
  574. }
  575. Status FileSystemImpl::Mkdirs(const std::string & path, uint16_t permissions, bool createparent) {
  576. LOG_DEBUG(kFileSystem,
  577. << "FileSystemImpl::[sync]Mkdirs(" << FMT_THIS_ADDR << ", path=" << path <<
  578. ", permissions=" << permissions << ", createparent=" << createparent << ") called");
  579. auto callstate = std::make_shared<std::promise<Status>>();
  580. std::future<Status> future(callstate->get_future());
  581. /* wrap async FileSystem::Mkdirs with promise to make it a blocking call */
  582. auto h = [callstate](const Status &s) {
  583. callstate->set_value(s);
  584. };
  585. Mkdirs(path, permissions, createparent, h);
  586. /* block until promise is set */
  587. auto returnstate = future.get();
  588. Status stat = returnstate;
  589. return stat;
  590. }
  591. void FileSystemImpl::Delete(const std::string &path, bool recursive,
  592. const std::function<void(const Status &)> &handler) {
  593. LOG_DEBUG(kFileSystem,
  594. << "FileSystemImpl::Delete(" << FMT_THIS_ADDR << ", path=" << path << ", recursive=" << recursive << ") called");
  595. if (path.empty()) {
  596. handler(Status::InvalidArgument("Delete: argument 'path' cannot be empty"));
  597. return;
  598. }
  599. nn_.Delete(path, recursive, handler);
  600. }
  601. Status FileSystemImpl::Delete(const std::string &path, bool recursive) {
  602. LOG_DEBUG(kFileSystem,
  603. << "FileSystemImpl::[sync]Delete(" << FMT_THIS_ADDR << ", path=" << path << ", recursive=" << recursive << ") called");
  604. auto callstate = std::make_shared<std::promise<Status>>();
  605. std::future<Status> future(callstate->get_future());
  606. /* wrap async FileSystem::Delete with promise to make it a blocking call */
  607. auto h = [callstate](const Status &s) {
  608. callstate->set_value(s);
  609. };
  610. Delete(path, recursive, h);
  611. /* block until promise is set */
  612. auto returnstate = future.get();
  613. Status stat = returnstate;
  614. return stat;
  615. }
  616. void FileSystemImpl::Rename(const std::string &oldPath, const std::string &newPath,
  617. const std::function<void(const Status &)> &handler) {
  618. LOG_DEBUG(kFileSystem,
  619. << "FileSystemImpl::Rename(" << FMT_THIS_ADDR << ", oldPath=" << oldPath << ", newPath=" << newPath << ") called");
  620. if (oldPath.empty()) {
  621. handler(Status::InvalidArgument("Rename: argument 'oldPath' cannot be empty"));
  622. return;
  623. }
  624. if (newPath.empty()) {
  625. handler(Status::InvalidArgument("Rename: argument 'newPath' cannot be empty"));
  626. return;
  627. }
  628. nn_.Rename(oldPath, newPath, handler);
  629. }
  630. Status FileSystemImpl::Rename(const std::string &oldPath, const std::string &newPath) {
  631. LOG_DEBUG(kFileSystem,
  632. << "FileSystemImpl::[sync]Rename(" << FMT_THIS_ADDR << ", oldPath=" << oldPath << ", newPath=" << newPath << ") called");
  633. auto callstate = std::make_shared<std::promise<Status>>();
  634. std::future<Status> future(callstate->get_future());
  635. /* wrap async FileSystem::Rename with promise to make it a blocking call */
  636. auto h = [callstate](const Status &s) {
  637. callstate->set_value(s);
  638. };
  639. Rename(oldPath, newPath, h);
  640. /* block until promise is set */
  641. auto returnstate = future.get();
  642. Status stat = returnstate;
  643. return stat;
  644. }
  645. void FileSystemImpl::SetPermission(const std::string & path,
  646. uint16_t permissions, const std::function<void(const Status &)> &handler) {
  647. LOG_DEBUG(kFileSystem,
  648. << "FileSystemImpl::SetPermission(" << FMT_THIS_ADDR << ", path=" << path << ", permissions=" << permissions << ") called");
  649. if (path.empty()) {
  650. handler(Status::InvalidArgument("SetPermission: argument 'path' cannot be empty"));
  651. return;
  652. }
  653. Status permStatus = FileSystem::CheckValidPermissionMask(permissions);
  654. if (!permStatus.ok()) {
  655. handler(permStatus);
  656. return;
  657. }
  658. nn_.SetPermission(path, permissions, handler);
  659. }
  660. Status FileSystemImpl::SetPermission(const std::string & path, uint16_t permissions) {
  661. LOG_DEBUG(kFileSystem,
  662. << "FileSystemImpl::[sync]SetPermission(" << FMT_THIS_ADDR << ", path=" << path << ", permissions=" << permissions << ") called");
  663. auto callstate = std::make_shared<std::promise<Status>>();
  664. std::future<Status> future(callstate->get_future());
  665. /* wrap async FileSystem::SetPermission with promise to make it a blocking call */
  666. auto h = [callstate](const Status &s) {
  667. callstate->set_value(s);
  668. };
  669. SetPermission(path, permissions, h);
  670. /* block until promise is set */
  671. Status stat = future.get();
  672. return stat;
  673. }
  674. void FileSystemImpl::SetOwner(const std::string & path, const std::string & username,
  675. const std::string & groupname, const std::function<void(const Status &)> &handler) {
  676. LOG_DEBUG(kFileSystem,
  677. << "FileSystemImpl::SetOwner(" << FMT_THIS_ADDR << ", path=" << path << ", username=" << username << ", groupname=" << groupname << ") called");
  678. if (path.empty()) {
  679. handler(Status::InvalidArgument("SetOwner: argument 'path' cannot be empty"));
  680. return;
  681. }
  682. nn_.SetOwner(path, username, groupname, handler);
  683. }
  684. Status FileSystemImpl::SetOwner(const std::string & path, const std::string & username,
  685. const std::string & groupname) {
  686. LOG_DEBUG(kFileSystem,
  687. << "FileSystemImpl::[sync]SetOwner(" << FMT_THIS_ADDR << ", path=" << path << ", username=" << username << ", groupname=" << groupname << ") called");
  688. auto callstate = std::make_shared<std::promise<Status>>();
  689. std::future<Status> future(callstate->get_future());
  690. /* wrap async FileSystem::SetOwner with promise to make it a blocking call */
  691. auto h = [callstate](const Status &s) {
  692. callstate->set_value(s);
  693. };
  694. SetOwner(path, username, groupname, h);
  695. /* block until promise is set */
  696. Status stat = future.get();
  697. return stat;
  698. }
  699. /**
  700. * Helper function for recursive Find calls.
  701. *
  702. * Some compilers don't like recursive lambdas, so we make the lambda call a
  703. * method, which in turn creates a lambda calling itself.
  704. *
  705. * ***High-level explanation***
  706. *
  707. * Since we are allowing to use wild cards in both path and name, we start by expanding the path first.
  708. * Boolean search_path is set to true when we search for the path and false when we search for the name.
  709. * When we search for the path we break the given path pattern into sub-directories. Starting from the
  710. * first sub-directory we list them one-by-one and recursively continue into directories that matched the
  711. * path pattern at the current depth. Directories that are large will be requested to continue sending
  712. * the results. We keep track of the current depth within the path pattern in the 'depth' variable.
  713. * This continues recursively until the depth reaches the end of the path. Next that we start matching
  714. * the name pattern. All directories that we find we recurse now, and all names that match the given name
  715. * pattern are being stored in outputs and later sent back to the user.
  716. */
  717. void FileSystemImpl::FindShim(const Status &stat, const std::vector<StatInfo> & stat_infos, bool directory_has_more,
  718. std::shared_ptr<FindOperationalState> operational_state, std::shared_ptr<FindSharedState> shared_state) {
  719. //We buffer the outputs then send them back at the end
  720. std::vector<StatInfo> outputs;
  721. //Return on error
  722. if(!stat.ok()){
  723. std::lock_guard<std::mutex> find_lock(shared_state->lock);
  724. //We send true becuase we do not want the user code to exit before all our requests finished
  725. shared_state->handler(stat, outputs, true);
  726. shared_state->aborted = true;
  727. }
  728. if(!shared_state->aborted){
  729. //User did not abort the operation
  730. if (directory_has_more) {
  731. //Directory is large and has more results
  732. //We launch another async call to get more results
  733. shared_state->outstanding_requests++;
  734. auto callback = [this, operational_state, shared_state](const Status &stat, const std::vector<StatInfo> & stat_infos, bool has_more) {
  735. FindShim(stat, stat_infos, has_more, operational_state, shared_state);
  736. };
  737. std::string last = stat_infos.back().path;
  738. nn_.GetListing(operational_state->path, callback, last);
  739. }
  740. if(operational_state->search_path && operational_state->depth < shared_state->dirs.size() - 1){
  741. //We are searching for the path and did not reach the end of the path yet
  742. for (StatInfo const& si : stat_infos) {
  743. //If we are at the last depth and it matches both path and name, we need to output it.
  744. if (operational_state->depth == shared_state->dirs.size() - 2
  745. && !fnmatch(shared_state->dirs[operational_state->depth + 1].c_str(), si.path.c_str(), 0)
  746. && !fnmatch(shared_state->name.c_str(), si.path.c_str(), 0)) {
  747. outputs.push_back(si);
  748. }
  749. //Skip if not directory
  750. if(si.file_type != StatInfo::IS_DIR) {
  751. continue;
  752. }
  753. //Checking for a match with the path at the current depth
  754. if(!fnmatch(shared_state->dirs[operational_state->depth + 1].c_str(), si.path.c_str(), 0)){
  755. //Launch a new requests for every matched directory
  756. shared_state->outstanding_requests++;
  757. auto callback = [this, si, operational_state, shared_state](const Status &stat, const std::vector<StatInfo> & stat_infos, bool has_more) {
  758. std::shared_ptr<FindOperationalState> new_current_state = std::make_shared<FindOperationalState>(si.full_path, operational_state->depth + 1, true); //true because searching for the path
  759. FindShim(stat, stat_infos, has_more, new_current_state, shared_state);
  760. };
  761. nn_.GetListing(si.full_path, callback);
  762. }
  763. }
  764. }
  765. else if(shared_state->maxdepth > operational_state->depth - shared_state->dirs.size() + 1){
  766. //We are searching for the name now and maxdepth has not been reached
  767. for (StatInfo const& si : stat_infos) {
  768. //Launch a new request for every directory
  769. if(si.file_type == StatInfo::IS_DIR) {
  770. shared_state->outstanding_requests++;
  771. auto callback = [this, si, operational_state, shared_state](const Status &stat, const std::vector<StatInfo> & stat_infos, bool has_more) {
  772. std::shared_ptr<FindOperationalState> new_current_state = std::make_shared<FindOperationalState>(si.full_path, operational_state->depth + 1, false); //false because searching for the name
  773. FindShim(stat, stat_infos, has_more, new_current_state, shared_state);
  774. };
  775. nn_.GetListing(si.full_path, callback);
  776. }
  777. //All names that match the specified name are saved to outputs
  778. if(!fnmatch(shared_state->name.c_str(), si.path.c_str(), 0)){
  779. outputs.push_back(si);
  780. }
  781. }
  782. }
  783. }
  784. //This section needs a lock to make sure we return the final chunk only once
  785. //and no results are sent after aborted is set
  786. std::lock_guard<std::mutex> find_lock(shared_state->lock);
  787. //Decrement the counter once since we are done with this chunk
  788. shared_state->outstanding_requests--;
  789. if(shared_state->outstanding_requests == 0){
  790. //Send the outputs back to the user and notify that this is the final chunk
  791. shared_state->handler(stat, outputs, false);
  792. } else {
  793. //There will be more results and we are not aborting
  794. if (outputs.size() > 0 && !shared_state->aborted){
  795. //Send the outputs back to the user and notify that there is more
  796. bool user_wants_more = shared_state->handler(stat, outputs, true);
  797. if(!user_wants_more) {
  798. //Abort if user doesn't want more
  799. shared_state->aborted = true;
  800. }
  801. }
  802. }
  803. }
  804. void FileSystemImpl::Find(
  805. const std::string &path, const std::string &name, const uint32_t maxdepth,
  806. const std::function<bool(const Status &, const std::vector<StatInfo> &, bool)> &handler) {
  807. LOG_DEBUG(kFileSystem, << "FileSystemImpl::Find("
  808. << FMT_THIS_ADDR << ", path="
  809. << path << ", name="
  810. << name << ") called");
  811. //Populating the operational state, which includes:
  812. //current search path, depth within the path, and the indication that we are currently searching for a path (not name yet).
  813. std::shared_ptr<FindOperationalState> operational_state = std::make_shared<FindOperationalState>(path, 0, true);
  814. //Populating the shared state, which includes:
  815. //vector of sub-directories constructed from path, name to search, handler to use for result returning, outstanding_requests counter, and aborted flag.
  816. std::shared_ptr<FindSharedState> shared_state = std::make_shared<FindSharedState>(path, name, maxdepth, handler, 1, false);
  817. auto callback = [this, operational_state, shared_state](const Status &stat, const std::vector<StatInfo> & stat_infos, bool directory_has_more) {
  818. FindShim(stat, stat_infos, directory_has_more, operational_state, shared_state);
  819. };
  820. nn_.GetListing("/", callback);
  821. }
  822. Status FileSystemImpl::Find(const std::string &path, const std::string &name, const uint32_t maxdepth, std::vector<StatInfo> * stat_infos) {
  823. LOG_DEBUG(kFileSystem, << "FileSystemImpl::[sync]Find("
  824. << FMT_THIS_ADDR << ", path="
  825. << path << ", name="
  826. << name << ") called");
  827. if (!stat_infos) {
  828. return Status::InvalidArgument("FileSystemImpl::Find: argument 'stat_infos' cannot be NULL");
  829. }
  830. // In this case, we're going to have the async code populate stat_infos.
  831. std::promise<void> promise = std::promise<void>();
  832. std::future<void> future(promise.get_future());
  833. Status status = Status::OK();
  834. /**
  835. * Keep requesting more until we get the entire listing. Set the promise
  836. * when we have the entire listing to stop.
  837. *
  838. * Find guarantees that the handler will only be called once at a time,
  839. * so we do not need any locking here
  840. */
  841. auto h = [&status, &promise, stat_infos](const Status &s, const std::vector<StatInfo> & si, bool has_more_results) -> bool {
  842. if (!si.empty()) {
  843. stat_infos->insert(stat_infos->end(), si.begin(), si.end());
  844. }
  845. if (!s.ok() && status.ok()){
  846. //We make sure we set 'status' only on the first error.
  847. status = s;
  848. }
  849. if (!has_more_results) {
  850. promise.set_value();
  851. return false;
  852. }
  853. return true;
  854. };
  855. Find(path, name, maxdepth, h);
  856. /* block until promise is set */
  857. future.get();
  858. return status;
  859. }
  860. void FileSystemImpl::CreateSnapshot(const std::string &path,
  861. const std::string &name,
  862. const std::function<void(const Status &)> &handler) {
  863. LOG_DEBUG(kFileSystem,
  864. << "FileSystemImpl::CreateSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ", name=" << name << ") called");
  865. if (path.empty()) {
  866. handler(Status::InvalidArgument("CreateSnapshot: argument 'path' cannot be empty"));
  867. return;
  868. }
  869. nn_.CreateSnapshot(path, name, handler);
  870. }
  871. Status FileSystemImpl::CreateSnapshot(const std::string &path,
  872. const std::string &name) {
  873. LOG_DEBUG(kFileSystem,
  874. << "FileSystemImpl::[sync]CreateSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ", name=" << name << ") called");
  875. auto callstate = std::make_shared<std::promise<Status>>();
  876. std::future<Status> future(callstate->get_future());
  877. /* wrap async FileSystem::CreateSnapshot with promise to make it a blocking call */
  878. auto h = [callstate](const Status &s) {
  879. callstate->set_value(s);
  880. };
  881. CreateSnapshot(path, name, h);
  882. /* block until promise is set */
  883. auto returnstate = future.get();
  884. Status stat = returnstate;
  885. return stat;
  886. }
  887. void FileSystemImpl::DeleteSnapshot(const std::string &path,
  888. const std::string &name,
  889. const std::function<void(const Status &)> &handler) {
  890. LOG_DEBUG(kFileSystem,
  891. << "FileSystemImpl::DeleteSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ", name=" << name << ") called");
  892. if (path.empty()) {
  893. handler(Status::InvalidArgument("DeleteSnapshot: argument 'path' cannot be empty"));
  894. return;
  895. }
  896. if (name.empty()) {
  897. handler(Status::InvalidArgument("DeleteSnapshot: argument 'name' cannot be empty"));
  898. return;
  899. }
  900. nn_.DeleteSnapshot(path, name, handler);
  901. }
  902. Status FileSystemImpl::DeleteSnapshot(const std::string &path,
  903. const std::string &name) {
  904. LOG_DEBUG(kFileSystem,
  905. << "FileSystemImpl::[sync]DeleteSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ", name=" << name << ") called");
  906. auto callstate = std::make_shared<std::promise<Status>>();
  907. std::future<Status> future(callstate->get_future());
  908. /* wrap async FileSystem::DeleteSnapshot with promise to make it a blocking call */
  909. auto h = [callstate](const Status &s) {
  910. callstate->set_value(s);
  911. };
  912. DeleteSnapshot(path, name, h);
  913. /* block until promise is set */
  914. auto returnstate = future.get();
  915. Status stat = returnstate;
  916. return stat;
  917. }
  918. void FileSystemImpl::AllowSnapshot(const std::string &path,
  919. const std::function<void(const Status &)> &handler) {
  920. LOG_DEBUG(kFileSystem,
  921. << "FileSystemImpl::AllowSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ") called");
  922. if (path.empty()) {
  923. handler(Status::InvalidArgument("AllowSnapshot: argument 'path' cannot be empty"));
  924. return;
  925. }
  926. nn_.AllowSnapshot(path, handler);
  927. }
  928. Status FileSystemImpl::AllowSnapshot(const std::string &path) {
  929. LOG_DEBUG(kFileSystem,
  930. << "FileSystemImpl::[sync]AllowSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ") called");
  931. auto callstate = std::make_shared<std::promise<Status>>();
  932. std::future<Status> future(callstate->get_future());
  933. /* wrap async FileSystem::AllowSnapshot with promise to make it a blocking call */
  934. auto h = [callstate](const Status &s) {
  935. callstate->set_value(s);
  936. };
  937. AllowSnapshot(path, h);
  938. /* block until promise is set */
  939. auto returnstate = future.get();
  940. Status stat = returnstate;
  941. return stat;
  942. }
  943. void FileSystemImpl::DisallowSnapshot(const std::string &path,
  944. const std::function<void(const Status &)> &handler) {
  945. LOG_DEBUG(kFileSystem,
  946. << "FileSystemImpl::DisallowSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ") called");
  947. if (path.empty()) {
  948. handler(Status::InvalidArgument("DisallowSnapshot: argument 'path' cannot be empty"));
  949. return;
  950. }
  951. nn_.DisallowSnapshot(path, handler);
  952. }
  953. Status FileSystemImpl::DisallowSnapshot(const std::string &path) {
  954. LOG_DEBUG(kFileSystem,
  955. << "FileSystemImpl::[sync]DisallowSnapshot(" << FMT_THIS_ADDR << ", path=" << path << ") called");
  956. auto callstate = std::make_shared<std::promise<Status>>();
  957. std::future<Status> future(callstate->get_future());
  958. /* wrap async FileSystem::DisallowSnapshot with promise to make it a blocking call */
  959. auto h = [callstate](const Status &s) {
  960. callstate->set_value(s);
  961. };
  962. DisallowSnapshot(path, h);
  963. /* block until promise is set */
  964. auto returnstate = future.get();
  965. Status stat = returnstate;
  966. return stat;
  967. }
  968. void FileSystemImpl::WorkerDeleter::operator()(std::thread *t) {
  969. // It is far too easy to destroy the filesystem (and thus the threadpool)
  970. // from within one of the worker threads, leading to a deadlock. Let's
  971. // provide some explicit protection.
  972. if(t->get_id() == std::this_thread::get_id()) {
  973. LOG_ERROR(kFileSystem, << "FileSystemImpl::WorkerDeleter::operator(treadptr="
  974. << t << ") : FATAL: Attempted to destroy a thread pool"
  975. "from within a callback of the thread pool!");
  976. }
  977. t->join();
  978. delete t;
  979. }
  980. void FileSystemImpl::SetFsEventCallback(fs_event_callback callback) {
  981. if (event_handlers_) {
  982. event_handlers_->set_fs_callback(callback);
  983. nn_.SetFsEventCallback(callback);
  984. }
  985. }
  986. std::shared_ptr<LibhdfsEvents> FileSystemImpl::get_event_handlers() {
  987. return event_handlers_;
  988. }
  989. Options FileSystemImpl::get_options() {
  990. return options_;
  991. }
  992. }