1
0

hdfs.cc 32 KB

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