1
0

hdfs.cc 30 KB

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