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 | |
---|
19 | #ifndef LIBHDFS_HDFS_H |
---|
20 | #define LIBHDFS_HDFS_H |
---|
21 | |
---|
22 | #include <sys/types.h> |
---|
23 | #include <sys/stat.h> |
---|
24 | |
---|
25 | #include <fcntl.h> |
---|
26 | #include <stdio.h> |
---|
27 | #include <stdint.h> |
---|
28 | #include <string.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <time.h> |
---|
31 | #include <errno.h> |
---|
32 | |
---|
33 | #include <jni.h> |
---|
34 | |
---|
35 | #ifndef O_RDONLY |
---|
36 | #define O_RDONLY 1 |
---|
37 | #endif |
---|
38 | |
---|
39 | #ifndef O_WRONLY |
---|
40 | #define O_WRONLY 2 |
---|
41 | #endif |
---|
42 | |
---|
43 | #ifndef EINTERNAL |
---|
44 | #define EINTERNAL 255 |
---|
45 | #endif |
---|
46 | |
---|
47 | |
---|
48 | /** All APIs set errno to meaningful values */ |
---|
49 | #ifdef __cplusplus |
---|
50 | extern "C" { |
---|
51 | #endif |
---|
52 | |
---|
53 | /** |
---|
54 | * Some utility decls used in libhdfs. |
---|
55 | */ |
---|
56 | |
---|
57 | typedef int32_t tSize; /// size of data for read/write io ops |
---|
58 | typedef time_t tTime; /// time type in seconds |
---|
59 | typedef int64_t tOffset;/// offset within the file |
---|
60 | typedef uint16_t tPort; /// port |
---|
61 | typedef enum tObjectKind { |
---|
62 | kObjectKindFile = 'F', |
---|
63 | kObjectKindDirectory = 'D', |
---|
64 | } tObjectKind; |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | * The C reflection of org.apache.org.hadoop.FileSystem . |
---|
69 | */ |
---|
70 | typedef void* hdfsFS; |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * The C equivalent of org.apache.org.hadoop.FSData(Input|Output)Stream . |
---|
75 | */ |
---|
76 | enum hdfsStreamType |
---|
77 | { |
---|
78 | UNINITIALIZED = 0, |
---|
79 | INPUT = 1, |
---|
80 | OUTPUT = 2, |
---|
81 | }; |
---|
82 | |
---|
83 | |
---|
84 | /** |
---|
85 | * The 'file-handle' to a file in hdfs. |
---|
86 | */ |
---|
87 | struct hdfsFile_internal { |
---|
88 | void* file; |
---|
89 | enum hdfsStreamType type; |
---|
90 | }; |
---|
91 | typedef struct hdfsFile_internal* hdfsFile; |
---|
92 | |
---|
93 | |
---|
94 | /** |
---|
95 | * hdfsConnectAsUser - Connect to a hdfs file system as a specific user |
---|
96 | * Connect to the hdfs. |
---|
97 | * @param host A string containing either a host name, or an ip address |
---|
98 | * of the namenode of a hdfs cluster. 'host' should be passed as NULL if |
---|
99 | * you want to connect to local filesystem. 'host' should be passed as |
---|
100 | * 'default' (and port as 0) to used the 'configured' filesystem |
---|
101 | * (core-site/core-default.xml). |
---|
102 | * @param port The port on which the server is listening. |
---|
103 | * @param user the user name (this is hadoop domain user). Or NULL is equivelant to hhdfsConnect(host, port) |
---|
104 | * @param groups the groups (these are hadoop domain groups) |
---|
105 | * @return Returns a handle to the filesystem or NULL on error. |
---|
106 | */ |
---|
107 | hdfsFS hdfsConnectAsUser(const char* host, tPort port, const char *user , const char *groups[], int groups_size ); |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * hdfsConnect - Connect to a hdfs file system. |
---|
112 | * Connect to the hdfs. |
---|
113 | * @param host A string containing either a host name, or an ip address |
---|
114 | * of the namenode of a hdfs cluster. 'host' should be passed as NULL if |
---|
115 | * you want to connect to local filesystem. 'host' should be passed as |
---|
116 | * 'default' (and port as 0) to used the 'configured' filesystem |
---|
117 | * (core-site/core-default.xml). |
---|
118 | * @param port The port on which the server is listening. |
---|
119 | * @return Returns a handle to the filesystem or NULL on error. |
---|
120 | */ |
---|
121 | hdfsFS hdfsConnect(const char* host, tPort port); |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * hdfsDisconnect - Disconnect from the hdfs file system. |
---|
126 | * Disconnect from hdfs. |
---|
127 | * @param fs The configured filesystem handle. |
---|
128 | * @return Returns 0 on success, -1 on error. |
---|
129 | */ |
---|
130 | int hdfsDisconnect(hdfsFS fs); |
---|
131 | |
---|
132 | |
---|
133 | /** |
---|
134 | * hdfsOpenFile - Open a hdfs file in given mode. |
---|
135 | * @param fs The configured filesystem handle. |
---|
136 | * @param path The full path to the file. |
---|
137 | * @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), |
---|
138 | * 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. |
---|
139 | * @param bufferSize Size of buffer for read/write - pass 0 if you want |
---|
140 | * to use the default configured values. |
---|
141 | * @param replication Block replication - pass 0 if you want to use |
---|
142 | * the default configured values. |
---|
143 | * @param blocksize Size of block - pass 0 if you want to use the |
---|
144 | * default configured values. |
---|
145 | * @return Returns the handle to the open file or NULL on error. |
---|
146 | */ |
---|
147 | hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags, |
---|
148 | int bufferSize, short replication, tSize blocksize); |
---|
149 | |
---|
150 | |
---|
151 | /** |
---|
152 | * hdfsCloseFile - Close an open file. |
---|
153 | * @param fs The configured filesystem handle. |
---|
154 | * @param file The file handle. |
---|
155 | * @return Returns 0 on success, -1 on error. |
---|
156 | */ |
---|
157 | int hdfsCloseFile(hdfsFS fs, hdfsFile file); |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * hdfsExists - Checks if a given path exsits on the filesystem |
---|
162 | * @param fs The configured filesystem handle. |
---|
163 | * @param path The path to look for |
---|
164 | * @return Returns 0 on success, -1 on error. |
---|
165 | */ |
---|
166 | int hdfsExists(hdfsFS fs, const char *path); |
---|
167 | |
---|
168 | |
---|
169 | /** |
---|
170 | * hdfsSeek - Seek to given offset in file. |
---|
171 | * This works only for files opened in read-only mode. |
---|
172 | * @param fs The configured filesystem handle. |
---|
173 | * @param file The file handle. |
---|
174 | * @param desiredPos Offset into the file to seek into. |
---|
175 | * @return Returns 0 on success, -1 on error. |
---|
176 | */ |
---|
177 | int hdfsSeek(hdfsFS fs, hdfsFile file, tOffset desiredPos); |
---|
178 | |
---|
179 | |
---|
180 | /** |
---|
181 | * hdfsTell - Get the current offset in the file, in bytes. |
---|
182 | * @param fs The configured filesystem handle. |
---|
183 | * @param file The file handle. |
---|
184 | * @return Current offset, -1 on error. |
---|
185 | */ |
---|
186 | tOffset hdfsTell(hdfsFS fs, hdfsFile file); |
---|
187 | |
---|
188 | |
---|
189 | /** |
---|
190 | * hdfsRead - Read data from an open file. |
---|
191 | * @param fs The configured filesystem handle. |
---|
192 | * @param file The file handle. |
---|
193 | * @param buffer The buffer to copy read bytes into. |
---|
194 | * @param length The length of the buffer. |
---|
195 | * @return Returns the number of bytes actually read, possibly less |
---|
196 | * than than length;-1 on error. |
---|
197 | */ |
---|
198 | tSize hdfsRead(hdfsFS fs, hdfsFile file, void* buffer, tSize length); |
---|
199 | |
---|
200 | |
---|
201 | /** |
---|
202 | * hdfsPread - Positional read of data from an open file. |
---|
203 | * @param fs The configured filesystem handle. |
---|
204 | * @param file The file handle. |
---|
205 | * @param position Position from which to read |
---|
206 | * @param buffer The buffer to copy read bytes into. |
---|
207 | * @param length The length of the buffer. |
---|
208 | * @return Returns the number of bytes actually read, possibly less than |
---|
209 | * than length;-1 on error. |
---|
210 | */ |
---|
211 | tSize hdfsPread(hdfsFS fs, hdfsFile file, tOffset position, |
---|
212 | void* buffer, tSize length); |
---|
213 | |
---|
214 | |
---|
215 | /** |
---|
216 | * hdfsWrite - Write data into an open file. |
---|
217 | * @param fs The configured filesystem handle. |
---|
218 | * @param file The file handle. |
---|
219 | * @param buffer The data. |
---|
220 | * @param length The no. of bytes to write. |
---|
221 | * @return Returns the number of bytes written, -1 on error. |
---|
222 | */ |
---|
223 | tSize hdfsWrite(hdfsFS fs, hdfsFile file, const void* buffer, |
---|
224 | tSize length); |
---|
225 | |
---|
226 | |
---|
227 | /** |
---|
228 | * hdfsWrite - Flush the data. |
---|
229 | * @param fs The configured filesystem handle. |
---|
230 | * @param file The file handle. |
---|
231 | * @return Returns 0 on success, -1 on error. |
---|
232 | */ |
---|
233 | int hdfsFlush(hdfsFS fs, hdfsFile file); |
---|
234 | |
---|
235 | |
---|
236 | /** |
---|
237 | * hdfsAvailable - Number of bytes that can be read from this |
---|
238 | * input stream without blocking. |
---|
239 | * @param fs The configured filesystem handle. |
---|
240 | * @param file The file handle. |
---|
241 | * @return Returns available bytes; -1 on error. |
---|
242 | */ |
---|
243 | int hdfsAvailable(hdfsFS fs, hdfsFile file); |
---|
244 | |
---|
245 | |
---|
246 | /** |
---|
247 | * hdfsCopy - Copy file from one filesystem to another. |
---|
248 | * @param srcFS The handle to source filesystem. |
---|
249 | * @param src The path of source file. |
---|
250 | * @param dstFS The handle to destination filesystem. |
---|
251 | * @param dst The path of destination file. |
---|
252 | * @return Returns 0 on success, -1 on error. |
---|
253 | */ |
---|
254 | int hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst); |
---|
255 | |
---|
256 | |
---|
257 | /** |
---|
258 | * hdfsMove - Move file from one filesystem to another. |
---|
259 | * @param srcFS The handle to source filesystem. |
---|
260 | * @param src The path of source file. |
---|
261 | * @param dstFS The handle to destination filesystem. |
---|
262 | * @param dst The path of destination file. |
---|
263 | * @return Returns 0 on success, -1 on error. |
---|
264 | */ |
---|
265 | int hdfsMove(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst); |
---|
266 | |
---|
267 | |
---|
268 | /** |
---|
269 | * hdfsDelete - Delete file. |
---|
270 | * @param fs The configured filesystem handle. |
---|
271 | * @param path The path of the file. |
---|
272 | * @return Returns 0 on success, -1 on error. |
---|
273 | */ |
---|
274 | int hdfsDelete(hdfsFS fs, const char* path); |
---|
275 | |
---|
276 | |
---|
277 | /** |
---|
278 | * hdfsRename - Rename file. |
---|
279 | * @param fs The configured filesystem handle. |
---|
280 | * @param oldPath The path of the source file. |
---|
281 | * @param newPath The path of the destination file. |
---|
282 | * @return Returns 0 on success, -1 on error. |
---|
283 | */ |
---|
284 | int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath); |
---|
285 | |
---|
286 | |
---|
287 | /** |
---|
288 | * hdfsGetWorkingDirectory - Get the current working directory for |
---|
289 | * the given filesystem. |
---|
290 | * @param fs The configured filesystem handle. |
---|
291 | * @param buffer The user-buffer to copy path of cwd into. |
---|
292 | * @param bufferSize The length of user-buffer. |
---|
293 | * @return Returns buffer, NULL on error. |
---|
294 | */ |
---|
295 | char* hdfsGetWorkingDirectory(hdfsFS fs, char *buffer, size_t bufferSize); |
---|
296 | |
---|
297 | |
---|
298 | /** |
---|
299 | * hdfsSetWorkingDirectory - Set the working directory. All relative |
---|
300 | * paths will be resolved relative to it. |
---|
301 | * @param fs The configured filesystem handle. |
---|
302 | * @param path The path of the new 'cwd'. |
---|
303 | * @return Returns 0 on success, -1 on error. |
---|
304 | */ |
---|
305 | int hdfsSetWorkingDirectory(hdfsFS fs, const char* path); |
---|
306 | |
---|
307 | |
---|
308 | /** |
---|
309 | * hdfsCreateDirectory - Make the given file and all non-existent |
---|
310 | * parents into directories. |
---|
311 | * @param fs The configured filesystem handle. |
---|
312 | * @param path The path of the directory. |
---|
313 | * @return Returns 0 on success, -1 on error. |
---|
314 | */ |
---|
315 | int hdfsCreateDirectory(hdfsFS fs, const char* path); |
---|
316 | |
---|
317 | |
---|
318 | /** |
---|
319 | * hdfsSetReplication - Set the replication of the specified |
---|
320 | * file to the supplied value |
---|
321 | * @param fs The configured filesystem handle. |
---|
322 | * @param path The path of the file. |
---|
323 | * @return Returns 0 on success, -1 on error. |
---|
324 | */ |
---|
325 | int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication); |
---|
326 | |
---|
327 | |
---|
328 | /** |
---|
329 | * hdfsFileInfo - Information about a file/directory. |
---|
330 | */ |
---|
331 | typedef struct { |
---|
332 | tObjectKind mKind; /* file or directory */ |
---|
333 | char *mName; /* the name of the file */ |
---|
334 | tTime mLastMod; /* the last modification time for the file in seconds */ |
---|
335 | tOffset mSize; /* the size of the file in bytes */ |
---|
336 | short mReplication; /* the count of replicas */ |
---|
337 | tOffset mBlockSize; /* the block size for the file */ |
---|
338 | char *mOwner; /* the owner of the file */ |
---|
339 | char *mGroup; /* the group associated with the file */ |
---|
340 | short mPermissions; /* the permissions associated with the file */ |
---|
341 | tTime mLastAccess; /* the last access time for the file in seconds */ |
---|
342 | } hdfsFileInfo; |
---|
343 | |
---|
344 | |
---|
345 | /** |
---|
346 | * hdfsListDirectory - Get list of files/directories for a given |
---|
347 | * directory-path. hdfsFreeFileInfo should be called to deallocate memory. |
---|
348 | * @param fs The configured filesystem handle. |
---|
349 | * @param path The path of the directory. |
---|
350 | * @param numEntries Set to the number of files/directories in path. |
---|
351 | * @return Returns a dynamically-allocated array of hdfsFileInfo |
---|
352 | * objects; NULL on error. |
---|
353 | */ |
---|
354 | hdfsFileInfo *hdfsListDirectory(hdfsFS fs, const char* path, |
---|
355 | int *numEntries); |
---|
356 | |
---|
357 | |
---|
358 | /** |
---|
359 | * hdfsGetPathInfo - Get information about a path as a (dynamically |
---|
360 | * allocated) single hdfsFileInfo struct. hdfsFreeFileInfo should be |
---|
361 | * called when the pointer is no longer needed. |
---|
362 | * @param fs The configured filesystem handle. |
---|
363 | * @param path The path of the file. |
---|
364 | * @return Returns a dynamically-allocated hdfsFileInfo object; |
---|
365 | * NULL on error. |
---|
366 | */ |
---|
367 | hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path); |
---|
368 | |
---|
369 | |
---|
370 | /** |
---|
371 | * hdfsFreeFileInfo - Free up the hdfsFileInfo array (including fields) |
---|
372 | * @param hdfsFileInfo The array of dynamically-allocated hdfsFileInfo |
---|
373 | * objects. |
---|
374 | * @param numEntries The size of the array. |
---|
375 | */ |
---|
376 | void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries); |
---|
377 | |
---|
378 | |
---|
379 | /** |
---|
380 | * hdfsGetHosts - Get hostnames where a particular block (determined by |
---|
381 | * pos & blocksize) of a file is stored. The last element in the array |
---|
382 | * is NULL. Due to replication, a single block could be present on |
---|
383 | * multiple hosts. |
---|
384 | * @param fs The configured filesystem handle. |
---|
385 | * @param path The path of the file. |
---|
386 | * @param start The start of the block. |
---|
387 | * @param length The length of the block. |
---|
388 | * @return Returns a dynamically-allocated 2-d array of blocks-hosts; |
---|
389 | * NULL on error. |
---|
390 | */ |
---|
391 | char*** hdfsGetHosts(hdfsFS fs, const char* path, |
---|
392 | tOffset start, tOffset length); |
---|
393 | |
---|
394 | |
---|
395 | /** |
---|
396 | * hdfsFreeHosts - Free up the structure returned by hdfsGetHosts |
---|
397 | * @param hdfsFileInfo The array of dynamically-allocated hdfsFileInfo |
---|
398 | * objects. |
---|
399 | * @param numEntries The size of the array. |
---|
400 | */ |
---|
401 | void hdfsFreeHosts(char ***blockHosts); |
---|
402 | |
---|
403 | |
---|
404 | /** |
---|
405 | * hdfsGetDefaultBlockSize - Get the optimum blocksize. |
---|
406 | * @param fs The configured filesystem handle. |
---|
407 | * @return Returns the blocksize; -1 on error. |
---|
408 | */ |
---|
409 | tOffset hdfsGetDefaultBlockSize(hdfsFS fs); |
---|
410 | |
---|
411 | |
---|
412 | /** |
---|
413 | * hdfsGetCapacity - Return the raw capacity of the filesystem. |
---|
414 | * @param fs The configured filesystem handle. |
---|
415 | * @return Returns the raw-capacity; -1 on error. |
---|
416 | */ |
---|
417 | tOffset hdfsGetCapacity(hdfsFS fs); |
---|
418 | |
---|
419 | |
---|
420 | /** |
---|
421 | * hdfsGetUsed - Return the total raw size of all files in the filesystem. |
---|
422 | * @param fs The configured filesystem handle. |
---|
423 | * @return Returns the total-size; -1 on error. |
---|
424 | */ |
---|
425 | tOffset hdfsGetUsed(hdfsFS fs); |
---|
426 | |
---|
427 | /** |
---|
428 | * hdfsChown |
---|
429 | * @param fs The configured filesystem handle. |
---|
430 | * @param path the path to the file or directory |
---|
431 | * @param owner this is a string in Hadoop land. Set to null or "" if only setting group |
---|
432 | * @param group this is a string in Hadoop land. Set to null or "" if only setting user |
---|
433 | * @return 0 on success else -1 |
---|
434 | */ |
---|
435 | int hdfsChown(hdfsFS fs, const char* path, const char *owner, const char *group); |
---|
436 | |
---|
437 | /** |
---|
438 | * hdfsChmod |
---|
439 | * @param fs The configured filesystem handle. |
---|
440 | * @param path the path to the file or directory |
---|
441 | * @param mode the bitmask to set it to |
---|
442 | * @return 0 on success else -1 |
---|
443 | */ |
---|
444 | int hdfsChmod(hdfsFS fs, const char* path, short mode); |
---|
445 | |
---|
446 | /** |
---|
447 | * hdfsUtime |
---|
448 | * @param fs The configured filesystem handle. |
---|
449 | * @param path the path to the file or directory |
---|
450 | * @param mtime new modification time or 0 for only set access time in seconds |
---|
451 | * @param atime new access time or 0 for only set modification time in seconds |
---|
452 | * @return 0 on success else -1 |
---|
453 | */ |
---|
454 | int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime); |
---|
455 | |
---|
456 | #ifdef __cplusplus |
---|
457 | } |
---|
458 | #endif |
---|
459 | |
---|
460 | #endif /*LIBHDFS_HDFS_H*/ |
---|
461 | |
---|
462 | /** |
---|
463 | * vim: ts=4: sw=4: et |
---|
464 | */ |
---|