hdfs_shim.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. REPORT_FUNCTION_NOT_IMPLEMENTED
  70. return NULL;
  71. }
  72. hdfsFS hdfsConnect(const char* nn, tPort port) {
  73. REPORT_FUNCTION_NOT_IMPLEMENTED
  74. return NULL;
  75. }
  76. hdfsFS hdfsConnectAsUserNewInstance(const char* nn, tPort port, const char *user ) {
  77. REPORT_FUNCTION_NOT_IMPLEMENTED
  78. return NULL;
  79. }
  80. hdfsFS hdfsConnectNewInstance(const char* nn, tPort port) {
  81. REPORT_FUNCTION_NOT_IMPLEMENTED
  82. return NULL;
  83. }
  84. hdfsFS hdfsBuilderConnect(struct hdfsBuilder *bld) {
  85. hdfsFS ret = calloc(1, sizeof(struct hdfs_internal));
  86. ret->libhdfsppRep = libhdfspp_hdfsConnect(bld->nn, bld->port);
  87. if (!ret->libhdfsppRep) {
  88. free(ret);
  89. ret = NULL;
  90. } else {
  91. /* Destroys bld object. */
  92. ret->libhdfsRep = libhdfs_hdfsBuilderConnect(bld);
  93. if (!ret->libhdfsRep) {
  94. libhdfspp_hdfsDisconnect(ret->libhdfsppRep);
  95. free(ret);
  96. ret = NULL;
  97. }
  98. }
  99. return ret;
  100. }
  101. struct hdfsBuilder *hdfsNewBuilder(void) {
  102. return libhdfs_hdfsNewBuilder();
  103. }
  104. void hdfsBuilderSetForceNewInstance(struct hdfsBuilder *bld) {
  105. libhdfs_hdfsBuilderSetForceNewInstance(bld);
  106. }
  107. void hdfsBuilderSetNameNode(struct hdfsBuilder *bld, const char *nn) {
  108. libhdfs_hdfsBuilderSetNameNode(bld, nn);
  109. }
  110. void hdfsBuilderSetNameNodePort(struct hdfsBuilder *bld, tPort port) {
  111. libhdfs_hdfsBuilderSetNameNodePort(bld, port);
  112. }
  113. void hdfsBuilderSetUserName(struct hdfsBuilder *bld, const char *userName) {
  114. libhdfs_hdfsBuilderSetUserName(bld, userName);
  115. }
  116. void hdfsBuilderSetKerbTicketCachePath(struct hdfsBuilder *bld,
  117. const char *kerbTicketCachePath) {
  118. libhdfs_hdfsBuilderSetKerbTicketCachePath(bld, kerbTicketCachePath);
  119. }
  120. void hdfsFreeBuilder(struct hdfsBuilder *bld) {
  121. libhdfs_hdfsFreeBuilder(bld);
  122. }
  123. int hdfsBuilderConfSetStr(struct hdfsBuilder *bld, const char *key,
  124. const char *val) {
  125. return libhdfs_hdfsBuilderConfSetStr(bld, key, val);
  126. }
  127. int hdfsConfGetStr(const char *key, char **val) {
  128. return libhdfs_hdfsConfGetStr(key, val);
  129. }
  130. int hdfsConfGetInt(const char *key, int32_t *val) {
  131. return libhdfs_hdfsConfGetInt(key, val);
  132. }
  133. void hdfsConfStrFree(char *val) {
  134. libhdfs_hdfsConfStrFree(val);
  135. }
  136. int hdfsDisconnect(hdfsFS fs) {
  137. int ret;
  138. libhdfspp_hdfsDisconnect(fs->libhdfsppRep);
  139. ret = libhdfs_hdfsDisconnect(fs->libhdfsRep);
  140. free(fs);
  141. return ret;
  142. }
  143. hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags,
  144. int bufferSize, short replication, tSize blocksize) {
  145. hdfsFile ret = calloc(1, sizeof(struct hdfsFile_internal));
  146. /* Currently only open libhdf++ for reads. */
  147. ret->libhdfsppRep = 0;
  148. if (flags == O_RDONLY) {
  149. ret->libhdfsppRep = libhdfspp_hdfsOpenFile(fs->libhdfsppRep, path, flags,
  150. bufferSize, replication, blocksize);
  151. }
  152. ret->libhdfsRep = libhdfs_hdfsOpenFile(fs->libhdfsRep, path,
  153. flags, bufferSize, replication, blocksize);
  154. if (!ret->libhdfsRep) {
  155. free(ret);
  156. ret = NULL;
  157. }
  158. return ret;
  159. }
  160. int hdfsTruncateFile(hdfsFS fs, const char* path, tOffset newlength) {
  161. return libhdfs_hdfsTruncateFile(fs->libhdfsRep, path, newlength);
  162. }
  163. int hdfsUnbufferFile(hdfsFile file) {
  164. return libhdfs_hdfsUnbufferFile(file->libhdfsRep);
  165. }
  166. int hdfsCloseFile(hdfsFS fs, hdfsFile file) {
  167. int ret;
  168. if (file->libhdfsppRep) {
  169. libhdfspp_hdfsCloseFile(fs->libhdfsppRep, file->libhdfsppRep);
  170. }
  171. ret = libhdfs_hdfsCloseFile(fs->libhdfsRep, file->libhdfsRep);
  172. free(file);
  173. return ret;
  174. }
  175. int hdfsExists(hdfsFS fs, const char *path) {
  176. return libhdfs_hdfsExists(fs->libhdfsRep, path);
  177. }
  178. int hdfsSeek(hdfsFS fs, hdfsFile file, tOffset desiredPos) {
  179. return libhdfs_hdfsSeek(fs->libhdfsRep, file->libhdfsRep, desiredPos);
  180. }
  181. tOffset hdfsTell(hdfsFS fs, hdfsFile file) {
  182. return libhdfs_hdfsTell(fs->libhdfsRep, file->libhdfsRep);
  183. }
  184. tSize hdfsRead(hdfsFS fs, hdfsFile file, void* buffer, tSize length) {
  185. return libhdfs_hdfsRead(fs->libhdfsRep, file->libhdfsRep, buffer, length);
  186. }
  187. tSize hdfsPread(hdfsFS fs, hdfsFile file, tOffset position,
  188. void* buffer, tSize length) {
  189. tSize ret = -1;
  190. if (!fs->libhdfsppRep) {
  191. fprintf(stderr, "hdfsPread failed: no libhdfs++ file system");
  192. } else if (!file->libhdfsppRep) {
  193. fprintf(stderr, "hdfsPread failed: no libhdfs++ file");
  194. } else {
  195. ret = libhdfspp_hdfsPread(fs->libhdfsppRep, file->libhdfsppRep,
  196. position, buffer, length);
  197. }
  198. return ret;
  199. }
  200. tSize hdfsWrite(hdfsFS fs, hdfsFile file, const void* buffer,
  201. tSize length) {
  202. return libhdfs_hdfsWrite(fs->libhdfsRep, file->libhdfsRep, buffer, length);
  203. }
  204. int hdfsFlush(hdfsFS fs, hdfsFile file) {
  205. return libhdfs_hdfsFlush(fs->libhdfsRep, file->libhdfsRep);
  206. }
  207. int hdfsHFlush(hdfsFS fs, hdfsFile file) {
  208. return libhdfs_hdfsHFlush(fs->libhdfsRep, file->libhdfsRep);
  209. }
  210. int hdfsHSync(hdfsFS fs, hdfsFile file) {
  211. return libhdfs_hdfsHSync(fs->libhdfsRep, file->libhdfsRep);
  212. }
  213. int hdfsAvailable(hdfsFS fs, hdfsFile file) {
  214. return libhdfs_hdfsAvailable(fs->libhdfsRep, file->libhdfsRep);
  215. }
  216. int hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst) {
  217. return libhdfs_hdfsCopy(srcFS->libhdfsRep, src, dstFS->libhdfsRep, dst);
  218. }
  219. int hdfsMove(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst) {
  220. return libhdfs_hdfsMove(srcFS->libhdfsRep, src, dstFS->libhdfsRep, dst);
  221. }
  222. int hdfsDelete(hdfsFS fs, const char* path, int recursive) {
  223. return libhdfs_hdfsDelete(fs->libhdfsRep, path, recursive);
  224. }
  225. int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath) {
  226. return libhdfs_hdfsRename(fs->libhdfsRep, oldPath, newPath);
  227. }
  228. char* hdfsGetWorkingDirectory(hdfsFS fs, char *buffer, size_t bufferSize) {
  229. return libhdfs_hdfsGetWorkingDirectory(fs->libhdfsRep, buffer, bufferSize);
  230. }
  231. int hdfsSetWorkingDirectory(hdfsFS fs, const char* path) {
  232. return libhdfs_hdfsSetWorkingDirectory(fs->libhdfsRep, path);
  233. }
  234. int hdfsCreateDirectory(hdfsFS fs, const char* path) {
  235. return libhdfs_hdfsCreateDirectory(fs->libhdfsRep, path);
  236. }
  237. int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication) {
  238. return libhdfs_hdfsSetReplication(fs->libhdfsRep, path, replication);
  239. }
  240. hdfsFileInfo *hdfsListDirectory(hdfsFS fs, const char* path,
  241. int *numEntries) {
  242. return (hdfsFileInfo *)libhdfs_hdfsListDirectory(fs->libhdfsRep, path, numEntries);
  243. }
  244. hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path) {
  245. return (hdfsFileInfo *)libhdfs_hdfsGetPathInfo(fs->libhdfsRep, path);
  246. }
  247. void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries) {
  248. return libhdfs_hdfsFreeFileInfo
  249. ((libhdfs_hdfsFileInfo *) hdfsFileInfo, numEntries);
  250. }
  251. int hdfsFileIsEncrypted(hdfsFileInfo *hdfsFileInfo) {
  252. return libhdfs_hdfsFileIsEncrypted
  253. ((libhdfs_hdfsFileInfo *) hdfsFileInfo);
  254. }
  255. char*** hdfsGetHosts(hdfsFS fs, const char* path,
  256. tOffset start, tOffset length) {
  257. return libhdfs_hdfsGetHosts(fs->libhdfsRep, path, start, length);
  258. }
  259. void hdfsFreeHosts(char ***blockHosts) {
  260. return libhdfs_hdfsFreeHosts(blockHosts);
  261. }
  262. tOffset hdfsGetDefaultBlockSize(hdfsFS fs) {
  263. return libhdfs_hdfsGetDefaultBlockSize(fs->libhdfsRep);
  264. }
  265. tOffset hdfsGetDefaultBlockSizeAtPath(hdfsFS fs, const char *path) {
  266. return libhdfs_hdfsGetDefaultBlockSizeAtPath(fs->libhdfsRep, path);
  267. }
  268. tOffset hdfsGetCapacity(hdfsFS fs) {
  269. return libhdfs_hdfsGetCapacity(fs->libhdfsRep);
  270. }
  271. tOffset hdfsGetUsed(hdfsFS fs) {
  272. return libhdfs_hdfsGetUsed(fs->libhdfsRep);
  273. }
  274. int hdfsChown(hdfsFS fs, const char* path, const char *owner,
  275. const char *group) {
  276. return libhdfs_hdfsChown(fs->libhdfsRep, path, owner, group);
  277. }
  278. int hdfsChmod(hdfsFS fs, const char* path, short mode) {
  279. return libhdfs_hdfsChmod(fs->libhdfsRep, path, mode);
  280. }
  281. int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime) {
  282. return libhdfs_hdfsUtime(fs->libhdfsRep, path, mtime, atime);
  283. }
  284. struct hadoopRzOptions *hadoopRzOptionsAlloc(void) {
  285. return libhdfs_hadoopRzOptionsAlloc();
  286. }
  287. int hadoopRzOptionsSetSkipChecksum(
  288. struct hadoopRzOptions *opts, int skip) {
  289. return libhdfs_hadoopRzOptionsSetSkipChecksum(opts, skip);
  290. }
  291. int hadoopRzOptionsSetByteBufferPool(
  292. struct hadoopRzOptions *opts, const char *className) {
  293. return libhdfs_hadoopRzOptionsSetByteBufferPool(opts, className);
  294. }
  295. void hadoopRzOptionsFree(struct hadoopRzOptions *opts) {
  296. libhdfs_hadoopRzOptionsFree(opts);
  297. }
  298. struct hadoopRzBuffer* hadoopReadZero(hdfsFile file,
  299. struct hadoopRzOptions *opts, int32_t maxLength) {
  300. return libhdfs_hadoopReadZero(file->libhdfsRep, opts, maxLength);
  301. }
  302. int32_t hadoopRzBufferLength(const struct hadoopRzBuffer *buffer) {
  303. return libhdfs_hadoopRzBufferLength(buffer);
  304. }
  305. const void *hadoopRzBufferGet(const struct hadoopRzBuffer *buffer) {
  306. return libhdfs_hadoopRzBufferGet(buffer);
  307. }
  308. void hadoopRzBufferFree(hdfsFile file, struct hadoopRzBuffer *buffer) {
  309. return libhdfs_hadoopRzBufferFree(file->libhdfsRep, buffer);
  310. }