hdfs.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. * hdfsTruncateFile - Truncate a hdfs file to given lenght.
  359. * @param fs The configured filesystem handle.
  360. * @param path The full path to the file.
  361. * @param newlength The size the file is to be truncated to
  362. * @return 1 if the file has been truncated to the desired newlength
  363. * and is immediately available to be reused for write operations
  364. * such as append.
  365. * 0 if a background process of adjusting the length of the last
  366. * block has been started, and clients should wait for it to
  367. * complete before proceeding with further file updates.
  368. * -1 on error.
  369. */
  370. int hdfsTruncateFile(hdfsFS fs, const char* path, tOffset newlength);
  371. /**
  372. * hdfsUnbufferFile - Reduce the buffering done on a file.
  373. *
  374. * @param file The file to unbuffer.
  375. * @return 0 on success
  376. * ENOTSUP if the file does not support unbuffering
  377. * Errno will also be set to this value.
  378. */
  379. LIBHDFS_EXTERNAL
  380. int hdfsUnbufferFile(hdfsFile file);
  381. /**
  382. * hdfsCloseFile - Close an open file.
  383. * @param fs The configured filesystem handle.
  384. * @param file The file handle.
  385. * @return Returns 0 on success, -1 on error.
  386. * On error, errno will be set appropriately.
  387. * If the hdfs file was valid, the memory associated with it will
  388. * be freed at the end of this call, even if there was an I/O
  389. * error.
  390. */
  391. LIBHDFS_EXTERNAL
  392. int hdfsCloseFile(hdfsFS fs, hdfsFile file);
  393. /**
  394. * hdfsExists - Checks if a given path exsits on the filesystem
  395. * @param fs The configured filesystem handle.
  396. * @param path The path to look for
  397. * @return Returns 0 on success, -1 on error.
  398. */
  399. LIBHDFS_EXTERNAL
  400. int hdfsExists(hdfsFS fs, const char *path);
  401. /**
  402. * hdfsSeek - Seek to given offset in file.
  403. * This works only for files opened in read-only mode.
  404. * @param fs The configured filesystem handle.
  405. * @param file The file handle.
  406. * @param desiredPos Offset into the file to seek into.
  407. * @return Returns 0 on success, -1 on error.
  408. */
  409. LIBHDFS_EXTERNAL
  410. int hdfsSeek(hdfsFS fs, hdfsFile file, tOffset desiredPos);
  411. /**
  412. * hdfsTell - Get the current offset in the file, in bytes.
  413. * @param fs The configured filesystem handle.
  414. * @param file The file handle.
  415. * @return Current offset, -1 on error.
  416. */
  417. LIBHDFS_EXTERNAL
  418. tOffset hdfsTell(hdfsFS fs, hdfsFile file);
  419. /**
  420. * hdfsRead - Read data from an open file.
  421. * @param fs The configured filesystem handle.
  422. * @param file The file handle.
  423. * @param buffer The buffer to copy read bytes into.
  424. * @param length The length of the buffer.
  425. * @return On success, a positive number indicating how many bytes
  426. * were read.
  427. * On end-of-file, 0.
  428. * On error, -1. Errno will be set to the error code.
  429. * Just like the POSIX read function, hdfsRead will return -1
  430. * and set errno to EINTR if data is temporarily unavailable,
  431. * but we are not yet at the end of the file.
  432. */
  433. LIBHDFS_EXTERNAL
  434. tSize hdfsRead(hdfsFS fs, hdfsFile file, void* buffer, tSize length);
  435. /**
  436. * hdfsPread - Positional read of data from an open file.
  437. * @param fs The configured filesystem handle.
  438. * @param file The file handle.
  439. * @param position Position from which to read
  440. * @param buffer The buffer to copy read bytes into.
  441. * @param length The length of the buffer.
  442. * @return See hdfsRead
  443. */
  444. LIBHDFS_EXTERNAL
  445. tSize hdfsPread(hdfsFS fs, hdfsFile file, tOffset position,
  446. void* buffer, tSize length);
  447. /**
  448. * hdfsWrite - Write data into an open file.
  449. * @param fs The configured filesystem handle.
  450. * @param file The file handle.
  451. * @param buffer The data.
  452. * @param length The no. of bytes to write.
  453. * @return Returns the number of bytes written, -1 on error.
  454. */
  455. LIBHDFS_EXTERNAL
  456. tSize hdfsWrite(hdfsFS fs, hdfsFile file, const void* buffer,
  457. tSize length);
  458. /**
  459. * hdfsWrite - Flush the data.
  460. * @param fs The configured filesystem handle.
  461. * @param file The file handle.
  462. * @return Returns 0 on success, -1 on error.
  463. */
  464. LIBHDFS_EXTERNAL
  465. int hdfsFlush(hdfsFS fs, hdfsFile file);
  466. /**
  467. * hdfsHFlush - Flush out the data in client's user buffer. After the
  468. * return of this call, new readers will see the data.
  469. * @param fs configured filesystem handle
  470. * @param file file handle
  471. * @return 0 on success, -1 on error and sets errno
  472. */
  473. LIBHDFS_EXTERNAL
  474. int hdfsHFlush(hdfsFS fs, hdfsFile file);
  475. /**
  476. * hdfsHSync - Similar to posix fsync, Flush out the data in client's
  477. * user buffer. all the way to the disk device (but the disk may have
  478. * it in its cache).
  479. * @param fs configured filesystem handle
  480. * @param file file handle
  481. * @return 0 on success, -1 on error and sets errno
  482. */
  483. LIBHDFS_EXTERNAL
  484. int hdfsHSync(hdfsFS fs, hdfsFile file);
  485. /**
  486. * hdfsAvailable - Number of bytes that can be read from this
  487. * input stream without blocking.
  488. * @param fs The configured filesystem handle.
  489. * @param file The file handle.
  490. * @return Returns available bytes; -1 on error.
  491. */
  492. LIBHDFS_EXTERNAL
  493. int hdfsAvailable(hdfsFS fs, hdfsFile file);
  494. /**
  495. * hdfsCopy - Copy file from one filesystem to another.
  496. * @param srcFS The handle to source filesystem.
  497. * @param src The path of source file.
  498. * @param dstFS The handle to destination filesystem.
  499. * @param dst The path of destination file.
  500. * @return Returns 0 on success, -1 on error.
  501. */
  502. LIBHDFS_EXTERNAL
  503. int hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst);
  504. /**
  505. * hdfsMove - Move file from one filesystem to another.
  506. * @param srcFS The handle to source filesystem.
  507. * @param src The path of source file.
  508. * @param dstFS The handle to destination filesystem.
  509. * @param dst The path of destination file.
  510. * @return Returns 0 on success, -1 on error.
  511. */
  512. LIBHDFS_EXTERNAL
  513. int hdfsMove(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst);
  514. /**
  515. * hdfsDelete - Delete file.
  516. * @param fs The configured filesystem handle.
  517. * @param path The path of the file.
  518. * @param recursive if path is a directory and set to
  519. * non-zero, the directory is deleted else throws an exception. In
  520. * case of a file the recursive argument is irrelevant.
  521. * @return Returns 0 on success, -1 on error.
  522. */
  523. LIBHDFS_EXTERNAL
  524. int hdfsDelete(hdfsFS fs, const char* path, int recursive);
  525. /**
  526. * hdfsRename - Rename file.
  527. * @param fs The configured filesystem handle.
  528. * @param oldPath The path of the source file.
  529. * @param newPath The path of the destination file.
  530. * @return Returns 0 on success, -1 on error.
  531. */
  532. LIBHDFS_EXTERNAL
  533. int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath);
  534. /**
  535. * hdfsGetWorkingDirectory - Get the current working directory for
  536. * the given filesystem.
  537. * @param fs The configured filesystem handle.
  538. * @param buffer The user-buffer to copy path of cwd into.
  539. * @param bufferSize The length of user-buffer.
  540. * @return Returns buffer, NULL on error.
  541. */
  542. LIBHDFS_EXTERNAL
  543. char* hdfsGetWorkingDirectory(hdfsFS fs, char *buffer, size_t bufferSize);
  544. /**
  545. * hdfsSetWorkingDirectory - Set the working directory. All relative
  546. * paths will be resolved relative to it.
  547. * @param fs The configured filesystem handle.
  548. * @param path The path of the new 'cwd'.
  549. * @return Returns 0 on success, -1 on error.
  550. */
  551. LIBHDFS_EXTERNAL
  552. int hdfsSetWorkingDirectory(hdfsFS fs, const char* path);
  553. /**
  554. * hdfsCreateDirectory - Make the given file and all non-existent
  555. * parents into directories.
  556. * @param fs The configured filesystem handle.
  557. * @param path The path of the directory.
  558. * @return Returns 0 on success, -1 on error.
  559. */
  560. LIBHDFS_EXTERNAL
  561. int hdfsCreateDirectory(hdfsFS fs, const char* path);
  562. /**
  563. * hdfsSetReplication - Set the replication of the specified
  564. * file to the supplied value
  565. * @param fs The configured filesystem handle.
  566. * @param path The path of the file.
  567. * @return Returns 0 on success, -1 on error.
  568. */
  569. LIBHDFS_EXTERNAL
  570. int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication);
  571. /**
  572. * hdfsFileInfo - Information about a file/directory.
  573. */
  574. typedef struct {
  575. tObjectKind mKind; /* file or directory */
  576. char *mName; /* the name of the file */
  577. tTime mLastMod; /* the last modification time for the file in seconds */
  578. tOffset mSize; /* the size of the file in bytes */
  579. short mReplication; /* the count of replicas */
  580. tOffset mBlockSize; /* the block size for the file */
  581. char *mOwner; /* the owner of the file */
  582. char *mGroup; /* the group associated with the file */
  583. short mPermissions; /* the permissions associated with the file */
  584. tTime mLastAccess; /* the last access time for the file in seconds */
  585. } hdfsFileInfo;
  586. /**
  587. * hdfsListDirectory - Get list of files/directories for a given
  588. * directory-path. hdfsFreeFileInfo should be called to deallocate memory.
  589. * @param fs The configured filesystem handle.
  590. * @param path The path of the directory.
  591. * @param numEntries Set to the number of files/directories in path.
  592. * @return Returns a dynamically-allocated array of hdfsFileInfo
  593. * objects; NULL on error.
  594. */
  595. LIBHDFS_EXTERNAL
  596. hdfsFileInfo *hdfsListDirectory(hdfsFS fs, const char* path,
  597. int *numEntries);
  598. /**
  599. * hdfsGetPathInfo - Get information about a path as a (dynamically
  600. * allocated) single hdfsFileInfo struct. hdfsFreeFileInfo should be
  601. * called when the pointer is no longer needed.
  602. * @param fs The configured filesystem handle.
  603. * @param path The path of the file.
  604. * @return Returns a dynamically-allocated hdfsFileInfo object;
  605. * NULL on error.
  606. */
  607. LIBHDFS_EXTERNAL
  608. hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path);
  609. /**
  610. * hdfsFreeFileInfo - Free up the hdfsFileInfo array (including fields)
  611. * @param hdfsFileInfo The array of dynamically-allocated hdfsFileInfo
  612. * objects.
  613. * @param numEntries The size of the array.
  614. */
  615. LIBHDFS_EXTERNAL
  616. void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries);
  617. /**
  618. * hdfsFileIsEncrypted: determine if a file is encrypted based on its
  619. * hdfsFileInfo.
  620. * @return -1 if there was an error (errno will be set), 0 if the file is
  621. * not encrypted, 1 if the file is encrypted.
  622. */
  623. LIBHDFS_EXTERNAL
  624. int hdfsFileIsEncrypted(hdfsFileInfo *hdfsFileInfo);
  625. /**
  626. * hdfsGetHosts - Get hostnames where a particular block (determined by
  627. * pos & blocksize) of a file is stored. The last element in the array
  628. * is NULL. Due to replication, a single block could be present on
  629. * multiple hosts.
  630. * @param fs The configured filesystem handle.
  631. * @param path The path of the file.
  632. * @param start The start of the block.
  633. * @param length The length of the block.
  634. * @return Returns a dynamically-allocated 2-d array of blocks-hosts;
  635. * NULL on error.
  636. */
  637. LIBHDFS_EXTERNAL
  638. char*** hdfsGetHosts(hdfsFS fs, const char* path,
  639. tOffset start, tOffset length);
  640. /**
  641. * hdfsFreeHosts - Free up the structure returned by hdfsGetHosts
  642. * @param hdfsFileInfo The array of dynamically-allocated hdfsFileInfo
  643. * objects.
  644. * @param numEntries The size of the array.
  645. */
  646. LIBHDFS_EXTERNAL
  647. void hdfsFreeHosts(char ***blockHosts);
  648. /**
  649. * hdfsGetDefaultBlockSize - Get the default blocksize.
  650. *
  651. * @param fs The configured filesystem handle.
  652. * @deprecated Use hdfsGetDefaultBlockSizeAtPath instead.
  653. *
  654. * @return Returns the default blocksize, or -1 on error.
  655. */
  656. LIBHDFS_EXTERNAL
  657. tOffset hdfsGetDefaultBlockSize(hdfsFS fs);
  658. /**
  659. * hdfsGetDefaultBlockSizeAtPath - Get the default blocksize at the
  660. * filesystem indicated by a given path.
  661. *
  662. * @param fs The configured filesystem handle.
  663. * @param path The given path will be used to locate the actual
  664. * filesystem. The full path does not have to exist.
  665. *
  666. * @return Returns the default blocksize, or -1 on error.
  667. */
  668. LIBHDFS_EXTERNAL
  669. tOffset hdfsGetDefaultBlockSizeAtPath(hdfsFS fs, const char *path);
  670. /**
  671. * hdfsGetCapacity - Return the raw capacity of the filesystem.
  672. * @param fs The configured filesystem handle.
  673. * @return Returns the raw-capacity; -1 on error.
  674. */
  675. LIBHDFS_EXTERNAL
  676. tOffset hdfsGetCapacity(hdfsFS fs);
  677. /**
  678. * hdfsGetUsed - Return the total raw size of all files in the filesystem.
  679. * @param fs The configured filesystem handle.
  680. * @return Returns the total-size; -1 on error.
  681. */
  682. LIBHDFS_EXTERNAL
  683. tOffset hdfsGetUsed(hdfsFS fs);
  684. /**
  685. * Change the user and/or group of a file or directory.
  686. *
  687. * @param fs The configured filesystem handle.
  688. * @param path the path to the file or directory
  689. * @param owner User string. Set to NULL for 'no change'
  690. * @param group Group string. Set to NULL for 'no change'
  691. * @return 0 on success else -1
  692. */
  693. LIBHDFS_EXTERNAL
  694. int hdfsChown(hdfsFS fs, const char* path, const char *owner,
  695. const char *group);
  696. /**
  697. * hdfsChmod
  698. * @param fs The configured filesystem handle.
  699. * @param path the path to the file or directory
  700. * @param mode the bitmask to set it to
  701. * @return 0 on success else -1
  702. */
  703. LIBHDFS_EXTERNAL
  704. int hdfsChmod(hdfsFS fs, const char* path, short mode);
  705. /**
  706. * hdfsUtime
  707. * @param fs The configured filesystem handle.
  708. * @param path the path to the file or directory
  709. * @param mtime new modification time or -1 for no change
  710. * @param atime new access time or -1 for no change
  711. * @return 0 on success else -1
  712. */
  713. LIBHDFS_EXTERNAL
  714. int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime);
  715. /**
  716. * Allocate a zero-copy options structure.
  717. *
  718. * You must free all options structures allocated with this function using
  719. * hadoopRzOptionsFree.
  720. *
  721. * @return A zero-copy options structure, or NULL if one could
  722. * not be allocated. If NULL is returned, errno will
  723. * contain the error number.
  724. */
  725. LIBHDFS_EXTERNAL
  726. struct hadoopRzOptions *hadoopRzOptionsAlloc(void);
  727. /**
  728. * Determine whether we should skip checksums in read0.
  729. *
  730. * @param opts The options structure.
  731. * @param skip Nonzero to skip checksums sometimes; zero to always
  732. * check them.
  733. *
  734. * @return 0 on success; -1 plus errno on failure.
  735. */
  736. LIBHDFS_EXTERNAL
  737. int hadoopRzOptionsSetSkipChecksum(
  738. struct hadoopRzOptions *opts, int skip);
  739. /**
  740. * Set the ByteBufferPool to use with read0.
  741. *
  742. * @param opts The options structure.
  743. * @param className If this is NULL, we will not use any
  744. * ByteBufferPool. If this is non-NULL, it will be
  745. * treated as the name of the pool class to use.
  746. * For example, you can use
  747. * ELASTIC_BYTE_BUFFER_POOL_CLASS.
  748. *
  749. * @return 0 if the ByteBufferPool class was found and
  750. * instantiated;
  751. * -1 plus errno otherwise.
  752. */
  753. LIBHDFS_EXTERNAL
  754. int hadoopRzOptionsSetByteBufferPool(
  755. struct hadoopRzOptions *opts, const char *className);
  756. /**
  757. * Free a hadoopRzOptionsFree structure.
  758. *
  759. * @param opts The options structure to free.
  760. * Any associated ByteBufferPool will also be freed.
  761. */
  762. LIBHDFS_EXTERNAL
  763. void hadoopRzOptionsFree(struct hadoopRzOptions *opts);
  764. /**
  765. * Perform a byte buffer read.
  766. * If possible, this will be a zero-copy (mmap) read.
  767. *
  768. * @param file The file to read from.
  769. * @param opts An options structure created by hadoopRzOptionsAlloc.
  770. * @param maxLength The maximum length to read. We may read fewer bytes
  771. * than this length.
  772. *
  773. * @return On success, we will return a new hadoopRzBuffer.
  774. * This buffer will continue to be valid and readable
  775. * until it is released by readZeroBufferFree. Failure to
  776. * release a buffer will lead to a memory leak.
  777. * You can access the data within the hadoopRzBuffer with
  778. * hadoopRzBufferGet. If you have reached EOF, the data
  779. * within the hadoopRzBuffer will be NULL. You must still
  780. * free hadoopRzBuffer instances containing NULL.
  781. *
  782. * On failure, we will return NULL plus an errno code.
  783. * errno = EOPNOTSUPP indicates that we could not do a
  784. * zero-copy read, and there was no ByteBufferPool
  785. * supplied.
  786. */
  787. LIBHDFS_EXTERNAL
  788. struct hadoopRzBuffer* hadoopReadZero(hdfsFile file,
  789. struct hadoopRzOptions *opts, int32_t maxLength);
  790. /**
  791. * Determine the length of the buffer returned from readZero.
  792. *
  793. * @param buffer a buffer returned from readZero.
  794. * @return the length of the buffer.
  795. */
  796. LIBHDFS_EXTERNAL
  797. int32_t hadoopRzBufferLength(const struct hadoopRzBuffer *buffer);
  798. /**
  799. * Get a pointer to the raw buffer returned from readZero.
  800. *
  801. * To find out how many bytes this buffer contains, call
  802. * hadoopRzBufferLength.
  803. *
  804. * @param buffer a buffer returned from readZero.
  805. * @return a pointer to the start of the buffer. This will be
  806. * NULL when end-of-file has been reached.
  807. */
  808. LIBHDFS_EXTERNAL
  809. const void *hadoopRzBufferGet(const struct hadoopRzBuffer *buffer);
  810. /**
  811. * Release a buffer obtained through readZero.
  812. *
  813. * @param file The hdfs stream that created this buffer. This must be
  814. * the same stream you called hadoopReadZero on.
  815. * @param buffer The buffer to release.
  816. */
  817. LIBHDFS_EXTERNAL
  818. void hadoopRzBufferFree(hdfsFile file, struct hadoopRzBuffer *buffer);
  819. #ifdef __cplusplus
  820. }
  821. #endif
  822. #undef LIBHDFS_EXTERNAL
  823. #endif /*LIBHDFS_HDFS_H*/
  824. /**
  825. * vim: ts=4: sw=4: et
  826. */