hdfs.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  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 "hdfs.h"
  19. #include "jni_helper.h"
  20. #include <stdio.h>
  21. #include <string.h>
  22. /* Some frequently used Java paths */
  23. #define HADOOP_CONF "org/apache/hadoop/conf/Configuration"
  24. #define HADOOP_PATH "org/apache/hadoop/fs/Path"
  25. #define HADOOP_LOCALFS "org/apache/hadoop/fs/LocalFileSystem"
  26. #define HADOOP_FS "org/apache/hadoop/fs/FileSystem"
  27. #define HADOOP_FSSTATUS "org/apache/hadoop/fs/FsStatus"
  28. #define HADOOP_BLK_LOC "org/apache/hadoop/fs/BlockLocation"
  29. #define HADOOP_DFS "org/apache/hadoop/hdfs/DistributedFileSystem"
  30. #define HADOOP_ISTRM "org/apache/hadoop/fs/FSDataInputStream"
  31. #define HADOOP_OSTRM "org/apache/hadoop/fs/FSDataOutputStream"
  32. #define HADOOP_STAT "org/apache/hadoop/fs/FileStatus"
  33. #define HADOOP_FSPERM "org/apache/hadoop/fs/permission/FsPermission"
  34. #define JAVA_NET_ISA "java/net/InetSocketAddress"
  35. #define JAVA_NET_URI "java/net/URI"
  36. #define JAVA_STRING "java/lang/String"
  37. #define JAVA_VOID "V"
  38. /* Macros for constructing method signatures */
  39. #define JPARAM(X) "L" X ";"
  40. #define JARRPARAM(X) "[L" X ";"
  41. #define JMETHOD1(X, R) "(" X ")" R
  42. #define JMETHOD2(X, Y, R) "(" X Y ")" R
  43. #define JMETHOD3(X, Y, Z, R) "(" X Y Z")" R
  44. #define KERBEROS_TICKET_CACHE_PATH "hadoop.security.kerberos.ticket.cache.path"
  45. // Bit fields for hdfsFile_internal flags
  46. #define HDFS_FILE_SUPPORTS_DIRECT_READ (1<<0)
  47. tSize readDirect(hdfsFS fs, hdfsFile f, void* buffer, tSize length);
  48. /**
  49. * The C equivalent of org.apache.org.hadoop.FSData(Input|Output)Stream .
  50. */
  51. enum hdfsStreamType
  52. {
  53. UNINITIALIZED = 0,
  54. INPUT = 1,
  55. OUTPUT = 2,
  56. };
  57. /**
  58. * The 'file-handle' to a file in hdfs.
  59. */
  60. struct hdfsFile_internal {
  61. void* file;
  62. enum hdfsStreamType type;
  63. int flags;
  64. };
  65. int hdfsFileIsOpenForRead(hdfsFile file)
  66. {
  67. return (file->type == INPUT);
  68. }
  69. int hdfsFileIsOpenForWrite(hdfsFile file)
  70. {
  71. return (file->type == OUTPUT);
  72. }
  73. int hdfsFileUsesDirectRead(hdfsFile file)
  74. {
  75. return !!(file->flags & HDFS_FILE_SUPPORTS_DIRECT_READ);
  76. }
  77. void hdfsFileDisableDirectRead(hdfsFile file)
  78. {
  79. file->flags &= ~HDFS_FILE_SUPPORTS_DIRECT_READ;
  80. }
  81. /**
  82. * hdfsJniEnv: A wrapper struct to be used as 'value'
  83. * while saving thread -> JNIEnv* mappings
  84. */
  85. typedef struct
  86. {
  87. JNIEnv* env;
  88. } hdfsJniEnv;
  89. /**
  90. * Helper function to destroy a local reference of java.lang.Object
  91. * @param env: The JNIEnv pointer.
  92. * @param jFile: The local reference of java.lang.Object object
  93. * @return None.
  94. */
  95. static void destroyLocalReference(JNIEnv *env, jobject jObject)
  96. {
  97. if (jObject)
  98. (*env)->DeleteLocalRef(env, jObject);
  99. }
  100. /**
  101. * Helper function to create a org.apache.hadoop.fs.Path object.
  102. * @param env: The JNIEnv pointer.
  103. * @param path: The file-path for which to construct org.apache.hadoop.fs.Path
  104. * object.
  105. * @return Returns a jobject on success and NULL on error.
  106. */
  107. static jobject constructNewObjectOfPath(JNIEnv *env, const char *path)
  108. {
  109. //Construct a java.lang.String object
  110. jstring jPathString = (*env)->NewStringUTF(env, path);
  111. //Construct the org.apache.hadoop.fs.Path object
  112. jobject jPath =
  113. constructNewObjectOfClass(env, NULL, "org/apache/hadoop/fs/Path",
  114. "(Ljava/lang/String;)V", jPathString);
  115. if (jPath == NULL) {
  116. fprintf(stderr, "Can't construct instance of class "
  117. "org.apache.hadoop.fs.Path for %s\n", path);
  118. errno = EINTERNAL;
  119. return NULL;
  120. }
  121. // Destroy the local reference to the java.lang.String object
  122. destroyLocalReference(env, jPathString);
  123. return jPath;
  124. }
  125. /**
  126. * Helper function to translate an exception into a meaningful errno value.
  127. * @param exc: The exception.
  128. * @param env: The JNIEnv Pointer.
  129. * @param method: The name of the method that threw the exception. This
  130. * may be format string to be used in conjuction with additional arguments.
  131. * @return Returns a meaningful errno value if possible, or EINTERNAL if not.
  132. */
  133. static int errnoFromException(jthrowable exc, JNIEnv *env,
  134. const char *method, ...)
  135. {
  136. va_list ap;
  137. int errnum = 0;
  138. char *excClass = NULL;
  139. if (exc == NULL)
  140. goto default_error;
  141. if ((excClass = classNameOfObject((jobject) exc, env)) == NULL) {
  142. errnum = EINTERNAL;
  143. goto done;
  144. }
  145. if (!strcmp(excClass, "java.lang.UnsupportedOperationException")) {
  146. errnum = ENOTSUP;
  147. goto done;
  148. }
  149. if (!strcmp(excClass, "org.apache.hadoop.security."
  150. "AccessControlException")) {
  151. errnum = EACCES;
  152. goto done;
  153. }
  154. if (!strcmp(excClass, "org.apache.hadoop.hdfs.protocol."
  155. "QuotaExceededException")) {
  156. errnum = EDQUOT;
  157. goto done;
  158. }
  159. if (!strcmp(excClass, "java.io.FileNotFoundException")) {
  160. errnum = ENOENT;
  161. goto done;
  162. }
  163. //TODO: interpret more exceptions; maybe examine exc.getMessage()
  164. default_error:
  165. //Can't tell what went wrong, so just punt
  166. (*env)->ExceptionDescribe(env);
  167. fprintf(stderr, "Call to ");
  168. va_start(ap, method);
  169. vfprintf(stderr, method, ap);
  170. va_end(ap);
  171. fprintf(stderr, " failed!\n");
  172. errnum = EINTERNAL;
  173. done:
  174. (*env)->ExceptionClear(env);
  175. if (excClass != NULL)
  176. free(excClass);
  177. return errnum;
  178. }
  179. /**
  180. * Set a configuration value.
  181. *
  182. * @param env The JNI environment
  183. * @param jConfiguration The configuration object to modify
  184. * @param key The key to modify
  185. * @param value The value to set the key to
  186. *
  187. * @return 0 on success; error code otherwise
  188. */
  189. static int hadoopConfSet(JNIEnv *env, jobject jConfiguration,
  190. const char *key, const char *value)
  191. {
  192. int ret;
  193. jthrowable jExc = NULL;
  194. jstring jkey = NULL, jvalue = NULL;
  195. char buf[1024];
  196. jkey = (*env)->NewStringUTF(env, key);
  197. if (!jkey) {
  198. ret = ENOMEM;
  199. goto done;
  200. }
  201. jvalue = (*env)->NewStringUTF(env, value);
  202. if (!jvalue) {
  203. ret = ENOMEM;
  204. goto done;
  205. }
  206. ret = invokeMethod(env, NULL, &jExc, INSTANCE, jConfiguration,
  207. HADOOP_CONF, "set", JMETHOD2(JPARAM(JAVA_STRING),
  208. JPARAM(JAVA_STRING), JAVA_VOID),
  209. jkey, jvalue);
  210. if (ret) {
  211. snprintf(buf, sizeof(buf), "hadoopConfSet(%s, %s)", key, value);
  212. ret = errnoFromException(jExc, env, buf);
  213. goto done;
  214. }
  215. done:
  216. if (jkey)
  217. destroyLocalReference(env, jkey);
  218. if (jvalue)
  219. destroyLocalReference(env, jvalue);
  220. if (ret)
  221. errno = ret;
  222. return ret;
  223. }
  224. /**
  225. * Convert a Java string into a C string.
  226. *
  227. * @param env The JNI environment
  228. * @param jStr The Java string to convert
  229. * @param cstr (out param) the C string.
  230. * This will be set to a dynamically allocated
  231. * UTF-8 C string on success.
  232. *
  233. * @return 0 on success; error code otherwise
  234. */
  235. static int jStrToCstr(JNIEnv *env, jstring jstr, char **cstr)
  236. {
  237. const char *tmp;
  238. tmp = (*env)->GetStringUTFChars(env, jstr, NULL);
  239. *cstr = strdup(tmp);
  240. (*env)->ReleaseStringUTFChars(env, jstr, tmp);
  241. return 0;
  242. }
  243. static int hadoopConfGet(JNIEnv *env, jobject jConfiguration,
  244. const char *key, char **val)
  245. {
  246. int ret;
  247. jthrowable jExc = NULL;
  248. jvalue jVal;
  249. jstring jkey = NULL;
  250. char buf[1024];
  251. jkey = (*env)->NewStringUTF(env, key);
  252. if (!jkey) {
  253. ret = ENOMEM;
  254. goto done;
  255. }
  256. ret = invokeMethod(env, &jVal, &jExc, INSTANCE, jConfiguration,
  257. HADOOP_CONF, "get", JMETHOD1(JPARAM(JAVA_STRING),
  258. JPARAM(JAVA_STRING)), jkey);
  259. if (ret) {
  260. snprintf(buf, sizeof(buf), "hadoopConfGet(%s)", key);
  261. ret = errnoFromException(jExc, env, buf);
  262. goto done;
  263. }
  264. if (!jVal.l) {
  265. *val = NULL;
  266. goto done;
  267. }
  268. ret = jStrToCstr(env, jVal.l, val);
  269. if (ret)
  270. goto done;
  271. done:
  272. if (jkey)
  273. destroyLocalReference(env, jkey);
  274. if (ret)
  275. errno = ret;
  276. return ret;
  277. }
  278. int hdfsConfGet(const char *key, char **val)
  279. {
  280. JNIEnv *env;
  281. int ret;
  282. jobject jConfiguration = NULL;
  283. env = getJNIEnv();
  284. if (env == NULL) {
  285. ret = EINTERNAL;
  286. goto done;
  287. }
  288. jConfiguration = constructNewObjectOfClass(env, NULL, HADOOP_CONF, "()V");
  289. if (jConfiguration == NULL) {
  290. fprintf(stderr, "Can't construct instance of class "
  291. "org.apache.hadoop.conf.Configuration\n");
  292. ret = EINTERNAL;
  293. goto done;
  294. }
  295. ret = hadoopConfGet(env, jConfiguration, key, val);
  296. if (ret)
  297. goto done;
  298. ret = 0;
  299. done:
  300. if (jConfiguration)
  301. destroyLocalReference(env, jConfiguration);
  302. if (ret)
  303. errno = ret;
  304. return ret;
  305. }
  306. void hdfsConfFree(char *val)
  307. {
  308. free(val);
  309. }
  310. struct hdfsBuilder {
  311. int forceNewInstance;
  312. const char *nn;
  313. tPort port;
  314. const char *kerbTicketCachePath;
  315. const char *userName;
  316. };
  317. struct hdfsBuilder *hdfsNewBuilder(void)
  318. {
  319. struct hdfsBuilder *bld = calloc(1, sizeof(struct hdfsBuilder));
  320. if (!bld) {
  321. errno = ENOMEM;
  322. return NULL;
  323. }
  324. return bld;
  325. }
  326. void hdfsFreeBuilder(struct hdfsBuilder *bld)
  327. {
  328. free(bld);
  329. }
  330. void hdfsBuilderSetForceNewInstance(struct hdfsBuilder *bld)
  331. {
  332. bld->forceNewInstance = 1;
  333. }
  334. void hdfsBuilderSetNameNode(struct hdfsBuilder *bld, const char *nn)
  335. {
  336. bld->nn = nn;
  337. }
  338. void hdfsBuilderSetNameNodePort(struct hdfsBuilder *bld, tPort port)
  339. {
  340. bld->port = port;
  341. }
  342. void hdfsBuilderSetUserName(struct hdfsBuilder *bld, const char *userName)
  343. {
  344. bld->userName = userName;
  345. }
  346. void hdfsBuilderSetKerbTicketCachePath(struct hdfsBuilder *bld,
  347. const char *kerbTicketCachePath)
  348. {
  349. bld->kerbTicketCachePath = kerbTicketCachePath;
  350. }
  351. hdfsFS hdfsConnect(const char* host, tPort port)
  352. {
  353. struct hdfsBuilder *bld = hdfsNewBuilder();
  354. if (!bld)
  355. return NULL;
  356. hdfsBuilderSetNameNode(bld, host);
  357. hdfsBuilderSetNameNodePort(bld, port);
  358. return hdfsBuilderConnect(bld);
  359. }
  360. /** Always return a new FileSystem handle */
  361. hdfsFS hdfsConnectNewInstance(const char* host, tPort port)
  362. {
  363. struct hdfsBuilder *bld = hdfsNewBuilder();
  364. if (!bld)
  365. return NULL;
  366. hdfsBuilderSetNameNode(bld, host);
  367. hdfsBuilderSetNameNodePort(bld, port);
  368. hdfsBuilderSetForceNewInstance(bld);
  369. return hdfsBuilderConnect(bld);
  370. }
  371. hdfsFS hdfsConnectAsUser(const char* host, tPort port, const char *user)
  372. {
  373. struct hdfsBuilder *bld = hdfsNewBuilder();
  374. if (!bld)
  375. return NULL;
  376. hdfsBuilderSetNameNode(bld, host);
  377. hdfsBuilderSetNameNodePort(bld, port);
  378. hdfsBuilderSetUserName(bld, user);
  379. return hdfsBuilderConnect(bld);
  380. }
  381. /** Always return a new FileSystem handle */
  382. hdfsFS hdfsConnectAsUserNewInstance(const char* host, tPort port,
  383. const char *user)
  384. {
  385. struct hdfsBuilder *bld = hdfsNewBuilder();
  386. if (!bld)
  387. return NULL;
  388. hdfsBuilderSetNameNode(bld, host);
  389. hdfsBuilderSetNameNodePort(bld, port);
  390. hdfsBuilderSetForceNewInstance(bld);
  391. hdfsBuilderSetUserName(bld, user);
  392. return hdfsBuilderConnect(bld);
  393. }
  394. /**
  395. * Calculate the effective URI to use, given a builder configuration.
  396. *
  397. * If there is not already a URI scheme, we prepend 'hdfs://'.
  398. *
  399. * If there is not already a port specified, and a port was given to the
  400. * builder, we suffix that port. If there is a port specified but also one in
  401. * the URI, that is an error.
  402. *
  403. * @param bld The hdfs builder object
  404. * @param uri (out param) dynamically allocated string representing the
  405. * effective URI
  406. *
  407. * @return 0 on success; error code otherwise
  408. */
  409. static int calcEffectiveURI(struct hdfsBuilder *bld, char ** uri)
  410. {
  411. const char *scheme;
  412. char suffix[64];
  413. const char *lastColon;
  414. char *u;
  415. size_t uriLen;
  416. if (!bld->nn)
  417. return EINVAL;
  418. scheme = (strstr(bld->nn, "://")) ? "" : "hdfs://";
  419. if (bld->port == 0) {
  420. suffix[0] = '\0';
  421. } else {
  422. lastColon = rindex(bld->nn, ':');
  423. if (lastColon && (strspn(lastColon + 1, "0123456789") ==
  424. strlen(lastColon + 1))) {
  425. fprintf(stderr, "port %d was given, but URI '%s' already "
  426. "contains a port!\n", bld->port, bld->nn);
  427. return EINVAL;
  428. }
  429. snprintf(suffix, sizeof(suffix), ":%d", bld->port);
  430. }
  431. uriLen = strlen(scheme) + strlen(bld->nn) + strlen(suffix);
  432. u = malloc((uriLen + 1) * (sizeof(char)));
  433. if (!u) {
  434. fprintf(stderr, "calcEffectiveURI: out of memory");
  435. return ENOMEM;
  436. }
  437. snprintf(u, uriLen + 1, "%s%s%s", scheme, bld->nn, suffix);
  438. *uri = u;
  439. return 0;
  440. }
  441. hdfsFS hdfsBuilderConnect(struct hdfsBuilder *bld)
  442. {
  443. JNIEnv *env = 0;
  444. jobject gFsRef = NULL;
  445. jobject jConfiguration = NULL, jFS = NULL, jURI = NULL, jCachePath = NULL;
  446. jstring jURIString = NULL, jUserString = NULL;
  447. jvalue jVal;
  448. jthrowable jExc = NULL;
  449. char *cURI = 0;
  450. int ret = 0;
  451. //Get the JNIEnv* corresponding to current thread
  452. env = getJNIEnv();
  453. if (env == NULL) {
  454. ret = EINTERNAL;
  455. goto done;
  456. }
  457. // jConfiguration = new Configuration();
  458. jConfiguration = constructNewObjectOfClass(env, NULL, HADOOP_CONF, "()V");
  459. if (jConfiguration == NULL) {
  460. fprintf(stderr, "Can't construct instance of class "
  461. "org.apache.hadoop.conf.Configuration\n");
  462. errno = EINTERNAL;
  463. goto done;
  464. }
  465. //Check what type of FileSystem the caller wants...
  466. if (bld->nn == NULL) {
  467. // Get a local filesystem.
  468. if (bld->forceNewInstance) {
  469. // fs = FileSytem::newInstanceLocal(conf);
  470. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, HADOOP_FS,
  471. "newInstanceLocal", JMETHOD1(JPARAM(HADOOP_CONF),
  472. JPARAM(HADOOP_LOCALFS)), jConfiguration)) {
  473. ret = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  474. "FileSystem::newInstanceLocal");
  475. goto done;
  476. }
  477. jFS = jVal.l;
  478. } else {
  479. // fs = FileSytem::getLocal(conf);
  480. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, HADOOP_FS, "getLocal",
  481. JMETHOD1(JPARAM(HADOOP_CONF),
  482. JPARAM(HADOOP_LOCALFS)),
  483. jConfiguration) != 0) {
  484. ret = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  485. "FileSystem::getLocal");
  486. goto done;
  487. }
  488. jFS = jVal.l;
  489. }
  490. } else {
  491. if (!strcmp(bld->nn, "default")) {
  492. // jURI = FileSystem.getDefaultUri(conf)
  493. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, HADOOP_FS,
  494. "getDefaultUri",
  495. "(Lorg/apache/hadoop/conf/Configuration;)Ljava/net/URI;",
  496. jConfiguration) != 0) {
  497. ret = errnoFromException(jExc, env, "org.apache.hadoop.fs.",
  498. "FileSystem::getDefaultUri");
  499. goto done;
  500. }
  501. jURI = jVal.l;
  502. } else {
  503. // fs = FileSystem::get(URI, conf, ugi);
  504. ret = calcEffectiveURI(bld, &cURI);
  505. if (ret)
  506. goto done;
  507. jURIString = (*env)->NewStringUTF(env, cURI);
  508. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, JAVA_NET_URI,
  509. "create", "(Ljava/lang/String;)Ljava/net/URI;",
  510. jURIString) != 0) {
  511. ret = errnoFromException(jExc, env, "java.net.URI::create");
  512. goto done;
  513. }
  514. jURI = jVal.l;
  515. }
  516. if (bld->kerbTicketCachePath) {
  517. ret = hadoopConfSet(env, jConfiguration,
  518. KERBEROS_TICKET_CACHE_PATH, bld->kerbTicketCachePath);
  519. if (ret)
  520. goto done;
  521. }
  522. if (bld->userName) {
  523. jUserString = (*env)->NewStringUTF(env, bld->userName);
  524. }
  525. if (bld->forceNewInstance) {
  526. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL,
  527. HADOOP_FS, "newInstance",
  528. JMETHOD3(JPARAM(JAVA_NET_URI), JPARAM(HADOOP_CONF),
  529. JPARAM(JAVA_STRING), JPARAM(HADOOP_FS)), jURI,
  530. jConfiguration, jUserString)) {
  531. ret = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  532. "Filesystem::newInstance(URI, Configuration)");
  533. goto done;
  534. }
  535. jFS = jVal.l;
  536. } else {
  537. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, HADOOP_FS, "get",
  538. JMETHOD3(JPARAM(JAVA_NET_URI), JPARAM(HADOOP_CONF),
  539. JPARAM(JAVA_STRING), JPARAM(HADOOP_FS)),
  540. jURI, jConfiguration, jUserString)) {
  541. ret = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  542. "Filesystem::get(URI, Configuration, String)");
  543. goto done;
  544. }
  545. jFS = jVal.l;
  546. }
  547. }
  548. done:
  549. if (jFS) {
  550. /* Create a global reference for this fs */
  551. gFsRef = (*env)->NewGlobalRef(env, jFS);
  552. }
  553. // Release unnecessary local references
  554. destroyLocalReference(env, jConfiguration);
  555. destroyLocalReference(env, jFS);
  556. destroyLocalReference(env, jURI);
  557. destroyLocalReference(env, jCachePath);
  558. destroyLocalReference(env, jURIString);
  559. destroyLocalReference(env, jUserString);
  560. free(cURI);
  561. free(bld);
  562. if (ret)
  563. errno = ret;
  564. return (hdfsFS)gFsRef;
  565. }
  566. int hdfsDisconnect(hdfsFS fs)
  567. {
  568. // JAVA EQUIVALENT:
  569. // fs.close()
  570. //Get the JNIEnv* corresponding to current thread
  571. JNIEnv* env = getJNIEnv();
  572. if (env == NULL) {
  573. errno = EINTERNAL;
  574. return -1;
  575. }
  576. //Parameters
  577. jobject jFS = (jobject)fs;
  578. //Caught exception
  579. jthrowable jExc = NULL;
  580. //Sanity check
  581. if (fs == NULL) {
  582. errno = EBADF;
  583. return -1;
  584. }
  585. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  586. "close", "()V") != 0) {
  587. errno = errnoFromException(jExc, env, "Filesystem::close");
  588. return -1;
  589. }
  590. //Release unnecessary references
  591. (*env)->DeleteGlobalRef(env, jFS);
  592. return 0;
  593. }
  594. hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags,
  595. int bufferSize, short replication, tSize blockSize)
  596. {
  597. /*
  598. JAVA EQUIVALENT:
  599. File f = new File(path);
  600. FSData{Input|Output}Stream f{is|os} = fs.create(f);
  601. return f{is|os};
  602. */
  603. /* Get the JNIEnv* corresponding to current thread */
  604. JNIEnv* env = getJNIEnv();
  605. if (env == NULL) {
  606. errno = EINTERNAL;
  607. return NULL;
  608. }
  609. jobject jFS = (jobject)fs;
  610. if (flags & O_RDWR) {
  611. fprintf(stderr, "ERROR: cannot open an hdfs file in O_RDWR mode\n");
  612. errno = ENOTSUP;
  613. return NULL;
  614. }
  615. if ((flags & O_CREAT) && (flags & O_EXCL)) {
  616. fprintf(stderr, "WARN: hdfs does not truly support O_CREATE && O_EXCL\n");
  617. }
  618. /* The hadoop java api/signature */
  619. const char* method = ((flags & O_WRONLY) == 0) ? "open" : (flags & O_APPEND) ? "append" : "create";
  620. const char* signature = ((flags & O_WRONLY) == 0) ?
  621. JMETHOD2(JPARAM(HADOOP_PATH), "I", JPARAM(HADOOP_ISTRM)) :
  622. (flags & O_APPEND) ?
  623. JMETHOD1(JPARAM(HADOOP_PATH), JPARAM(HADOOP_OSTRM)) :
  624. JMETHOD2(JPARAM(HADOOP_PATH), "ZISJ", JPARAM(HADOOP_OSTRM));
  625. /* Return value */
  626. hdfsFile file = NULL;
  627. /* Create an object of org.apache.hadoop.fs.Path */
  628. jobject jPath = constructNewObjectOfPath(env, path);
  629. if (jPath == NULL) {
  630. return NULL;
  631. }
  632. /* Get the Configuration object from the FileSystem object */
  633. jvalue jVal;
  634. jobject jConfiguration = NULL;
  635. jthrowable jExc = NULL;
  636. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  637. "getConf", JMETHOD1("", JPARAM(HADOOP_CONF))) != 0) {
  638. errno = errnoFromException(jExc, env, "get configuration object "
  639. "from filesystem");
  640. destroyLocalReference(env, jPath);
  641. return NULL;
  642. }
  643. jConfiguration = jVal.l;
  644. jint jBufferSize = bufferSize;
  645. jshort jReplication = replication;
  646. jlong jBlockSize = blockSize;
  647. jstring jStrBufferSize = (*env)->NewStringUTF(env, "io.file.buffer.size");
  648. jstring jStrReplication = (*env)->NewStringUTF(env, "dfs.replication");
  649. jstring jStrBlockSize = (*env)->NewStringUTF(env, "dfs.block.size");
  650. //bufferSize
  651. if (!bufferSize) {
  652. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jConfiguration,
  653. HADOOP_CONF, "getInt", "(Ljava/lang/String;I)I",
  654. jStrBufferSize, 4096) != 0) {
  655. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  656. "Configuration::getInt");
  657. goto done;
  658. }
  659. jBufferSize = jVal.i;
  660. }
  661. if ((flags & O_WRONLY) && (flags & O_APPEND) == 0) {
  662. //replication
  663. if (!replication) {
  664. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jConfiguration,
  665. HADOOP_CONF, "getInt", "(Ljava/lang/String;I)I",
  666. jStrReplication, 1) != 0) {
  667. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  668. "Configuration::getInt");
  669. goto done;
  670. }
  671. jReplication = jVal.i;
  672. }
  673. //blockSize
  674. if (!blockSize) {
  675. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jConfiguration,
  676. HADOOP_CONF, "getLong", "(Ljava/lang/String;J)J",
  677. jStrBlockSize, (jlong)67108864)) {
  678. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  679. "FileSystem::%s(%s)", method,
  680. signature);
  681. goto done;
  682. }
  683. jBlockSize = jVal.j;
  684. }
  685. }
  686. /* Create and return either the FSDataInputStream or
  687. FSDataOutputStream references jobject jStream */
  688. // READ?
  689. if ((flags & O_WRONLY) == 0) {
  690. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  691. method, signature, jPath, jBufferSize)) {
  692. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  693. "FileSystem::%s(%s)", method,
  694. signature);
  695. goto done;
  696. }
  697. } else if ((flags & O_WRONLY) && (flags & O_APPEND)) {
  698. // WRITE/APPEND?
  699. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  700. method, signature, jPath)) {
  701. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  702. "FileSystem::%s(%s)", method,
  703. signature);
  704. goto done;
  705. }
  706. } else {
  707. // WRITE/CREATE
  708. jboolean jOverWrite = 1;
  709. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  710. method, signature, jPath, jOverWrite,
  711. jBufferSize, jReplication, jBlockSize)) {
  712. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  713. "FileSystem::%s(%s)", method,
  714. signature);
  715. goto done;
  716. }
  717. }
  718. file = malloc(sizeof(struct hdfsFile_internal));
  719. if (!file) {
  720. errno = ENOMEM;
  721. } else {
  722. file->file = (*env)->NewGlobalRef(env, jVal.l);
  723. file->type = (((flags & O_WRONLY) == 0) ? INPUT : OUTPUT);
  724. file->flags = 0;
  725. destroyLocalReference(env, jVal.l);
  726. if ((flags & O_WRONLY) == 0) {
  727. // Try a test read to see if we can do direct reads
  728. errno = 0;
  729. char buf;
  730. if (readDirect(fs, file, &buf, 0) == 0) {
  731. // Success - 0-byte read should return 0
  732. file->flags |= HDFS_FILE_SUPPORTS_DIRECT_READ;
  733. } else {
  734. if (errno != ENOTSUP) {
  735. // Unexpected error. Clear it, don't set the direct flag.
  736. fprintf(stderr,
  737. "WARN: Unexpected error %d when testing "
  738. "for direct read compatibility\n", errno);
  739. errno = 0;
  740. goto done;
  741. }
  742. }
  743. errno = 0;
  744. }
  745. }
  746. done:
  747. //Delete unnecessary local references
  748. destroyLocalReference(env, jStrBufferSize);
  749. destroyLocalReference(env, jStrReplication);
  750. destroyLocalReference(env, jStrBlockSize);
  751. destroyLocalReference(env, jConfiguration);
  752. destroyLocalReference(env, jPath);
  753. return file;
  754. }
  755. int hdfsCloseFile(hdfsFS fs, hdfsFile file)
  756. {
  757. // JAVA EQUIVALENT:
  758. // file.close
  759. //Get the JNIEnv* corresponding to current thread
  760. JNIEnv* env = getJNIEnv();
  761. if (env == NULL) {
  762. errno = EINTERNAL;
  763. return -1;
  764. }
  765. //Parameters
  766. jobject jStream = (jobject)(file ? file->file : NULL);
  767. //Caught exception
  768. jthrowable jExc = NULL;
  769. //Sanity check
  770. if (!file || file->type == UNINITIALIZED) {
  771. errno = EBADF;
  772. return -1;
  773. }
  774. //The interface whose 'close' method to be called
  775. const char* interface = (file->type == INPUT) ?
  776. HADOOP_ISTRM : HADOOP_OSTRM;
  777. if (invokeMethod(env, NULL, &jExc, INSTANCE, jStream, interface,
  778. "close", "()V") != 0) {
  779. errno = errnoFromException(jExc, env, "%s::close", interface);
  780. return -1;
  781. }
  782. //De-allocate memory
  783. free(file);
  784. (*env)->DeleteGlobalRef(env, jStream);
  785. return 0;
  786. }
  787. int hdfsExists(hdfsFS fs, const char *path)
  788. {
  789. JNIEnv *env = getJNIEnv();
  790. if (env == NULL) {
  791. errno = EINTERNAL;
  792. return -1;
  793. }
  794. jobject jPath = constructNewObjectOfPath(env, path);
  795. jvalue jVal;
  796. jthrowable jExc = NULL;
  797. jobject jFS = (jobject)fs;
  798. if (jPath == NULL) {
  799. return -1;
  800. }
  801. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  802. "exists", JMETHOD1(JPARAM(HADOOP_PATH), "Z"),
  803. jPath) != 0) {
  804. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  805. "FileSystem::exists");
  806. destroyLocalReference(env, jPath);
  807. return -1;
  808. }
  809. destroyLocalReference(env, jPath);
  810. return jVal.z ? 0 : -1;
  811. }
  812. // Checks input file for readiness for reading.
  813. static int readPrepare(JNIEnv* env, hdfsFS fs, hdfsFile f,
  814. jobject* jInputStream)
  815. {
  816. *jInputStream = (jobject)(f ? f->file : NULL);
  817. //Sanity check
  818. if (!f || f->type == UNINITIALIZED) {
  819. errno = EBADF;
  820. return -1;
  821. }
  822. //Error checking... make sure that this file is 'readable'
  823. if (f->type != INPUT) {
  824. fprintf(stderr, "Cannot read from a non-InputStream object!\n");
  825. errno = EINVAL;
  826. return -1;
  827. }
  828. return 0;
  829. }
  830. // Common error-handling code between read paths.
  831. static int handleReadResult(int success, jvalue jVal, jthrowable jExc,
  832. JNIEnv* env)
  833. {
  834. int noReadBytes;
  835. if (success != 0) {
  836. if ((*env)->ExceptionCheck(env)) {
  837. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  838. "FSDataInputStream::read");
  839. }
  840. noReadBytes = -1;
  841. } else {
  842. noReadBytes = jVal.i;
  843. if (noReadBytes == 0) {
  844. // 0 from Java means try again, which is EINTR here
  845. errno = EINTR;
  846. noReadBytes = -1;
  847. } else if (noReadBytes < 0) {
  848. // -1 from Java is EOF, which is 0 here
  849. errno = 0;
  850. noReadBytes = 0;
  851. }
  852. }
  853. return noReadBytes;
  854. }
  855. tSize hdfsRead(hdfsFS fs, hdfsFile f, void* buffer, tSize length)
  856. {
  857. if (f->flags & HDFS_FILE_SUPPORTS_DIRECT_READ) {
  858. return readDirect(fs, f, buffer, length);
  859. }
  860. // JAVA EQUIVALENT:
  861. // byte [] bR = new byte[length];
  862. // fis.read(bR);
  863. //Get the JNIEnv* corresponding to current thread
  864. JNIEnv* env = getJNIEnv();
  865. if (env == NULL) {
  866. errno = EINTERNAL;
  867. return -1;
  868. }
  869. //Parameters
  870. jobject jInputStream;
  871. if (readPrepare(env, fs, f, &jInputStream) == -1) {
  872. return -1;
  873. }
  874. jbyteArray jbRarray;
  875. jint noReadBytes = 0;
  876. jvalue jVal;
  877. jthrowable jExc = NULL;
  878. //Read the requisite bytes
  879. jbRarray = (*env)->NewByteArray(env, length);
  880. int success = invokeMethod(env, &jVal, &jExc, INSTANCE, jInputStream, HADOOP_ISTRM,
  881. "read", "([B)I", jbRarray);
  882. noReadBytes = handleReadResult(success, jVal, jExc, env);;
  883. if (noReadBytes > 0) {
  884. (*env)->GetByteArrayRegion(env, jbRarray, 0, noReadBytes, buffer);
  885. }
  886. destroyLocalReference(env, jbRarray);
  887. return noReadBytes;
  888. }
  889. // Reads using the read(ByteBuffer) API, which does fewer copies
  890. tSize readDirect(hdfsFS fs, hdfsFile f, void* buffer, tSize length)
  891. {
  892. // JAVA EQUIVALENT:
  893. // ByteBuffer bbuffer = ByteBuffer.allocateDirect(length) // wraps C buffer
  894. // fis.read(bbuffer);
  895. //Get the JNIEnv* corresponding to current thread
  896. JNIEnv* env = getJNIEnv();
  897. if (env == NULL) {
  898. errno = EINTERNAL;
  899. return -1;
  900. }
  901. jobject jInputStream;
  902. if (readPrepare(env, fs, f, &jInputStream) == -1) {
  903. return -1;
  904. }
  905. jint noReadBytes = 0;
  906. jvalue jVal;
  907. jthrowable jExc = NULL;
  908. //Read the requisite bytes
  909. jobject bb = (*env)->NewDirectByteBuffer(env, buffer, length);
  910. if (bb == NULL) {
  911. fprintf(stderr, "Could not allocate ByteBuffer");
  912. if ((*env)->ExceptionCheck(env)) {
  913. errno = errnoFromException(NULL, env, "JNIEnv::NewDirectByteBuffer");
  914. } else {
  915. errno = ENOMEM; // Best guess if there's no exception waiting
  916. }
  917. return -1;
  918. }
  919. int success = invokeMethod(env, &jVal, &jExc, INSTANCE, jInputStream,
  920. HADOOP_ISTRM, "read", "(Ljava/nio/ByteBuffer;)I",
  921. bb);
  922. noReadBytes = handleReadResult(success, jVal, jExc, env);
  923. destroyLocalReference(env, bb);
  924. return noReadBytes;
  925. }
  926. tSize hdfsPread(hdfsFS fs, hdfsFile f, tOffset position,
  927. void* buffer, tSize length)
  928. {
  929. // JAVA EQUIVALENT:
  930. // byte [] bR = new byte[length];
  931. // fis.read(pos, bR, 0, length);
  932. //Get the JNIEnv* corresponding to current thread
  933. JNIEnv* env = getJNIEnv();
  934. if (env == NULL) {
  935. errno = EINTERNAL;
  936. return -1;
  937. }
  938. //Parameters
  939. jobject jInputStream = (jobject)(f ? f->file : NULL);
  940. jbyteArray jbRarray;
  941. jint noReadBytes = 0;
  942. jvalue jVal;
  943. jthrowable jExc = NULL;
  944. //Sanity check
  945. if (!f || f->type == UNINITIALIZED) {
  946. errno = EBADF;
  947. return -1;
  948. }
  949. //Error checking... make sure that this file is 'readable'
  950. if (f->type != INPUT) {
  951. fprintf(stderr, "Cannot read from a non-InputStream object!\n");
  952. errno = EINVAL;
  953. return -1;
  954. }
  955. //Read the requisite bytes
  956. jbRarray = (*env)->NewByteArray(env, length);
  957. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jInputStream, HADOOP_ISTRM,
  958. "read", "(J[BII)I", position, jbRarray, 0, length) != 0) {
  959. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  960. "FSDataInputStream::read");
  961. noReadBytes = -1;
  962. }
  963. else {
  964. noReadBytes = jVal.i;
  965. if (noReadBytes > 0) {
  966. (*env)->GetByteArrayRegion(env, jbRarray, 0, noReadBytes, buffer);
  967. } else {
  968. //This is a valid case: there aren't any bytes left to read!
  969. if (noReadBytes == 0 || noReadBytes < -1) {
  970. fprintf(stderr, "WARN: FSDataInputStream.read returned invalid return code - libhdfs returning EOF, i.e., 0: %d\n", noReadBytes);
  971. }
  972. noReadBytes = 0;
  973. }
  974. errno = 0;
  975. }
  976. destroyLocalReference(env, jbRarray);
  977. return noReadBytes;
  978. }
  979. tSize hdfsWrite(hdfsFS fs, hdfsFile f, const void* buffer, tSize length)
  980. {
  981. // JAVA EQUIVALENT
  982. // byte b[] = str.getBytes();
  983. // fso.write(b);
  984. //Get the JNIEnv* corresponding to current thread
  985. JNIEnv* env = getJNIEnv();
  986. if (env == NULL) {
  987. errno = EINTERNAL;
  988. return -1;
  989. }
  990. //Parameters
  991. jobject jOutputStream = (jobject)(f ? f->file : 0);
  992. jbyteArray jbWarray;
  993. //Caught exception
  994. jthrowable jExc = NULL;
  995. //Sanity check
  996. if (!f || f->type == UNINITIALIZED) {
  997. errno = EBADF;
  998. return -1;
  999. }
  1000. if (length < 0) {
  1001. errno = EINVAL;
  1002. return -1;
  1003. }
  1004. //Error checking... make sure that this file is 'writable'
  1005. if (f->type != OUTPUT) {
  1006. fprintf(stderr, "Cannot write into a non-OutputStream object!\n");
  1007. errno = EINVAL;
  1008. return -1;
  1009. }
  1010. // 'length' equals 'zero' is a valid use-case according to Posix!
  1011. if (length != 0) {
  1012. //Write the requisite bytes into the file
  1013. jbWarray = (*env)->NewByteArray(env, length);
  1014. (*env)->SetByteArrayRegion(env, jbWarray, 0, length, buffer);
  1015. if (invokeMethod(env, NULL, &jExc, INSTANCE, jOutputStream,
  1016. HADOOP_OSTRM, "write",
  1017. "([B)V", jbWarray) != 0) {
  1018. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1019. "FSDataOutputStream::write");
  1020. length = -1;
  1021. }
  1022. destroyLocalReference(env, jbWarray);
  1023. }
  1024. //Return no. of bytes succesfully written (libc way)
  1025. //i.e. 'length' itself! ;-)
  1026. return length;
  1027. }
  1028. int hdfsSeek(hdfsFS fs, hdfsFile f, tOffset desiredPos)
  1029. {
  1030. // JAVA EQUIVALENT
  1031. // fis.seek(pos);
  1032. //Get the JNIEnv* corresponding to current thread
  1033. JNIEnv* env = getJNIEnv();
  1034. if (env == NULL) {
  1035. errno = EINTERNAL;
  1036. return -1;
  1037. }
  1038. //Parameters
  1039. jobject jInputStream = (jobject)(f ? f->file : 0);
  1040. //Caught exception
  1041. jthrowable jExc = NULL;
  1042. //Sanity check
  1043. if (!f || f->type != INPUT) {
  1044. errno = EBADF;
  1045. return -1;
  1046. }
  1047. if (invokeMethod(env, NULL, &jExc, INSTANCE, jInputStream, HADOOP_ISTRM,
  1048. "seek", "(J)V", desiredPos) != 0) {
  1049. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1050. "FSDataInputStream::seek");
  1051. return -1;
  1052. }
  1053. return 0;
  1054. }
  1055. tOffset hdfsTell(hdfsFS fs, hdfsFile f)
  1056. {
  1057. // JAVA EQUIVALENT
  1058. // pos = f.getPos();
  1059. //Get the JNIEnv* corresponding to current thread
  1060. JNIEnv* env = getJNIEnv();
  1061. if (env == NULL) {
  1062. errno = EINTERNAL;
  1063. return -1;
  1064. }
  1065. //Parameters
  1066. jobject jStream = (jobject)(f ? f->file : 0);
  1067. //Sanity check
  1068. if (!f || f->type == UNINITIALIZED) {
  1069. errno = EBADF;
  1070. return -1;
  1071. }
  1072. const char* interface = (f->type == INPUT) ?
  1073. HADOOP_ISTRM : HADOOP_OSTRM;
  1074. jlong currentPos = -1;
  1075. jvalue jVal;
  1076. jthrowable jExc = NULL;
  1077. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStream,
  1078. interface, "getPos", "()J") != 0) {
  1079. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1080. "FSDataInputStream::getPos");
  1081. return -1;
  1082. }
  1083. currentPos = jVal.j;
  1084. return (tOffset)currentPos;
  1085. }
  1086. int hdfsFlush(hdfsFS fs, hdfsFile f)
  1087. {
  1088. // JAVA EQUIVALENT
  1089. // fos.flush();
  1090. //Get the JNIEnv* corresponding to current thread
  1091. JNIEnv* env = getJNIEnv();
  1092. if (env == NULL) {
  1093. errno = EINTERNAL;
  1094. return -1;
  1095. }
  1096. //Parameters
  1097. jobject jOutputStream = (jobject)(f ? f->file : 0);
  1098. //Caught exception
  1099. jthrowable jExc = NULL;
  1100. //Sanity check
  1101. if (!f || f->type != OUTPUT) {
  1102. errno = EBADF;
  1103. return -1;
  1104. }
  1105. if (invokeMethod(env, NULL, &jExc, INSTANCE, jOutputStream,
  1106. HADOOP_OSTRM, "flush", "()V") != 0) {
  1107. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1108. "FSDataInputStream::flush");
  1109. return -1;
  1110. }
  1111. return 0;
  1112. }
  1113. int hdfsHFlush(hdfsFS fs, hdfsFile f)
  1114. {
  1115. //Get the JNIEnv* corresponding to current thread
  1116. JNIEnv* env = getJNIEnv();
  1117. if (env == NULL) {
  1118. errno = EINTERNAL;
  1119. return -1;
  1120. }
  1121. //Parameters
  1122. jobject jOutputStream = (jobject)(f ? f->file : 0);
  1123. //Caught exception
  1124. jthrowable jExc = NULL;
  1125. //Sanity check
  1126. if (!f || f->type != OUTPUT) {
  1127. errno = EBADF;
  1128. return -1;
  1129. }
  1130. if (invokeMethod(env, NULL, &jExc, INSTANCE, jOutputStream,
  1131. HADOOP_OSTRM, "hflush", "()V") != 0) {
  1132. errno = errnoFromException(jExc, env, HADOOP_OSTRM "::hflush");
  1133. return -1;
  1134. }
  1135. return 0;
  1136. }
  1137. int hdfsAvailable(hdfsFS fs, hdfsFile f)
  1138. {
  1139. // JAVA EQUIVALENT
  1140. // fis.available();
  1141. //Get the JNIEnv* corresponding to current thread
  1142. JNIEnv* env = getJNIEnv();
  1143. if (env == NULL) {
  1144. errno = EINTERNAL;
  1145. return -1;
  1146. }
  1147. //Parameters
  1148. jobject jInputStream = (jobject)(f ? f->file : 0);
  1149. //Caught exception
  1150. jthrowable jExc = NULL;
  1151. //Sanity check
  1152. if (!f || f->type != INPUT) {
  1153. errno = EBADF;
  1154. return -1;
  1155. }
  1156. jint available = -1;
  1157. jvalue jVal;
  1158. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jInputStream,
  1159. HADOOP_ISTRM, "available", "()I") != 0) {
  1160. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1161. "FSDataInputStream::available");
  1162. return -1;
  1163. }
  1164. available = jVal.i;
  1165. return available;
  1166. }
  1167. int hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst)
  1168. {
  1169. //JAVA EQUIVALENT
  1170. // FileUtil::copy(srcFS, srcPath, dstFS, dstPath,
  1171. // deleteSource = false, conf)
  1172. //Get the JNIEnv* corresponding to current thread
  1173. JNIEnv* env = getJNIEnv();
  1174. if (env == NULL) {
  1175. errno = EINTERNAL;
  1176. return -1;
  1177. }
  1178. //Parameters
  1179. jobject jSrcFS = (jobject)srcFS;
  1180. jobject jDstFS = (jobject)dstFS;
  1181. jobject jSrcPath = NULL;
  1182. jobject jDstPath = NULL;
  1183. jSrcPath = constructNewObjectOfPath(env, src);
  1184. if (jSrcPath == NULL) {
  1185. return -1;
  1186. }
  1187. jDstPath = constructNewObjectOfPath(env, dst);
  1188. if (jDstPath == NULL) {
  1189. destroyLocalReference(env, jSrcPath);
  1190. return -1;
  1191. }
  1192. int retval = 0;
  1193. //Create the org.apache.hadoop.conf.Configuration object
  1194. jobject jConfiguration =
  1195. constructNewObjectOfClass(env, NULL, HADOOP_CONF, "()V");
  1196. if (jConfiguration == NULL) {
  1197. fprintf(stderr, "Can't construct instance of class "
  1198. "org.apache.hadoop.conf.Configuration\n");
  1199. destroyLocalReference(env, jSrcPath);
  1200. destroyLocalReference(env, jDstPath);
  1201. errno = EINTERNAL;
  1202. return -1;
  1203. }
  1204. //FileUtil::copy
  1205. jboolean deleteSource = 0; //Only copy
  1206. jvalue jVal;
  1207. jthrowable jExc = NULL;
  1208. if (invokeMethod(env, &jVal, &jExc, STATIC,
  1209. NULL, "org/apache/hadoop/fs/FileUtil", "copy",
  1210. "(Lorg/apache/hadoop/fs/FileSystem;Lorg/apache/hadoop/fs/Path;Lorg/apache/hadoop/fs/FileSystem;Lorg/apache/hadoop/fs/Path;ZLorg/apache/hadoop/conf/Configuration;)Z",
  1211. jSrcFS, jSrcPath, jDstFS, jDstPath, deleteSource,
  1212. jConfiguration) != 0) {
  1213. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1214. "FileUtil::copy");
  1215. retval = -1;
  1216. goto done;
  1217. }
  1218. done:
  1219. //Delete unnecessary local references
  1220. destroyLocalReference(env, jConfiguration);
  1221. destroyLocalReference(env, jSrcPath);
  1222. destroyLocalReference(env, jDstPath);
  1223. return retval;
  1224. }
  1225. int hdfsMove(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst)
  1226. {
  1227. //JAVA EQUIVALENT
  1228. // FileUtil::copy(srcFS, srcPath, dstFS, dstPath,
  1229. // deleteSource = true, conf)
  1230. //Get the JNIEnv* corresponding to current thread
  1231. JNIEnv* env = getJNIEnv();
  1232. if (env == NULL) {
  1233. errno = EINTERNAL;
  1234. return -1;
  1235. }
  1236. //Parameters
  1237. jobject jSrcFS = (jobject)srcFS;
  1238. jobject jDstFS = (jobject)dstFS;
  1239. jobject jSrcPath = NULL;
  1240. jobject jDstPath = NULL;
  1241. jSrcPath = constructNewObjectOfPath(env, src);
  1242. if (jSrcPath == NULL) {
  1243. return -1;
  1244. }
  1245. jDstPath = constructNewObjectOfPath(env, dst);
  1246. if (jDstPath == NULL) {
  1247. destroyLocalReference(env, jSrcPath);
  1248. return -1;
  1249. }
  1250. int retval = 0;
  1251. //Create the org.apache.hadoop.conf.Configuration object
  1252. jobject jConfiguration =
  1253. constructNewObjectOfClass(env, NULL, HADOOP_CONF, "()V");
  1254. if (jConfiguration == NULL) {
  1255. fprintf(stderr, "Can't construct instance of class "
  1256. "org.apache.hadoop.conf.Configuration\n");
  1257. destroyLocalReference(env, jSrcPath);
  1258. destroyLocalReference(env, jDstPath);
  1259. errno = EINTERNAL;
  1260. return -1;
  1261. }
  1262. //FileUtil::copy
  1263. jboolean deleteSource = 1; //Delete src after copy
  1264. jvalue jVal;
  1265. jthrowable jExc = NULL;
  1266. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL,
  1267. "org/apache/hadoop/fs/FileUtil", "copy",
  1268. "(Lorg/apache/hadoop/fs/FileSystem;Lorg/apache/hadoop/fs/Path;Lorg/apache/hadoop/fs/FileSystem;Lorg/apache/hadoop/fs/Path;ZLorg/apache/hadoop/conf/Configuration;)Z",
  1269. jSrcFS, jSrcPath, jDstFS, jDstPath, deleteSource,
  1270. jConfiguration) != 0) {
  1271. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1272. "FileUtil::copy(move)");
  1273. retval = -1;
  1274. goto done;
  1275. }
  1276. done:
  1277. //Delete unnecessary local references
  1278. destroyLocalReference(env, jConfiguration);
  1279. destroyLocalReference(env, jSrcPath);
  1280. destroyLocalReference(env, jDstPath);
  1281. return retval;
  1282. }
  1283. int hdfsDelete(hdfsFS fs, const char* path, int recursive)
  1284. {
  1285. // JAVA EQUIVALENT:
  1286. // File f = new File(path);
  1287. // bool retval = fs.delete(f);
  1288. //Get the JNIEnv* corresponding to current thread
  1289. JNIEnv* env = getJNIEnv();
  1290. if (env == NULL) {
  1291. errno = EINTERNAL;
  1292. return -1;
  1293. }
  1294. jobject jFS = (jobject)fs;
  1295. //Create an object of java.io.File
  1296. jobject jPath = constructNewObjectOfPath(env, path);
  1297. if (jPath == NULL) {
  1298. return -1;
  1299. }
  1300. //Delete the file
  1301. jvalue jVal;
  1302. jthrowable jExc = NULL;
  1303. jboolean jRecursive = recursive ? JNI_TRUE : JNI_FALSE;
  1304. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1305. "delete", "(Lorg/apache/hadoop/fs/Path;Z)Z",
  1306. jPath, jRecursive) != 0) {
  1307. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1308. "FileSystem::delete");
  1309. destroyLocalReference(env, jPath);
  1310. return -1;
  1311. }
  1312. //Delete unnecessary local references
  1313. destroyLocalReference(env, jPath);
  1314. return (jVal.z) ? 0 : -1;
  1315. }
  1316. int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath)
  1317. {
  1318. // JAVA EQUIVALENT:
  1319. // Path old = new Path(oldPath);
  1320. // Path new = new Path(newPath);
  1321. // fs.rename(old, new);
  1322. //Get the JNIEnv* corresponding to current thread
  1323. JNIEnv* env = getJNIEnv();
  1324. if (env == NULL) {
  1325. errno = EINTERNAL;
  1326. return -1;
  1327. }
  1328. jobject jFS = (jobject)fs;
  1329. //Create objects of org.apache.hadoop.fs.Path
  1330. jobject jOldPath = NULL;
  1331. jobject jNewPath = NULL;
  1332. jOldPath = constructNewObjectOfPath(env, oldPath);
  1333. if (jOldPath == NULL) {
  1334. return -1;
  1335. }
  1336. jNewPath = constructNewObjectOfPath(env, newPath);
  1337. if (jNewPath == NULL) {
  1338. destroyLocalReference(env, jOldPath);
  1339. return -1;
  1340. }
  1341. //Rename the file
  1342. jvalue jVal;
  1343. jthrowable jExc = NULL;
  1344. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS, "rename",
  1345. JMETHOD2(JPARAM(HADOOP_PATH), JPARAM(HADOOP_PATH), "Z"),
  1346. jOldPath, jNewPath) != 0) {
  1347. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1348. "FileSystem::rename");
  1349. destroyLocalReference(env, jOldPath);
  1350. destroyLocalReference(env, jNewPath);
  1351. return -1;
  1352. }
  1353. //Delete unnecessary local references
  1354. destroyLocalReference(env, jOldPath);
  1355. destroyLocalReference(env, jNewPath);
  1356. return (jVal.z) ? 0 : -1;
  1357. }
  1358. char* hdfsGetWorkingDirectory(hdfsFS fs, char* buffer, size_t bufferSize)
  1359. {
  1360. // JAVA EQUIVALENT:
  1361. // Path p = fs.getWorkingDirectory();
  1362. // return p.toString()
  1363. //Get the JNIEnv* corresponding to current thread
  1364. JNIEnv* env = getJNIEnv();
  1365. if (env == NULL) {
  1366. errno = EINTERNAL;
  1367. return NULL;
  1368. }
  1369. jobject jFS = (jobject)fs;
  1370. jobject jPath = NULL;
  1371. jvalue jVal;
  1372. jthrowable jExc = NULL;
  1373. //FileSystem::getWorkingDirectory()
  1374. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS,
  1375. HADOOP_FS, "getWorkingDirectory",
  1376. "()Lorg/apache/hadoop/fs/Path;") != 0 ||
  1377. jVal.l == NULL) {
  1378. errno = errnoFromException(jExc, env, "FileSystem::"
  1379. "getWorkingDirectory");
  1380. return NULL;
  1381. }
  1382. jPath = jVal.l;
  1383. //Path::toString()
  1384. jstring jPathString;
  1385. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jPath,
  1386. "org/apache/hadoop/fs/Path", "toString",
  1387. "()Ljava/lang/String;") != 0) {
  1388. errno = errnoFromException(jExc, env, "Path::toString");
  1389. destroyLocalReference(env, jPath);
  1390. return NULL;
  1391. }
  1392. jPathString = jVal.l;
  1393. const char *jPathChars = (const char*)
  1394. ((*env)->GetStringUTFChars(env, jPathString, NULL));
  1395. //Copy to user-provided buffer
  1396. strncpy(buffer, jPathChars, bufferSize);
  1397. //Delete unnecessary local references
  1398. (*env)->ReleaseStringUTFChars(env, jPathString, jPathChars);
  1399. destroyLocalReference(env, jPathString);
  1400. destroyLocalReference(env, jPath);
  1401. return buffer;
  1402. }
  1403. int hdfsSetWorkingDirectory(hdfsFS fs, const char* path)
  1404. {
  1405. // JAVA EQUIVALENT:
  1406. // fs.setWorkingDirectory(Path(path));
  1407. //Get the JNIEnv* corresponding to current thread
  1408. JNIEnv* env = getJNIEnv();
  1409. if (env == NULL) {
  1410. errno = EINTERNAL;
  1411. return -1;
  1412. }
  1413. jobject jFS = (jobject)fs;
  1414. int retval = 0;
  1415. jthrowable jExc = NULL;
  1416. //Create an object of org.apache.hadoop.fs.Path
  1417. jobject jPath = constructNewObjectOfPath(env, path);
  1418. if (jPath == NULL) {
  1419. return -1;
  1420. }
  1421. //FileSystem::setWorkingDirectory()
  1422. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  1423. "setWorkingDirectory",
  1424. "(Lorg/apache/hadoop/fs/Path;)V", jPath) != 0) {
  1425. errno = errnoFromException(jExc, env, "FileSystem::"
  1426. "setWorkingDirectory");
  1427. retval = -1;
  1428. }
  1429. //Delete unnecessary local references
  1430. destroyLocalReference(env, jPath);
  1431. return retval;
  1432. }
  1433. int hdfsCreateDirectory(hdfsFS fs, const char* path)
  1434. {
  1435. // JAVA EQUIVALENT:
  1436. // fs.mkdirs(new Path(path));
  1437. //Get the JNIEnv* corresponding to current thread
  1438. JNIEnv* env = getJNIEnv();
  1439. if (env == NULL) {
  1440. errno = EINTERNAL;
  1441. return -1;
  1442. }
  1443. jobject jFS = (jobject)fs;
  1444. //Create an object of org.apache.hadoop.fs.Path
  1445. jobject jPath = constructNewObjectOfPath(env, path);
  1446. if (jPath == NULL) {
  1447. return -1;
  1448. }
  1449. //Create the directory
  1450. jvalue jVal;
  1451. jVal.z = 0;
  1452. jthrowable jExc = NULL;
  1453. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1454. "mkdirs", "(Lorg/apache/hadoop/fs/Path;)Z",
  1455. jPath) != 0) {
  1456. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1457. "FileSystem::mkdirs");
  1458. goto done;
  1459. }
  1460. done:
  1461. //Delete unnecessary local references
  1462. destroyLocalReference(env, jPath);
  1463. return (jVal.z) ? 0 : -1;
  1464. }
  1465. int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication)
  1466. {
  1467. // JAVA EQUIVALENT:
  1468. // fs.setReplication(new Path(path), replication);
  1469. //Get the JNIEnv* corresponding to current thread
  1470. JNIEnv* env = getJNIEnv();
  1471. if (env == NULL) {
  1472. errno = EINTERNAL;
  1473. return -1;
  1474. }
  1475. jobject jFS = (jobject)fs;
  1476. //Create an object of org.apache.hadoop.fs.Path
  1477. jobject jPath = constructNewObjectOfPath(env, path);
  1478. if (jPath == NULL) {
  1479. return -1;
  1480. }
  1481. //Create the directory
  1482. jvalue jVal;
  1483. jthrowable jExc = NULL;
  1484. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1485. "setReplication", "(Lorg/apache/hadoop/fs/Path;S)Z",
  1486. jPath, replication) != 0) {
  1487. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1488. "FileSystem::setReplication");
  1489. goto done;
  1490. }
  1491. done:
  1492. //Delete unnecessary local references
  1493. destroyLocalReference(env, jPath);
  1494. return (jVal.z) ? 0 : -1;
  1495. }
  1496. int hdfsChown(hdfsFS fs, const char* path, const char *owner, const char *group)
  1497. {
  1498. // JAVA EQUIVALENT:
  1499. // fs.setOwner(path, owner, group)
  1500. //Get the JNIEnv* corresponding to current thread
  1501. JNIEnv* env = getJNIEnv();
  1502. if (env == NULL) {
  1503. errno = EINTERNAL;
  1504. return -1;
  1505. }
  1506. if (owner == NULL && group == NULL) {
  1507. fprintf(stderr, "Both owner and group cannot be null in chown");
  1508. errno = EINVAL;
  1509. return -1;
  1510. }
  1511. jobject jFS = (jobject)fs;
  1512. jobject jPath = constructNewObjectOfPath(env, path);
  1513. if (jPath == NULL) {
  1514. return -1;
  1515. }
  1516. jstring jOwnerString = (*env)->NewStringUTF(env, owner);
  1517. jstring jGroupString = (*env)->NewStringUTF(env, group);
  1518. //Create the directory
  1519. int ret = 0;
  1520. jthrowable jExc = NULL;
  1521. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  1522. "setOwner", JMETHOD3(JPARAM(HADOOP_PATH), JPARAM(JAVA_STRING), JPARAM(JAVA_STRING), JAVA_VOID),
  1523. jPath, jOwnerString, jGroupString) != 0) {
  1524. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1525. "FileSystem::setOwner");
  1526. ret = -1;
  1527. goto done;
  1528. }
  1529. done:
  1530. destroyLocalReference(env, jPath);
  1531. destroyLocalReference(env, jOwnerString);
  1532. destroyLocalReference(env, jGroupString);
  1533. return ret;
  1534. }
  1535. int hdfsChmod(hdfsFS fs, const char* path, short mode)
  1536. {
  1537. int ret;
  1538. // JAVA EQUIVALENT:
  1539. // fs.setPermission(path, FsPermission)
  1540. //Get the JNIEnv* corresponding to current thread
  1541. JNIEnv* env = getJNIEnv();
  1542. if (env == NULL) {
  1543. errno = EINTERNAL;
  1544. return -1;
  1545. }
  1546. jobject jFS = (jobject)fs;
  1547. // construct jPerm = FsPermission.createImmutable(short mode);
  1548. jshort jmode = mode;
  1549. jobject jPermObj =
  1550. constructNewObjectOfClass(env, NULL, HADOOP_FSPERM,"(S)V",jmode);
  1551. if (jPermObj == NULL) {
  1552. errno = EINTERNAL;
  1553. return -1;
  1554. }
  1555. //Create an object of org.apache.hadoop.fs.Path
  1556. jobject jPath = constructNewObjectOfPath(env, path);
  1557. if (jPath == NULL) {
  1558. destroyLocalReference(env, jPermObj);
  1559. return -1;
  1560. }
  1561. //Create the directory
  1562. jthrowable jExc = NULL;
  1563. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  1564. "setPermission", JMETHOD2(JPARAM(HADOOP_PATH), JPARAM(HADOOP_FSPERM), JAVA_VOID),
  1565. jPath, jPermObj) != 0) {
  1566. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1567. "FileSystem::setPermission");
  1568. ret = -1;
  1569. goto done;
  1570. }
  1571. ret = 0;
  1572. done:
  1573. destroyLocalReference(env, jPath);
  1574. destroyLocalReference(env, jPermObj);
  1575. return ret;
  1576. }
  1577. int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime)
  1578. {
  1579. // JAVA EQUIVALENT:
  1580. // fs.setTimes(src, mtime, atime)
  1581. //Get the JNIEnv* corresponding to current thread
  1582. JNIEnv* env = getJNIEnv();
  1583. if (env == NULL) {
  1584. errno = EINTERNAL;
  1585. return -1;
  1586. }
  1587. jobject jFS = (jobject)fs;
  1588. //Create an object of org.apache.hadoop.fs.Path
  1589. jobject jPath = constructNewObjectOfPath(env, path);
  1590. if (jPath == NULL) {
  1591. fprintf(stderr, "could not construct path object\n");
  1592. return -1;
  1593. }
  1594. const tTime NO_CHANGE = -1;
  1595. jlong jmtime = (mtime == NO_CHANGE) ? -1 : (mtime * (jlong)1000);
  1596. jlong jatime = (atime == NO_CHANGE) ? -1 : (atime * (jlong)1000);
  1597. int ret = 0;
  1598. jthrowable jExc = NULL;
  1599. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  1600. "setTimes", JMETHOD3(JPARAM(HADOOP_PATH), "J", "J", JAVA_VOID),
  1601. jPath, jmtime, jatime) != 0) {
  1602. fprintf(stderr, "call to setTime failed\n");
  1603. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1604. "FileSystem::setTimes");
  1605. ret = -1;
  1606. goto done;
  1607. }
  1608. done:
  1609. destroyLocalReference(env, jPath);
  1610. return ret;
  1611. }
  1612. char***
  1613. hdfsGetHosts(hdfsFS fs, const char* path, tOffset start, tOffset length)
  1614. {
  1615. // JAVA EQUIVALENT:
  1616. // fs.getFileBlockLoctions(new Path(path), start, length);
  1617. //Get the JNIEnv* corresponding to current thread
  1618. JNIEnv* env = getJNIEnv();
  1619. if (env == NULL) {
  1620. errno = EINTERNAL;
  1621. return NULL;
  1622. }
  1623. jobject jFS = (jobject)fs;
  1624. //Create an object of org.apache.hadoop.fs.Path
  1625. jobject jPath = constructNewObjectOfPath(env, path);
  1626. if (jPath == NULL) {
  1627. return NULL;
  1628. }
  1629. jvalue jFSVal;
  1630. jthrowable jFSExc = NULL;
  1631. if (invokeMethod(env, &jFSVal, &jFSExc, INSTANCE, jFS,
  1632. HADOOP_FS, "getFileStatus",
  1633. "(Lorg/apache/hadoop/fs/Path;)"
  1634. "Lorg/apache/hadoop/fs/FileStatus;",
  1635. jPath) != 0) {
  1636. errno = errnoFromException(jFSExc, env, "org.apache.hadoop.fs."
  1637. "FileSystem::getFileStatus");
  1638. destroyLocalReference(env, jPath);
  1639. return NULL;
  1640. }
  1641. jobject jFileStatus = jFSVal.l;
  1642. //org.apache.hadoop.fs.FileSystem::getFileBlockLocations
  1643. char*** blockHosts = NULL;
  1644. jobjectArray jBlockLocations;;
  1645. jvalue jVal;
  1646. jthrowable jExc = NULL;
  1647. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS,
  1648. HADOOP_FS, "getFileBlockLocations",
  1649. "(Lorg/apache/hadoop/fs/FileStatus;JJ)"
  1650. "[Lorg/apache/hadoop/fs/BlockLocation;",
  1651. jFileStatus, start, length) != 0) {
  1652. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1653. "FileSystem::getFileBlockLocations");
  1654. destroyLocalReference(env, jPath);
  1655. destroyLocalReference(env, jFileStatus);
  1656. return NULL;
  1657. }
  1658. jBlockLocations = jVal.l;
  1659. //Figure out no of entries in jBlockLocations
  1660. //Allocate memory and add NULL at the end
  1661. jsize jNumFileBlocks = (*env)->GetArrayLength(env, jBlockLocations);
  1662. blockHosts = malloc(sizeof(char**) * (jNumFileBlocks+1));
  1663. if (blockHosts == NULL) {
  1664. errno = ENOMEM;
  1665. goto done;
  1666. }
  1667. blockHosts[jNumFileBlocks] = NULL;
  1668. if (jNumFileBlocks == 0) {
  1669. errno = 0;
  1670. goto done;
  1671. }
  1672. //Now parse each block to get hostnames
  1673. int i = 0;
  1674. for (i=0; i < jNumFileBlocks; ++i) {
  1675. jobject jFileBlock =
  1676. (*env)->GetObjectArrayElement(env, jBlockLocations, i);
  1677. jvalue jVal;
  1678. jobjectArray jFileBlockHosts;
  1679. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFileBlock, HADOOP_BLK_LOC,
  1680. "getHosts", "()[Ljava/lang/String;") ||
  1681. jVal.l == NULL) {
  1682. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1683. "BlockLocation::getHosts");
  1684. destroyLocalReference(env, jPath);
  1685. destroyLocalReference(env, jFileStatus);
  1686. destroyLocalReference(env, jBlockLocations);
  1687. return NULL;
  1688. }
  1689. jFileBlockHosts = jVal.l;
  1690. //Figure out no of hosts in jFileBlockHosts
  1691. //Allocate memory and add NULL at the end
  1692. jsize jNumBlockHosts = (*env)->GetArrayLength(env, jFileBlockHosts);
  1693. blockHosts[i] = malloc(sizeof(char*) * (jNumBlockHosts+1));
  1694. if (blockHosts[i] == NULL) {
  1695. int x = 0;
  1696. for (x=0; x < i; ++x) {
  1697. free(blockHosts[x]);
  1698. }
  1699. free(blockHosts);
  1700. errno = ENOMEM;
  1701. goto done;
  1702. }
  1703. blockHosts[i][jNumBlockHosts] = NULL;
  1704. //Now parse each hostname
  1705. int j = 0;
  1706. const char *hostName;
  1707. for (j=0; j < jNumBlockHosts; ++j) {
  1708. jstring jHost =
  1709. (*env)->GetObjectArrayElement(env, jFileBlockHosts, j);
  1710. hostName =
  1711. (const char*)((*env)->GetStringUTFChars(env, jHost, NULL));
  1712. blockHosts[i][j] = strdup(hostName);
  1713. (*env)->ReleaseStringUTFChars(env, jHost, hostName);
  1714. destroyLocalReference(env, jHost);
  1715. }
  1716. destroyLocalReference(env, jFileBlockHosts);
  1717. }
  1718. done:
  1719. //Delete unnecessary local references
  1720. destroyLocalReference(env, jPath);
  1721. destroyLocalReference(env, jFileStatus);
  1722. destroyLocalReference(env, jBlockLocations);
  1723. return blockHosts;
  1724. }
  1725. void hdfsFreeHosts(char ***blockHosts)
  1726. {
  1727. int i, j;
  1728. for (i=0; blockHosts[i]; i++) {
  1729. for (j=0; blockHosts[i][j]; j++) {
  1730. free(blockHosts[i][j]);
  1731. }
  1732. free(blockHosts[i]);
  1733. }
  1734. free(blockHosts);
  1735. }
  1736. tOffset hdfsGetDefaultBlockSize(hdfsFS fs)
  1737. {
  1738. // JAVA EQUIVALENT:
  1739. // fs.getDefaultBlockSize();
  1740. //Get the JNIEnv* corresponding to current thread
  1741. JNIEnv* env = getJNIEnv();
  1742. if (env == NULL) {
  1743. errno = EINTERNAL;
  1744. return -1;
  1745. }
  1746. jobject jFS = (jobject)fs;
  1747. //FileSystem::getDefaultBlockSize()
  1748. tOffset blockSize = -1;
  1749. jvalue jVal;
  1750. jthrowable jExc = NULL;
  1751. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1752. "getDefaultBlockSize", "()J") != 0) {
  1753. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1754. "FileSystem::getDefaultBlockSize");
  1755. return -1;
  1756. }
  1757. blockSize = jVal.j;
  1758. return blockSize;
  1759. }
  1760. tOffset hdfsGetCapacity(hdfsFS fs)
  1761. {
  1762. // JAVA EQUIVALENT:
  1763. // FsStatus fss = fs.getStatus();
  1764. // return Fss.getCapacity();
  1765. //Get the JNIEnv* corresponding to current thread
  1766. JNIEnv* env = getJNIEnv();
  1767. if (env == NULL) {
  1768. errno = EINTERNAL;
  1769. return -1;
  1770. }
  1771. jobject jFS = (jobject)fs;
  1772. //FileSystem::getStatus
  1773. jvalue jVal;
  1774. jthrowable jExc = NULL;
  1775. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1776. "getStatus", "()Lorg/apache/hadoop/fs/FsStatus;") != 0) {
  1777. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1778. "FileSystem::getStatus");
  1779. return -1;
  1780. }
  1781. jobject fss = (jobject)jVal.l;
  1782. if (invokeMethod(env, &jVal, &jExc, INSTANCE, fss, HADOOP_FSSTATUS,
  1783. "getCapacity", "()J") != 0) {
  1784. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1785. "FsStatus::getCapacity");
  1786. destroyLocalReference(env, fss);
  1787. return -1;
  1788. }
  1789. destroyLocalReference(env, fss);
  1790. return jVal.j;
  1791. }
  1792. tOffset hdfsGetUsed(hdfsFS fs)
  1793. {
  1794. // JAVA EQUIVALENT:
  1795. // FsStatus fss = fs.getStatus();
  1796. // return Fss.getUsed();
  1797. //Get the JNIEnv* corresponding to current thread
  1798. JNIEnv* env = getJNIEnv();
  1799. if (env == NULL) {
  1800. errno = EINTERNAL;
  1801. return -1;
  1802. }
  1803. jobject jFS = (jobject)fs;
  1804. //FileSystem::getStatus
  1805. jvalue jVal;
  1806. jthrowable jExc = NULL;
  1807. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1808. "getStatus", "()Lorg/apache/hadoop/fs/FsStatus;") != 0) {
  1809. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1810. "FileSystem::getStatus");
  1811. return -1;
  1812. }
  1813. jobject fss = (jobject)jVal.l;
  1814. if (invokeMethod(env, &jVal, &jExc, INSTANCE, fss, HADOOP_FSSTATUS,
  1815. "getUsed", "()J") != 0) {
  1816. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1817. "FsStatus::getUsed");
  1818. destroyLocalReference(env, fss);
  1819. return -1;
  1820. }
  1821. destroyLocalReference(env, fss);
  1822. return jVal.j;
  1823. }
  1824. static int
  1825. getFileInfoFromStat(JNIEnv *env, jobject jStat, hdfsFileInfo *fileInfo)
  1826. {
  1827. jvalue jVal;
  1828. jthrowable jExc = NULL;
  1829. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1830. HADOOP_STAT, "isDir", "()Z") != 0) {
  1831. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1832. "FileStatus::isDir");
  1833. return -1;
  1834. }
  1835. fileInfo->mKind = jVal.z ? kObjectKindDirectory : kObjectKindFile;
  1836. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1837. HADOOP_STAT, "getReplication", "()S") != 0) {
  1838. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1839. "FileStatus::getReplication");
  1840. return -1;
  1841. }
  1842. fileInfo->mReplication = jVal.s;
  1843. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1844. HADOOP_STAT, "getBlockSize", "()J") != 0) {
  1845. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1846. "FileStatus::getBlockSize");
  1847. return -1;
  1848. }
  1849. fileInfo->mBlockSize = jVal.j;
  1850. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1851. HADOOP_STAT, "getModificationTime", "()J") != 0) {
  1852. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1853. "FileStatus::getModificationTime");
  1854. return -1;
  1855. }
  1856. fileInfo->mLastMod = jVal.j / 1000;
  1857. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1858. HADOOP_STAT, "getAccessTime", "()J") != 0) {
  1859. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1860. "FileStatus::getAccessTime");
  1861. return -1;
  1862. }
  1863. fileInfo->mLastAccess = (tTime) (jVal.j / 1000);
  1864. if (fileInfo->mKind == kObjectKindFile) {
  1865. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1866. HADOOP_STAT, "getLen", "()J") != 0) {
  1867. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1868. "FileStatus::getLen");
  1869. return -1;
  1870. }
  1871. fileInfo->mSize = jVal.j;
  1872. }
  1873. jobject jPath;
  1874. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat, HADOOP_STAT,
  1875. "getPath", "()Lorg/apache/hadoop/fs/Path;") ||
  1876. jVal.l == NULL) {
  1877. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1878. "Path::getPath");
  1879. return -1;
  1880. }
  1881. jPath = jVal.l;
  1882. jstring jPathName;
  1883. const char *cPathName;
  1884. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jPath, HADOOP_PATH,
  1885. "toString", "()Ljava/lang/String;")) {
  1886. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1887. "Path::toString");
  1888. destroyLocalReference(env, jPath);
  1889. return -1;
  1890. }
  1891. jPathName = jVal.l;
  1892. cPathName = (const char*) ((*env)->GetStringUTFChars(env, jPathName, NULL));
  1893. fileInfo->mName = strdup(cPathName);
  1894. (*env)->ReleaseStringUTFChars(env, jPathName, cPathName);
  1895. destroyLocalReference(env, jPath);
  1896. destroyLocalReference(env, jPathName);
  1897. jstring jUserName;
  1898. const char* cUserName;
  1899. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat, HADOOP_STAT,
  1900. "getOwner", "()Ljava/lang/String;")) {
  1901. fprintf(stderr, "Call to org.apache.hadoop.fs."
  1902. "FileStatus::getOwner failed!\n");
  1903. errno = EINTERNAL;
  1904. return -1;
  1905. }
  1906. jUserName = jVal.l;
  1907. cUserName = (const char*) ((*env)->GetStringUTFChars(env, jUserName, NULL));
  1908. fileInfo->mOwner = strdup(cUserName);
  1909. (*env)->ReleaseStringUTFChars(env, jUserName, cUserName);
  1910. destroyLocalReference(env, jUserName);
  1911. jstring jGroupName;
  1912. const char* cGroupName;
  1913. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat, HADOOP_STAT,
  1914. "getGroup", "()Ljava/lang/String;")) {
  1915. fprintf(stderr, "Call to org.apache.hadoop.fs."
  1916. "FileStatus::getGroup failed!\n");
  1917. errno = EINTERNAL;
  1918. return -1;
  1919. }
  1920. jGroupName = jVal.l;
  1921. cGroupName = (const char*) ((*env)->GetStringUTFChars(env, jGroupName, NULL));
  1922. fileInfo->mGroup = strdup(cGroupName);
  1923. (*env)->ReleaseStringUTFChars(env, jGroupName, cGroupName);
  1924. destroyLocalReference(env, jGroupName);
  1925. jobject jPermission;
  1926. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat, HADOOP_STAT,
  1927. "getPermission", "()Lorg/apache/hadoop/fs/permission/FsPermission;") ||
  1928. jVal.l == NULL) {
  1929. fprintf(stderr, "Call to org.apache.hadoop.fs."
  1930. "FileStatus::getPermission failed!\n");
  1931. errno = EINTERNAL;
  1932. return -1;
  1933. }
  1934. jPermission = jVal.l;
  1935. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jPermission, HADOOP_FSPERM,
  1936. "toShort", "()S") != 0) {
  1937. fprintf(stderr, "Call to org.apache.hadoop.fs.permission."
  1938. "FsPermission::toShort failed!\n");
  1939. errno = EINTERNAL;
  1940. return -1;
  1941. }
  1942. fileInfo->mPermissions = jVal.s;
  1943. destroyLocalReference(env, jPermission);
  1944. return 0;
  1945. }
  1946. static int
  1947. getFileInfo(JNIEnv *env, jobject jFS, jobject jPath, hdfsFileInfo *fileInfo)
  1948. {
  1949. // JAVA EQUIVALENT:
  1950. // fs.isDirectory(f)
  1951. // fs.getModificationTime()
  1952. // fs.getAccessTime()
  1953. // fs.getLength(f)
  1954. // f.getPath()
  1955. // f.getOwner()
  1956. // f.getGroup()
  1957. // f.getPermission().toShort()
  1958. jobject jStat;
  1959. jvalue jVal;
  1960. jthrowable jExc = NULL;
  1961. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1962. "exists", JMETHOD1(JPARAM(HADOOP_PATH), "Z"),
  1963. jPath) != 0) {
  1964. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1965. "FileSystem::exists");
  1966. return -1;
  1967. }
  1968. if (jVal.z == 0) {
  1969. errno = ENOENT;
  1970. return -1;
  1971. }
  1972. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1973. "getFileStatus", JMETHOD1(JPARAM(HADOOP_PATH), JPARAM(HADOOP_STAT)),
  1974. jPath) != 0) {
  1975. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1976. "FileSystem::getFileStatus");
  1977. return -1;
  1978. }
  1979. jStat = jVal.l;
  1980. int ret = getFileInfoFromStat(env, jStat, fileInfo);
  1981. destroyLocalReference(env, jStat);
  1982. return ret;
  1983. }
  1984. hdfsFileInfo* hdfsListDirectory(hdfsFS fs, const char* path, int *numEntries)
  1985. {
  1986. // JAVA EQUIVALENT:
  1987. // Path p(path);
  1988. // Path []pathList = fs.listPaths(p)
  1989. // foreach path in pathList
  1990. // getFileInfo(path)
  1991. //Get the JNIEnv* corresponding to current thread
  1992. JNIEnv* env = getJNIEnv();
  1993. if (env == NULL) {
  1994. errno = EINTERNAL;
  1995. return NULL;
  1996. }
  1997. jobject jFS = (jobject)fs;
  1998. //Create an object of org.apache.hadoop.fs.Path
  1999. jobject jPath = constructNewObjectOfPath(env, path);
  2000. if (jPath == NULL) {
  2001. return NULL;
  2002. }
  2003. hdfsFileInfo *pathList = 0;
  2004. jobjectArray jPathList = NULL;
  2005. jvalue jVal;
  2006. jthrowable jExc = NULL;
  2007. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_DFS, "listStatus",
  2008. JMETHOD1(JPARAM(HADOOP_PATH), JARRPARAM(HADOOP_STAT)),
  2009. jPath) != 0) {
  2010. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  2011. "FileSystem::listStatus");
  2012. destroyLocalReference(env, jPath);
  2013. return NULL;
  2014. }
  2015. jPathList = jVal.l;
  2016. //Figure out no of entries in that directory
  2017. jsize jPathListSize = (*env)->GetArrayLength(env, jPathList);
  2018. *numEntries = jPathListSize;
  2019. if (jPathListSize == 0) {
  2020. errno = 0;
  2021. goto done;
  2022. }
  2023. //Allocate memory
  2024. pathList = calloc(jPathListSize, sizeof(hdfsFileInfo));
  2025. if (pathList == NULL) {
  2026. errno = ENOMEM;
  2027. goto done;
  2028. }
  2029. //Save path information in pathList
  2030. jsize i;
  2031. jobject tmpStat;
  2032. for (i=0; i < jPathListSize; ++i) {
  2033. tmpStat = (*env)->GetObjectArrayElement(env, jPathList, i);
  2034. if (getFileInfoFromStat(env, tmpStat, &pathList[i])) {
  2035. hdfsFreeFileInfo(pathList, jPathListSize);
  2036. destroyLocalReference(env, tmpStat);
  2037. pathList = NULL;
  2038. goto done;
  2039. }
  2040. destroyLocalReference(env, tmpStat);
  2041. }
  2042. done:
  2043. //Delete unnecessary local references
  2044. destroyLocalReference(env, jPath);
  2045. destroyLocalReference(env, jPathList);
  2046. return pathList;
  2047. }
  2048. hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path)
  2049. {
  2050. // JAVA EQUIVALENT:
  2051. // File f(path);
  2052. // fs.isDirectory(f)
  2053. // fs.lastModified() ??
  2054. // fs.getLength(f)
  2055. // f.getPath()
  2056. //Get the JNIEnv* corresponding to current thread
  2057. JNIEnv* env = getJNIEnv();
  2058. if (env == NULL) {
  2059. errno = EINTERNAL;
  2060. return NULL;
  2061. }
  2062. jobject jFS = (jobject)fs;
  2063. //Create an object of org.apache.hadoop.fs.Path
  2064. jobject jPath = constructNewObjectOfPath(env, path);
  2065. if (jPath == NULL) {
  2066. return NULL;
  2067. }
  2068. hdfsFileInfo *fileInfo = calloc(1, sizeof(hdfsFileInfo));
  2069. if (getFileInfo(env, jFS, jPath, fileInfo)) {
  2070. hdfsFreeFileInfo(fileInfo, 1);
  2071. fileInfo = NULL;
  2072. goto done;
  2073. }
  2074. done:
  2075. //Delete unnecessary local references
  2076. destroyLocalReference(env, jPath);
  2077. return fileInfo;
  2078. }
  2079. void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries)
  2080. {
  2081. //Free the mName, mOwner, and mGroup
  2082. int i;
  2083. for (i=0; i < numEntries; ++i) {
  2084. if (hdfsFileInfo[i].mName) {
  2085. free(hdfsFileInfo[i].mName);
  2086. }
  2087. if (hdfsFileInfo[i].mOwner) {
  2088. free(hdfsFileInfo[i].mOwner);
  2089. }
  2090. if (hdfsFileInfo[i].mGroup) {
  2091. free(hdfsFileInfo[i].mGroup);
  2092. }
  2093. }
  2094. //Free entire block
  2095. free(hdfsFileInfo);
  2096. }
  2097. /**
  2098. * vim: ts=4: sw=4: et:
  2099. */