hdfs_shim.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 "libhdfs_wrapper.h"
  19. #include "libhdfspp_wrapper.h"
  20. #include "hdfs/hdfs.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. /* Cheat for now and use the same hdfsBuilder as libhdfs */
  25. /* (libhdfspp doesn't have an hdfsBuilder yet). */
  26. struct hdfsBuilder {
  27. int forceNewInstance;
  28. const char *nn;
  29. tPort port;
  30. const char *kerbTicketCachePath;
  31. const char *userName;
  32. struct hdfsBuilderConfOpt *opts;
  33. };
  34. /* Shim structs and functions that delegate to libhdfspp and libhdfs. */
  35. struct hdfs_internal {
  36. libhdfs_hdfsFS libhdfsRep;
  37. libhdfspp_hdfsFS libhdfsppRep;
  38. };
  39. typedef struct hdfs_internal* hdfsFS;
  40. struct hdfsFile_internal {
  41. libhdfs_hdfsFile libhdfsRep;
  42. libhdfspp_hdfsFile libhdfsppRep;
  43. };
  44. typedef struct hdfsFile_internal* hdfsFile;
  45. #define REPORT_FUNCTION_NOT_IMPLEMENTED \
  46. fprintf(stderr, "%s failed: function not implemented by " \
  47. "libhdfs++ test shim", __PRETTY_FUNCTION__);
  48. int hdfsFileIsOpenForWrite(hdfsFile file) {
  49. return libhdfs_hdfsFileIsOpenForWrite(file->libhdfsRep);
  50. }
  51. int hdfsFileGetReadStatistics(hdfsFile file,
  52. struct hdfsReadStatistics **stats) {
  53. return libhdfs_hdfsFileGetReadStatistics
  54. (file->libhdfsRep, (struct libhdfs_hdfsReadStatistics **)stats);
  55. }
  56. int64_t hdfsReadStatisticsGetRemoteBytesRead(
  57. const struct hdfsReadStatistics *stats) {
  58. return libhdfs_hdfsReadStatisticsGetRemoteBytesRead
  59. ((struct libhdfs_hdfsReadStatistics *)stats);
  60. }
  61. int hdfsFileClearReadStatistics(hdfsFile file) {
  62. return libhdfs_hdfsFileClearReadStatistics(file->libhdfsRep);
  63. }
  64. void hdfsFileFreeReadStatistics(struct hdfsReadStatistics *stats) {
  65. libhdfs_hdfsFileFreeReadStatistics(
  66. (struct libhdfs_hdfsReadStatistics *)stats);
  67. }
  68. hdfsFS hdfsConnectAsUser(const char* nn, tPort port, const char *user) {
  69. return (hdfsFS) libhdfspp_hdfsConnectAsUser(nn, port, user);
  70. }
  71. hdfsFS hdfsConnect(const char* nn, tPort port) {
  72. REPORT_FUNCTION_NOT_IMPLEMENTED
  73. return NULL;
  74. }
  75. hdfsFS hdfsConnectAsUserNewInstance(const char* nn, tPort port, const char *user ) {
  76. REPORT_FUNCTION_NOT_IMPLEMENTED
  77. return NULL;
  78. }
  79. hdfsFS hdfsConnectNewInstance(const char* nn, tPort port) {
  80. REPORT_FUNCTION_NOT_IMPLEMENTED
  81. return NULL;
  82. }
  83. hdfsFS hdfsBuilderConnect(struct hdfsBuilder *bld) {
  84. hdfsFS ret = calloc(1, sizeof(struct hdfs_internal));
  85. ret->libhdfsppRep = libhdfspp_hdfsConnect(bld->nn, bld->port);
  86. if (!ret->libhdfsppRep) {
  87. free(ret);
  88. ret = NULL;
  89. } else {
  90. /* Destroys bld object. */
  91. ret->libhdfsRep = libhdfs_hdfsBuilderConnect(bld);
  92. if (!ret->libhdfsRep) {
  93. libhdfspp_hdfsDisconnect(ret->libhdfsppRep);
  94. free(ret);
  95. ret = NULL;
  96. }
  97. }
  98. return ret;
  99. }
  100. struct hdfsBuilder *hdfsNewBuilder(void) {
  101. return libhdfs_hdfsNewBuilder();
  102. }
  103. void hdfsBuilderSetForceNewInstance(struct hdfsBuilder *bld) {
  104. libhdfs_hdfsBuilderSetForceNewInstance(bld);
  105. }
  106. void hdfsBuilderSetNameNode(struct hdfsBuilder *bld, const char *nn) {
  107. libhdfs_hdfsBuilderSetNameNode(bld, nn);
  108. }
  109. void hdfsBuilderSetNameNodePort(struct hdfsBuilder *bld, tPort port) {
  110. libhdfs_hdfsBuilderSetNameNodePort(bld, port);
  111. }
  112. void hdfsBuilderSetUserName(struct hdfsBuilder *bld, const char *userName) {
  113. libhdfs_hdfsBuilderSetUserName(bld, userName);
  114. }
  115. void hdfsBuilderSetKerbTicketCachePath(struct hdfsBuilder *bld,
  116. const char *kerbTicketCachePath) {
  117. libhdfs_hdfsBuilderSetKerbTicketCachePath(bld, kerbTicketCachePath);
  118. }
  119. void hdfsFreeBuilder(struct hdfsBuilder *bld) {
  120. libhdfs_hdfsFreeBuilder(bld);
  121. }
  122. int hdfsBuilderConfSetStr(struct hdfsBuilder *bld, const char *key,
  123. const char *val) {
  124. return libhdfs_hdfsBuilderConfSetStr(bld, key, val);
  125. }
  126. int hdfsConfGetStr(const char *key, char **val) {
  127. return libhdfs_hdfsConfGetStr(key, val);
  128. }
  129. int hdfsConfGetInt(const char *key, int32_t *val) {
  130. return libhdfs_hdfsConfGetInt(key, val);
  131. }
  132. void hdfsConfStrFree(char *val) {
  133. libhdfs_hdfsConfStrFree(val);
  134. }
  135. int hdfsDisconnect(hdfsFS fs) {
  136. int ret;
  137. libhdfspp_hdfsDisconnect(fs->libhdfsppRep);
  138. ret = libhdfs_hdfsDisconnect(fs->libhdfsRep);
  139. free(fs);
  140. return ret;
  141. }
  142. hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags,
  143. int bufferSize, short replication, tSize blocksize) {
  144. hdfsFile ret = calloc(1, sizeof(struct hdfsFile_internal));
  145. /* Currently only open libhdf++ for reads. */
  146. ret->libhdfsppRep = 0;
  147. if (flags == O_RDONLY) {
  148. ret->libhdfsppRep = libhdfspp_hdfsOpenFile(fs->libhdfsppRep, path, flags,
  149. bufferSize, replication, blocksize);
  150. }
  151. ret->libhdfsRep = libhdfs_hdfsOpenFile(fs->libhdfsRep, path,
  152. flags, bufferSize, replication, blocksize);
  153. if (!ret->libhdfsRep) {
  154. free(ret);
  155. ret = NULL;
  156. }
  157. return ret;
  158. }
  159. int hdfsTruncateFile(hdfsFS fs, const char* path, tOffset newlength) {
  160. return libhdfs_hdfsTruncateFile(fs->libhdfsRep, path, newlength);
  161. }
  162. int hdfsUnbufferFile(hdfsFile file) {
  163. return libhdfs_hdfsUnbufferFile(file->libhdfsRep);
  164. }
  165. int hdfsCloseFile(hdfsFS fs, hdfsFile file) {
  166. int ret;
  167. if (file->libhdfsppRep) {
  168. libhdfspp_hdfsCloseFile(fs->libhdfsppRep, file->libhdfsppRep);
  169. }
  170. ret = libhdfs_hdfsCloseFile(fs->libhdfsRep, file->libhdfsRep);
  171. free(file);
  172. return ret;
  173. }
  174. int hdfsExists(hdfsFS fs, const char *path) {
  175. return libhdfs_hdfsExists(fs->libhdfsRep, path);
  176. }
  177. int hdfsSeek(hdfsFS fs, hdfsFile file, tOffset desiredPos) {
  178. return libhdfs_hdfsSeek(fs->libhdfsRep, file->libhdfsRep, desiredPos);
  179. }
  180. tOffset hdfsTell(hdfsFS fs, hdfsFile file) {
  181. return libhdfs_hdfsTell(fs->libhdfsRep, file->libhdfsRep);
  182. }
  183. tSize hdfsRead(hdfsFS fs, hdfsFile file, void* buffer, tSize length) {
  184. // Read to update stats.
  185. tSize nRead = libhdfs_hdfsRead(fs->libhdfsRep, file->libhdfsRep, buffer, length);
  186. // Clear to avoid false positives.
  187. if (nRead > 0) memset(buffer, 0, nRead);
  188. return libhdfspp_hdfsRead(fs->libhdfsppRep, file->libhdfsppRep, buffer, length);
  189. }
  190. tSize hdfsPread(hdfsFS fs, hdfsFile file, tOffset position,
  191. void* buffer, tSize length) {
  192. tSize ret = -1;
  193. if (!fs->libhdfsppRep) {
  194. fprintf(stderr, "hdfsPread failed: no libhdfs++ file system");
  195. } else if (!file->libhdfsppRep) {
  196. fprintf(stderr, "hdfsPread failed: no libhdfs++ file");
  197. } else {
  198. ret = libhdfspp_hdfsPread(fs->libhdfsppRep, file->libhdfsppRep,
  199. position, buffer, length);
  200. }
  201. return ret;
  202. }
  203. tSize hdfsWrite(hdfsFS fs, hdfsFile file, const void* buffer,
  204. tSize length) {
  205. return libhdfs_hdfsWrite(fs->libhdfsRep, file->libhdfsRep, buffer, length);
  206. }
  207. int hdfsFlush(hdfsFS fs, hdfsFile file) {
  208. return libhdfs_hdfsFlush(fs->libhdfsRep, file->libhdfsRep);
  209. }
  210. int hdfsHFlush(hdfsFS fs, hdfsFile file) {
  211. return libhdfs_hdfsHFlush(fs->libhdfsRep, file->libhdfsRep);
  212. }
  213. int hdfsHSync(hdfsFS fs, hdfsFile file) {
  214. return libhdfs_hdfsHSync(fs->libhdfsRep, file->libhdfsRep);
  215. }
  216. int hdfsAvailable(hdfsFS fs, hdfsFile file) {
  217. return libhdfs_hdfsAvailable(fs->libhdfsRep, file->libhdfsRep);
  218. }
  219. int hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst) {
  220. return libhdfs_hdfsCopy(srcFS->libhdfsRep, src, dstFS->libhdfsRep, dst);
  221. }
  222. int hdfsMove(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst) {
  223. return libhdfs_hdfsMove(srcFS->libhdfsRep, src, dstFS->libhdfsRep, dst);
  224. }
  225. int hdfsDelete(hdfsFS fs, const char* path, int recursive) {
  226. return libhdfs_hdfsDelete(fs->libhdfsRep, path, recursive);
  227. }
  228. int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath) {
  229. return libhdfs_hdfsRename(fs->libhdfsRep, oldPath, newPath);
  230. }
  231. char* hdfsGetWorkingDirectory(hdfsFS fs, char *buffer, size_t bufferSize) {
  232. return libhdfs_hdfsGetWorkingDirectory(fs->libhdfsRep, buffer, bufferSize);
  233. }
  234. int hdfsSetWorkingDirectory(hdfsFS fs, const char* path) {
  235. return libhdfs_hdfsSetWorkingDirectory(fs->libhdfsRep, path);
  236. }
  237. int hdfsCreateDirectory(hdfsFS fs, const char* path) {
  238. return libhdfs_hdfsCreateDirectory(fs->libhdfsRep, path);
  239. }
  240. int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication) {
  241. return libhdfs_hdfsSetReplication(fs->libhdfsRep, path, replication);
  242. }
  243. hdfsFileInfo *hdfsListDirectory(hdfsFS fs, const char* path,
  244. int *numEntries) {
  245. return (hdfsFileInfo *)libhdfspp_hdfsListDirectory(fs->libhdfsppRep, path, numEntries);
  246. }
  247. hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path) {
  248. return (hdfsFileInfo *)libhdfspp_hdfsGetPathInfo(fs->libhdfsppRep, path);
  249. }
  250. void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries) {
  251. return libhdfspp_hdfsFreeFileInfo
  252. ((libhdfspp_hdfsFileInfo *) hdfsFileInfo, numEntries);
  253. }
  254. int hdfsFileIsEncrypted(hdfsFileInfo *hdfsFileInfo) {
  255. return libhdfs_hdfsFileIsEncrypted
  256. ((libhdfs_hdfsFileInfo *) hdfsFileInfo);
  257. }
  258. char*** hdfsGetHosts(hdfsFS fs, const char* path,
  259. tOffset start, tOffset length) {
  260. return libhdfs_hdfsGetHosts(fs->libhdfsRep, path, start, length);
  261. }
  262. void hdfsFreeHosts(char ***blockHosts) {
  263. return libhdfs_hdfsFreeHosts(blockHosts);
  264. }
  265. tOffset hdfsGetDefaultBlockSize(hdfsFS fs) {
  266. return libhdfs_hdfsGetDefaultBlockSize(fs->libhdfsRep);
  267. }
  268. tOffset hdfsGetDefaultBlockSizeAtPath(hdfsFS fs, const char *path) {
  269. return libhdfs_hdfsGetDefaultBlockSizeAtPath(fs->libhdfsRep, path);
  270. }
  271. tOffset hdfsGetCapacity(hdfsFS fs) {
  272. return libhdfspp_hdfsGetCapacity(fs->libhdfsppRep);
  273. }
  274. tOffset hdfsGetUsed(hdfsFS fs) {
  275. return libhdfspp_hdfsGetUsed(fs->libhdfsppRep);
  276. }
  277. int hdfsChown(hdfsFS fs, const char* path, const char *owner,
  278. const char *group) {
  279. return libhdfs_hdfsChown(fs->libhdfsRep, path, owner, group);
  280. }
  281. int hdfsChmod(hdfsFS fs, const char* path, short mode) {
  282. return libhdfs_hdfsChmod(fs->libhdfsRep, path, mode);
  283. }
  284. int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime) {
  285. return libhdfs_hdfsUtime(fs->libhdfsRep, path, mtime, atime);
  286. }
  287. struct hadoopRzOptions *hadoopRzOptionsAlloc(void) {
  288. return libhdfs_hadoopRzOptionsAlloc();
  289. }
  290. int hadoopRzOptionsSetSkipChecksum(
  291. struct hadoopRzOptions *opts, int skip) {
  292. return libhdfs_hadoopRzOptionsSetSkipChecksum(opts, skip);
  293. }
  294. int hadoopRzOptionsSetByteBufferPool(
  295. struct hadoopRzOptions *opts, const char *className) {
  296. return libhdfs_hadoopRzOptionsSetByteBufferPool(opts, className);
  297. }
  298. void hadoopRzOptionsFree(struct hadoopRzOptions *opts) {
  299. libhdfs_hadoopRzOptionsFree(opts);
  300. }
  301. struct hadoopRzBuffer* hadoopReadZero(hdfsFile file,
  302. struct hadoopRzOptions *opts, int32_t maxLength) {
  303. return libhdfs_hadoopReadZero(file->libhdfsRep, opts, maxLength);
  304. }
  305. int32_t hadoopRzBufferLength(const struct hadoopRzBuffer *buffer) {
  306. return libhdfs_hadoopRzBufferLength(buffer);
  307. }
  308. const void *hadoopRzBufferGet(const struct hadoopRzBuffer *buffer) {
  309. return libhdfs_hadoopRzBufferGet(buffer);
  310. }
  311. void hadoopRzBufferFree(hdfsFile file, struct hadoopRzBuffer *buffer) {
  312. return libhdfs_hadoopRzBufferFree(file->libhdfsRep, buffer);
  313. }
  314. /*************
  315. * hdfs_ext functions
  316. */
  317. int hdfsGetLastError(char *buf, int len) {
  318. return libhdfspp_hdfsGetLastError(buf, len);
  319. }
  320. int hdfsCancel(hdfsFS fs, hdfsFile file) {
  321. return libhdfspp_hdfsCancel(fs->libhdfsppRep, file->libhdfsppRep);
  322. }
  323. int hdfsGetBlockLocations(hdfsFS fs, const char *path, struct hdfsBlockLocations ** locations) {
  324. return libhdfspp_hdfsGetBlockLocations(fs->libhdfsppRep, path, locations);
  325. }
  326. int hdfsFreeBlockLocations(struct hdfsBlockLocations * locations) {
  327. return libhdfspp_hdfsFreeBlockLocations(locations);
  328. }
  329. int hdfsCreateSnapshot(hdfsFS fs, const char* path, const char* name) {
  330. return libhdfspp_hdfsCreateSnapshot(fs->libhdfsppRep, path, name);
  331. }
  332. int hdfsDeleteSnapshot(hdfsFS fs, const char* path, const char* name) {
  333. return libhdfspp_hdfsDeleteSnapshot(fs->libhdfsppRep, path, name);
  334. }
  335. int hdfsAllowSnapshot(hdfsFS fs, const char* path) {
  336. return libhdfspp_hdfsAllowSnapshot(fs->libhdfsppRep, path);
  337. }
  338. int hdfsDisallowSnapshot(hdfsFS fs, const char* path) {
  339. return libhdfspp_hdfsDisallowSnapshot(fs->libhdfsppRep, path);
  340. }