hdfs.cc 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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 "hdfspp/hdfspp.h"
  19. #include "fs/filesystem.h"
  20. #include "common/hdfs_configuration.h"
  21. #include "common/configuration_loader.h"
  22. #include "common/logging.h"
  23. #include <hdfs/hdfs.h>
  24. #include <hdfspp/hdfs_ext.h>
  25. #include <libgen.h>
  26. #include "limits.h"
  27. #include <string>
  28. #include <cstring>
  29. #include <iostream>
  30. #include <algorithm>
  31. #include <functional>
  32. using namespace hdfs;
  33. using std::experimental::nullopt;
  34. using namespace std::placeholders;
  35. static constexpr tPort kDefaultPort = 8020;
  36. /* Separate the handles used by the C api from the C++ API*/
  37. struct hdfs_internal {
  38. hdfs_internal(FileSystem *p) : filesystem_(p) {}
  39. hdfs_internal(std::unique_ptr<FileSystem> p)
  40. : filesystem_(std::move(p)) {}
  41. virtual ~hdfs_internal(){};
  42. FileSystem *get_impl() { return filesystem_.get(); }
  43. const FileSystem *get_impl() const { return filesystem_.get(); }
  44. private:
  45. std::unique_ptr<FileSystem> filesystem_;
  46. };
  47. struct hdfsFile_internal {
  48. hdfsFile_internal(FileHandle *p) : file_(p) {}
  49. hdfsFile_internal(std::unique_ptr<FileHandle> p) : file_(std::move(p)) {}
  50. virtual ~hdfsFile_internal(){};
  51. FileHandle *get_impl() { return file_.get(); }
  52. const FileHandle *get_impl() const { return file_.get(); }
  53. private:
  54. std::unique_ptr<FileHandle> file_;
  55. };
  56. /* Keep thread local copy of last error string */
  57. thread_local std::string errstr;
  58. /* Fetch last error that happened in this thread */
  59. void hdfsGetLastError(char *buf, int len) {
  60. if(nullptr == buf || len < 1) {
  61. return;
  62. }
  63. /* leave space for a trailing null */
  64. size_t copylen = std::min((size_t)errstr.size(), (size_t)len);
  65. if(copylen == (size_t)len) {
  66. copylen--;
  67. }
  68. strncpy(buf, errstr.c_str(), copylen);
  69. /* stick in null */
  70. buf[copylen] = 0;
  71. }
  72. /* Event callbacks for next open calls */
  73. thread_local std::experimental::optional<fs_event_callback> fsEventCallback;
  74. thread_local std::experimental::optional<file_event_callback> fileEventCallback;
  75. struct hdfsBuilder {
  76. hdfsBuilder();
  77. hdfsBuilder(const char * directory);
  78. virtual ~hdfsBuilder() {}
  79. ConfigurationLoader loader;
  80. HdfsConfiguration config;
  81. optional<std::string> overrideHost;
  82. optional<tPort> overridePort;
  83. optional<std::string> user;
  84. static constexpr tPort kUseDefaultPort = 0;
  85. };
  86. /* Error handling with optional debug to stderr */
  87. static void ReportError(int errnum, const std::string & msg) {
  88. errno = errnum;
  89. errstr = msg;
  90. #ifdef LIBHDFSPP_C_API_ENABLE_DEBUG
  91. std::cerr << "Error: errno=" << strerror(errnum) << " message=\"" << msg
  92. << "\"" << std::endl;
  93. #else
  94. (void)msg;
  95. #endif
  96. }
  97. /* Convert Status wrapped error into appropriate errno and return code */
  98. static int Error(const Status &stat) {
  99. const char * default_message;
  100. int errnum;
  101. int code = stat.code();
  102. switch (code) {
  103. case Status::Code::kOk:
  104. return 0;
  105. case Status::Code::kInvalidArgument:
  106. errnum = EINVAL;
  107. default_message = "Invalid argument";
  108. break;
  109. case Status::Code::kResourceUnavailable:
  110. errnum = EAGAIN;
  111. default_message = "Resource temporarily unavailable";
  112. break;
  113. case Status::Code::kUnimplemented:
  114. errnum = ENOSYS;
  115. default_message = "Function not implemented";
  116. break;
  117. case Status::Code::kException:
  118. errnum = EINTR;
  119. default_message = "Exception raised";
  120. break;
  121. case Status::Code::kOperationCanceled:
  122. errnum = EINTR;
  123. default_message = "Operation canceled";
  124. break;
  125. case Status::Code::kPermissionDenied:
  126. errnum = EACCES;
  127. default_message = "Permission denied";
  128. break;
  129. case Status::Code::kPathNotFound:
  130. errnum = ENOENT;
  131. default_message = "No such file or directory";
  132. break;
  133. case Status::Code::kNotADirectory:
  134. errnum = ENOTDIR;
  135. default_message = "Not a directory";
  136. break;
  137. default:
  138. errnum = ENOSYS;
  139. default_message = "Error: unrecognised code";
  140. }
  141. if (stat.ToString().empty())
  142. ReportError(errnum, default_message);
  143. else
  144. ReportError(errnum, stat.ToString());
  145. return -1;
  146. }
  147. static int ReportException(const std::exception & e)
  148. {
  149. return Error(Status::Exception("Uncaught exception", e.what()));
  150. }
  151. static int ReportCaughtNonException()
  152. {
  153. return Error(Status::Exception("Uncaught value not derived from std::exception", ""));
  154. }
  155. bool CheckSystem(hdfsFS fs) {
  156. if (!fs) {
  157. ReportError(ENODEV, "Cannot perform FS operations with null FS handle.");
  158. return false;
  159. }
  160. return true;
  161. }
  162. /* return false on failure */
  163. bool CheckSystemAndHandle(hdfsFS fs, hdfsFile file) {
  164. if (!CheckSystem(fs))
  165. return false;
  166. if (!file) {
  167. ReportError(EBADF, "Cannot perform FS operations with null File handle.");
  168. return false;
  169. }
  170. return true;
  171. }
  172. /**
  173. * C API implementations
  174. **/
  175. int hdfsFileIsOpenForRead(hdfsFile file) {
  176. /* files can only be open for reads at the moment, do a quick check */
  177. if (file) {
  178. return 1; // Update implementation when we get file writing
  179. }
  180. return 0;
  181. }
  182. hdfsFS doHdfsConnect(optional<std::string> nn, optional<tPort> port, optional<std::string> user, const Options & options) {
  183. try
  184. {
  185. IoService * io_service = IoService::New();
  186. FileSystem *fs = FileSystem::New(io_service, user.value_or(""), options);
  187. if (!fs) {
  188. ReportError(ENODEV, "Could not create FileSystem object");
  189. return nullptr;
  190. }
  191. if (fsEventCallback) {
  192. fs->SetFsEventCallback(fsEventCallback.value());
  193. }
  194. Status status;
  195. if (nn || port) {
  196. if (!port) {
  197. port = kDefaultPort;
  198. }
  199. std::string port_as_string = std::to_string(*port);
  200. status = fs->Connect(nn.value_or(""), port_as_string);
  201. } else {
  202. status = fs->ConnectToDefaultFs();
  203. }
  204. if (!status.ok()) {
  205. Error(status);
  206. // FileSystem's ctor might take ownership of the io_service; if it does,
  207. // it will null out the pointer
  208. if (io_service)
  209. delete io_service;
  210. delete fs;
  211. return nullptr;
  212. }
  213. return new hdfs_internal(fs);
  214. } catch (const std::exception & e) {
  215. ReportException(e);
  216. return nullptr;
  217. } catch (...) {
  218. ReportCaughtNonException();
  219. return nullptr;
  220. }
  221. }
  222. hdfsFS hdfsConnect(const char *nn, tPort port) {
  223. return hdfsConnectAsUser(nn, port, "");
  224. }
  225. hdfsFS hdfsConnectAsUser(const char* nn, tPort port, const char *user) {
  226. return doHdfsConnect(std::string(nn), port, std::string(user), Options());
  227. }
  228. int hdfsDisconnect(hdfsFS fs) {
  229. try
  230. {
  231. if (!fs) {
  232. ReportError(ENODEV, "Cannot disconnect null FS handle.");
  233. return -1;
  234. }
  235. delete fs;
  236. return 0;
  237. } catch (const std::exception & e) {
  238. return ReportException(e);
  239. } catch (...) {
  240. return ReportCaughtNonException();
  241. }
  242. }
  243. hdfsFile hdfsOpenFile(hdfsFS fs, const char *path, int flags, int bufferSize,
  244. short replication, tSize blocksize) {
  245. try
  246. {
  247. (void)flags;
  248. (void)bufferSize;
  249. (void)replication;
  250. (void)blocksize;
  251. if (!fs) {
  252. ReportError(ENODEV, "Cannot perform FS operations with null FS handle.");
  253. return nullptr;
  254. }
  255. FileHandle *f = nullptr;
  256. Status stat = fs->get_impl()->Open(path, &f);
  257. if (!stat.ok()) {
  258. Error(stat);
  259. return nullptr;
  260. }
  261. return new hdfsFile_internal(f);
  262. } catch (const std::exception & e) {
  263. ReportException(e);
  264. return nullptr;
  265. } catch (...) {
  266. ReportCaughtNonException();
  267. return nullptr;
  268. }
  269. }
  270. int hdfsCloseFile(hdfsFS fs, hdfsFile file) {
  271. try
  272. {
  273. if (!CheckSystemAndHandle(fs, file)) {
  274. return -1;
  275. }
  276. delete file;
  277. return 0;
  278. } catch (const std::exception & e) {
  279. return ReportException(e);
  280. } catch (...) {
  281. return ReportCaughtNonException();
  282. }
  283. }
  284. tOffset hdfsGetCapacity(hdfsFS fs) {
  285. try {
  286. errno = 0;
  287. if (!CheckSystem(fs)) {
  288. return -1;
  289. }
  290. hdfs::FsInfo fs_info;
  291. Status stat = fs->get_impl()->GetFsStats(fs_info);
  292. if (!stat.ok()) {
  293. Error(stat);
  294. return -1;
  295. }
  296. return fs_info.capacity;
  297. } catch (const std::exception & e) {
  298. ReportException(e);
  299. return -1;
  300. } catch (...) {
  301. ReportCaughtNonException();
  302. return -1;
  303. }
  304. }
  305. tOffset hdfsGetUsed(hdfsFS fs) {
  306. try {
  307. errno = 0;
  308. if (!CheckSystem(fs)) {
  309. return -1;
  310. }
  311. hdfs::FsInfo fs_info;
  312. Status stat = fs->get_impl()->GetFsStats(fs_info);
  313. if (!stat.ok()) {
  314. Error(stat);
  315. return -1;
  316. }
  317. return fs_info.used;
  318. } catch (const std::exception & e) {
  319. ReportException(e);
  320. return -1;
  321. } catch (...) {
  322. ReportCaughtNonException();
  323. return -1;
  324. }
  325. }
  326. void StatInfoToHdfsFileInfo(hdfsFileInfo * file_info,
  327. const hdfs::StatInfo & stat_info) {
  328. /* file or directory */
  329. if (stat_info.file_type == StatInfo::IS_DIR) {
  330. file_info->mKind = kObjectKindDirectory;
  331. } else if (stat_info.file_type == StatInfo::IS_FILE) {
  332. file_info->mKind = kObjectKindFile;
  333. } else {
  334. file_info->mKind = kObjectKindFile;
  335. LOG_WARN(kFileSystem, << "Symlink is not supported! Reporting as a file: ");
  336. }
  337. /* the name of the file */
  338. char copyOfPath[PATH_MAX];
  339. strncpy(copyOfPath, stat_info.path.c_str(), PATH_MAX);
  340. copyOfPath[PATH_MAX - 1] = '\0'; // in case strncpy ran out of space
  341. char * mName = basename(copyOfPath);
  342. size_t mName_size = strlen(mName);
  343. file_info->mName = new char[mName_size+1];
  344. strncpy(file_info->mName, basename(copyOfPath), mName_size + 1);
  345. /* the last modification time for the file in seconds */
  346. file_info->mLastMod = (tTime) stat_info.modification_time;
  347. /* the size of the file in bytes */
  348. file_info->mSize = (tOffset) stat_info.length;
  349. /* the count of replicas */
  350. file_info->mReplication = (short) stat_info.block_replication;
  351. /* the block size for the file */
  352. file_info->mBlockSize = (tOffset) stat_info.blocksize;
  353. /* the owner of the file */
  354. file_info->mOwner = new char[stat_info.owner.size() + 1];
  355. strncpy(file_info->mOwner, stat_info.owner.c_str(), stat_info.owner.size() + 1);
  356. /* the group associated with the file */
  357. file_info->mGroup = new char[stat_info.group.size() + 1];
  358. strncpy(file_info->mGroup, stat_info.group.c_str(), stat_info.group.size() + 1);
  359. /* the permissions associated with the file encoded as an octal number (0777)*/
  360. file_info->mPermissions = (short) stat_info.permissions;
  361. /* the last access time for the file in seconds since the epoch*/
  362. file_info->mLastAccess = stat_info.access_time;
  363. }
  364. hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path) {
  365. try {
  366. if (!CheckSystem(fs)) {
  367. return nullptr;
  368. }
  369. hdfs::StatInfo stat_info;
  370. Status stat = fs->get_impl()->GetFileInfo(path, stat_info);
  371. if (!stat.ok()) {
  372. Error(stat);
  373. return nullptr;
  374. }
  375. hdfsFileInfo *file_info = new hdfsFileInfo[1];
  376. StatInfoToHdfsFileInfo(file_info, stat_info);
  377. return file_info;
  378. } catch (const std::exception & e) {
  379. ReportException(e);
  380. return nullptr;
  381. } catch (...) {
  382. ReportCaughtNonException();
  383. return nullptr;
  384. }
  385. }
  386. hdfsFileInfo *hdfsListDirectory(hdfsFS fs, const char* path, int *numEntries) {
  387. try {
  388. if (!CheckSystem(fs)) {
  389. *numEntries = 0;
  390. return nullptr;
  391. }
  392. std::shared_ptr<std::vector<StatInfo>> stat_infos;
  393. Status stat = fs->get_impl()->GetListing(path, stat_infos);
  394. if (!stat.ok()) {
  395. Error(stat);
  396. *numEntries = 0;
  397. return nullptr;
  398. }
  399. //Existing API expects nullptr if size is 0
  400. if(!stat_infos || stat_infos->size()==0){
  401. *numEntries = 0;
  402. return nullptr;
  403. }
  404. *numEntries = stat_infos->size();
  405. hdfsFileInfo *file_infos = new hdfsFileInfo[stat_infos->size()];
  406. for(std::vector<StatInfo>::size_type i = 0; i < stat_infos->size(); i++) {
  407. StatInfoToHdfsFileInfo(&file_infos[i], stat_infos->at(i));
  408. }
  409. return file_infos;
  410. } catch (const std::exception & e) {
  411. ReportException(e);
  412. *numEntries = 0;
  413. return nullptr;
  414. } catch (...) {
  415. ReportCaughtNonException();
  416. *numEntries = 0;
  417. return nullptr;
  418. }
  419. }
  420. void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries)
  421. {
  422. int i;
  423. for (i = 0; i < numEntries; ++i) {
  424. delete[] hdfsFileInfo[i].mName;
  425. delete[] hdfsFileInfo[i].mOwner;
  426. delete[] hdfsFileInfo[i].mGroup;
  427. }
  428. delete[] hdfsFileInfo;
  429. }
  430. int hdfsCreateSnapshot(hdfsFS fs, const char* path, const char* name) {
  431. try {
  432. errno = 0;
  433. if (!CheckSystem(fs)) {
  434. return -1;
  435. }
  436. if (!path) {
  437. return Error(Status::InvalidArgument("Argument 'path' cannot be NULL"));
  438. }
  439. Status stat;
  440. if(!name){
  441. stat = fs->get_impl()->CreateSnapshot(path, "");
  442. } else {
  443. stat = fs->get_impl()->CreateSnapshot(path, name);
  444. }
  445. if (!stat.ok()) {
  446. return Error(stat);
  447. }
  448. return 0;
  449. } catch (const std::exception & e) {
  450. return ReportException(e);
  451. } catch (...) {
  452. return ReportCaughtNonException();
  453. }
  454. }
  455. int hdfsDeleteSnapshot(hdfsFS fs, const char* path, const char* name) {
  456. try {
  457. errno = 0;
  458. if (!CheckSystem(fs)) {
  459. return -1;
  460. }
  461. if (!path) {
  462. return Error(Status::InvalidArgument("Argument 'path' cannot be NULL"));
  463. }
  464. if (!name) {
  465. return Error(Status::InvalidArgument("Argument 'name' cannot be NULL"));
  466. }
  467. Status stat;
  468. stat = fs->get_impl()->DeleteSnapshot(path, name);
  469. if (!stat.ok()) {
  470. return Error(stat);
  471. }
  472. return 0;
  473. } catch (const std::exception & e) {
  474. return ReportException(e);
  475. } catch (...) {
  476. return ReportCaughtNonException();
  477. }
  478. }
  479. int hdfsAllowSnapshot(hdfsFS fs, const char* path) {
  480. try {
  481. errno = 0;
  482. if (!CheckSystem(fs)) {
  483. return -1;
  484. }
  485. if (!path) {
  486. return Error(Status::InvalidArgument("Argument 'path' cannot be NULL"));
  487. }
  488. Status stat;
  489. stat = fs->get_impl()->AllowSnapshot(path);
  490. if (!stat.ok()) {
  491. return Error(stat);
  492. }
  493. return 0;
  494. } catch (const std::exception & e) {
  495. return ReportException(e);
  496. } catch (...) {
  497. return ReportCaughtNonException();
  498. }
  499. }
  500. int hdfsDisallowSnapshot(hdfsFS fs, const char* path) {
  501. try {
  502. errno = 0;
  503. if (!CheckSystem(fs)) {
  504. return -1;
  505. }
  506. if (!path) {
  507. return Error(Status::InvalidArgument("Argument 'path' cannot be NULL"));
  508. }
  509. Status stat;
  510. stat = fs->get_impl()->DisallowSnapshot(path);
  511. if (!stat.ok()) {
  512. return Error(stat);
  513. }
  514. return 0;
  515. } catch (const std::exception & e) {
  516. return ReportException(e);
  517. } catch (...) {
  518. return ReportCaughtNonException();
  519. }
  520. }
  521. tSize hdfsPread(hdfsFS fs, hdfsFile file, tOffset position, void *buffer,
  522. tSize length) {
  523. try
  524. {
  525. if (!CheckSystemAndHandle(fs, file)) {
  526. return -1;
  527. }
  528. size_t len = length;
  529. Status stat = file->get_impl()->PositionRead(buffer, &len, position);
  530. if(!stat.ok()) {
  531. return Error(stat);
  532. }
  533. return (tSize)len;
  534. } catch (const std::exception & e) {
  535. return ReportException(e);
  536. } catch (...) {
  537. return ReportCaughtNonException();
  538. }
  539. }
  540. tSize hdfsRead(hdfsFS fs, hdfsFile file, void *buffer, tSize length) {
  541. try
  542. {
  543. if (!CheckSystemAndHandle(fs, file)) {
  544. return -1;
  545. }
  546. size_t len = length;
  547. Status stat = file->get_impl()->Read(buffer, &len);
  548. if (!stat.ok()) {
  549. return Error(stat);
  550. }
  551. return (tSize)len;
  552. } catch (const std::exception & e) {
  553. return ReportException(e);
  554. } catch (...) {
  555. return ReportCaughtNonException();
  556. }
  557. }
  558. /* 0 on success, -1 on error*/
  559. int hdfsSeek(hdfsFS fs, hdfsFile file, tOffset desiredPos) {
  560. try
  561. {
  562. if (!CheckSystemAndHandle(fs, file)) {
  563. return -1;
  564. }
  565. off_t desired = desiredPos;
  566. Status stat = file->get_impl()->Seek(&desired, std::ios_base::beg);
  567. if (!stat.ok()) {
  568. return Error(stat);
  569. }
  570. return 0;
  571. } catch (const std::exception & e) {
  572. return ReportException(e);
  573. } catch (...) {
  574. return ReportCaughtNonException();
  575. }
  576. }
  577. tOffset hdfsTell(hdfsFS fs, hdfsFile file) {
  578. try
  579. {
  580. if (!CheckSystemAndHandle(fs, file)) {
  581. return -1;
  582. }
  583. ssize_t offset = 0;
  584. Status stat = file->get_impl()->Seek(&offset, std::ios_base::cur);
  585. if (!stat.ok()) {
  586. return Error(stat);
  587. }
  588. return offset;
  589. } catch (const std::exception & e) {
  590. return ReportException(e);
  591. } catch (...) {
  592. return ReportCaughtNonException();
  593. }
  594. }
  595. /* extended API */
  596. int hdfsCancel(hdfsFS fs, hdfsFile file) {
  597. try
  598. {
  599. if (!CheckSystemAndHandle(fs, file)) {
  600. return -1;
  601. }
  602. static_cast<FileHandleImpl*>(file->get_impl())->CancelOperations();
  603. return 0;
  604. } catch (const std::exception & e) {
  605. return ReportException(e);
  606. } catch (...) {
  607. return ReportCaughtNonException();
  608. }
  609. }
  610. int hdfsGetBlockLocations(hdfsFS fs, const char *path, struct hdfsBlockLocations ** locations_out)
  611. {
  612. try
  613. {
  614. if (!CheckSystem(fs)) {
  615. return -1;
  616. }
  617. if (locations_out == nullptr) {
  618. ReportError(EINVAL, "Null pointer passed to hdfsGetBlockLocations");
  619. return -2;
  620. }
  621. std::shared_ptr<FileBlockLocation> ppLocations;
  622. Status stat = fs->get_impl()->GetBlockLocations(path, &ppLocations);
  623. if (!stat.ok()) {
  624. return Error(stat);
  625. }
  626. hdfsBlockLocations *locations = new struct hdfsBlockLocations();
  627. (*locations_out) = locations;
  628. bzero(locations, sizeof(*locations));
  629. locations->fileLength = ppLocations->getFileLength();
  630. locations->isLastBlockComplete = ppLocations->isLastBlockComplete();
  631. locations->isUnderConstruction = ppLocations->isUnderConstruction();
  632. const std::vector<BlockLocation> & ppBlockLocations = ppLocations->getBlockLocations();
  633. locations->num_blocks = ppBlockLocations.size();
  634. locations->blocks = new struct hdfsBlockInfo[locations->num_blocks];
  635. for (size_t i=0; i < ppBlockLocations.size(); i++) {
  636. auto ppBlockLocation = ppBlockLocations[i];
  637. auto block = &locations->blocks[i];
  638. block->num_bytes = ppBlockLocation.getLength();
  639. block->start_offset = ppBlockLocation.getOffset();
  640. const std::vector<DNInfo> & ppDNInfos = ppBlockLocation.getDataNodes();
  641. block->num_locations = ppDNInfos.size();
  642. block->locations = new hdfsDNInfo[block->num_locations];
  643. for (size_t j=0; j < block->num_locations; j++) {
  644. auto ppDNInfo = ppDNInfos[j];
  645. auto dn_info = &block->locations[j];
  646. dn_info->xfer_port = ppDNInfo.getXferPort();
  647. dn_info->info_port = ppDNInfo.getInfoPort();
  648. dn_info->IPC_port = ppDNInfo.getIPCPort();
  649. dn_info->info_secure_port = ppDNInfo.getInfoSecurePort();
  650. char * buf;
  651. buf = new char[ppDNInfo.getHostname().size() + 1];
  652. strncpy(buf, ppDNInfo.getHostname().c_str(), ppDNInfo.getHostname().size());
  653. dn_info->hostname = buf;
  654. buf = new char[ppDNInfo.getIPAddr().size() + 1];
  655. strncpy(buf, ppDNInfo.getIPAddr().c_str(), ppDNInfo.getIPAddr().size());
  656. dn_info->ip_address = buf;
  657. }
  658. }
  659. return 0;
  660. } catch (const std::exception & e) {
  661. return ReportException(e);
  662. } catch (...) {
  663. return ReportCaughtNonException();
  664. }
  665. }
  666. int hdfsFreeBlockLocations(struct hdfsBlockLocations * blockLocations) {
  667. if (blockLocations == nullptr)
  668. return 0;
  669. for (size_t i=0; i < blockLocations->num_blocks; i++) {
  670. auto block = &blockLocations->blocks[i];
  671. for (size_t j=0; j < block->num_locations; j++) {
  672. auto location = &block->locations[j];
  673. delete[] location->hostname;
  674. delete[] location->ip_address;
  675. }
  676. }
  677. delete[] blockLocations->blocks;
  678. delete blockLocations;
  679. return 0;
  680. }
  681. /*******************************************************************
  682. * EVENT CALLBACKS
  683. *******************************************************************/
  684. const char * FS_NN_CONNECT_EVENT = hdfs::FS_NN_CONNECT_EVENT;
  685. const char * FS_NN_READ_EVENT = hdfs::FS_NN_READ_EVENT;
  686. const char * FS_NN_WRITE_EVENT = hdfs::FS_NN_WRITE_EVENT;
  687. const char * FILE_DN_CONNECT_EVENT = hdfs::FILE_DN_CONNECT_EVENT;
  688. const char * FILE_DN_READ_EVENT = hdfs::FILE_DN_READ_EVENT;
  689. const char * FILE_DN_WRITE_EVENT = hdfs::FILE_DN_WRITE_EVENT;
  690. event_response fs_callback_glue(libhdfspp_fs_event_callback handler,
  691. int64_t cookie,
  692. const char * event,
  693. const char * cluster,
  694. int64_t value) {
  695. int result = handler(event, cluster, value, cookie);
  696. if (result == LIBHDFSPP_EVENT_OK) {
  697. return event_response::ok();
  698. }
  699. #ifndef NDEBUG
  700. if (result == DEBUG_SIMULATE_ERROR) {
  701. return event_response::test_err(Status::Error("Simulated error"));
  702. }
  703. #endif
  704. return event_response::ok();
  705. }
  706. event_response file_callback_glue(libhdfspp_file_event_callback handler,
  707. int64_t cookie,
  708. const char * event,
  709. const char * cluster,
  710. const char * file,
  711. int64_t value) {
  712. int result = handler(event, cluster, file, value, cookie);
  713. if (result == LIBHDFSPP_EVENT_OK) {
  714. return event_response::ok();
  715. }
  716. #ifndef NDEBUG
  717. if (result == DEBUG_SIMULATE_ERROR) {
  718. return event_response::test_err(Status::Error("Simulated error"));
  719. }
  720. #endif
  721. return event_response::ok();
  722. }
  723. int hdfsPreAttachFSMonitor(libhdfspp_fs_event_callback handler, int64_t cookie)
  724. {
  725. fs_event_callback callback = std::bind(fs_callback_glue, handler, cookie, _1, _2, _3);
  726. fsEventCallback = callback;
  727. return 0;
  728. }
  729. int hdfsPreAttachFileMonitor(libhdfspp_file_event_callback handler, int64_t cookie)
  730. {
  731. file_event_callback callback = std::bind(file_callback_glue, handler, cookie, _1, _2, _3, _4);
  732. fileEventCallback = callback;
  733. return 0;
  734. }
  735. /*******************************************************************
  736. * BUILDER INTERFACE
  737. *******************************************************************/
  738. HdfsConfiguration LoadDefault(ConfigurationLoader & loader)
  739. {
  740. optional<HdfsConfiguration> result = loader.LoadDefaultResources<HdfsConfiguration>();
  741. if (result)
  742. {
  743. return result.value();
  744. }
  745. else
  746. {
  747. return loader.New<HdfsConfiguration>();
  748. }
  749. }
  750. hdfsBuilder::hdfsBuilder() : config(loader.New<HdfsConfiguration>())
  751. {
  752. loader.SetDefaultSearchPath();
  753. config = LoadDefault(loader);
  754. }
  755. hdfsBuilder::hdfsBuilder(const char * directory) :
  756. config(loader.New<HdfsConfiguration>())
  757. {
  758. loader.SetSearchPath(directory);
  759. config = LoadDefault(loader);
  760. }
  761. struct hdfsBuilder *hdfsNewBuilder(void)
  762. {
  763. try
  764. {
  765. return new struct hdfsBuilder();
  766. } catch (const std::exception & e) {
  767. ReportException(e);
  768. return nullptr;
  769. } catch (...) {
  770. ReportCaughtNonException();
  771. return nullptr;
  772. }
  773. }
  774. void hdfsBuilderSetNameNode(struct hdfsBuilder *bld, const char *nn)
  775. {
  776. bld->overrideHost = std::string(nn);
  777. }
  778. void hdfsBuilderSetNameNodePort(struct hdfsBuilder *bld, tPort port)
  779. {
  780. bld->overridePort = port;
  781. }
  782. void hdfsBuilderSetUserName(struct hdfsBuilder *bld, const char *userName)
  783. {
  784. if (userName && *userName) {
  785. bld->user = std::string(userName);
  786. }
  787. }
  788. void hdfsFreeBuilder(struct hdfsBuilder *bld)
  789. {
  790. try
  791. {
  792. delete bld;
  793. } catch (const std::exception & e) {
  794. ReportException(e);
  795. } catch (...) {
  796. ReportCaughtNonException();
  797. }
  798. }
  799. int hdfsBuilderConfSetStr(struct hdfsBuilder *bld, const char *key,
  800. const char *val)
  801. {
  802. try
  803. {
  804. optional<HdfsConfiguration> newConfig = bld->loader.OverlayValue(bld->config, key, val);
  805. if (newConfig)
  806. {
  807. bld->config = newConfig.value();
  808. return 0;
  809. }
  810. else
  811. {
  812. ReportError(EINVAL, "Could not change Builder value");
  813. return 1;
  814. }
  815. } catch (const std::exception & e) {
  816. return ReportException(e);
  817. } catch (...) {
  818. return ReportCaughtNonException();
  819. }
  820. }
  821. void hdfsConfStrFree(char *val)
  822. {
  823. free(val);
  824. }
  825. hdfsFS hdfsBuilderConnect(struct hdfsBuilder *bld) {
  826. return doHdfsConnect(bld->overrideHost, bld->overridePort, bld->user, bld->config.GetOptions());
  827. }
  828. int hdfsConfGetStr(const char *key, char **val)
  829. {
  830. try
  831. {
  832. hdfsBuilder builder;
  833. return hdfsBuilderConfGetStr(&builder, key, val);
  834. } catch (const std::exception & e) {
  835. return ReportException(e);
  836. } catch (...) {
  837. return ReportCaughtNonException();
  838. }
  839. }
  840. int hdfsConfGetInt(const char *key, int32_t *val)
  841. {
  842. try
  843. {
  844. hdfsBuilder builder;
  845. return hdfsBuilderConfGetInt(&builder, key, val);
  846. } catch (const std::exception & e) {
  847. return ReportException(e);
  848. } catch (...) {
  849. return ReportCaughtNonException();
  850. }
  851. }
  852. //
  853. // Extended builder interface
  854. //
  855. struct hdfsBuilder *hdfsNewBuilderFromDirectory(const char * configDirectory)
  856. {
  857. try
  858. {
  859. return new struct hdfsBuilder(configDirectory);
  860. } catch (const std::exception & e) {
  861. ReportException(e);
  862. return nullptr;
  863. } catch (...) {
  864. ReportCaughtNonException();
  865. return nullptr;
  866. }
  867. }
  868. int hdfsBuilderConfGetStr(struct hdfsBuilder *bld, const char *key,
  869. char **val)
  870. {
  871. try
  872. {
  873. optional<std::string> value = bld->config.Get(key);
  874. if (value)
  875. {
  876. size_t len = value->length() + 1;
  877. *val = static_cast<char *>(malloc(len));
  878. strncpy(*val, value->c_str(), len);
  879. }
  880. else
  881. {
  882. *val = nullptr;
  883. }
  884. return 0;
  885. } catch (const std::exception & e) {
  886. return ReportException(e);
  887. } catch (...) {
  888. return ReportCaughtNonException();
  889. }
  890. }
  891. // If we're running on a 32-bit platform, we might get 64-bit values that
  892. // don't fit in an int, and int is specified by the java hdfs.h interface
  893. bool isValidInt(int64_t value)
  894. {
  895. return (value >= std::numeric_limits<int>::min() &&
  896. value <= std::numeric_limits<int>::max());
  897. }
  898. int hdfsBuilderConfGetInt(struct hdfsBuilder *bld, const char *key, int32_t *val)
  899. {
  900. try
  901. {
  902. // Pull from default configuration
  903. optional<int64_t> value = bld->config.GetInt(key);
  904. if (value)
  905. {
  906. if (!isValidInt(*value))
  907. return 1;
  908. *val = *value;
  909. }
  910. // If not found, don't change val
  911. ReportError(EINVAL, "Could not get Builder value");
  912. return 0;
  913. } catch (const std::exception & e) {
  914. return ReportException(e);
  915. } catch (...) {
  916. return ReportCaughtNonException();
  917. }
  918. }
  919. /**
  920. * Logging functions
  921. **/
  922. class CForwardingLogger : public LoggerInterface {
  923. public:
  924. CForwardingLogger() : callback_(nullptr) {};
  925. // Converts LogMessage into LogData, a POD type,
  926. // and invokes callback_ if it's not null.
  927. void Write(const LogMessage& msg);
  928. // pass in NULL to clear the hook
  929. void SetCallback(void (*callback)(LogData*));
  930. //return a copy, or null on failure.
  931. static LogData *CopyLogData(const LogData*);
  932. //free LogData allocated with CopyLogData
  933. static void FreeLogData(LogData*);
  934. private:
  935. void (*callback_)(LogData*);
  936. };
  937. /**
  938. * Plugin to forward message to a C function pointer
  939. **/
  940. void CForwardingLogger::Write(const LogMessage& msg) {
  941. if(!callback_)
  942. return;
  943. const std::string text = msg.MsgString();
  944. LogData data;
  945. data.level = msg.level();
  946. data.component = msg.component();
  947. data.msg = text.c_str();
  948. data.file_name = msg.file_name();
  949. data.file_line = msg.file_line();
  950. callback_(&data);
  951. }
  952. void CForwardingLogger::SetCallback(void (*callback)(LogData*)) {
  953. callback_ = callback;
  954. }
  955. LogData *CForwardingLogger::CopyLogData(const LogData *orig) {
  956. if(!orig)
  957. return nullptr;
  958. LogData *copy = (LogData*)malloc(sizeof(LogData));
  959. if(!copy)
  960. return nullptr;
  961. copy->level = orig->level;
  962. copy->component = orig->component;
  963. if(orig->msg)
  964. copy->msg = strdup(orig->msg);
  965. copy->file_name = orig->file_name;
  966. copy->file_line = orig->file_line;
  967. return copy;
  968. }
  969. void CForwardingLogger::FreeLogData(LogData *data) {
  970. if(!data)
  971. return;
  972. if(data->msg)
  973. free((void*)data->msg);
  974. // Inexpensive way to help catch use-after-free
  975. memset(data, 0, sizeof(LogData));
  976. free(data);
  977. }
  978. LogData *hdfsCopyLogData(LogData *data) {
  979. return CForwardingLogger::CopyLogData(data);
  980. }
  981. void hdfsFreeLogData(LogData *data) {
  982. CForwardingLogger::FreeLogData(data);
  983. }
  984. void hdfsSetLogFunction(void (*callback)(LogData*)) {
  985. CForwardingLogger *logger = new CForwardingLogger();
  986. logger->SetCallback(callback);
  987. LogManager::SetLoggerImplementation(std::unique_ptr<LoggerInterface>(logger));
  988. }
  989. static bool IsLevelValid(int component) {
  990. if(component < HDFSPP_LOG_LEVEL_TRACE || component > HDFSPP_LOG_LEVEL_ERROR)
  991. return false;
  992. return true;
  993. }
  994. // should use __builtin_popcnt as optimization on some platforms
  995. static int popcnt(int val) {
  996. int bits = sizeof(val) * 8;
  997. int count = 0;
  998. for(int i=0; i<bits; i++) {
  999. if((val >> i) & 0x1)
  1000. count++;
  1001. }
  1002. return count;
  1003. }
  1004. static bool IsComponentValid(int component) {
  1005. if(component < HDFSPP_LOG_COMPONENT_UNKNOWN || component > HDFSPP_LOG_COMPONENT_FILESYSTEM)
  1006. return false;
  1007. if(popcnt(component) != 1)
  1008. return false;
  1009. return true;
  1010. }
  1011. int hdfsEnableLoggingForComponent(int component) {
  1012. if(!IsComponentValid(component))
  1013. return 1;
  1014. LogManager::EnableLogForComponent(static_cast<LogSourceComponent>(component));
  1015. return 0;
  1016. }
  1017. int hdfsDisableLoggingForComponent(int component) {
  1018. if(!IsComponentValid(component))
  1019. return 1;
  1020. LogManager::DisableLogForComponent(static_cast<LogSourceComponent>(component));
  1021. return 0;
  1022. }
  1023. int hdfsSetLoggingLevel(int level) {
  1024. if(!IsLevelValid(level))
  1025. return 1;
  1026. LogManager::SetLogLevel(static_cast<LogLevel>(level));
  1027. return 0;
  1028. }