hdfs.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. #ifndef LIBHDFS_HDFS_H
  19. #define LIBHDFS_HDFS_H
  20. #include <errno.h> /* for EINTERNAL, etc. */
  21. #include <fcntl.h> /* for O_RDONLY, O_WRONLY */
  22. #include <stdint.h> /* for uint64_t, etc. */
  23. #include <time.h> /* for time_t */
  24. /*
  25. * Support export of DLL symbols during libhdfs build, and import of DLL symbols
  26. * during client application build. A client application may optionally define
  27. * symbol LIBHDFS_DLL_IMPORT in its build. This is not strictly required, but
  28. * the compiler can produce more efficient code with it.
  29. */
  30. #ifdef WIN32
  31. #ifdef LIBHDFS_DLL_EXPORT
  32. #define LIBHDFS_EXTERNAL __declspec(dllexport)
  33. #elif LIBHDFS_DLL_IMPORT
  34. #define LIBHDFS_EXTERNAL __declspec(dllimport)
  35. #else
  36. #define LIBHDFS_EXTERNAL
  37. #endif
  38. #else
  39. #ifdef LIBHDFS_DLL_EXPORT
  40. #define LIBHDFS_EXTERNAL __attribute__((visibility("default")))
  41. #elif LIBHDFS_DLL_IMPORT
  42. #define LIBHDFS_EXTERNAL __attribute__((visibility("default")))
  43. #else
  44. #define LIBHDFS_EXTERNAL
  45. #endif
  46. #endif
  47. #ifndef O_RDONLY
  48. #define O_RDONLY 1
  49. #endif
  50. #ifndef O_WRONLY
  51. #define O_WRONLY 2
  52. #endif
  53. #ifndef EINTERNAL
  54. #define EINTERNAL 255
  55. #endif
  56. #define ELASTIC_BYTE_BUFFER_POOL_CLASS \
  57. "org/apache/hadoop/io/ElasticByteBufferPool"
  58. /** All APIs set errno to meaningful values */
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. /**
  63. * Some utility decls used in libhdfs.
  64. */
  65. struct hdfsBuilder;
  66. typedef int32_t tSize; /// size of data for read/write io ops
  67. typedef time_t tTime; /// time type in seconds
  68. typedef int64_t tOffset;/// offset within the file
  69. typedef uint16_t tPort; /// port
  70. typedef enum tObjectKind {
  71. kObjectKindFile = 'F',
  72. kObjectKindDirectory = 'D',
  73. } tObjectKind;
  74. /**
  75. * The C reflection of org.apache.org.hadoop.FileSystem .
  76. */
  77. struct hdfs_internal;
  78. typedef struct hdfs_internal* hdfsFS;
  79. struct hdfsFile_internal;
  80. typedef struct hdfsFile_internal* hdfsFile;
  81. struct hadoopRzOptions;
  82. struct hadoopRzBuffer;
  83. /**
  84. * Determine if a file is open for read.
  85. *
  86. * @param file The HDFS file
  87. * @return 1 if the file is open for read; 0 otherwise
  88. */
  89. LIBHDFS_EXTERNAL
  90. int hdfsFileIsOpenForRead(hdfsFile file);
  91. /**
  92. * Determine if a file is open for write.
  93. *
  94. * @param file The HDFS file
  95. * @return 1 if the file is open for write; 0 otherwise
  96. */
  97. LIBHDFS_EXTERNAL
  98. int hdfsFileIsOpenForWrite(hdfsFile file);
  99. struct hdfsReadStatistics {
  100. uint64_t totalBytesRead;
  101. uint64_t totalLocalBytesRead;
  102. uint64_t totalShortCircuitBytesRead;
  103. uint64_t totalZeroCopyBytesRead;
  104. };
  105. /**
  106. * Get read statistics about a file. This is only applicable to files
  107. * opened for reading.
  108. *
  109. * @param file The HDFS file
  110. * @param stats (out parameter) on a successful return, the read
  111. * statistics. Unchanged otherwise. You must free the
  112. * returned statistics with hdfsFileFreeReadStatistics.
  113. * @return 0 if the statistics were successfully returned,
  114. * -1 otherwise. On a failure, please check errno against
  115. * ENOTSUP. webhdfs, LocalFilesystem, and so forth may
  116. * not support read statistics.
  117. */
  118. LIBHDFS_EXTERNAL
  119. int hdfsFileGetReadStatistics(hdfsFile file,
  120. struct hdfsReadStatistics **stats);
  121. /**
  122. * @param stats HDFS read statistics for a file.
  123. *
  124. * @return the number of remote bytes read.
  125. */
  126. LIBHDFS_EXTERNAL
  127. int64_t hdfsReadStatisticsGetRemoteBytesRead(
  128. const struct hdfsReadStatistics *stats);
  129. /**
  130. * Clear the read statistics for a file.
  131. *
  132. * @param file The file to clear the read statistics of.
  133. *
  134. * @return 0 on success; the error code otherwise.
  135. * EINVAL: the file is not open for reading.
  136. * ENOTSUP: the file does not support clearing the read
  137. * statistics.
  138. * Errno will also be set to this code on failure.
  139. */
  140. LIBHDFS_EXTERNAL
  141. int hdfsFileClearReadStatistics(hdfsFile file);
  142. /**
  143. * Free some HDFS read statistics.
  144. *
  145. * @param stats The HDFS read statistics to free.
  146. */
  147. LIBHDFS_EXTERNAL
  148. void hdfsFileFreeReadStatistics(struct hdfsReadStatistics *stats);
  149. /**
  150. * hdfsConnectAsUser - Connect to a hdfs file system as a specific user
  151. * Connect to the hdfs.
  152. * @param nn The NameNode. See hdfsBuilderSetNameNode for details.
  153. * @param port The port on which the server is listening.
  154. * @param user the user name (this is hadoop domain user). Or NULL is equivelant to hhdfsConnect(host, port)
  155. * @return Returns a handle to the filesystem or NULL on error.
  156. * @deprecated Use hdfsBuilderConnect instead.
  157. */
  158. LIBHDFS_EXTERNAL
  159. hdfsFS hdfsConnectAsUser(const char* nn, tPort port, const char *user);
  160. /**
  161. * hdfsConnect - Connect to a hdfs file system.
  162. * Connect to the hdfs.
  163. * @param nn The NameNode. See hdfsBuilderSetNameNode for details.
  164. * @param port The port on which the server is listening.
  165. * @return Returns a handle to the filesystem or NULL on error.
  166. * @deprecated Use hdfsBuilderConnect instead.
  167. */
  168. LIBHDFS_EXTERNAL
  169. hdfsFS hdfsConnect(const char* nn, tPort port);
  170. /**
  171. * hdfsConnect - Connect to an hdfs file system.
  172. *
  173. * Forces a new instance to be created
  174. *
  175. * @param nn The NameNode. See hdfsBuilderSetNameNode for details.
  176. * @param port The port on which the server is listening.
  177. * @param user The user name to use when connecting
  178. * @return Returns a handle to the filesystem or NULL on error.
  179. * @deprecated Use hdfsBuilderConnect instead.
  180. */
  181. LIBHDFS_EXTERNAL
  182. hdfsFS hdfsConnectAsUserNewInstance(const char* nn, tPort port, const char *user );
  183. /**
  184. * hdfsConnect - Connect to an hdfs file system.
  185. *
  186. * Forces a new instance to be created
  187. *
  188. * @param nn The NameNode. See hdfsBuilderSetNameNode for details.
  189. * @param port The port on which the server is listening.
  190. * @return Returns a handle to the filesystem or NULL on error.
  191. * @deprecated Use hdfsBuilderConnect instead.
  192. */
  193. LIBHDFS_EXTERNAL
  194. hdfsFS hdfsConnectNewInstance(const char* nn, tPort port);
  195. /**
  196. * Connect to HDFS using the parameters defined by the builder.
  197. *
  198. * The HDFS builder will be freed, whether or not the connection was
  199. * successful.
  200. *
  201. * Every successful call to hdfsBuilderConnect should be matched with a call
  202. * to hdfsDisconnect, when the hdfsFS is no longer needed.
  203. *
  204. * @param bld The HDFS builder
  205. * @return Returns a handle to the filesystem, or NULL on error.
  206. */
  207. LIBHDFS_EXTERNAL
  208. hdfsFS hdfsBuilderConnect(struct hdfsBuilder *bld);
  209. /**
  210. * Create an HDFS builder.
  211. *
  212. * @return The HDFS builder, or NULL on error.
  213. */
  214. LIBHDFS_EXTERNAL
  215. struct hdfsBuilder *hdfsNewBuilder(void);
  216. /**
  217. * Force the builder to always create a new instance of the FileSystem,
  218. * rather than possibly finding one in the cache.
  219. *
  220. * @param bld The HDFS builder
  221. */
  222. LIBHDFS_EXTERNAL
  223. void hdfsBuilderSetForceNewInstance(struct hdfsBuilder *bld);
  224. /**
  225. * Set the HDFS NameNode to connect to.
  226. *
  227. * @param bld The HDFS builder
  228. * @param nn The NameNode to use.
  229. *
  230. * If the string given is 'default', the default NameNode
  231. * configuration will be used (from the XML configuration files)
  232. *
  233. * If NULL is given, a LocalFileSystem will be created.
  234. *
  235. * If the string starts with a protocol type such as file:// or
  236. * hdfs://, this protocol type will be used. If not, the
  237. * hdfs:// protocol type will be used.
  238. *
  239. * You may specify a NameNode port in the usual way by
  240. * passing a string of the format hdfs://<hostname>:<port>.
  241. * Alternately, you may set the port with
  242. * hdfsBuilderSetNameNodePort. However, you must not pass the
  243. * port in two different ways.
  244. */
  245. LIBHDFS_EXTERNAL
  246. void hdfsBuilderSetNameNode(struct hdfsBuilder *bld, const char *nn);
  247. /**
  248. * Set the port of the HDFS NameNode to connect to.
  249. *
  250. * @param bld The HDFS builder
  251. * @param port The port.
  252. */
  253. LIBHDFS_EXTERNAL
  254. void hdfsBuilderSetNameNodePort(struct hdfsBuilder *bld, tPort port);
  255. /**
  256. * Set the username to use when connecting to the HDFS cluster.
  257. *
  258. * @param bld The HDFS builder
  259. * @param userName The user name. The string will be shallow-copied.
  260. */
  261. LIBHDFS_EXTERNAL
  262. void hdfsBuilderSetUserName(struct hdfsBuilder *bld, const char *userName);
  263. /**
  264. * Set the path to the Kerberos ticket cache to use when connecting to
  265. * the HDFS cluster.
  266. *
  267. * @param bld The HDFS builder
  268. * @param kerbTicketCachePath The Kerberos ticket cache path. The string
  269. * will be shallow-copied.
  270. */
  271. LIBHDFS_EXTERNAL
  272. void hdfsBuilderSetKerbTicketCachePath(struct hdfsBuilder *bld,
  273. const char *kerbTicketCachePath);
  274. /**
  275. * Free an HDFS builder.
  276. *
  277. * It is normally not necessary to call this function since
  278. * hdfsBuilderConnect frees the builder.
  279. *
  280. * @param bld The HDFS builder
  281. */
  282. LIBHDFS_EXTERNAL
  283. void hdfsFreeBuilder(struct hdfsBuilder *bld);
  284. /**
  285. * Set a configuration string for an HdfsBuilder.
  286. *
  287. * @param key The key to set.
  288. * @param val The value, or NULL to set no value.
  289. * This will be shallow-copied. You are responsible for
  290. * ensuring that it remains valid until the builder is
  291. * freed.
  292. *
  293. * @return 0 on success; nonzero error code otherwise.
  294. */
  295. LIBHDFS_EXTERNAL
  296. int hdfsBuilderConfSetStr(struct hdfsBuilder *bld, const char *key,
  297. const char *val);
  298. /**
  299. * Get a configuration string.
  300. *
  301. * @param key The key to find
  302. * @param val (out param) The value. This will be set to NULL if the
  303. * key isn't found. You must free this string with
  304. * hdfsConfStrFree.
  305. *
  306. * @return 0 on success; nonzero error code otherwise.
  307. * Failure to find the key is not an error.
  308. */
  309. LIBHDFS_EXTERNAL
  310. int hdfsConfGetStr(const char *key, char **val);
  311. /**
  312. * Get a configuration integer.
  313. *
  314. * @param key The key to find
  315. * @param val (out param) The value. This will NOT be changed if the
  316. * key isn't found.
  317. *
  318. * @return 0 on success; nonzero error code otherwise.
  319. * Failure to find the key is not an error.
  320. */
  321. LIBHDFS_EXTERNAL
  322. int hdfsConfGetInt(const char *key, int32_t *val);
  323. /**
  324. * Free a configuration string found with hdfsConfGetStr.
  325. *
  326. * @param val A configuration string obtained from hdfsConfGetStr
  327. */
  328. LIBHDFS_EXTERNAL
  329. void hdfsConfStrFree(char *val);
  330. /**
  331. * hdfsDisconnect - Disconnect from the hdfs file system.
  332. * Disconnect from hdfs.
  333. * @param fs The configured filesystem handle.
  334. * @return Returns 0 on success, -1 on error.
  335. * Even if there is an error, the resources associated with the
  336. * hdfsFS will be freed.
  337. */
  338. LIBHDFS_EXTERNAL
  339. int hdfsDisconnect(hdfsFS fs);
  340. /**
  341. * hdfsOpenFile - Open a hdfs file in given mode.
  342. * @param fs The configured filesystem handle.
  343. * @param path The full path to the file.
  344. * @param flags - an | of bits/fcntl.h file flags - supported flags are O_RDONLY, O_WRONLY (meaning create or overwrite i.e., implies O_TRUNCAT),
  345. * O_WRONLY|O_APPEND. Other flags are generally ignored other than (O_RDWR || (O_EXCL & O_CREAT)) which return NULL and set errno equal ENOTSUP.
  346. * @param bufferSize Size of buffer for read/write - pass 0 if you want
  347. * to use the default configured values.
  348. * @param replication Block replication - pass 0 if you want to use
  349. * the default configured values.
  350. * @param blocksize Size of block - pass 0 if you want to use the
  351. * default configured values.
  352. * @return Returns the handle to the open file or NULL on error.
  353. */
  354. LIBHDFS_EXTERNAL
  355. hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags,
  356. int bufferSize, short replication, tSize blocksize);
  357. /**
  358. * hdfsUnbufferFile - Reduce the buffering done on a file.
  359. *
  360. * @param file The file to unbuffer.
  361. * @return 0 on success
  362. * ENOTSUP if the file does not support unbuffering
  363. * Errno will also be set to this value.
  364. */
  365. LIBHDFS_EXTERNAL
  366. int hdfsUnbufferFile(hdfsFile file);
  367. /**
  368. * hdfsCloseFile - Close an open file.
  369. * @param fs The configured filesystem handle.
  370. * @param file The file handle.
  371. * @return Returns 0 on success, -1 on error.
  372. * On error, errno will be set appropriately.
  373. * If the hdfs file was valid, the memory associated with it will
  374. * be freed at the end of this call, even if there was an I/O
  375. * error.
  376. */
  377. LIBHDFS_EXTERNAL
  378. int hdfsCloseFile(hdfsFS fs, hdfsFile file);
  379. /**
  380. * hdfsExists - Checks if a given path exsits on the filesystem
  381. * @param fs The configured filesystem handle.
  382. * @param path The path to look for
  383. * @return Returns 0 on success, -1 on error.
  384. */
  385. LIBHDFS_EXTERNAL
  386. int hdfsExists(hdfsFS fs, const char *path);
  387. /**
  388. * hdfsSeek - Seek to given offset in file.
  389. * This works only for files opened in read-only mode.
  390. * @param fs The configured filesystem handle.
  391. * @param file The file handle.
  392. * @param desiredPos Offset into the file to seek into.
  393. * @return Returns 0 on success, -1 on error.
  394. */
  395. LIBHDFS_EXTERNAL
  396. int hdfsSeek(hdfsFS fs, hdfsFile file, tOffset desiredPos);
  397. /**
  398. * hdfsTell - Get the current offset in the file, in bytes.
  399. * @param fs The configured filesystem handle.
  400. * @param file The file handle.
  401. * @return Current offset, -1 on error.
  402. */
  403. LIBHDFS_EXTERNAL
  404. tOffset hdfsTell(hdfsFS fs, hdfsFile file);
  405. /**
  406. * hdfsRead - Read data from an open file.
  407. * @param fs The configured filesystem handle.
  408. * @param file The file handle.
  409. * @param buffer The buffer to copy read bytes into.
  410. * @param length The length of the buffer.
  411. * @return On success, a positive number indicating how many bytes
  412. * were read.
  413. * On end-of-file, 0.
  414. * On error, -1. Errno will be set to the error code.
  415. * Just like the POSIX read function, hdfsRead will return -1
  416. * and set errno to EINTR if data is temporarily unavailable,
  417. * but we are not yet at the end of the file.
  418. */
  419. LIBHDFS_EXTERNAL
  420. tSize hdfsRead(hdfsFS fs, hdfsFile file, void* buffer, tSize length);
  421. /**
  422. * hdfsPread - Positional read of data from an open file.
  423. * @param fs The configured filesystem handle.
  424. * @param file The file handle.
  425. * @param position Position from which to read
  426. * @param buffer The buffer to copy read bytes into.
  427. * @param length The length of the buffer.
  428. * @return See hdfsRead
  429. */
  430. LIBHDFS_EXTERNAL
  431. tSize hdfsPread(hdfsFS fs, hdfsFile file, tOffset position,
  432. void* buffer, tSize length);
  433. /**
  434. * hdfsWrite - Write data into an open file.
  435. * @param fs The configured filesystem handle.
  436. * @param file The file handle.
  437. * @param buffer The data.
  438. * @param length The no. of bytes to write.
  439. * @return Returns the number of bytes written, -1 on error.
  440. */
  441. LIBHDFS_EXTERNAL
  442. tSize hdfsWrite(hdfsFS fs, hdfsFile file, const void* buffer,
  443. tSize length);
  444. /**
  445. * hdfsWrite - Flush the data.
  446. * @param fs The configured filesystem handle.
  447. * @param file The file handle.
  448. * @return Returns 0 on success, -1 on error.
  449. */
  450. LIBHDFS_EXTERNAL
  451. int hdfsFlush(hdfsFS fs, hdfsFile file);
  452. /**
  453. * hdfsHFlush - Flush out the data in client's user buffer. After the
  454. * return of this call, new readers will see the data.
  455. * @param fs configured filesystem handle
  456. * @param file file handle
  457. * @return 0 on success, -1 on error and sets errno
  458. */
  459. LIBHDFS_EXTERNAL
  460. int hdfsHFlush(hdfsFS fs, hdfsFile file);
  461. /**
  462. * hdfsHSync - Similar to posix fsync, Flush out the data in client's
  463. * user buffer. all the way to the disk device (but the disk may have
  464. * it in its cache).
  465. * @param fs configured filesystem handle
  466. * @param file file handle
  467. * @return 0 on success, -1 on error and sets errno
  468. */
  469. LIBHDFS_EXTERNAL
  470. int hdfsHSync(hdfsFS fs, hdfsFile file);
  471. /**
  472. * hdfsAvailable - Number of bytes that can be read from this
  473. * input stream without blocking.
  474. * @param fs The configured filesystem handle.
  475. * @param file The file handle.
  476. * @return Returns available bytes; -1 on error.
  477. */
  478. LIBHDFS_EXTERNAL
  479. int hdfsAvailable(hdfsFS fs, hdfsFile file);
  480. /**
  481. * hdfsCopy - Copy file from one filesystem to another.
  482. * @param srcFS The handle to source filesystem.
  483. * @param src The path of source file.
  484. * @param dstFS The handle to destination filesystem.
  485. * @param dst The path of destination file.
  486. * @return Returns 0 on success, -1 on error.
  487. */
  488. LIBHDFS_EXTERNAL
  489. int hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst);
  490. /**
  491. * hdfsMove - Move file from one filesystem to another.
  492. * @param srcFS The handle to source filesystem.
  493. * @param src The path of source file.
  494. * @param dstFS The handle to destination filesystem.
  495. * @param dst The path of destination file.
  496. * @return Returns 0 on success, -1 on error.
  497. */
  498. LIBHDFS_EXTERNAL
  499. int hdfsMove(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst);
  500. /**
  501. * hdfsDelete - Delete file.
  502. * @param fs The configured filesystem handle.
  503. * @param path The path of the file.
  504. * @param recursive if path is a directory and set to
  505. * non-zero, the directory is deleted else throws an exception. In
  506. * case of a file the recursive argument is irrelevant.
  507. * @return Returns 0 on success, -1 on error.
  508. */
  509. LIBHDFS_EXTERNAL
  510. int hdfsDelete(hdfsFS fs, const char* path, int recursive);
  511. /**
  512. * hdfsRename - Rename file.
  513. * @param fs The configured filesystem handle.
  514. * @param oldPath The path of the source file.
  515. * @param newPath The path of the destination file.
  516. * @return Returns 0 on success, -1 on error.
  517. */
  518. LIBHDFS_EXTERNAL
  519. int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath);
  520. /**
  521. * hdfsGetWorkingDirectory - Get the current working directory for
  522. * the given filesystem.
  523. * @param fs The configured filesystem handle.
  524. * @param buffer The user-buffer to copy path of cwd into.
  525. * @param bufferSize The length of user-buffer.
  526. * @return Returns buffer, NULL on error.
  527. */
  528. LIBHDFS_EXTERNAL
  529. char* hdfsGetWorkingDirectory(hdfsFS fs, char *buffer, size_t bufferSize);
  530. /**
  531. * hdfsSetWorkingDirectory - Set the working directory. All relative
  532. * paths will be resolved relative to it.
  533. * @param fs The configured filesystem handle.
  534. * @param path The path of the new 'cwd'.
  535. * @return Returns 0 on success, -1 on error.
  536. */
  537. LIBHDFS_EXTERNAL
  538. int hdfsSetWorkingDirectory(hdfsFS fs, const char* path);
  539. /**
  540. * hdfsCreateDirectory - Make the given file and all non-existent
  541. * parents into directories.
  542. * @param fs The configured filesystem handle.
  543. * @param path The path of the directory.
  544. * @return Returns 0 on success, -1 on error.
  545. */
  546. LIBHDFS_EXTERNAL
  547. int hdfsCreateDirectory(hdfsFS fs, const char* path);
  548. /**
  549. * hdfsSetReplication - Set the replication of the specified
  550. * file to the supplied value
  551. * @param fs The configured filesystem handle.
  552. * @param path The path of the file.
  553. * @return Returns 0 on success, -1 on error.
  554. */
  555. LIBHDFS_EXTERNAL
  556. int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication);
  557. /**
  558. * hdfsFileInfo - Information about a file/directory.
  559. */
  560. typedef struct {
  561. tObjectKind mKind; /* file or directory */
  562. char *mName; /* the name of the file */
  563. tTime mLastMod; /* the last modification time for the file in seconds */
  564. tOffset mSize; /* the size of the file in bytes */
  565. short mReplication; /* the count of replicas */
  566. tOffset mBlockSize; /* the block size for the file */
  567. char *mOwner; /* the owner of the file */
  568. char *mGroup; /* the group associated with the file */
  569. short mPermissions; /* the permissions associated with the file */
  570. tTime mLastAccess; /* the last access time for the file in seconds */
  571. } hdfsFileInfo;
  572. /**
  573. * hdfsListDirectory - Get list of files/directories for a given
  574. * directory-path. hdfsFreeFileInfo should be called to deallocate memory.
  575. * @param fs The configured filesystem handle.
  576. * @param path The path of the directory.
  577. * @param numEntries Set to the number of files/directories in path.
  578. * @return Returns a dynamically-allocated array of hdfsFileInfo
  579. * objects; NULL on error.
  580. */
  581. LIBHDFS_EXTERNAL
  582. hdfsFileInfo *hdfsListDirectory(hdfsFS fs, const char* path,
  583. int *numEntries);
  584. /**
  585. * hdfsGetPathInfo - Get information about a path as a (dynamically
  586. * allocated) single hdfsFileInfo struct. hdfsFreeFileInfo should be
  587. * called when the pointer is no longer needed.
  588. * @param fs The configured filesystem handle.
  589. * @param path The path of the file.
  590. * @return Returns a dynamically-allocated hdfsFileInfo object;
  591. * NULL on error.
  592. */
  593. LIBHDFS_EXTERNAL
  594. hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path);
  595. /**
  596. * hdfsFreeFileInfo - Free up the hdfsFileInfo array (including fields)
  597. * @param hdfsFileInfo The array of dynamically-allocated hdfsFileInfo
  598. * objects.
  599. * @param numEntries The size of the array.
  600. */
  601. LIBHDFS_EXTERNAL
  602. void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries);
  603. /**
  604. * hdfsFileIsEncrypted: determine if a file is encrypted based on its
  605. * hdfsFileInfo.
  606. * @return -1 if there was an error (errno will be set), 0 if the file is
  607. * not encrypted, 1 if the file is encrypted.
  608. */
  609. LIBHDFS_EXTERNAL
  610. int hdfsFileIsEncrypted(hdfsFileInfo *hdfsFileInfo);
  611. /**
  612. * hdfsGetHosts - Get hostnames where a particular block (determined by
  613. * pos & blocksize) of a file is stored. The last element in the array
  614. * is NULL. Due to replication, a single block could be present on
  615. * multiple hosts.
  616. * @param fs The configured filesystem handle.
  617. * @param path The path of the file.
  618. * @param start The start of the block.
  619. * @param length The length of the block.
  620. * @return Returns a dynamically-allocated 2-d array of blocks-hosts;
  621. * NULL on error.
  622. */
  623. LIBHDFS_EXTERNAL
  624. char*** hdfsGetHosts(hdfsFS fs, const char* path,
  625. tOffset start, tOffset length);
  626. /**
  627. * hdfsFreeHosts - Free up the structure returned by hdfsGetHosts
  628. * @param hdfsFileInfo The array of dynamically-allocated hdfsFileInfo
  629. * objects.
  630. * @param numEntries The size of the array.
  631. */
  632. LIBHDFS_EXTERNAL
  633. void hdfsFreeHosts(char ***blockHosts);
  634. /**
  635. * hdfsGetDefaultBlockSize - Get the default blocksize.
  636. *
  637. * @param fs The configured filesystem handle.
  638. * @deprecated Use hdfsGetDefaultBlockSizeAtPath instead.
  639. *
  640. * @return Returns the default blocksize, or -1 on error.
  641. */
  642. LIBHDFS_EXTERNAL
  643. tOffset hdfsGetDefaultBlockSize(hdfsFS fs);
  644. /**
  645. * hdfsGetDefaultBlockSizeAtPath - Get the default blocksize at the
  646. * filesystem indicated by a given path.
  647. *
  648. * @param fs The configured filesystem handle.
  649. * @param path The given path will be used to locate the actual
  650. * filesystem. The full path does not have to exist.
  651. *
  652. * @return Returns the default blocksize, or -1 on error.
  653. */
  654. LIBHDFS_EXTERNAL
  655. tOffset hdfsGetDefaultBlockSizeAtPath(hdfsFS fs, const char *path);
  656. /**
  657. * hdfsGetCapacity - Return the raw capacity of the filesystem.
  658. * @param fs The configured filesystem handle.
  659. * @return Returns the raw-capacity; -1 on error.
  660. */
  661. LIBHDFS_EXTERNAL
  662. tOffset hdfsGetCapacity(hdfsFS fs);
  663. /**
  664. * hdfsGetUsed - Return the total raw size of all files in the filesystem.
  665. * @param fs The configured filesystem handle.
  666. * @return Returns the total-size; -1 on error.
  667. */
  668. LIBHDFS_EXTERNAL
  669. tOffset hdfsGetUsed(hdfsFS fs);
  670. /**
  671. * Change the user and/or group of a file or directory.
  672. *
  673. * @param fs The configured filesystem handle.
  674. * @param path the path to the file or directory
  675. * @param owner User string. Set to NULL for 'no change'
  676. * @param group Group string. Set to NULL for 'no change'
  677. * @return 0 on success else -1
  678. */
  679. LIBHDFS_EXTERNAL
  680. int hdfsChown(hdfsFS fs, const char* path, const char *owner,
  681. const char *group);
  682. /**
  683. * hdfsChmod
  684. * @param fs The configured filesystem handle.
  685. * @param path the path to the file or directory
  686. * @param mode the bitmask to set it to
  687. * @return 0 on success else -1
  688. */
  689. LIBHDFS_EXTERNAL
  690. int hdfsChmod(hdfsFS fs, const char* path, short mode);
  691. /**
  692. * hdfsUtime
  693. * @param fs The configured filesystem handle.
  694. * @param path the path to the file or directory
  695. * @param mtime new modification time or -1 for no change
  696. * @param atime new access time or -1 for no change
  697. * @return 0 on success else -1
  698. */
  699. LIBHDFS_EXTERNAL
  700. int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime);
  701. /**
  702. * Allocate a zero-copy options structure.
  703. *
  704. * You must free all options structures allocated with this function using
  705. * hadoopRzOptionsFree.
  706. *
  707. * @return A zero-copy options structure, or NULL if one could
  708. * not be allocated. If NULL is returned, errno will
  709. * contain the error number.
  710. */
  711. LIBHDFS_EXTERNAL
  712. struct hadoopRzOptions *hadoopRzOptionsAlloc(void);
  713. /**
  714. * Determine whether we should skip checksums in read0.
  715. *
  716. * @param opts The options structure.
  717. * @param skip Nonzero to skip checksums sometimes; zero to always
  718. * check them.
  719. *
  720. * @return 0 on success; -1 plus errno on failure.
  721. */
  722. LIBHDFS_EXTERNAL
  723. int hadoopRzOptionsSetSkipChecksum(
  724. struct hadoopRzOptions *opts, int skip);
  725. /**
  726. * Set the ByteBufferPool to use with read0.
  727. *
  728. * @param opts The options structure.
  729. * @param className If this is NULL, we will not use any
  730. * ByteBufferPool. If this is non-NULL, it will be
  731. * treated as the name of the pool class to use.
  732. * For example, you can use
  733. * ELASTIC_BYTE_BUFFER_POOL_CLASS.
  734. *
  735. * @return 0 if the ByteBufferPool class was found and
  736. * instantiated;
  737. * -1 plus errno otherwise.
  738. */
  739. LIBHDFS_EXTERNAL
  740. int hadoopRzOptionsSetByteBufferPool(
  741. struct hadoopRzOptions *opts, const char *className);
  742. /**
  743. * Free a hadoopRzOptionsFree structure.
  744. *
  745. * @param opts The options structure to free.
  746. * Any associated ByteBufferPool will also be freed.
  747. */
  748. LIBHDFS_EXTERNAL
  749. void hadoopRzOptionsFree(struct hadoopRzOptions *opts);
  750. /**
  751. * Perform a byte buffer read.
  752. * If possible, this will be a zero-copy (mmap) read.
  753. *
  754. * @param file The file to read from.
  755. * @param opts An options structure created by hadoopRzOptionsAlloc.
  756. * @param maxLength The maximum length to read. We may read fewer bytes
  757. * than this length.
  758. *
  759. * @return On success, we will return a new hadoopRzBuffer.
  760. * This buffer will continue to be valid and readable
  761. * until it is released by readZeroBufferFree. Failure to
  762. * release a buffer will lead to a memory leak.
  763. * You can access the data within the hadoopRzBuffer with
  764. * hadoopRzBufferGet. If you have reached EOF, the data
  765. * within the hadoopRzBuffer will be NULL. You must still
  766. * free hadoopRzBuffer instances containing NULL.
  767. *
  768. * On failure, we will return NULL plus an errno code.
  769. * errno = EOPNOTSUPP indicates that we could not do a
  770. * zero-copy read, and there was no ByteBufferPool
  771. * supplied.
  772. */
  773. LIBHDFS_EXTERNAL
  774. struct hadoopRzBuffer* hadoopReadZero(hdfsFile file,
  775. struct hadoopRzOptions *opts, int32_t maxLength);
  776. /**
  777. * Determine the length of the buffer returned from readZero.
  778. *
  779. * @param buffer a buffer returned from readZero.
  780. * @return the length of the buffer.
  781. */
  782. LIBHDFS_EXTERNAL
  783. int32_t hadoopRzBufferLength(const struct hadoopRzBuffer *buffer);
  784. /**
  785. * Get a pointer to the raw buffer returned from readZero.
  786. *
  787. * To find out how many bytes this buffer contains, call
  788. * hadoopRzBufferLength.
  789. *
  790. * @param buffer a buffer returned from readZero.
  791. * @return a pointer to the start of the buffer. This will be
  792. * NULL when end-of-file has been reached.
  793. */
  794. LIBHDFS_EXTERNAL
  795. const void *hadoopRzBufferGet(const struct hadoopRzBuffer *buffer);
  796. /**
  797. * Release a buffer obtained through readZero.
  798. *
  799. * @param file The hdfs stream that created this buffer. This must be
  800. * the same stream you called hadoopReadZero on.
  801. * @param buffer The buffer to release.
  802. */
  803. LIBHDFS_EXTERNAL
  804. void hadoopRzBufferFree(hdfsFile file, struct hadoopRzBuffer *buffer);
  805. #ifdef __cplusplus
  806. }
  807. #endif
  808. #undef LIBHDFS_EXTERNAL
  809. #endif /*LIBHDFS_HDFS_H*/
  810. /**
  811. * vim: ts=4: sw=4: et
  812. */