hdfs.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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 "hdfsJniHelper.h"
  20. /* Some frequently used Java paths */
  21. #define HADOOP_CONF "org/apache/hadoop/conf/Configuration"
  22. #define HADOOP_PATH "org/apache/hadoop/fs/Path"
  23. #define HADOOP_LOCALFS "org/apache/hadoop/fs/LocalFileSystem"
  24. #define HADOOP_FS "org/apache/hadoop/fs/FileSystem"
  25. #define HADOOP_FSSTATUS "org/apache/hadoop/fs/FsStatus"
  26. #define HADOOP_BLK_LOC "org/apache/hadoop/fs/BlockLocation"
  27. #define HADOOP_DFS "org/apache/hadoop/hdfs/DistributedFileSystem"
  28. #define HADOOP_ISTRM "org/apache/hadoop/fs/FSDataInputStream"
  29. #define HADOOP_OSTRM "org/apache/hadoop/fs/FSDataOutputStream"
  30. #define HADOOP_STAT "org/apache/hadoop/fs/FileStatus"
  31. #define HADOOP_FSPERM "org/apache/hadoop/fs/permission/FsPermission"
  32. #define HADOOP_UNIX_USER_GROUP_INFO "org/apache/hadoop/security/UnixUserGroupInformation"
  33. #define HADOOP_USER_GROUP_INFO "org/apache/hadoop/security/UserGroupInformation"
  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. /**
  45. * hdfsJniEnv: A wrapper struct to be used as 'value'
  46. * while saving thread -> JNIEnv* mappings
  47. */
  48. typedef struct
  49. {
  50. JNIEnv* env;
  51. } hdfsJniEnv;
  52. /**
  53. * Helper function to destroy a local reference of java.lang.Object
  54. * @param env: The JNIEnv pointer.
  55. * @param jFile: The local reference of java.lang.Object object
  56. * @return None.
  57. */
  58. static void destroyLocalReference(JNIEnv *env, jobject jObject)
  59. {
  60. if (jObject)
  61. (*env)->DeleteLocalRef(env, jObject);
  62. }
  63. /**
  64. * Helper function to create a org.apache.hadoop.fs.Path object.
  65. * @param env: The JNIEnv pointer.
  66. * @param path: The file-path for which to construct org.apache.hadoop.fs.Path
  67. * object.
  68. * @return Returns a jobject on success and NULL on error.
  69. */
  70. static jobject constructNewObjectOfPath(JNIEnv *env, const char *path)
  71. {
  72. //Construct a java.lang.String object
  73. jstring jPathString = (*env)->NewStringUTF(env, path);
  74. //Construct the org.apache.hadoop.fs.Path object
  75. jobject jPath =
  76. constructNewObjectOfClass(env, NULL, "org/apache/hadoop/fs/Path",
  77. "(Ljava/lang/String;)V", jPathString);
  78. if (jPath == NULL) {
  79. fprintf(stderr, "Can't construct instance of class "
  80. "org.apache.hadoop.fs.Path for %s\n", path);
  81. errno = EINTERNAL;
  82. return NULL;
  83. }
  84. // Destroy the local reference to the java.lang.String object
  85. destroyLocalReference(env, jPathString);
  86. return jPath;
  87. }
  88. /**
  89. * Helper function to translate an exception into a meaningful errno value.
  90. * @param exc: The exception.
  91. * @param env: The JNIEnv Pointer.
  92. * @param method: The name of the method that threw the exception. This
  93. * may be format string to be used in conjuction with additional arguments.
  94. * @return Returns a meaningful errno value if possible, or EINTERNAL if not.
  95. */
  96. static int errnoFromException(jthrowable exc, JNIEnv *env,
  97. const char *method, ...)
  98. {
  99. va_list ap;
  100. int errnum = 0;
  101. char *excClass = NULL;
  102. if (exc == NULL)
  103. goto default_error;
  104. if ((excClass = classNameOfObject((jobject) exc, env)) == NULL) {
  105. errnum = EINTERNAL;
  106. goto done;
  107. }
  108. if (!strcmp(excClass, "org.apache.hadoop.fs.permission."
  109. "AccessControlException")) {
  110. errnum = EACCES;
  111. goto done;
  112. }
  113. //TODO: interpret more exceptions; maybe examine exc.getMessage()
  114. default_error:
  115. //Can't tell what went wrong, so just punt
  116. (*env)->ExceptionDescribe(env);
  117. fprintf(stderr, "Call to ");
  118. va_start(ap, method);
  119. vfprintf(stderr, method, ap);
  120. va_end(ap);
  121. fprintf(stderr, " failed!\n");
  122. errnum = EINTERNAL;
  123. done:
  124. (*env)->ExceptionClear(env);
  125. if (excClass != NULL)
  126. free(excClass);
  127. return errnum;
  128. }
  129. hdfsFS hdfsConnect(const char* host, tPort port) {
  130. // conect with NULL as user name/groups
  131. return hdfsConnectAsUser(host, port, NULL, NULL, 0);
  132. }
  133. hdfsFS hdfsConnectAsUser(const char* host, tPort port, const char *user , const char **groups, int groups_size )
  134. {
  135. // JAVA EQUIVALENT:
  136. // FileSystem fs = FileSystem.get(new Configuration());
  137. // return fs;
  138. JNIEnv *env = 0;
  139. jobject jConfiguration = NULL;
  140. jobject jFS = NULL;
  141. jobject jURI = NULL;
  142. jstring jURIString = NULL;
  143. jvalue jVal;
  144. jthrowable jExc = NULL;
  145. char *cURI = 0;
  146. jobject gFsRef = NULL;
  147. //Get the JNIEnv* corresponding to current thread
  148. env = getJNIEnv();
  149. if (env == NULL) {
  150. errno = EINTERNAL;
  151. return NULL;
  152. }
  153. //Create the org.apache.hadoop.conf.Configuration object
  154. jConfiguration =
  155. constructNewObjectOfClass(env, NULL, HADOOP_CONF, "()V");
  156. if (jConfiguration == NULL) {
  157. fprintf(stderr, "Can't construct instance of class "
  158. "org.apache.hadoop.conf.Configuration\n");
  159. errno = EINTERNAL;
  160. return NULL;
  161. }
  162. if (user != NULL) {
  163. if (groups == NULL || groups_size <= 0) {
  164. fprintf(stderr, "ERROR: groups must not be empty/null\n");
  165. errno = EINVAL;
  166. return NULL;
  167. }
  168. jstring jUserString = (*env)->NewStringUTF(env, user);
  169. jarray jGroups = constructNewArrayString(env, &jExc, groups, groups_size);
  170. if (jGroups == NULL) {
  171. errno = EINTERNAL;
  172. fprintf(stderr, "ERROR: could not construct groups array\n");
  173. return NULL;
  174. }
  175. jobject jUgi;
  176. if ((jUgi = constructNewObjectOfClass(env, &jExc, HADOOP_UNIX_USER_GROUP_INFO, JMETHOD2(JPARAM(JAVA_STRING), JARRPARAM(JAVA_STRING), JAVA_VOID), jUserString, jGroups)) == NULL) {
  177. fprintf(stderr,"failed to construct hadoop user unix group info object\n");
  178. errno = errnoFromException(jExc, env, HADOOP_UNIX_USER_GROUP_INFO,
  179. "init");
  180. destroyLocalReference(env, jConfiguration);
  181. destroyLocalReference(env, jUserString);
  182. if (jGroups != NULL) {
  183. destroyLocalReference(env, jGroups);
  184. }
  185. return NULL;
  186. }
  187. #define USE_UUGI
  188. #ifdef USE_UUGI
  189. // UnixUserGroupInformation.UGI_PROPERTY_NAME
  190. jstring jAttrString = (*env)->NewStringUTF(env,"hadoop.job.ugi");
  191. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, HADOOP_UNIX_USER_GROUP_INFO, "saveToConf",
  192. JMETHOD3(JPARAM(HADOOP_CONF), JPARAM(JAVA_STRING), JPARAM(HADOOP_UNIX_USER_GROUP_INFO), JAVA_VOID),
  193. jConfiguration, jAttrString, jUgi) != 0) {
  194. errno = errnoFromException(jExc, env, HADOOP_FSPERM,
  195. "init");
  196. destroyLocalReference(env, jConfiguration);
  197. destroyLocalReference(env, jUserString);
  198. if (jGroups != NULL) {
  199. destroyLocalReference(env, jGroups);
  200. }
  201. destroyLocalReference(env, jUgi);
  202. return NULL;
  203. }
  204. destroyLocalReference(env, jUserString);
  205. destroyLocalReference(env, jGroups);
  206. destroyLocalReference(env, jUgi);
  207. }
  208. #else
  209. // what does "current" mean in the context of libhdfs ? does it mean for the last hdfs connection we used?
  210. // that's why this code cannot be activated. We know the above use of the conf object should work well with
  211. // multiple connections.
  212. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, HADOOP_USER_GROUP_INFO, "setCurrentUGI",
  213. JMETHOD1(JPARAM(HADOOP_USER_GROUP_INFO), JAVA_VOID),
  214. jUgi) != 0) {
  215. errno = errnoFromException(jExc, env, HADOOP_USER_GROUP_INFO,
  216. "setCurrentUGI");
  217. destroyLocalReference(env, jConfiguration);
  218. destroyLocalReference(env, jUserString);
  219. if (jGroups != NULL) {
  220. destroyLocalReference(env, jGroups);
  221. }
  222. destroyLocalReference(env, jUgi);
  223. return NULL;
  224. }
  225. destroyLocalReference(env, jUserString);
  226. destroyLocalReference(env, jGroups);
  227. destroyLocalReference(env, jUgi);
  228. }
  229. #endif
  230. //Check what type of FileSystem the caller wants...
  231. if (host == NULL) {
  232. // fs = FileSytem::getLocal(conf);
  233. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, HADOOP_FS, "getLocal",
  234. JMETHOD1(JPARAM(HADOOP_CONF),
  235. JPARAM(HADOOP_LOCALFS)),
  236. jConfiguration) != 0) {
  237. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  238. "FileSystem::getLocal");
  239. goto done;
  240. }
  241. jFS = jVal.l;
  242. }
  243. else if (!strcmp(host, "default") && port == 0) {
  244. //fs = FileSystem::get(conf);
  245. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL,
  246. HADOOP_FS, "get",
  247. JMETHOD1(JPARAM(HADOOP_CONF),
  248. JPARAM(HADOOP_FS)),
  249. jConfiguration) != 0) {
  250. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  251. "FileSystem::get");
  252. goto done;
  253. }
  254. jFS = jVal.l;
  255. }
  256. else {
  257. // fs = FileSystem::get(URI, conf);
  258. cURI = malloc(strlen(host)+16);
  259. sprintf(cURI, "hdfs://%s:%d", host, (int)(port));
  260. jURIString = (*env)->NewStringUTF(env, cURI);
  261. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, JAVA_NET_URI,
  262. "create", "(Ljava/lang/String;)Ljava/net/URI;",
  263. jURIString) != 0) {
  264. errno = errnoFromException(jExc, env, "java.net.URI::create");
  265. goto done;
  266. }
  267. jURI = jVal.l;
  268. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL, HADOOP_FS, "get",
  269. JMETHOD2(JPARAM(JAVA_NET_URI),
  270. JPARAM(HADOOP_CONF), JPARAM(HADOOP_FS)),
  271. jURI, jConfiguration) != 0) {
  272. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  273. "Filesystem::get(URI, Configuration)");
  274. goto done;
  275. }
  276. jFS = jVal.l;
  277. }
  278. done:
  279. // Release unnecessary local references
  280. destroyLocalReference(env, jConfiguration);
  281. destroyLocalReference(env, jURIString);
  282. destroyLocalReference(env, jURI);
  283. if (cURI) free(cURI);
  284. /* Create a global reference for this fs */
  285. if (jFS) {
  286. gFsRef = (*env)->NewGlobalRef(env, jFS);
  287. destroyLocalReference(env, jFS);
  288. }
  289. return gFsRef;
  290. }
  291. int hdfsDisconnect(hdfsFS fs)
  292. {
  293. // JAVA EQUIVALENT:
  294. // fs.close()
  295. //Get the JNIEnv* corresponding to current thread
  296. JNIEnv* env = getJNIEnv();
  297. if (env == NULL) {
  298. errno = EINTERNAL;
  299. return -2;
  300. }
  301. //Parameters
  302. jobject jFS = (jobject)fs;
  303. //Caught exception
  304. jthrowable jExc = NULL;
  305. //Sanity check
  306. if (fs == NULL) {
  307. errno = EBADF;
  308. return -1;
  309. }
  310. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  311. "close", "()V") != 0) {
  312. errno = errnoFromException(jExc, env, "Filesystem::close");
  313. return -1;
  314. }
  315. //Release unnecessary references
  316. (*env)->DeleteGlobalRef(env, fs);
  317. return 0;
  318. }
  319. hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags,
  320. int bufferSize, short replication, tSize blockSize)
  321. {
  322. /*
  323. JAVA EQUIVALENT:
  324. File f = new File(path);
  325. FSData{Input|Output}Stream f{is|os} = fs.create(f);
  326. return f{is|os};
  327. */
  328. /* Get the JNIEnv* corresponding to current thread */
  329. JNIEnv* env = getJNIEnv();
  330. if (env == NULL) {
  331. errno = EINTERNAL;
  332. return NULL;
  333. }
  334. jobject jFS = (jobject)fs;
  335. if (flags & O_RDWR) {
  336. fprintf(stderr, "ERROR: cannot open an hdfs file in O_RDWR mode\n");
  337. errno = ENOTSUP;
  338. return NULL;
  339. }
  340. /* The hadoop java api/signature */
  341. const char* method = ((flags & O_WRONLY) == 0) ? "open" : "create";
  342. const char* signature = ((flags & O_WRONLY) == 0) ?
  343. JMETHOD2(JPARAM(HADOOP_PATH), "I", JPARAM(HADOOP_ISTRM)) :
  344. JMETHOD2(JPARAM(HADOOP_PATH), "ZISJ", JPARAM(HADOOP_OSTRM));
  345. /* Return value */
  346. hdfsFile file = NULL;
  347. /* Create an object of org.apache.hadoop.fs.Path */
  348. jobject jPath = constructNewObjectOfPath(env, path);
  349. if (jPath == NULL) {
  350. return NULL;
  351. }
  352. /* Get the Configuration object from the FileSystem object */
  353. jvalue jVal;
  354. jobject jConfiguration = NULL;
  355. jthrowable jExc = NULL;
  356. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  357. "getConf", JMETHOD1("", JPARAM(HADOOP_CONF))) != 0) {
  358. errno = errnoFromException(jExc, env, "get configuration object "
  359. "from filesystem");
  360. destroyLocalReference(env, jPath);
  361. return NULL;
  362. }
  363. jConfiguration = jVal.l;
  364. jint jBufferSize = bufferSize;
  365. jshort jReplication = replication;
  366. jlong jBlockSize = blockSize;
  367. jstring jStrBufferSize = (*env)->NewStringUTF(env, "io.file.buffer.size");
  368. jstring jStrReplication = (*env)->NewStringUTF(env, "dfs.replication");
  369. jstring jStrBlockSize = (*env)->NewStringUTF(env, "dfs.block.size");
  370. //bufferSize
  371. if (!bufferSize) {
  372. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jConfiguration,
  373. HADOOP_CONF, "getInt", "(Ljava/lang/String;I)I",
  374. jStrBufferSize, 4096) != 0) {
  375. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  376. "Configuration::getInt");
  377. goto done;
  378. }
  379. jBufferSize = jVal.i;
  380. }
  381. if (flags & O_WRONLY) {
  382. //replication
  383. if (!replication) {
  384. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jConfiguration,
  385. HADOOP_CONF, "getInt", "(Ljava/lang/String;I)I",
  386. jStrReplication, 1) != 0) {
  387. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  388. "Configuration::getInt");
  389. goto done;
  390. }
  391. jReplication = jVal.i;
  392. }
  393. //blockSize
  394. if (!blockSize) {
  395. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jConfiguration,
  396. HADOOP_CONF, "getLong", "(Ljava/lang/String;J)J",
  397. jStrBlockSize, 67108864)) {
  398. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  399. "FileSystem::%s(%s)", method,
  400. signature);
  401. goto done;
  402. }
  403. jBlockSize = jVal.j;
  404. }
  405. }
  406. /* Create and return either the FSDataInputStream or
  407. FSDataOutputStream references jobject jStream */
  408. if ((flags & O_WRONLY) == 0) {
  409. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  410. method, signature, jPath, jBufferSize)) {
  411. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  412. "FileSystem::%s(%s)", method,
  413. signature);
  414. goto done;
  415. }
  416. }
  417. else {
  418. jboolean jOverWrite = 1;
  419. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  420. method, signature, jPath, jOverWrite,
  421. jBufferSize, jReplication, jBlockSize)) {
  422. errno = errnoFromException(jExc, env, "org.apache.hadoop.conf."
  423. "FileSystem::%s(%s)", method,
  424. signature);
  425. goto done;
  426. }
  427. }
  428. file = malloc(sizeof(struct hdfsFile_internal));
  429. if (!file) {
  430. errno = ENOMEM;
  431. return NULL;
  432. }
  433. file->file = (*env)->NewGlobalRef(env, jVal.l);
  434. file->type = (((flags & O_WRONLY) == 0) ? INPUT : OUTPUT);
  435. destroyLocalReference(env, jVal.l);
  436. done:
  437. //Delete unnecessary local references
  438. destroyLocalReference(env, jStrBufferSize);
  439. destroyLocalReference(env, jStrReplication);
  440. destroyLocalReference(env, jStrBlockSize);
  441. destroyLocalReference(env, jConfiguration);
  442. destroyLocalReference(env, jPath);
  443. return file;
  444. }
  445. int hdfsCloseFile(hdfsFS fs, hdfsFile file)
  446. {
  447. // JAVA EQUIVALENT:
  448. // file.close
  449. //Get the JNIEnv* corresponding to current thread
  450. JNIEnv* env = getJNIEnv();
  451. if (env == NULL) {
  452. errno = EINTERNAL;
  453. return -2;
  454. }
  455. //Parameters
  456. jobject jStream = (jobject)(file ? file->file : NULL);
  457. //Caught exception
  458. jthrowable jExc = NULL;
  459. //Sanity check
  460. if (!file || file->type == UNINITIALIZED) {
  461. errno = EBADF;
  462. return -1;
  463. }
  464. //The interface whose 'close' method to be called
  465. const char* interface = (file->type == INPUT) ?
  466. HADOOP_ISTRM : HADOOP_OSTRM;
  467. if (invokeMethod(env, NULL, &jExc, INSTANCE, jStream, interface,
  468. "close", "()V") != 0) {
  469. errno = errnoFromException(jExc, env, "%s::close", interface);
  470. return -1;
  471. }
  472. //De-allocate memory
  473. free(file);
  474. (*env)->DeleteGlobalRef(env, jStream);
  475. return 0;
  476. }
  477. int hdfsExists(hdfsFS fs, const char *path)
  478. {
  479. JNIEnv *env = getJNIEnv();
  480. if (env == NULL) {
  481. errno = EINTERNAL;
  482. return -2;
  483. }
  484. jobject jPath = constructNewObjectOfPath(env, path);
  485. jvalue jVal;
  486. jthrowable jExc = NULL;
  487. jobject jFS = (jobject)fs;
  488. if (jPath == NULL) {
  489. return -1;
  490. }
  491. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  492. "exists", JMETHOD1(JPARAM(HADOOP_PATH), "Z"),
  493. jPath) != 0) {
  494. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  495. "FileSystem::exists");
  496. return -1;
  497. }
  498. return jVal.z ? 0 : -1;
  499. }
  500. tSize hdfsRead(hdfsFS fs, hdfsFile f, void* buffer, tSize length)
  501. {
  502. // JAVA EQUIVALENT:
  503. // byte [] bR = new byte[length];
  504. // fis.read(bR);
  505. //Get the JNIEnv* corresponding to current thread
  506. JNIEnv* env = getJNIEnv();
  507. if (env == NULL) {
  508. errno = EINTERNAL;
  509. return -1;
  510. }
  511. //Parameters
  512. jobject jInputStream = (jobject)(f ? f->file : NULL);
  513. jbyteArray jbRarray;
  514. jint noReadBytes = 0;
  515. jvalue jVal;
  516. jthrowable jExc = NULL;
  517. //Sanity check
  518. if (!f || f->type == UNINITIALIZED) {
  519. errno = EBADF;
  520. return -1;
  521. }
  522. //Error checking... make sure that this file is 'readable'
  523. if (f->type != INPUT) {
  524. fprintf(stderr, "Cannot read from a non-InputStream object!\n");
  525. errno = EINVAL;
  526. return -1;
  527. }
  528. //Read the requisite bytes
  529. jbRarray = (*env)->NewByteArray(env, length);
  530. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jInputStream, HADOOP_ISTRM,
  531. "read", "([B)I", jbRarray) != 0) {
  532. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  533. "FSDataInputStream::read");
  534. noReadBytes = -1;
  535. }
  536. else {
  537. noReadBytes = jVal.i;
  538. if (noReadBytes > 0) {
  539. (*env)->GetByteArrayRegion(env, jbRarray, 0, noReadBytes, buffer);
  540. } else {
  541. //This is a valid case: there aren't any bytes left to read!
  542. if (noReadBytes == 0 || noReadBytes < -1) {
  543. fprintf(stderr, "WARN: FSDataInputStream.read returned invalid return code - libhdfs returning EOF, i.e., 0: %d\n", noReadBytes);
  544. }
  545. noReadBytes = 0;
  546. }
  547. errno = 0;
  548. }
  549. destroyLocalReference(env, jbRarray);
  550. return noReadBytes;
  551. }
  552. tSize hdfsPread(hdfsFS fs, hdfsFile f, tOffset position,
  553. void* buffer, tSize length)
  554. {
  555. // JAVA EQUIVALENT:
  556. // byte [] bR = new byte[length];
  557. // fis.read(pos, bR, 0, length);
  558. //Get the JNIEnv* corresponding to current thread
  559. JNIEnv* env = getJNIEnv();
  560. if (env == NULL) {
  561. errno = EINTERNAL;
  562. return -1;
  563. }
  564. //Parameters
  565. jobject jInputStream = (jobject)(f ? f->file : NULL);
  566. jbyteArray jbRarray;
  567. jint noReadBytes = 0;
  568. jvalue jVal;
  569. jthrowable jExc = NULL;
  570. //Sanity check
  571. if (!f || f->type == UNINITIALIZED) {
  572. errno = EBADF;
  573. return -1;
  574. }
  575. //Error checking... make sure that this file is 'readable'
  576. if (f->type != INPUT) {
  577. fprintf(stderr, "Cannot read from a non-InputStream object!\n");
  578. errno = EINVAL;
  579. return -1;
  580. }
  581. //Read the requisite bytes
  582. jbRarray = (*env)->NewByteArray(env, length);
  583. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jInputStream, HADOOP_ISTRM,
  584. "read", "(J[BII)I", position, jbRarray, 0, length) != 0) {
  585. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  586. "FSDataInputStream::read");
  587. noReadBytes = -1;
  588. }
  589. else {
  590. noReadBytes = jVal.i;
  591. if (noReadBytes > 0) {
  592. (*env)->GetByteArrayRegion(env, jbRarray, 0, noReadBytes, buffer);
  593. } else {
  594. //This is a valid case: there aren't any bytes left to read!
  595. if (noReadBytes == 0 || noReadBytes < -1) {
  596. fprintf(stderr, "WARN: FSDataInputStream.read returned invalid return code - libhdfs returning EOF, i.e., 0: %d\n", noReadBytes);
  597. }
  598. noReadBytes = 0;
  599. }
  600. errno = 0;
  601. }
  602. destroyLocalReference(env, jbRarray);
  603. return noReadBytes;
  604. }
  605. tSize hdfsWrite(hdfsFS fs, hdfsFile f, const void* buffer, tSize length)
  606. {
  607. // JAVA EQUIVALENT
  608. // byte b[] = str.getBytes();
  609. // fso.write(b);
  610. //Get the JNIEnv* corresponding to current thread
  611. JNIEnv* env = getJNIEnv();
  612. if (env == NULL) {
  613. errno = EINTERNAL;
  614. return -1;
  615. }
  616. //Parameters
  617. jobject jOutputStream = (jobject)(f ? f->file : 0);
  618. jbyteArray jbWarray;
  619. //Caught exception
  620. jthrowable jExc = NULL;
  621. //Sanity check
  622. if (!f || f->type == UNINITIALIZED) {
  623. errno = EBADF;
  624. return -1;
  625. }
  626. if (length < 0) {
  627. errno = EINVAL;
  628. return -1;
  629. }
  630. //Error checking... make sure that this file is 'writable'
  631. if (f->type != OUTPUT) {
  632. fprintf(stderr, "Cannot write into a non-OutputStream object!\n");
  633. errno = EINVAL;
  634. return -1;
  635. }
  636. // 'length' equals 'zero' is a valid use-case according to Posix!
  637. if (length != 0) {
  638. //Write the requisite bytes into the file
  639. jbWarray = (*env)->NewByteArray(env, length);
  640. (*env)->SetByteArrayRegion(env, jbWarray, 0, length, buffer);
  641. if (invokeMethod(env, NULL, &jExc, INSTANCE, jOutputStream,
  642. HADOOP_OSTRM, "write",
  643. "([B)V", jbWarray) != 0) {
  644. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  645. "FSDataOutputStream::write");
  646. length = -1;
  647. }
  648. destroyLocalReference(env, jbWarray);
  649. }
  650. //Return no. of bytes succesfully written (libc way)
  651. //i.e. 'length' itself! ;-)
  652. return length;
  653. }
  654. int hdfsSeek(hdfsFS fs, hdfsFile f, tOffset desiredPos)
  655. {
  656. // JAVA EQUIVALENT
  657. // fis.seek(pos);
  658. //Get the JNIEnv* corresponding to current thread
  659. JNIEnv* env = getJNIEnv();
  660. if (env == NULL) {
  661. errno = EINTERNAL;
  662. return -1;
  663. }
  664. //Parameters
  665. jobject jInputStream = (jobject)(f ? f->file : 0);
  666. //Caught exception
  667. jthrowable jExc = NULL;
  668. //Sanity check
  669. if (!f || f->type != INPUT) {
  670. errno = EBADF;
  671. return -1;
  672. }
  673. if (invokeMethod(env, NULL, &jExc, INSTANCE, jInputStream, HADOOP_ISTRM,
  674. "seek", "(J)V", desiredPos) != 0) {
  675. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  676. "FSDataInputStream::seek");
  677. return -1;
  678. }
  679. return 0;
  680. }
  681. tOffset hdfsTell(hdfsFS fs, hdfsFile f)
  682. {
  683. // JAVA EQUIVALENT
  684. // pos = f.getPos();
  685. //Get the JNIEnv* corresponding to current thread
  686. JNIEnv* env = getJNIEnv();
  687. if (env == NULL) {
  688. errno = EINTERNAL;
  689. return -1;
  690. }
  691. //Parameters
  692. jobject jStream = (jobject)(f ? f->file : 0);
  693. //Sanity check
  694. if (!f || f->type == UNINITIALIZED) {
  695. errno = EBADF;
  696. return -1;
  697. }
  698. const char* interface = (f->type == INPUT) ?
  699. HADOOP_ISTRM : HADOOP_OSTRM;
  700. jlong currentPos = -1;
  701. jvalue jVal;
  702. jthrowable jExc = NULL;
  703. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStream,
  704. interface, "getPos", "()J") != 0) {
  705. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  706. "FSDataInputStream::getPos");
  707. return -1;
  708. }
  709. currentPos = jVal.j;
  710. return (tOffset)currentPos;
  711. }
  712. int hdfsFlush(hdfsFS fs, hdfsFile f)
  713. {
  714. // JAVA EQUIVALENT
  715. // fos.flush();
  716. //Get the JNIEnv* corresponding to current thread
  717. JNIEnv* env = getJNIEnv();
  718. if (env == NULL) {
  719. errno = EINTERNAL;
  720. return -1;
  721. }
  722. //Parameters
  723. jobject jOutputStream = (jobject)(f ? f->file : 0);
  724. //Caught exception
  725. jthrowable jExc = NULL;
  726. //Sanity check
  727. if (!f || f->type != OUTPUT) {
  728. errno = EBADF;
  729. return -1;
  730. }
  731. if (invokeMethod(env, NULL, &jExc, INSTANCE, jOutputStream,
  732. HADOOP_OSTRM, "flush", "()V") != 0) {
  733. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  734. "FSDataInputStream::flush");
  735. return -1;
  736. }
  737. return 0;
  738. }
  739. int hdfsAvailable(hdfsFS fs, hdfsFile f)
  740. {
  741. // JAVA EQUIVALENT
  742. // fis.available();
  743. //Get the JNIEnv* corresponding to current thread
  744. JNIEnv* env = getJNIEnv();
  745. if (env == NULL) {
  746. errno = EINTERNAL;
  747. return -1;
  748. }
  749. //Parameters
  750. jobject jInputStream = (jobject)(f ? f->file : 0);
  751. //Caught exception
  752. jthrowable jExc = NULL;
  753. //Sanity check
  754. if (!f || f->type != INPUT) {
  755. errno = EBADF;
  756. return -1;
  757. }
  758. jint available = -1;
  759. jvalue jVal;
  760. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jInputStream,
  761. HADOOP_ISTRM, "available", "()I") != 0) {
  762. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  763. "FSDataInputStream::available");
  764. return -1;
  765. }
  766. available = jVal.i;
  767. return available;
  768. }
  769. int hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst)
  770. {
  771. //JAVA EQUIVALENT
  772. // FileUtil::copy(srcFS, srcPath, dstFS, dstPath,
  773. // deleteSource = false, conf)
  774. //Get the JNIEnv* corresponding to current thread
  775. JNIEnv* env = getJNIEnv();
  776. if (env == NULL) {
  777. errno = EINTERNAL;
  778. return -1;
  779. }
  780. //Parameters
  781. jobject jSrcFS = (jobject)srcFS;
  782. jobject jDstFS = (jobject)dstFS;
  783. jobject jSrcPath = NULL;
  784. jobject jDstPath = NULL;
  785. jSrcPath = constructNewObjectOfPath(env, src);
  786. if (jSrcPath == NULL) {
  787. return -1;
  788. }
  789. jDstPath = constructNewObjectOfPath(env, dst);
  790. if (jDstPath == NULL) {
  791. destroyLocalReference(env, jSrcPath);
  792. return -1;
  793. }
  794. int retval = 0;
  795. //Create the org.apache.hadoop.conf.Configuration object
  796. jobject jConfiguration =
  797. constructNewObjectOfClass(env, NULL, HADOOP_CONF, "()V");
  798. if (jConfiguration == NULL) {
  799. fprintf(stderr, "Can't construct instance of class "
  800. "org.apache.hadoop.conf.Configuration\n");
  801. errno = EINTERNAL;
  802. destroyLocalReference(env, jSrcPath);
  803. destroyLocalReference(env, jDstPath);
  804. return -1;
  805. }
  806. //FileUtil::copy
  807. jboolean deleteSource = 0; //Only copy
  808. jvalue jVal;
  809. jthrowable jExc = NULL;
  810. if (invokeMethod(env, &jVal, &jExc, STATIC,
  811. NULL, "org/apache/hadoop/fs/FileUtil", "copy",
  812. "(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",
  813. jSrcFS, jSrcPath, jDstFS, jDstPath, deleteSource,
  814. jConfiguration) != 0) {
  815. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  816. "FileUtil::copy");
  817. retval = -1;
  818. goto done;
  819. }
  820. done:
  821. //Delete unnecessary local references
  822. destroyLocalReference(env, jConfiguration);
  823. destroyLocalReference(env, jSrcPath);
  824. destroyLocalReference(env, jDstPath);
  825. return retval;
  826. }
  827. int hdfsMove(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst)
  828. {
  829. //JAVA EQUIVALENT
  830. // FileUtil::copy(srcFS, srcPath, dstFS, dstPath,
  831. // deleteSource = true, conf)
  832. //Get the JNIEnv* corresponding to current thread
  833. JNIEnv* env = getJNIEnv();
  834. if (env == NULL) {
  835. errno = EINTERNAL;
  836. return -1;
  837. }
  838. //Parameters
  839. jobject jSrcFS = (jobject)srcFS;
  840. jobject jDstFS = (jobject)dstFS;
  841. jobject jSrcPath = NULL;
  842. jobject jDstPath = NULL;
  843. jSrcPath = constructNewObjectOfPath(env, src);
  844. if (jSrcPath == NULL) {
  845. return -1;
  846. }
  847. jDstPath = constructNewObjectOfPath(env, dst);
  848. if (jDstPath == NULL) {
  849. destroyLocalReference(env, jSrcPath);
  850. return -1;
  851. }
  852. int retval = 0;
  853. //Create the org.apache.hadoop.conf.Configuration object
  854. jobject jConfiguration =
  855. constructNewObjectOfClass(env, NULL, HADOOP_CONF, "()V");
  856. if (jConfiguration == NULL) {
  857. fprintf(stderr, "Can't construct instance of class "
  858. "org.apache.hadoop.conf.Configuration\n");
  859. errno = EINTERNAL;
  860. destroyLocalReference(env, jSrcPath);
  861. destroyLocalReference(env, jDstPath);
  862. return -1;
  863. }
  864. //FileUtil::copy
  865. jboolean deleteSource = 1; //Delete src after copy
  866. jvalue jVal;
  867. jthrowable jExc = NULL;
  868. if (invokeMethod(env, &jVal, &jExc, STATIC, NULL,
  869. "org/apache/hadoop/fs/FileUtil", "copy",
  870. "(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",
  871. jSrcFS, jSrcPath, jDstFS, jDstPath, deleteSource,
  872. jConfiguration) != 0) {
  873. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  874. "FileUtil::copy(move)");
  875. retval = -1;
  876. goto done;
  877. }
  878. done:
  879. //Delete unnecessary local references
  880. destroyLocalReference(env, jConfiguration);
  881. destroyLocalReference(env, jSrcPath);
  882. destroyLocalReference(env, jDstPath);
  883. return retval;
  884. }
  885. int hdfsDelete(hdfsFS fs, const char* path, int recursive)
  886. {
  887. // JAVA EQUIVALENT:
  888. // File f = new File(path);
  889. // bool retval = fs.delete(f);
  890. //Get the JNIEnv* corresponding to current thread
  891. JNIEnv* env = getJNIEnv();
  892. if (env == NULL) {
  893. errno = EINTERNAL;
  894. return -1;
  895. }
  896. jobject jFS = (jobject)fs;
  897. //Create an object of java.io.File
  898. jobject jPath = constructNewObjectOfPath(env, path);
  899. if (jPath == NULL) {
  900. return -1;
  901. }
  902. //Delete the file
  903. jvalue jVal;
  904. jthrowable jExc = NULL;
  905. jboolean jRecursive = recursive;
  906. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  907. "delete", "(Lorg/apache/hadoop/fs/Path;Z)Z",
  908. jPath, jRecursive) != 0) {
  909. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  910. "FileSystem::delete");
  911. return -1;
  912. }
  913. //Delete unnecessary local references
  914. destroyLocalReference(env, jPath);
  915. return (jVal.z) ? 0 : -1;
  916. }
  917. int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath)
  918. {
  919. // JAVA EQUIVALENT:
  920. // Path old = new Path(oldPath);
  921. // Path new = new Path(newPath);
  922. // fs.rename(old, new);
  923. //Get the JNIEnv* corresponding to current thread
  924. JNIEnv* env = getJNIEnv();
  925. if (env == NULL) {
  926. errno = EINTERNAL;
  927. return -1;
  928. }
  929. jobject jFS = (jobject)fs;
  930. //Create objects of org.apache.hadoop.fs.Path
  931. jobject jOldPath = NULL;
  932. jobject jNewPath = NULL;
  933. jOldPath = constructNewObjectOfPath(env, oldPath);
  934. if (jOldPath == NULL) {
  935. return -1;
  936. }
  937. jNewPath = constructNewObjectOfPath(env, newPath);
  938. if (jNewPath == NULL) {
  939. destroyLocalReference(env, jOldPath);
  940. return -1;
  941. }
  942. //Rename the file
  943. jvalue jVal;
  944. jthrowable jExc = NULL;
  945. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS, "rename",
  946. JMETHOD2(JPARAM(HADOOP_PATH), JPARAM(HADOOP_PATH), "Z"),
  947. jOldPath, jNewPath) != 0) {
  948. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  949. "FileSystem::rename");
  950. return -1;
  951. }
  952. //Delete unnecessary local references
  953. destroyLocalReference(env, jOldPath);
  954. destroyLocalReference(env, jNewPath);
  955. return (jVal.z) ? 0 : -1;
  956. }
  957. char* hdfsGetWorkingDirectory(hdfsFS fs, char* buffer, size_t bufferSize)
  958. {
  959. // JAVA EQUIVALENT:
  960. // Path p = fs.getWorkingDirectory();
  961. // return p.toString()
  962. //Get the JNIEnv* corresponding to current thread
  963. JNIEnv* env = getJNIEnv();
  964. if (env == NULL) {
  965. errno = EINTERNAL;
  966. return NULL;
  967. }
  968. jobject jFS = (jobject)fs;
  969. jobject jPath = NULL;
  970. jvalue jVal;
  971. jthrowable jExc = NULL;
  972. //FileSystem::getWorkingDirectory()
  973. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS,
  974. HADOOP_FS, "getWorkingDirectory",
  975. "()Lorg/apache/hadoop/fs/Path;") != 0 ||
  976. jVal.l == NULL) {
  977. errno = errnoFromException(jExc, env, "FileSystem::"
  978. "getWorkingDirectory");
  979. return NULL;
  980. }
  981. jPath = jVal.l;
  982. //Path::toString()
  983. jstring jPathString;
  984. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jPath,
  985. "org/apache/hadoop/fs/Path", "toString",
  986. "()Ljava/lang/String;") != 0) {
  987. errno = errnoFromException(jExc, env, "Path::toString");
  988. destroyLocalReference(env, jPath);
  989. return NULL;
  990. }
  991. jPathString = jVal.l;
  992. const char *jPathChars = (const char*)
  993. ((*env)->GetStringUTFChars(env, jPathString, NULL));
  994. //Copy to user-provided buffer
  995. strncpy(buffer, jPathChars, bufferSize);
  996. //Delete unnecessary local references
  997. (*env)->ReleaseStringUTFChars(env, jPathString, jPathChars);
  998. destroyLocalReference(env, jPathString);
  999. destroyLocalReference(env, jPath);
  1000. return buffer;
  1001. }
  1002. int hdfsSetWorkingDirectory(hdfsFS fs, const char* path)
  1003. {
  1004. // JAVA EQUIVALENT:
  1005. // fs.setWorkingDirectory(Path(path));
  1006. //Get the JNIEnv* corresponding to current thread
  1007. JNIEnv* env = getJNIEnv();
  1008. if (env == NULL) {
  1009. errno = EINTERNAL;
  1010. return -1;
  1011. }
  1012. jobject jFS = (jobject)fs;
  1013. int retval = 0;
  1014. jthrowable jExc = NULL;
  1015. //Create an object of org.apache.hadoop.fs.Path
  1016. jobject jPath = constructNewObjectOfPath(env, path);
  1017. if (jPath == NULL) {
  1018. return -1;
  1019. }
  1020. //FileSystem::setWorkingDirectory()
  1021. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  1022. "setWorkingDirectory",
  1023. "(Lorg/apache/hadoop/fs/Path;)V", jPath) != 0) {
  1024. errno = errnoFromException(jExc, env, "FileSystem::"
  1025. "setWorkingDirectory");
  1026. retval = -1;
  1027. }
  1028. //Delete unnecessary local references
  1029. destroyLocalReference(env, jPath);
  1030. return retval;
  1031. }
  1032. int hdfsCreateDirectory(hdfsFS fs, const char* path)
  1033. {
  1034. // JAVA EQUIVALENT:
  1035. // fs.mkdirs(new Path(path));
  1036. //Get the JNIEnv* corresponding to current thread
  1037. JNIEnv* env = getJNIEnv();
  1038. if (env == NULL) {
  1039. errno = EINTERNAL;
  1040. return -1;
  1041. }
  1042. jobject jFS = (jobject)fs;
  1043. //Create an object of org.apache.hadoop.fs.Path
  1044. jobject jPath = constructNewObjectOfPath(env, path);
  1045. if (jPath == NULL) {
  1046. return -1;
  1047. }
  1048. //Create the directory
  1049. jvalue jVal;
  1050. jVal.z = 0;
  1051. jthrowable jExc = NULL;
  1052. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1053. "mkdirs", "(Lorg/apache/hadoop/fs/Path;)Z",
  1054. jPath) != 0) {
  1055. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1056. "FileSystem::mkdirs");
  1057. goto done;
  1058. }
  1059. done:
  1060. //Delete unnecessary local references
  1061. destroyLocalReference(env, jPath);
  1062. return (jVal.z) ? 0 : -1;
  1063. }
  1064. int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication)
  1065. {
  1066. // JAVA EQUIVALENT:
  1067. // fs.setReplication(new Path(path), replication);
  1068. //Get the JNIEnv* corresponding to current thread
  1069. JNIEnv* env = getJNIEnv();
  1070. if (env == NULL) {
  1071. errno = EINTERNAL;
  1072. return -1;
  1073. }
  1074. jobject jFS = (jobject)fs;
  1075. //Create an object of org.apache.hadoop.fs.Path
  1076. jobject jPath = constructNewObjectOfPath(env, path);
  1077. if (jPath == NULL) {
  1078. return -1;
  1079. }
  1080. //Create the directory
  1081. jvalue jVal;
  1082. jthrowable jExc = NULL;
  1083. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1084. "setReplication", "(Lorg/apache/hadoop/fs/Path;S)Z",
  1085. jPath, replication) != 0) {
  1086. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1087. "FileSystem::setReplication");
  1088. goto done;
  1089. }
  1090. done:
  1091. //Delete unnecessary local references
  1092. destroyLocalReference(env, jPath);
  1093. return (jVal.z) ? 0 : -1;
  1094. }
  1095. int hdfsChown(hdfsFS fs, const char* path, const char *owner, const char *group)
  1096. {
  1097. // JAVA EQUIVALENT:
  1098. // fs.setOwner(path, owner, group)
  1099. //Get the JNIEnv* corresponding to current thread
  1100. JNIEnv* env = getJNIEnv();
  1101. if (env == NULL) {
  1102. errno = EINTERNAL;
  1103. return -1;
  1104. }
  1105. if (owner == NULL && group == NULL) {
  1106. fprintf(stderr, "Both owner and group cannot be null in chown");
  1107. errno = EINVAL;
  1108. return -1;
  1109. }
  1110. jobject jFS = (jobject)fs;
  1111. jobject jPath = constructNewObjectOfPath(env, path);
  1112. if (jPath == NULL) {
  1113. return -1;
  1114. }
  1115. jstring jOwnerString = (*env)->NewStringUTF(env, owner);
  1116. jstring jGroupString = (*env)->NewStringUTF(env, group);
  1117. //Create the directory
  1118. int ret = 0;
  1119. jthrowable jExc = NULL;
  1120. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  1121. "setOwner", JMETHOD3(JPARAM(HADOOP_PATH), JPARAM(JAVA_STRING), JPARAM(JAVA_STRING), JAVA_VOID),
  1122. jPath, jOwnerString, jGroupString) != 0) {
  1123. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1124. "FileSystem::setOwner");
  1125. ret = -1;
  1126. goto done;
  1127. }
  1128. done:
  1129. destroyLocalReference(env, jPath);
  1130. destroyLocalReference(env, jOwnerString);
  1131. destroyLocalReference(env, jGroupString);
  1132. return ret;
  1133. }
  1134. int hdfsChmod(hdfsFS fs, const char* path, short mode)
  1135. {
  1136. // JAVA EQUIVALENT:
  1137. // fs.setPermission(path, FsPermission)
  1138. //Get the JNIEnv* corresponding to current thread
  1139. JNIEnv* env = getJNIEnv();
  1140. if (env == NULL) {
  1141. errno = EINTERNAL;
  1142. return -1;
  1143. }
  1144. jobject jFS = (jobject)fs;
  1145. // construct jPerm = FsPermission.createImmutable(short mode);
  1146. jshort jmode = mode;
  1147. jobject jPermObj =
  1148. constructNewObjectOfClass(env, NULL, HADOOP_FSPERM,"(S)V",jmode);
  1149. if (jPermObj == NULL) {
  1150. return -2;
  1151. }
  1152. //Create an object of org.apache.hadoop.fs.Path
  1153. jobject jPath = constructNewObjectOfPath(env, path);
  1154. if (jPath == NULL) {
  1155. return -3;
  1156. }
  1157. //Create the directory
  1158. int ret = 0;
  1159. jthrowable jExc = NULL;
  1160. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  1161. "setPermission", JMETHOD2(JPARAM(HADOOP_PATH), JPARAM(HADOOP_FSPERM), JAVA_VOID),
  1162. jPath, jPermObj) != 0) {
  1163. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1164. "FileSystem::setPermission");
  1165. ret = -1;
  1166. goto done;
  1167. }
  1168. done:
  1169. destroyLocalReference(env, jPath);
  1170. destroyLocalReference(env, jPermObj);
  1171. return ret;
  1172. }
  1173. int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime)
  1174. {
  1175. // JAVA EQUIVALENT:
  1176. // fs.setTimes(src, mtime, atime)
  1177. //Get the JNIEnv* corresponding to current thread
  1178. JNIEnv* env = getJNIEnv();
  1179. if (env == NULL) {
  1180. errno = EINTERNAL;
  1181. return -1;
  1182. }
  1183. jobject jFS = (jobject)fs;
  1184. //Create an object of org.apache.hadoop.fs.Path
  1185. jobject jPath = constructNewObjectOfPath(env, path);
  1186. if (jPath == NULL) {
  1187. fprintf(stderr, "could not construct path object\n");
  1188. return -2;
  1189. }
  1190. jlong jmtime = mtime * (jlong)1000;
  1191. jlong jatime = atime * (jlong)1000;
  1192. int ret = 0;
  1193. jthrowable jExc = NULL;
  1194. if (invokeMethod(env, NULL, &jExc, INSTANCE, jFS, HADOOP_FS,
  1195. "setTimes", JMETHOD3(JPARAM(HADOOP_PATH), "J", "J", JAVA_VOID),
  1196. jPath, jmtime, jatime) != 0) {
  1197. fprintf(stderr, "call to setTime failed\n");
  1198. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1199. "FileSystem::setTimes");
  1200. ret = -1;
  1201. goto done;
  1202. }
  1203. done:
  1204. destroyLocalReference(env, jPath);
  1205. return ret;
  1206. }
  1207. char***
  1208. hdfsGetHosts(hdfsFS fs, const char* path, tOffset start, tOffset length)
  1209. {
  1210. // JAVA EQUIVALENT:
  1211. // fs.getFileBlockLoctions(new Path(path), start, length);
  1212. //Get the JNIEnv* corresponding to current thread
  1213. JNIEnv* env = getJNIEnv();
  1214. if (env == NULL) {
  1215. errno = EINTERNAL;
  1216. return NULL;
  1217. }
  1218. jobject jFS = (jobject)fs;
  1219. //Create an object of org.apache.hadoop.fs.Path
  1220. jobject jPath = constructNewObjectOfPath(env, path);
  1221. if (jPath == NULL) {
  1222. return NULL;
  1223. }
  1224. jvalue jFSVal;
  1225. jthrowable jFSExc = NULL;
  1226. if (invokeMethod(env, &jFSVal, &jFSExc, INSTANCE, jFS,
  1227. HADOOP_FS, "getFileStatus",
  1228. "(Lorg/apache/hadoop/fs/Path;)"
  1229. "Lorg/apache/hadoop/fs/FileStatus;",
  1230. jPath) != 0) {
  1231. errno = errnoFromException(jFSExc, env, "org.apache.hadoop.fs."
  1232. "FileSystem::getFileStatus");
  1233. destroyLocalReference(env, jPath);
  1234. return NULL;
  1235. }
  1236. jobject jFileStatus = jFSVal.l;
  1237. //org.apache.hadoop.fs.FileSystem::getFileBlockLocations
  1238. char*** blockHosts = NULL;
  1239. jobjectArray jBlockLocations;;
  1240. jvalue jVal;
  1241. jthrowable jExc = NULL;
  1242. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS,
  1243. HADOOP_FS, "getFileBlockLocations",
  1244. "(Lorg/apache/hadoop/fs/FileStatus;JJ)"
  1245. "[Lorg/apache/hadoop/fs/BlockLocation;",
  1246. jFileStatus, start, length) != 0) {
  1247. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1248. "FileSystem::getFileBlockLocations");
  1249. destroyLocalReference(env, jPath);
  1250. destroyLocalReference(env, jFileStatus);
  1251. return NULL;
  1252. }
  1253. jBlockLocations = jVal.l;
  1254. //Figure out no of entries in jBlockLocations
  1255. //Allocate memory and add NULL at the end
  1256. jsize jNumFileBlocks = (*env)->GetArrayLength(env, jBlockLocations);
  1257. blockHosts = malloc(sizeof(char**) * (jNumFileBlocks+1));
  1258. if (blockHosts == NULL) {
  1259. errno = ENOMEM;
  1260. goto done;
  1261. }
  1262. blockHosts[jNumFileBlocks] = NULL;
  1263. if (jNumFileBlocks == 0) {
  1264. errno = 0;
  1265. goto done;
  1266. }
  1267. //Now parse each block to get hostnames
  1268. int i = 0;
  1269. for (i=0; i < jNumFileBlocks; ++i) {
  1270. jobject jFileBlock =
  1271. (*env)->GetObjectArrayElement(env, jBlockLocations, i);
  1272. jvalue jVal;
  1273. jobjectArray jFileBlockHosts;
  1274. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFileBlock, HADOOP_BLK_LOC,
  1275. "getHosts", "()[Ljava/lang/String;") ||
  1276. jVal.l == NULL) {
  1277. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1278. "BlockLocation::getHosts");
  1279. destroyLocalReference(env, jPath);
  1280. destroyLocalReference(env, jFileStatus);
  1281. destroyLocalReference(env, jBlockLocations);
  1282. return NULL;
  1283. }
  1284. jFileBlockHosts = jVal.l;
  1285. //Figure out no of hosts in jFileBlockHosts
  1286. //Allocate memory and add NULL at the end
  1287. jsize jNumBlockHosts = (*env)->GetArrayLength(env, jFileBlockHosts);
  1288. blockHosts[i] = malloc(sizeof(char*) * (jNumBlockHosts+1));
  1289. if (blockHosts[i] == NULL) {
  1290. int x = 0;
  1291. for (x=0; x < i; ++x) {
  1292. free(blockHosts[x]);
  1293. }
  1294. free(blockHosts);
  1295. errno = ENOMEM;
  1296. goto done;
  1297. }
  1298. blockHosts[i][jNumBlockHosts] = NULL;
  1299. //Now parse each hostname
  1300. int j = 0;
  1301. const char *hostName;
  1302. for (j=0; j < jNumBlockHosts; ++j) {
  1303. jstring jHost =
  1304. (*env)->GetObjectArrayElement(env, jFileBlockHosts, j);
  1305. hostName =
  1306. (const char*)((*env)->GetStringUTFChars(env, jHost, NULL));
  1307. blockHosts[i][j] = strdup(hostName);
  1308. (*env)->ReleaseStringUTFChars(env, jHost, hostName);
  1309. destroyLocalReference(env, jHost);
  1310. }
  1311. destroyLocalReference(env, jFileBlockHosts);
  1312. }
  1313. done:
  1314. //Delete unnecessary local references
  1315. destroyLocalReference(env, jPath);
  1316. destroyLocalReference(env, jFileStatus);
  1317. destroyLocalReference(env, jBlockLocations);
  1318. return blockHosts;
  1319. }
  1320. void hdfsFreeHosts(char ***blockHosts)
  1321. {
  1322. int i, j;
  1323. for (i=0; blockHosts[i]; i++) {
  1324. for (j=0; blockHosts[i][j]; j++) {
  1325. free(blockHosts[i][j]);
  1326. }
  1327. free(blockHosts[i]);
  1328. }
  1329. free(blockHosts);
  1330. }
  1331. tOffset hdfsGetDefaultBlockSize(hdfsFS fs)
  1332. {
  1333. // JAVA EQUIVALENT:
  1334. // fs.getDefaultBlockSize();
  1335. //Get the JNIEnv* corresponding to current thread
  1336. JNIEnv* env = getJNIEnv();
  1337. if (env == NULL) {
  1338. errno = EINTERNAL;
  1339. return -1;
  1340. }
  1341. jobject jFS = (jobject)fs;
  1342. //FileSystem::getDefaultBlockSize()
  1343. tOffset blockSize = -1;
  1344. jvalue jVal;
  1345. jthrowable jExc = NULL;
  1346. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1347. "getDefaultBlockSize", "()J") != 0) {
  1348. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1349. "FileSystem::getDefaultBlockSize");
  1350. return -1;
  1351. }
  1352. blockSize = jVal.j;
  1353. return blockSize;
  1354. }
  1355. tOffset hdfsGetCapacity(hdfsFS fs)
  1356. {
  1357. // JAVA EQUIVALENT:
  1358. // FsStatus fss = fs.getStatus();
  1359. // return Fss.getCapacity();
  1360. //Get the JNIEnv* corresponding to current thread
  1361. JNIEnv* env = getJNIEnv();
  1362. if (env == NULL) {
  1363. errno = EINTERNAL;
  1364. return -1;
  1365. }
  1366. jobject jFS = (jobject)fs;
  1367. //FileSystem::getStatus
  1368. jvalue jVal;
  1369. jthrowable jExc = NULL;
  1370. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1371. "getStatus", "()Lorg/apache/hadoop/fs/FsStatus;") != 0) {
  1372. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1373. "FileSystem::getStatus");
  1374. return -1;
  1375. }
  1376. jobject fss = (jobject)jVal.l;
  1377. if (invokeMethod(env, &jVal, &jExc, INSTANCE, fss, HADOOP_FSSTATUS,
  1378. "getCapacity", "()J") != 0) {
  1379. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1380. "FsStatus::getCapacity");
  1381. return -1;
  1382. }
  1383. return jVal.j;
  1384. }
  1385. tOffset hdfsGetUsed(hdfsFS fs)
  1386. {
  1387. // JAVA EQUIVALENT:
  1388. // FsStatus fss = fs.getStatus();
  1389. // return Fss.getUsed();
  1390. //Get the JNIEnv* corresponding to current thread
  1391. JNIEnv* env = getJNIEnv();
  1392. if (env == NULL) {
  1393. errno = EINTERNAL;
  1394. return -1;
  1395. }
  1396. jobject jFS = (jobject)fs;
  1397. //FileSystem::getStatus
  1398. jvalue jVal;
  1399. jthrowable jExc = NULL;
  1400. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1401. "getStatus", "()Lorg/apache/hadoop/fs/FsStatus;") != 0) {
  1402. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1403. "FileSystem::getStatus");
  1404. return -1;
  1405. }
  1406. jobject fss = (jobject)jVal.l;
  1407. if (invokeMethod(env, &jVal, &jExc, INSTANCE, fss, HADOOP_FSSTATUS,
  1408. "getUsed", "()J") != 0) {
  1409. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1410. "FsStatus::getUsed");
  1411. return -1;
  1412. }
  1413. return jVal.j;
  1414. }
  1415. static int
  1416. getFileInfoFromStat(JNIEnv *env, jobject jStat, hdfsFileInfo *fileInfo)
  1417. {
  1418. jvalue jVal;
  1419. jthrowable jExc = NULL;
  1420. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1421. HADOOP_STAT, "isDir", "()Z") != 0) {
  1422. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1423. "FileStatus::isDir");
  1424. return -1;
  1425. }
  1426. fileInfo->mKind = jVal.z ? kObjectKindDirectory : kObjectKindFile;
  1427. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1428. HADOOP_STAT, "getReplication", "()S") != 0) {
  1429. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1430. "FileStatus::getReplication");
  1431. return -1;
  1432. }
  1433. fileInfo->mReplication = jVal.s;
  1434. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1435. HADOOP_STAT, "getBlockSize", "()J") != 0) {
  1436. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1437. "FileStatus::getBlockSize");
  1438. return -1;
  1439. }
  1440. fileInfo->mBlockSize = jVal.j;
  1441. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1442. HADOOP_STAT, "getModificationTime", "()J") != 0) {
  1443. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1444. "FileStatus::getModificationTime");
  1445. return -1;
  1446. }
  1447. fileInfo->mLastMod = (tTime) (jVal.j / 1000);
  1448. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1449. HADOOP_STAT, "getAccessTime", "()J") != 0) {
  1450. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1451. "FileStatus::getAccessTime");
  1452. return -1;
  1453. }
  1454. fileInfo->mLastAccess = (tTime) (jVal.j / 1000);
  1455. if (fileInfo->mKind == kObjectKindFile) {
  1456. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat,
  1457. HADOOP_STAT, "getLen", "()J") != 0) {
  1458. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1459. "FileStatus::getLen");
  1460. return -1;
  1461. }
  1462. fileInfo->mSize = jVal.j;
  1463. }
  1464. jobject jPath;
  1465. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat, HADOOP_STAT,
  1466. "getPath", "()Lorg/apache/hadoop/fs/Path;") ||
  1467. jVal.l == NULL) {
  1468. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1469. "Path::getPath");
  1470. return -1;
  1471. }
  1472. jPath = jVal.l;
  1473. jstring jPathName;
  1474. const char *cPathName;
  1475. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jPath, HADOOP_PATH,
  1476. "toString", "()Ljava/lang/String;")) {
  1477. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1478. "Path::toString");
  1479. destroyLocalReference(env, jPath);
  1480. return -1;
  1481. }
  1482. jPathName = jVal.l;
  1483. cPathName = (const char*) ((*env)->GetStringUTFChars(env, jPathName, NULL));
  1484. fileInfo->mName = strdup(cPathName);
  1485. (*env)->ReleaseStringUTFChars(env, jPathName, cPathName);
  1486. destroyLocalReference(env, jPath);
  1487. destroyLocalReference(env, jPathName);
  1488. jstring jUserName;
  1489. const char* cUserName;
  1490. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat, HADOOP_STAT,
  1491. "getOwner", "()Ljava/lang/String;")) {
  1492. fprintf(stderr, "Call to org.apache.hadoop.fs."
  1493. "FileStatus::getOwner failed!\n");
  1494. errno = EINTERNAL;
  1495. return -1;
  1496. }
  1497. jUserName = jVal.l;
  1498. cUserName = (const char*) ((*env)->GetStringUTFChars(env, jUserName, NULL));
  1499. fileInfo->mOwner = strdup(cUserName);
  1500. (*env)->ReleaseStringUTFChars(env, jUserName, cUserName);
  1501. destroyLocalReference(env, jUserName);
  1502. jstring jGroupName;
  1503. const char* cGroupName;
  1504. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat, HADOOP_STAT,
  1505. "getGroup", "()Ljava/lang/String;")) {
  1506. fprintf(stderr, "Call to org.apache.hadoop.fs."
  1507. "FileStatus::getGroup failed!\n");
  1508. errno = EINTERNAL;
  1509. return -1;
  1510. }
  1511. jGroupName = jVal.l;
  1512. cGroupName = (const char*) ((*env)->GetStringUTFChars(env, jGroupName, NULL));
  1513. fileInfo->mGroup = strdup(cGroupName);
  1514. (*env)->ReleaseStringUTFChars(env, jGroupName, cGroupName);
  1515. destroyLocalReference(env, jGroupName);
  1516. jobject jPermission;
  1517. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jStat, HADOOP_STAT,
  1518. "getPermission", "()Lorg/apache/hadoop/fs/permission/FsPermission;") ||
  1519. jVal.l == NULL) {
  1520. fprintf(stderr, "Call to org.apache.hadoop.fs."
  1521. "FileStatus::getPermission failed!\n");
  1522. errno = EINTERNAL;
  1523. return -1;
  1524. }
  1525. jPermission = jVal.l;
  1526. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jPermission, HADOOP_FSPERM,
  1527. "toShort", "()S") != 0) {
  1528. fprintf(stderr, "Call to org.apache.hadoop.fs.permission."
  1529. "FsPermission::toShort failed!\n");
  1530. errno = EINTERNAL;
  1531. return -1;
  1532. }
  1533. fileInfo->mPermissions = jVal.s;
  1534. destroyLocalReference(env, jPermission);
  1535. return 0;
  1536. }
  1537. static int
  1538. getFileInfo(JNIEnv *env, jobject jFS, jobject jPath, hdfsFileInfo *fileInfo)
  1539. {
  1540. // JAVA EQUIVALENT:
  1541. // fs.isDirectory(f)
  1542. // fs.getModificationTime()
  1543. // fs.getAccessTime()
  1544. // fs.getLength(f)
  1545. // f.getPath()
  1546. // f.getOwner()
  1547. // f.getGroup()
  1548. // f.getPermission().toShort()
  1549. jobject jStat;
  1550. jvalue jVal;
  1551. jthrowable jExc = NULL;
  1552. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1553. "exists", JMETHOD1(JPARAM(HADOOP_PATH), "Z"),
  1554. jPath) != 0) {
  1555. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1556. "FileSystem::exists");
  1557. return -1;
  1558. }
  1559. if (jVal.z == 0) {
  1560. errno = ENOENT;
  1561. return -1;
  1562. }
  1563. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_FS,
  1564. "getFileStatus", JMETHOD1(JPARAM(HADOOP_PATH), JPARAM(HADOOP_STAT)),
  1565. jPath) != 0) {
  1566. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1567. "FileSystem::getFileStatus");
  1568. return -1;
  1569. }
  1570. jStat = jVal.l;
  1571. int ret = getFileInfoFromStat(env, jStat, fileInfo);
  1572. destroyLocalReference(env, jStat);
  1573. return ret;
  1574. }
  1575. hdfsFileInfo* hdfsListDirectory(hdfsFS fs, const char* path, int *numEntries)
  1576. {
  1577. // JAVA EQUIVALENT:
  1578. // Path p(path);
  1579. // Path []pathList = fs.listPaths(p)
  1580. // foreach path in pathList
  1581. // getFileInfo(path)
  1582. //Get the JNIEnv* corresponding to current thread
  1583. JNIEnv* env = getJNIEnv();
  1584. if (env == NULL) {
  1585. errno = EINTERNAL;
  1586. return NULL;
  1587. }
  1588. jobject jFS = (jobject)fs;
  1589. //Create an object of org.apache.hadoop.fs.Path
  1590. jobject jPath = constructNewObjectOfPath(env, path);
  1591. if (jPath == NULL) {
  1592. return NULL;
  1593. }
  1594. hdfsFileInfo *pathList = 0;
  1595. jobjectArray jPathList = NULL;
  1596. jvalue jVal;
  1597. jthrowable jExc = NULL;
  1598. if (invokeMethod(env, &jVal, &jExc, INSTANCE, jFS, HADOOP_DFS, "listStatus",
  1599. JMETHOD1(JPARAM(HADOOP_PATH), JARRPARAM(HADOOP_STAT)),
  1600. jPath) != 0) {
  1601. errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
  1602. "FileSystem::listStatus");
  1603. destroyLocalReference(env, jPath);
  1604. return NULL;
  1605. }
  1606. jPathList = jVal.l;
  1607. //Figure out no of entries in that directory
  1608. jsize jPathListSize = (*env)->GetArrayLength(env, jPathList);
  1609. *numEntries = jPathListSize;
  1610. if (jPathListSize == 0) {
  1611. errno = 0;
  1612. goto done;
  1613. }
  1614. //Allocate memory
  1615. pathList = calloc(jPathListSize, sizeof(hdfsFileInfo));
  1616. if (pathList == NULL) {
  1617. errno = ENOMEM;
  1618. goto done;
  1619. }
  1620. //Save path information in pathList
  1621. jsize i;
  1622. jobject tmpStat;
  1623. for (i=0; i < jPathListSize; ++i) {
  1624. tmpStat = (*env)->GetObjectArrayElement(env, jPathList, i);
  1625. if (getFileInfoFromStat(env, tmpStat, &pathList[i])) {
  1626. hdfsFreeFileInfo(pathList, jPathListSize);
  1627. destroyLocalReference(env, tmpStat);
  1628. pathList = NULL;
  1629. goto done;
  1630. }
  1631. destroyLocalReference(env, tmpStat);
  1632. }
  1633. done:
  1634. //Delete unnecessary local references
  1635. destroyLocalReference(env, jPath);
  1636. destroyLocalReference(env, jPathList);
  1637. return pathList;
  1638. }
  1639. hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path)
  1640. {
  1641. // JAVA EQUIVALENT:
  1642. // File f(path);
  1643. // fs.isDirectory(f)
  1644. // fs.lastModified() ??
  1645. // fs.getLength(f)
  1646. // f.getPath()
  1647. //Get the JNIEnv* corresponding to current thread
  1648. JNIEnv* env = getJNIEnv();
  1649. if (env == NULL) {
  1650. errno = EINTERNAL;
  1651. return NULL;
  1652. }
  1653. jobject jFS = (jobject)fs;
  1654. //Create an object of org.apache.hadoop.fs.Path
  1655. jobject jPath = constructNewObjectOfPath(env, path);
  1656. if (jPath == NULL) {
  1657. return NULL;
  1658. }
  1659. hdfsFileInfo *fileInfo = calloc(1, sizeof(hdfsFileInfo));
  1660. if (getFileInfo(env, jFS, jPath, fileInfo)) {
  1661. hdfsFreeFileInfo(fileInfo, 1);
  1662. fileInfo = NULL;
  1663. goto done;
  1664. }
  1665. done:
  1666. //Delete unnecessary local references
  1667. destroyLocalReference(env, jPath);
  1668. return fileInfo;
  1669. }
  1670. void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries)
  1671. {
  1672. //Free the mName
  1673. int i;
  1674. for (i=0; i < numEntries; ++i) {
  1675. if (hdfsFileInfo[i].mName) {
  1676. free(hdfsFileInfo[i].mName);
  1677. }
  1678. }
  1679. //Free entire block
  1680. free(hdfsFileInfo);
  1681. }
  1682. /**
  1683. * vim: ts=4: sw=4: et:
  1684. */