hdfs_web.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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 <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include "libhdfs/exception.h"
  22. #include "hdfs/hdfs.h"
  23. #include "hdfs_http_client.h"
  24. #include "hdfs_http_query.h"
  25. #include "hdfs_json_parser.h"
  26. #include "jni_helper.h"
  27. #define HADOOP_HDFS_CONF "org/apache/hadoop/hdfs/HdfsConfiguration"
  28. #define HADOOP_NAMENODE "org/apache/hadoop/hdfs/server/namenode/NameNode"
  29. #define JAVA_INETSOCKETADDRESS "java/net/InetSocketAddress"
  30. struct hdfsBuilder {
  31. int forceNewInstance;
  32. const char *nn;
  33. tPort port;
  34. const char *kerbTicketCachePath;
  35. const char *userName;
  36. };
  37. /**
  38. * The information required for accessing webhdfs,
  39. * including the network address of the namenode and the user name
  40. *
  41. * Unlike the string in hdfsBuilder, the strings in this structure are
  42. * dynamically allocated. This structure will not be freed until we disconnect
  43. * from HDFS.
  44. */
  45. struct hdfs_internal {
  46. char *nn;
  47. tPort port;
  48. char *userName;
  49. /**
  50. * Working directory -- stored with a trailing slash.
  51. */
  52. char *workingDir;
  53. };
  54. /**
  55. * The 'file-handle' to a file in hdfs.
  56. */
  57. struct hdfsFile_internal {
  58. struct webhdfsFileHandle* file;
  59. enum hdfsStreamType type; /* INPUT or OUTPUT */
  60. int flags; /* Flag indicate read/create/append etc. */
  61. tOffset offset; /* Current offset position in the file */
  62. };
  63. /**
  64. * Create, initialize and return a webhdfsBuffer
  65. */
  66. static int initWebHdfsBuffer(struct webhdfsBuffer **webhdfsBuffer)
  67. {
  68. int ret = 0;
  69. struct webhdfsBuffer *buffer = calloc(1, sizeof(struct webhdfsBuffer));
  70. if (!buffer) {
  71. fprintf(stderr,
  72. "ERROR: fail to allocate memory for webhdfsBuffer.\n");
  73. return ENOMEM;
  74. }
  75. ret = pthread_mutex_init(&buffer->writeMutex, NULL);
  76. if (ret) {
  77. fprintf(stderr, "ERROR: fail in pthread_mutex_init for writeMutex "
  78. "in initWebHdfsBuffer, <%d>: %s.\n",
  79. ret, hdfs_strerror(ret));
  80. goto done;
  81. }
  82. ret = pthread_cond_init(&buffer->newwrite_or_close, NULL);
  83. if (ret) {
  84. fprintf(stderr,
  85. "ERROR: fail in pthread_cond_init for newwrite_or_close "
  86. "in initWebHdfsBuffer, <%d>: %s.\n",
  87. ret, hdfs_strerror(ret));
  88. goto done;
  89. }
  90. ret = pthread_cond_init(&buffer->transfer_finish, NULL);
  91. if (ret) {
  92. fprintf(stderr,
  93. "ERROR: fail in pthread_cond_init for transfer_finish "
  94. "in initWebHdfsBuffer, <%d>: %s.\n",
  95. ret, hdfs_strerror(ret));
  96. goto done;
  97. }
  98. done:
  99. if (ret) {
  100. free(buffer);
  101. return ret;
  102. }
  103. *webhdfsBuffer = buffer;
  104. return 0;
  105. }
  106. /**
  107. * Reset the webhdfsBuffer. This is used in a block way
  108. * when hdfsWrite is called with a new buffer to write.
  109. * The writing thread in libcurl will be waken up to continue writing,
  110. * and the caller of this function is blocked waiting for writing to finish.
  111. *
  112. * @param wb The handle of the webhdfsBuffer
  113. * @param buffer The buffer provided by user to write
  114. * @param length The length of bytes to write
  115. * @return Updated webhdfsBuffer.
  116. */
  117. static struct webhdfsBuffer *resetWebhdfsBuffer(struct webhdfsBuffer *wb,
  118. const char *buffer, size_t length)
  119. {
  120. if (buffer && length > 0) {
  121. pthread_mutex_lock(&wb->writeMutex);
  122. wb->wbuffer = buffer;
  123. wb->offset = 0;
  124. wb->remaining = length;
  125. pthread_cond_signal(&wb->newwrite_or_close);
  126. while (wb->remaining != 0) {
  127. pthread_cond_wait(&wb->transfer_finish, &wb->writeMutex);
  128. }
  129. pthread_mutex_unlock(&wb->writeMutex);
  130. }
  131. return wb;
  132. }
  133. /**
  134. * Free the webhdfsBuffer and destroy its pthread conditions/mutex
  135. * @param buffer The webhdfsBuffer to free
  136. */
  137. static void freeWebhdfsBuffer(struct webhdfsBuffer *buffer)
  138. {
  139. int ret = 0;
  140. if (buffer) {
  141. ret = pthread_cond_destroy(&buffer->newwrite_or_close);
  142. if (ret) {
  143. fprintf(stderr,
  144. "WARN: fail in pthread_cond_destroy for newwrite_or_close "
  145. "in freeWebhdfsBuffer, <%d>: %s.\n",
  146. ret, hdfs_strerror(ret));
  147. errno = ret;
  148. }
  149. ret = pthread_cond_destroy(&buffer->transfer_finish);
  150. if (ret) {
  151. fprintf(stderr,
  152. "WARN: fail in pthread_cond_destroy for transfer_finish "
  153. "in freeWebhdfsBuffer, <%d>: %s.\n",
  154. ret, hdfs_strerror(ret));
  155. errno = ret;
  156. }
  157. ret = pthread_mutex_destroy(&buffer->writeMutex);
  158. if (ret) {
  159. fprintf(stderr,
  160. "WARN: fail in pthread_mutex_destroy for writeMutex "
  161. "in freeWebhdfsBuffer, <%d>: %s.\n",
  162. ret, hdfs_strerror(ret));
  163. errno = ret;
  164. }
  165. free(buffer);
  166. buffer = NULL;
  167. }
  168. }
  169. /**
  170. * To free the webhdfsFileHandle, which includes a webhdfsBuffer and strings
  171. * @param handle The webhdfsFileHandle to free
  172. */
  173. static void freeWebFileHandle(struct webhdfsFileHandle * handle)
  174. {
  175. if (!handle)
  176. return;
  177. freeWebhdfsBuffer(handle->uploadBuffer);
  178. free(handle->datanode);
  179. free(handle->absPath);
  180. free(handle);
  181. }
  182. static const char *maybeNull(const char *str)
  183. {
  184. return str ? str : "(NULL)";
  185. }
  186. /** To print a hdfsBuilder as string */
  187. static const char *hdfsBuilderToStr(const struct hdfsBuilder *bld,
  188. char *buf, size_t bufLen)
  189. {
  190. int strlength = snprintf(buf, bufLen, "nn=%s, port=%d, "
  191. "kerbTicketCachePath=%s, userName=%s",
  192. maybeNull(bld->nn), bld->port,
  193. maybeNull(bld->kerbTicketCachePath), maybeNull(bld->userName));
  194. if (strlength < 0 || strlength >= bufLen) {
  195. fprintf(stderr, "failed to print a hdfsBuilder as string.\n");
  196. return NULL;
  197. }
  198. return buf;
  199. }
  200. /**
  201. * Free a hdfs_internal handle
  202. * @param fs The hdfs_internal handle to free
  203. */
  204. static void freeWebHdfsInternal(struct hdfs_internal *fs)
  205. {
  206. if (fs) {
  207. free(fs->nn);
  208. free(fs->userName);
  209. free(fs->workingDir);
  210. }
  211. }
  212. struct hdfsBuilder *hdfsNewBuilder(void)
  213. {
  214. struct hdfsBuilder *bld = calloc(1, sizeof(struct hdfsBuilder));
  215. if (!bld) {
  216. errno = ENOMEM;
  217. return NULL;
  218. }
  219. return bld;
  220. }
  221. void hdfsFreeBuilder(struct hdfsBuilder *bld)
  222. {
  223. free(bld);
  224. }
  225. void hdfsBuilderSetForceNewInstance(struct hdfsBuilder *bld)
  226. {
  227. // We don't cache instances in libwebhdfs, so this is not applicable.
  228. }
  229. void hdfsBuilderSetNameNode(struct hdfsBuilder *bld, const char *nn)
  230. {
  231. if (bld) {
  232. bld->nn = nn;
  233. }
  234. }
  235. void hdfsBuilderSetNameNodePort(struct hdfsBuilder *bld, tPort port)
  236. {
  237. if (bld) {
  238. bld->port = port;
  239. }
  240. }
  241. void hdfsBuilderSetUserName(struct hdfsBuilder *bld, const char *userName)
  242. {
  243. if (bld) {
  244. bld->userName = userName;
  245. }
  246. }
  247. void hdfsBuilderSetKerbTicketCachePath(struct hdfsBuilder *bld,
  248. const char *kerbTicketCachePath)
  249. {
  250. if (bld) {
  251. bld->kerbTicketCachePath = kerbTicketCachePath;
  252. }
  253. }
  254. hdfsFS hdfsConnectAsUser(const char* nn, tPort port, const char *user)
  255. {
  256. struct hdfsBuilder* bld = hdfsNewBuilder();
  257. if (!bld) {
  258. return NULL;
  259. }
  260. hdfsBuilderSetNameNode(bld, nn);
  261. hdfsBuilderSetNameNodePort(bld, port);
  262. hdfsBuilderSetUserName(bld, user);
  263. return hdfsBuilderConnect(bld);
  264. }
  265. hdfsFS hdfsConnect(const char* nn, tPort port)
  266. {
  267. return hdfsConnectAsUser(nn, port, NULL);
  268. }
  269. hdfsFS hdfsConnectNewInstance(const char* nn, tPort port)
  270. {
  271. return hdfsConnect(nn, port);
  272. }
  273. hdfsFS hdfsConnectAsUserNewInstance(const char* host, tPort port,
  274. const char *user)
  275. {
  276. struct hdfsBuilder *bld = hdfsNewBuilder();
  277. if (!bld)
  278. return NULL;
  279. hdfsBuilderSetNameNode(bld, host);
  280. hdfsBuilderSetNameNodePort(bld, port);
  281. hdfsBuilderSetUserName(bld, user);
  282. hdfsBuilderSetForceNewInstance(bld);
  283. return hdfsBuilderConnect(bld);
  284. }
  285. /**
  286. * To retrieve the default configuration value for NameNode's hostName and port
  287. * TODO: This function currently is using JNI,
  288. * we need to do this without using JNI (HDFS-3917)
  289. *
  290. * @param bld The hdfsBuilder handle
  291. * @param port Used to get the default value for NameNode's port
  292. * @param nn Used to get the default value for NameNode's hostName
  293. * @return 0 for success and non-zero value for failure
  294. */
  295. static int retrieveDefaults(const struct hdfsBuilder *bld, tPort *port,
  296. char **nn)
  297. {
  298. JNIEnv *env = 0;
  299. jobject jHDFSConf = NULL, jAddress = NULL;
  300. jstring jHostName = NULL;
  301. jvalue jVal;
  302. jthrowable jthr = NULL;
  303. int ret = 0;
  304. char buf[512];
  305. env = getJNIEnv();
  306. if (!env) {
  307. return EINTERNAL;
  308. }
  309. jthr = constructNewObjectOfClass(env, &jHDFSConf, HADOOP_HDFS_CONF, "()V");
  310. if (jthr) {
  311. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  312. "hdfsBuilderConnect(%s)",
  313. hdfsBuilderToStr(bld, buf, sizeof(buf)));
  314. goto done;
  315. }
  316. jthr = invokeMethod(env, &jVal, STATIC, NULL,
  317. HADOOP_NAMENODE, "getHttpAddress",
  318. "(Lorg/apache/hadoop/conf/Configuration;)Ljava/net/InetSocketAddress;",
  319. jHDFSConf);
  320. if (jthr) {
  321. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  322. "hdfsBuilderConnect(%s)",
  323. hdfsBuilderToStr(bld, buf, sizeof(buf)));
  324. goto done;
  325. }
  326. jAddress = jVal.l;
  327. jthr = invokeMethod(env, &jVal, INSTANCE, jAddress,
  328. JAVA_INETSOCKETADDRESS, "getPort", "()I");
  329. if (jthr) {
  330. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  331. "hdfsBuilderConnect(%s)",
  332. hdfsBuilderToStr(bld, buf, sizeof(buf)));
  333. goto done;
  334. }
  335. *port = jVal.i;
  336. jthr = invokeMethod(env, &jVal, INSTANCE, jAddress,
  337. JAVA_INETSOCKETADDRESS,
  338. "getHostName", "()Ljava/lang/String;");
  339. if (jthr) {
  340. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  341. "hdfsBuilderConnect(%s)",
  342. hdfsBuilderToStr(bld, buf, sizeof(buf)));
  343. goto done;
  344. }
  345. jHostName = jVal.l;
  346. jthr = newCStr(env, jHostName, nn);
  347. if (jthr) {
  348. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  349. "hdfsBuilderConnect(%s)",
  350. hdfsBuilderToStr(bld, buf, sizeof(buf)));
  351. goto done;
  352. }
  353. done:
  354. destroyLocalReference(env, jHDFSConf);
  355. destroyLocalReference(env, jAddress);
  356. destroyLocalReference(env, jHostName);
  357. return ret;
  358. }
  359. hdfsFS hdfsBuilderConnect(struct hdfsBuilder *bld)
  360. {
  361. struct hdfs_internal *fs = NULL;
  362. int ret = 0;
  363. if (!bld) {
  364. ret = EINVAL;
  365. goto done;
  366. }
  367. if (bld->nn == NULL) {
  368. // In the JNI version of libhdfs this returns a LocalFileSystem.
  369. ret = ENOTSUP;
  370. goto done;
  371. }
  372. fs = calloc(1, sizeof(*fs));
  373. if (!fs) {
  374. ret = ENOMEM;
  375. goto done;
  376. }
  377. // If the namenode is "default" and/or the port of namenode is 0,
  378. // get the default namenode/port
  379. if (bld->port == 0 || !strcasecmp("default", bld->nn)) {
  380. ret = retrieveDefaults(bld, &fs->port, &fs->nn);
  381. if (ret)
  382. goto done;
  383. } else {
  384. fs->port = bld->port;
  385. fs->nn = strdup(bld->nn);
  386. if (!fs->nn) {
  387. ret = ENOMEM;
  388. goto done;
  389. }
  390. }
  391. if (bld->userName) {
  392. // userName may be NULL
  393. fs->userName = strdup(bld->userName);
  394. if (!fs->userName) {
  395. ret = ENOMEM;
  396. goto done;
  397. }
  398. }
  399. // The working directory starts out as root.
  400. fs->workingDir = strdup("/");
  401. if (!fs->workingDir) {
  402. ret = ENOMEM;
  403. goto done;
  404. }
  405. // For debug
  406. fprintf(stderr, "namenode: %s:%d\n", bld->nn, bld->port);
  407. done:
  408. free(bld);
  409. if (ret) {
  410. freeWebHdfsInternal(fs);
  411. errno = ret;
  412. return NULL;
  413. }
  414. return fs;
  415. }
  416. int hdfsDisconnect(hdfsFS fs)
  417. {
  418. if (fs == NULL) {
  419. errno = EINVAL;
  420. return -1;
  421. }
  422. freeWebHdfsInternal(fs);
  423. return 0;
  424. }
  425. /**
  426. * Based on the working directory stored in hdfsFS,
  427. * generate the absolute path for the given path
  428. *
  429. * @param fs The hdfsFS handle which stores the current working directory
  430. * @param path The given path which may not be an absolute path
  431. * @param absPath To hold generated absolute path for the given path
  432. * @return 0 on success, non-zero value indicating error
  433. */
  434. static int getAbsolutePath(hdfsFS fs, const char *path, char **absPath)
  435. {
  436. char *tempPath = NULL;
  437. size_t absPathLen;
  438. int strlength;
  439. if (path[0] == '/') {
  440. // Path is already absolute.
  441. tempPath = strdup(path);
  442. if (!tempPath) {
  443. return ENOMEM;
  444. }
  445. *absPath = tempPath;
  446. return 0;
  447. }
  448. // Prepend the workingDir to the path.
  449. absPathLen = strlen(fs->workingDir) + strlen(path) + 1;
  450. tempPath = malloc(absPathLen);
  451. if (!tempPath) {
  452. return ENOMEM;
  453. }
  454. strlength = snprintf(tempPath, absPathLen, "%s%s", fs->workingDir, path);
  455. if (strlength < 0 || strlength >= absPathLen) {
  456. free(tempPath);
  457. return EIO;
  458. }
  459. *absPath = tempPath;
  460. return 0;
  461. }
  462. int hdfsCreateDirectory(hdfsFS fs, const char* path)
  463. {
  464. char *url = NULL, *absPath = NULL;
  465. struct Response *resp = NULL;
  466. int ret = 0;
  467. if (fs == NULL || path == NULL) {
  468. ret = EINVAL;
  469. goto done;
  470. }
  471. ret = getAbsolutePath(fs, path, &absPath);
  472. if (ret) {
  473. goto done;
  474. }
  475. ret = createUrlForMKDIR(fs->nn, fs->port, absPath, fs->userName, &url);
  476. if (ret) {
  477. goto done;
  478. }
  479. ret = launchMKDIR(url, &resp);
  480. if (ret) {
  481. goto done;
  482. }
  483. ret = parseMKDIR(resp->body->content);
  484. done:
  485. freeResponse(resp);
  486. free(url);
  487. free(absPath);
  488. if (ret) {
  489. errno = ret;
  490. return -1;
  491. }
  492. return 0;
  493. }
  494. int hdfsChmod(hdfsFS fs, const char* path, short mode)
  495. {
  496. char *absPath = NULL, *url = NULL;
  497. struct Response *resp = NULL;
  498. int ret = 0;
  499. if (fs == NULL || path == NULL) {
  500. ret = EINVAL;
  501. goto done;
  502. }
  503. ret = getAbsolutePath(fs, path, &absPath);
  504. if (ret) {
  505. goto done;
  506. }
  507. ret = createUrlForCHMOD(fs->nn, fs->port, absPath, (int) mode,
  508. fs->userName, &url);
  509. if (ret) {
  510. goto done;
  511. }
  512. ret = launchCHMOD(url, &resp);
  513. if (ret) {
  514. goto done;
  515. }
  516. ret = parseCHMOD(resp->header->content, resp->body->content);
  517. done:
  518. freeResponse(resp);
  519. free(absPath);
  520. free(url);
  521. if (ret) {
  522. errno = ret;
  523. return -1;
  524. }
  525. return 0;
  526. }
  527. int hdfsChown(hdfsFS fs, const char* path, const char *owner, const char *group)
  528. {
  529. int ret = 0;
  530. char *absPath = NULL, *url = NULL;
  531. struct Response *resp = NULL;
  532. if (fs == NULL || path == NULL) {
  533. ret = EINVAL;
  534. goto done;
  535. }
  536. ret = getAbsolutePath(fs, path, &absPath);
  537. if (ret) {
  538. goto done;
  539. }
  540. ret = createUrlForCHOWN(fs->nn, fs->port, absPath,
  541. owner, group, fs->userName, &url);
  542. if (ret) {
  543. goto done;
  544. }
  545. ret = launchCHOWN(url, &resp);
  546. if (ret) {
  547. goto done;
  548. }
  549. ret = parseCHOWN(resp->header->content, resp->body->content);
  550. done:
  551. freeResponse(resp);
  552. free(absPath);
  553. free(url);
  554. if (ret) {
  555. errno = ret;
  556. return -1;
  557. }
  558. return 0;
  559. }
  560. int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath)
  561. {
  562. char *oldAbsPath = NULL, *newAbsPath = NULL, *url = NULL;
  563. int ret = 0;
  564. struct Response *resp = NULL;
  565. if (fs == NULL || oldPath == NULL || newPath == NULL) {
  566. ret = EINVAL;
  567. goto done;
  568. }
  569. ret = getAbsolutePath(fs, oldPath, &oldAbsPath);
  570. if (ret) {
  571. goto done;
  572. }
  573. ret = getAbsolutePath(fs, newPath, &newAbsPath);
  574. if (ret) {
  575. goto done;
  576. }
  577. ret = createUrlForRENAME(fs->nn, fs->port, oldAbsPath,
  578. newAbsPath, fs->userName, &url);
  579. if (ret) {
  580. goto done;
  581. }
  582. ret = launchRENAME(url, &resp);
  583. if (ret) {
  584. goto done;
  585. }
  586. ret = parseRENAME(resp->body->content);
  587. done:
  588. freeResponse(resp);
  589. free(oldAbsPath);
  590. free(newAbsPath);
  591. free(url);
  592. if (ret) {
  593. errno = ret;
  594. return -1;
  595. }
  596. return 0;
  597. }
  598. /**
  599. * Get the file status for a given path.
  600. *
  601. * @param fs hdfsFS handle containing
  602. * NameNode hostName/port information
  603. * @param path Path for file
  604. * @param printError Whether or not to print out error information
  605. * (mainly remote FileNotFoundException)
  606. * @return File information for the given path
  607. */
  608. static hdfsFileInfo *hdfsGetPathInfoImpl(hdfsFS fs, const char* path,
  609. int printError)
  610. {
  611. char *absPath = NULL;
  612. char *url=NULL;
  613. struct Response *resp = NULL;
  614. int ret = 0;
  615. hdfsFileInfo *fileInfo = NULL;
  616. if (fs == NULL || path == NULL) {
  617. ret = EINVAL;
  618. goto done;
  619. }
  620. ret = getAbsolutePath(fs, path, &absPath);
  621. if (ret) {
  622. goto done;
  623. }
  624. fileInfo = (hdfsFileInfo *) calloc(1, sizeof(hdfsFileInfo));
  625. if (!fileInfo) {
  626. ret = ENOMEM;
  627. goto done;
  628. }
  629. fileInfo->mKind = kObjectKindFile;
  630. ret = createUrlForGetFileStatus(fs->nn, fs->port, absPath,
  631. fs->userName, &url);
  632. if (ret) {
  633. goto done;
  634. }
  635. ret = launchGFS(url, &resp);
  636. if (ret) {
  637. goto done;
  638. }
  639. ret = parseGFS(resp->body->content, fileInfo, printError);
  640. done:
  641. freeResponse(resp);
  642. free(absPath);
  643. free(url);
  644. if (ret == 0) {
  645. return fileInfo;
  646. } else {
  647. free(fileInfo);
  648. errno = ret;
  649. return NULL;
  650. }
  651. }
  652. hdfsFileInfo *hdfsGetPathInfo(hdfsFS fs, const char* path)
  653. {
  654. return hdfsGetPathInfoImpl(fs, path, 1);
  655. }
  656. hdfsFileInfo *hdfsListDirectory(hdfsFS fs, const char* path, int *numEntries)
  657. {
  658. char *url = NULL, *absPath = NULL;
  659. struct Response *resp = NULL;
  660. int ret = 0;
  661. hdfsFileInfo *fileInfo = NULL;
  662. if (fs == NULL || path == NULL) {
  663. ret = EINVAL;
  664. goto done;
  665. }
  666. ret = getAbsolutePath(fs, path, &absPath);
  667. if (ret) {
  668. goto done;
  669. }
  670. fileInfo = calloc(1, sizeof(*fileInfo));
  671. if (!fileInfo) {
  672. ret = ENOMEM;
  673. goto done;
  674. }
  675. ret = createUrlForLS(fs->nn, fs->port, absPath, fs->userName, &url);
  676. if (ret) {
  677. goto done;
  678. }
  679. ret = launchLS(url, &resp);
  680. if (ret) {
  681. goto done;
  682. }
  683. ret = parseLS(resp->body->content, &fileInfo, numEntries);
  684. done:
  685. freeResponse(resp);
  686. free(absPath);
  687. free(url);
  688. if (ret == 0) {
  689. return fileInfo;
  690. } else {
  691. errno = ret;
  692. return NULL;
  693. }
  694. }
  695. int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication)
  696. {
  697. char *url = NULL, *absPath = NULL;
  698. struct Response *resp = NULL;
  699. int ret = 0;
  700. if (fs == NULL || path == NULL) {
  701. ret = EINVAL;
  702. goto done;
  703. }
  704. ret = getAbsolutePath(fs, path, &absPath);
  705. if (ret) {
  706. goto done;
  707. }
  708. ret = createUrlForSETREPLICATION(fs->nn, fs->port, absPath,
  709. replication, fs->userName, &url);
  710. if (ret) {
  711. goto done;
  712. }
  713. ret = launchSETREPLICATION(url, &resp);
  714. if (ret) {
  715. goto done;
  716. }
  717. ret = parseSETREPLICATION(resp->body->content);
  718. done:
  719. freeResponse(resp);
  720. free(absPath);
  721. free(url);
  722. if (ret) {
  723. errno = ret;
  724. return -1;
  725. }
  726. return 0;
  727. }
  728. void hdfsFreeFileInfo(hdfsFileInfo *hdfsFileInfo, int numEntries)
  729. {
  730. int i;
  731. for (i = 0; i < numEntries; ++i) {
  732. free(hdfsFileInfo[i].mName);
  733. free(hdfsFileInfo[i].mOwner);
  734. free(hdfsFileInfo[i].mGroup);
  735. }
  736. free(hdfsFileInfo);
  737. }
  738. int hdfsDelete(hdfsFS fs, const char* path, int recursive)
  739. {
  740. char *url = NULL, *absPath = NULL;
  741. struct Response *resp = NULL;
  742. int ret = 0;
  743. if (fs == NULL || path == NULL) {
  744. ret = EINVAL;
  745. goto done;
  746. }
  747. ret = getAbsolutePath(fs, path, &absPath);
  748. if (ret) {
  749. goto done;
  750. }
  751. ret = createUrlForDELETE(fs->nn, fs->port, absPath,
  752. recursive, fs->userName, &url);
  753. if (ret) {
  754. goto done;
  755. }
  756. ret = launchDELETE(url, &resp);
  757. if (ret) {
  758. goto done;
  759. }
  760. ret = parseDELETE(resp->body->content);
  761. done:
  762. freeResponse(resp);
  763. free(absPath);
  764. free(url);
  765. if (ret) {
  766. errno = ret;
  767. return -1;
  768. }
  769. return 0;
  770. }
  771. int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime)
  772. {
  773. char *url = NULL, *absPath = NULL;
  774. struct Response *resp = NULL;
  775. int ret = 0;
  776. if (fs == NULL || path == NULL) {
  777. ret = EINVAL;
  778. goto done;
  779. }
  780. ret = getAbsolutePath(fs, path, &absPath);
  781. if (ret) {
  782. goto done;
  783. }
  784. ret = createUrlForUTIMES(fs->nn, fs->port, absPath, mtime, atime,
  785. fs->userName, &url);
  786. if (ret) {
  787. goto done;
  788. }
  789. ret = launchUTIMES(url, &resp);
  790. if (ret) {
  791. goto done;
  792. }
  793. ret = parseUTIMES(resp->header->content, resp->body->content);
  794. done:
  795. freeResponse(resp);
  796. free(absPath);
  797. free(url);
  798. if (ret) {
  799. errno = ret;
  800. return -1;
  801. }
  802. return 0;
  803. }
  804. int hdfsExists(hdfsFS fs, const char *path)
  805. {
  806. hdfsFileInfo *fileInfo = hdfsGetPathInfoImpl(fs, path, 0);
  807. if (!fileInfo) {
  808. // (errno will have been set by hdfsGetPathInfo)
  809. return -1;
  810. }
  811. hdfsFreeFileInfo(fileInfo, 1);
  812. return 0;
  813. }
  814. /**
  815. * The information hold by the thread which writes data to hdfs through http
  816. */
  817. typedef struct {
  818. char *url; /* the url of the target datanode for writing*/
  819. struct webhdfsBuffer *uploadBuffer; /* buffer storing data to write */
  820. int flags; /* flag indicating writing mode: create or append */
  821. struct Response *resp; /* response from the target datanode */
  822. } threadData;
  823. /**
  824. * Free the threadData struct instance,
  825. * including the response and url contained in it
  826. * @param data The threadData instance to free
  827. */
  828. static void freeThreadData(threadData *data)
  829. {
  830. if (data) {
  831. if (data->url) {
  832. free(data->url);
  833. }
  834. if (data->resp) {
  835. freeResponse(data->resp);
  836. }
  837. // The uploadBuffer would be freed by freeWebFileHandle()
  838. free(data);
  839. data = NULL;
  840. }
  841. }
  842. /**
  843. * The action of the thread that writes data to
  844. * the target datanode for hdfsWrite.
  845. * The writing can be either create or append, which is specified by flag
  846. */
  847. static void *writeThreadOperation(void *v)
  848. {
  849. int ret = 0;
  850. threadData *data = v;
  851. if (data->flags & O_APPEND) {
  852. ret = launchDnAPPEND(data->url, data->uploadBuffer, &(data->resp));
  853. } else {
  854. ret = launchDnWRITE(data->url, data->uploadBuffer, &(data->resp));
  855. }
  856. if (ret) {
  857. fprintf(stderr, "Failed to write to datanode %s, <%d>: %s.\n",
  858. data->url, ret, hdfs_strerror(ret));
  859. }
  860. return data;
  861. }
  862. /**
  863. * Free the memory associated with a webHDFS file handle.
  864. *
  865. * No other resources will be freed.
  866. *
  867. * @param file The webhdfs file handle
  868. */
  869. static void freeFileInternal(hdfsFile file)
  870. {
  871. if (!file)
  872. return;
  873. freeWebFileHandle(file->file);
  874. free(file);
  875. }
  876. /**
  877. * Helper function for opening a file for OUTPUT.
  878. *
  879. * As part of the open process for OUTPUT files, we have to connect to the
  880. * NameNode and get the URL of the corresponding DataNode.
  881. * We also create a background thread here for doing I/O.
  882. *
  883. * @param webhandle The webhandle being opened
  884. * @return 0 on success; error code otherwise
  885. */
  886. static int hdfsOpenOutputFileImpl(hdfsFS fs, hdfsFile file)
  887. {
  888. struct webhdfsFileHandle *webhandle = file->file;
  889. struct Response *resp = NULL;
  890. int append, ret = 0;
  891. char *nnUrl = NULL, *dnUrl = NULL;
  892. threadData *data = NULL;
  893. ret = initWebHdfsBuffer(&webhandle->uploadBuffer);
  894. if (ret) {
  895. goto done;
  896. }
  897. append = file->flags & O_APPEND;
  898. if (!append) {
  899. // If we're not appending, send a create request to the NN
  900. ret = createUrlForNnWRITE(fs->nn, fs->port, webhandle->absPath,
  901. fs->userName, webhandle->replication,
  902. webhandle->blockSize, &nnUrl);
  903. } else {
  904. ret = createUrlForNnAPPEND(fs->nn, fs->port, webhandle->absPath,
  905. fs->userName, &nnUrl);
  906. }
  907. if (ret) {
  908. fprintf(stderr, "Failed to create the url connecting to namenode "
  909. "for file creation/appending, <%d>: %s.\n",
  910. ret, hdfs_strerror(ret));
  911. goto done;
  912. }
  913. if (!append) {
  914. ret = launchNnWRITE(nnUrl, &resp);
  915. } else {
  916. ret = launchNnAPPEND(nnUrl, &resp);
  917. }
  918. if (ret) {
  919. fprintf(stderr, "fail to get the response from namenode for "
  920. "file creation/appending, <%d>: %s.\n",
  921. ret, hdfs_strerror(ret));
  922. goto done;
  923. }
  924. if (!append) {
  925. ret = parseNnWRITE(resp->header->content, resp->body->content);
  926. } else {
  927. ret = parseNnAPPEND(resp->header->content, resp->body->content);
  928. }
  929. if (ret) {
  930. fprintf(stderr, "fail to parse the response from namenode for "
  931. "file creation/appending, <%d>: %s.\n",
  932. ret, hdfs_strerror(ret));
  933. goto done;
  934. }
  935. ret = parseDnLoc(resp->header->content, &dnUrl);
  936. if (ret) {
  937. fprintf(stderr, "fail to get the datanode url from namenode "
  938. "for file creation/appending, <%d>: %s.\n",
  939. ret, hdfs_strerror(ret));
  940. goto done;
  941. }
  942. //store the datanode url in the file handle
  943. webhandle->datanode = strdup(dnUrl);
  944. if (!webhandle->datanode) {
  945. ret = ENOMEM;
  946. goto done;
  947. }
  948. //create a new thread for performing the http transferring
  949. data = calloc(1, sizeof(*data));
  950. if (!data) {
  951. ret = ENOMEM;
  952. goto done;
  953. }
  954. data->url = strdup(dnUrl);
  955. if (!data->url) {
  956. ret = ENOMEM;
  957. goto done;
  958. }
  959. data->flags = file->flags;
  960. data->uploadBuffer = webhandle->uploadBuffer;
  961. ret = pthread_create(&webhandle->connThread, NULL,
  962. writeThreadOperation, data);
  963. if (ret) {
  964. fprintf(stderr, "ERROR: failed to create the writing thread "
  965. "in hdfsOpenOutputFileImpl, <%d>: %s.\n",
  966. ret, hdfs_strerror(ret));
  967. goto done;
  968. }
  969. webhandle->uploadBuffer->openFlag = 1;
  970. done:
  971. freeResponse(resp);
  972. free(nnUrl);
  973. free(dnUrl);
  974. if (ret) {
  975. errno = ret;
  976. if (data) {
  977. free(data->url);
  978. free(data);
  979. }
  980. }
  981. return ret;
  982. }
  983. hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags,
  984. int bufferSize, short replication, tSize blockSize)
  985. {
  986. int ret = 0;
  987. int accmode = flags & O_ACCMODE;
  988. struct webhdfsFileHandle *webhandle = NULL;
  989. hdfsFile file = NULL;
  990. if (fs == NULL || path == NULL) {
  991. ret = EINVAL;
  992. goto done;
  993. }
  994. if (accmode == O_RDWR) {
  995. // TODO: the original libhdfs has very hackish support for this; should
  996. // we do the same? It would actually be a lot easier in libwebhdfs
  997. // since the protocol isn't connection-oriented.
  998. fprintf(stderr, "ERROR: cannot open an hdfs file in O_RDWR mode\n");
  999. ret = ENOTSUP;
  1000. goto done;
  1001. }
  1002. if ((flags & O_CREAT) && (flags & O_EXCL)) {
  1003. fprintf(stderr,
  1004. "WARN: hdfs does not truly support O_CREATE && O_EXCL\n");
  1005. }
  1006. file = calloc(1, sizeof(struct hdfsFile_internal));
  1007. if (!file) {
  1008. ret = ENOMEM;
  1009. goto done;
  1010. }
  1011. file->flags = flags;
  1012. file->type = accmode == O_RDONLY ? INPUT : OUTPUT;
  1013. file->offset = 0;
  1014. webhandle = calloc(1, sizeof(struct webhdfsFileHandle));
  1015. if (!webhandle) {
  1016. ret = ENOMEM;
  1017. goto done;
  1018. }
  1019. webhandle->bufferSize = bufferSize;
  1020. webhandle->replication = replication;
  1021. webhandle->blockSize = blockSize;
  1022. ret = getAbsolutePath(fs, path, &webhandle->absPath);
  1023. if (ret) {
  1024. goto done;
  1025. }
  1026. file->file = webhandle;
  1027. // If open for write/append,
  1028. // open and keep the connection with the target datanode for writing
  1029. if (file->type == OUTPUT) {
  1030. ret = hdfsOpenOutputFileImpl(fs, file);
  1031. if (ret) {
  1032. goto done;
  1033. }
  1034. }
  1035. done:
  1036. if (ret) {
  1037. if (file) {
  1038. freeFileInternal(file); // Also frees webhandle
  1039. } else {
  1040. freeWebFileHandle(webhandle);
  1041. }
  1042. errno = ret;
  1043. return NULL;
  1044. }
  1045. return file;
  1046. }
  1047. int hdfsTruncateFile(hdfsFS fs, const char* path, tOffset newlength)
  1048. {
  1049. errno = ENOTSUP;
  1050. return -1;
  1051. }
  1052. tSize hdfsWrite(hdfsFS fs, hdfsFile file, const void* buffer, tSize length)
  1053. {
  1054. if (length == 0) {
  1055. return 0;
  1056. }
  1057. if (fs == NULL || file == NULL || file->type != OUTPUT || length < 0) {
  1058. errno = EBADF;
  1059. return -1;
  1060. }
  1061. struct webhdfsFileHandle *wfile = file->file;
  1062. if (wfile->uploadBuffer && wfile->uploadBuffer->openFlag) {
  1063. resetWebhdfsBuffer(wfile->uploadBuffer, buffer, length);
  1064. return length;
  1065. } else {
  1066. fprintf(stderr,
  1067. "Error: have not opened the file %s for writing yet.\n",
  1068. wfile->absPath);
  1069. errno = EBADF;
  1070. return -1;
  1071. }
  1072. }
  1073. int hdfsCloseFile(hdfsFS fs, hdfsFile file)
  1074. {
  1075. void *respv = NULL;
  1076. threadData *tdata = NULL;
  1077. int ret = 0;
  1078. struct webhdfsFileHandle *wfile = NULL;
  1079. if (file->type == OUTPUT) {
  1080. wfile = file->file;
  1081. pthread_mutex_lock(&(wfile->uploadBuffer->writeMutex));
  1082. wfile->uploadBuffer->closeFlag = 1;
  1083. pthread_cond_signal(&wfile->uploadBuffer->newwrite_or_close);
  1084. pthread_mutex_unlock(&(wfile->uploadBuffer->writeMutex));
  1085. // Waiting for the writing thread to terminate
  1086. ret = pthread_join(wfile->connThread, &respv);
  1087. if (ret) {
  1088. fprintf(stderr, "Error when pthread_join in hdfsClose, <%d>: %s.\n",
  1089. ret, hdfs_strerror(ret));
  1090. }
  1091. // Parse the response
  1092. tdata = respv;
  1093. if (!tdata || !(tdata->resp)) {
  1094. fprintf(stderr,
  1095. "ERROR: response from the writing thread is NULL.\n");
  1096. ret = EIO;
  1097. }
  1098. if (file->flags & O_APPEND) {
  1099. ret = parseDnAPPEND(tdata->resp->header->content,
  1100. tdata->resp->body->content);
  1101. } else {
  1102. ret = parseDnWRITE(tdata->resp->header->content,
  1103. tdata->resp->body->content);
  1104. }
  1105. // Free the threaddata
  1106. freeThreadData(tdata);
  1107. }
  1108. freeFileInternal(file);
  1109. if (ret) {
  1110. errno = ret;
  1111. return -1;
  1112. }
  1113. return 0;
  1114. }
  1115. int hdfsFileIsOpenForRead(hdfsFile file)
  1116. {
  1117. return (file->type == INPUT);
  1118. }
  1119. int hdfsFileGetReadStatistics(hdfsFile file,
  1120. struct hdfsReadStatistics **stats)
  1121. {
  1122. errno = ENOTSUP;
  1123. return -1;
  1124. }
  1125. int64_t hdfsReadStatisticsGetRemoteBytesRead(
  1126. const struct hdfsReadStatistics *stats)
  1127. {
  1128. return stats->totalBytesRead - stats->totalLocalBytesRead;
  1129. }
  1130. void hdfsFileFreeReadStatistics(struct hdfsReadStatistics *stats)
  1131. {
  1132. free(stats);
  1133. }
  1134. int hdfsFileIsOpenForWrite(hdfsFile file)
  1135. {
  1136. return (file->type == OUTPUT);
  1137. }
  1138. static int hdfsReadImpl(hdfsFS fs, hdfsFile file, void* buffer, tSize off,
  1139. tSize length, tSize *numRead)
  1140. {
  1141. int ret = 0;
  1142. char *url = NULL;
  1143. struct Response *resp = NULL;
  1144. if (fs == NULL || file == NULL || file->type != INPUT || buffer == NULL ||
  1145. length < 0) {
  1146. ret = EINVAL;
  1147. goto done;
  1148. }
  1149. if (length == 0) {
  1150. // Special case: the user supplied a buffer of zero length, so there is
  1151. // nothing to do.
  1152. *numRead = 0;
  1153. goto done;
  1154. }
  1155. resp = calloc(1, sizeof(*resp)); // resp is actually a pointer type
  1156. if (!resp) {
  1157. ret = ENOMEM;
  1158. goto done;
  1159. }
  1160. ret = initResponseBuffer(&(resp->header));
  1161. if (ret) {
  1162. goto done;
  1163. }
  1164. ret = initResponseBuffer(&(resp->body));
  1165. if (ret) {
  1166. goto done;
  1167. }
  1168. memset(buffer, 0, length);
  1169. resp->body->content = buffer;
  1170. resp->body->remaining = length;
  1171. ret = createUrlForOPEN(fs->nn, fs->port, file->file->absPath,
  1172. fs->userName, off, length, &url);
  1173. if (ret) {
  1174. goto done;
  1175. }
  1176. ret = launchOPEN(url, resp);
  1177. if (ret) {
  1178. goto done;
  1179. }
  1180. ret = parseOPEN(resp->header->content, resp->body->content);
  1181. if (ret == -1) {
  1182. // Special case: if parseOPEN returns -1, we asked for a byte range
  1183. // with outside what the file contains. In this case, hdfsRead and
  1184. // hdfsPread return 0, meaning end-of-file.
  1185. *numRead = 0;
  1186. } else if (ret == 0) {
  1187. *numRead = (tSize) resp->body->offset;
  1188. }
  1189. done:
  1190. if (resp) {
  1191. freeResponseBuffer(resp->header);
  1192. free(resp->body);
  1193. }
  1194. free(resp);
  1195. free(url);
  1196. return ret;
  1197. }
  1198. tSize hdfsRead(hdfsFS fs, hdfsFile file, void* buffer, tSize length)
  1199. {
  1200. int ret = 0;
  1201. tSize numRead = 0;
  1202. ret = hdfsReadImpl(fs, file, buffer, (tSize) file->offset,
  1203. length, &numRead);
  1204. if (ret > 0) { // ret == -1 means end of file
  1205. errno = ret;
  1206. return -1;
  1207. }
  1208. file->offset += numRead;
  1209. return numRead;
  1210. }
  1211. int hdfsAvailable(hdfsFS fs, hdfsFile file)
  1212. {
  1213. /* We actually always block when reading from webhdfs, currently. So the
  1214. * number of bytes that can be read without blocking is currently 0.
  1215. */
  1216. return 0;
  1217. }
  1218. int hdfsSeek(hdfsFS fs, hdfsFile file, tOffset desiredPos)
  1219. {
  1220. struct webhdfsFileHandle *wf;
  1221. hdfsFileInfo *fileInfo = NULL;
  1222. int ret = 0;
  1223. if (!fs || !file || (file->type == OUTPUT) || (desiredPos < 0)) {
  1224. ret = EINVAL;
  1225. goto done;
  1226. }
  1227. wf = file->file;
  1228. if (!wf) {
  1229. ret = EINVAL;
  1230. goto done;
  1231. }
  1232. fileInfo = hdfsGetPathInfo(fs, wf->absPath);
  1233. if (!fileInfo) {
  1234. ret = errno;
  1235. goto done;
  1236. }
  1237. if (desiredPos > fileInfo->mSize) {
  1238. fprintf(stderr,
  1239. "hdfsSeek for %s failed since the desired position %" PRId64
  1240. " is beyond the size of the file %" PRId64 "\n",
  1241. wf->absPath, desiredPos, fileInfo->mSize);
  1242. ret = ENOTSUP;
  1243. goto done;
  1244. }
  1245. file->offset = desiredPos;
  1246. done:
  1247. if (fileInfo) {
  1248. hdfsFreeFileInfo(fileInfo, 1);
  1249. }
  1250. if (ret) {
  1251. errno = ret;
  1252. return -1;
  1253. }
  1254. return 0;
  1255. }
  1256. tSize hdfsPread(hdfsFS fs, hdfsFile file, tOffset position,
  1257. void* buffer, tSize length)
  1258. {
  1259. int ret;
  1260. tSize numRead = 0;
  1261. if (position < 0) {
  1262. errno = EINVAL;
  1263. return -1;
  1264. }
  1265. ret = hdfsReadImpl(fs, file, buffer, (tSize) position, length, &numRead);
  1266. if (ret > 0) {
  1267. errno = ret;
  1268. return -1;
  1269. }
  1270. return numRead;
  1271. }
  1272. tOffset hdfsTell(hdfsFS fs, hdfsFile file)
  1273. {
  1274. if (!file) {
  1275. errno = EINVAL;
  1276. return -1;
  1277. }
  1278. return file->offset;
  1279. }
  1280. char* hdfsGetWorkingDirectory(hdfsFS fs, char *buffer, size_t bufferSize)
  1281. {
  1282. int strlength;
  1283. if (fs == NULL || buffer == NULL || bufferSize <= 0) {
  1284. errno = EINVAL;
  1285. return NULL;
  1286. }
  1287. strlength = snprintf(buffer, bufferSize, "%s", fs->workingDir);
  1288. if (strlength >= bufferSize) {
  1289. errno = ENAMETOOLONG;
  1290. return NULL;
  1291. } else if (strlength < 0) {
  1292. errno = EIO;
  1293. return NULL;
  1294. }
  1295. return buffer;
  1296. }
  1297. /** Replace "//" with "/" in path */
  1298. static void normalizePath(char *path)
  1299. {
  1300. int i = 0, j = 0, sawslash = 0;
  1301. for (i = j = sawslash = 0; path[i] != '\0'; i++) {
  1302. if (path[i] != '/') {
  1303. sawslash = 0;
  1304. path[j++] = path[i];
  1305. } else if (path[i] == '/' && !sawslash) {
  1306. sawslash = 1;
  1307. path[j++] = '/';
  1308. }
  1309. }
  1310. path[j] = '\0';
  1311. }
  1312. int hdfsSetWorkingDirectory(hdfsFS fs, const char* path)
  1313. {
  1314. char *newWorkingDir = NULL;
  1315. size_t strlenPath = 0, newWorkingDirLen = 0;
  1316. int strlength;
  1317. if (fs == NULL || path == NULL) {
  1318. errno = EINVAL;
  1319. return -1;
  1320. }
  1321. strlenPath = strlen(path);
  1322. if (strlenPath < 1) {
  1323. errno = EINVAL;
  1324. return -1;
  1325. }
  1326. // the max string length of the new working dir is
  1327. // (length of old working dir) + (length of given path) + strlen("/") + 1
  1328. newWorkingDirLen = strlen(fs->workingDir) + strlenPath + 2;
  1329. newWorkingDir = malloc(newWorkingDirLen);
  1330. if (!newWorkingDir) {
  1331. errno = ENOMEM;
  1332. return -1;
  1333. }
  1334. strlength = snprintf(newWorkingDir, newWorkingDirLen, "%s%s%s",
  1335. (path[0] == '/') ? "" : fs->workingDir,
  1336. path, (path[strlenPath - 1] == '/') ? "" : "/");
  1337. if (strlength < 0 || strlength >= newWorkingDirLen) {
  1338. free(newWorkingDir);
  1339. errno = EIO;
  1340. return -1;
  1341. }
  1342. if (strstr(path, "//")) {
  1343. // normalize the path by replacing "//" with "/"
  1344. normalizePath(newWorkingDir);
  1345. }
  1346. free(fs->workingDir);
  1347. fs->workingDir = newWorkingDir;
  1348. return 0;
  1349. }
  1350. void hdfsFreeHosts(char ***blockHosts)
  1351. {
  1352. int i, j;
  1353. for (i=0; blockHosts[i]; i++) {
  1354. for (j=0; blockHosts[i][j]; j++) {
  1355. free(blockHosts[i][j]);
  1356. }
  1357. free(blockHosts[i]);
  1358. }
  1359. free(blockHosts);
  1360. }
  1361. tOffset hdfsGetDefaultBlockSize(hdfsFS fs)
  1362. {
  1363. errno = ENOTSUP;
  1364. return -1;
  1365. }
  1366. int hdfsFileUsesDirectRead(hdfsFile file)
  1367. {
  1368. return 0; // webhdfs never performs direct reads.
  1369. }
  1370. void hdfsFileDisableDirectRead(hdfsFile file)
  1371. {
  1372. // webhdfs never performs direct reads
  1373. }
  1374. int hdfsHFlush(hdfsFS fs, hdfsFile file)
  1375. {
  1376. if (file->type != OUTPUT) {
  1377. errno = EINVAL;
  1378. return -1;
  1379. }
  1380. // TODO: block until our write buffer is flushed (HDFS-3952)
  1381. return 0;
  1382. }
  1383. int hdfsFlush(hdfsFS fs, hdfsFile file)
  1384. {
  1385. if (file->type != OUTPUT) {
  1386. errno = EINVAL;
  1387. return -1;
  1388. }
  1389. // TODO: block until our write buffer is flushed (HDFS-3952)
  1390. return 0;
  1391. }
  1392. char*** hdfsGetHosts(hdfsFS fs, const char* path,
  1393. tOffset start, tOffset length)
  1394. {
  1395. errno = ENOTSUP;
  1396. return NULL;
  1397. }
  1398. tOffset hdfsGetCapacity(hdfsFS fs)
  1399. {
  1400. errno = ENOTSUP;
  1401. return -1;
  1402. }
  1403. tOffset hdfsGetUsed(hdfsFS fs)
  1404. {
  1405. errno = ENOTSUP;
  1406. return -1;
  1407. }
  1408. int hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst)
  1409. {
  1410. errno = ENOTSUP;
  1411. return -1;
  1412. }
  1413. int hdfsMove(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst)
  1414. {
  1415. errno = ENOTSUP;
  1416. return -1;
  1417. }