hdfs_test.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. void permission_disp(short permissions, char *rtr) {
  20. rtr[9] = '\0';
  21. int i;
  22. for(i=2;i>=0;i--)
  23. {
  24. short permissionsId = permissions >> (i * 3) & (short)7;
  25. char* perm;
  26. switch(permissionsId) {
  27. case 7:
  28. perm = "rwx"; break;
  29. case 6:
  30. perm = "rw-"; break;
  31. case 5:
  32. perm = "r-x"; break;
  33. case 4:
  34. perm = "r--"; break;
  35. case 3:
  36. perm = "-wx"; break;
  37. case 2:
  38. perm = "-w-"; break;
  39. case 1:
  40. perm = "--x"; break;
  41. case 0:
  42. perm = "---"; break;
  43. default:
  44. perm = "???";
  45. }
  46. strncpy(rtr, perm, 3);
  47. rtr+=3;
  48. }
  49. }
  50. int main(int argc, char **argv) {
  51. hdfsFS fs = hdfsConnect("default", 0);
  52. if(!fs) {
  53. fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
  54. exit(-1);
  55. }
  56. hdfsFS lfs = hdfsConnect(NULL, 0);
  57. if(!lfs) {
  58. fprintf(stderr, "Oops! Failed to connect to 'local' hdfs!\n");
  59. exit(-1);
  60. }
  61. const char* writePath = "/tmp/testfile.txt";
  62. {
  63. //Write tests
  64. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
  65. if(!writeFile) {
  66. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  67. exit(-1);
  68. }
  69. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  70. char* buffer = "Hello, World!";
  71. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
  72. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  73. tOffset currentPos = -1;
  74. if ((currentPos = hdfsTell(fs, writeFile)) == -1) {
  75. fprintf(stderr,
  76. "Failed to get current file position correctly! Got %ld!\n",
  77. currentPos);
  78. exit(-1);
  79. }
  80. fprintf(stderr, "Current position: %ld\n", currentPos);
  81. if (hdfsFlush(fs, writeFile)) {
  82. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  83. exit(-1);
  84. }
  85. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  86. hdfsCloseFile(fs, writeFile);
  87. }
  88. {
  89. //Read tests
  90. const char* readPath = "/tmp/testfile.txt";
  91. int exists = hdfsExists(fs, readPath);
  92. if (exists) {
  93. fprintf(stderr, "Failed to validate existence of %s\n", readPath);
  94. exit(-1);
  95. }
  96. hdfsFile readFile = hdfsOpenFile(fs, readPath, O_RDONLY, 0, 0, 0);
  97. if (!readFile) {
  98. fprintf(stderr, "Failed to open %s for reading!\n", readPath);
  99. exit(-1);
  100. }
  101. fprintf(stderr, "hdfsAvailable: %d\n", hdfsAvailable(fs, readFile));
  102. tOffset seekPos = 1;
  103. if(hdfsSeek(fs, readFile, seekPos)) {
  104. fprintf(stderr, "Failed to seek %s for reading!\n", readPath);
  105. exit(-1);
  106. }
  107. tOffset currentPos = -1;
  108. if((currentPos = hdfsTell(fs, readFile)) != seekPos) {
  109. fprintf(stderr,
  110. "Failed to get current file position correctly! Got %ld!\n",
  111. currentPos);
  112. exit(-1);
  113. }
  114. fprintf(stderr, "Current position: %ld\n", currentPos);
  115. static char buffer[32];
  116. tSize num_read_bytes = hdfsRead(fs, readFile, (void*)buffer,
  117. sizeof(buffer));
  118. fprintf(stderr, "Read following %d bytes:\n%s\n",
  119. num_read_bytes, buffer);
  120. num_read_bytes = hdfsPread(fs, readFile, 0, (void*)buffer,
  121. sizeof(buffer));
  122. fprintf(stderr, "Read following %d bytes:\n%s\n",
  123. num_read_bytes, buffer);
  124. hdfsCloseFile(fs, readFile);
  125. }
  126. int totalResult = 0;
  127. int result = 0;
  128. {
  129. //Generic file-system operations
  130. const char* srcPath = "/tmp/testfile.txt";
  131. const char* dstPath = "/tmp/testfile2.txt";
  132. fprintf(stderr, "hdfsCopy(remote-local): %s\n", ((result = hdfsCopy(fs, srcPath, lfs, srcPath)) ? "Failed!" : "Success!"));
  133. totalResult += result;
  134. fprintf(stderr, "hdfsCopy(remote-remote): %s\n", ((result = hdfsCopy(fs, srcPath, fs, dstPath)) ? "Failed!" : "Success!"));
  135. totalResult += result;
  136. fprintf(stderr, "hdfsMove(local-local): %s\n", ((result = hdfsMove(lfs, srcPath, lfs, dstPath)) ? "Failed!" : "Success!"));
  137. totalResult += result;
  138. fprintf(stderr, "hdfsMove(remote-local): %s\n", ((result = hdfsMove(fs, srcPath, lfs, srcPath)) ? "Failed!" : "Success!"));
  139. totalResult += result;
  140. fprintf(stderr, "hdfsRename: %s\n", ((result = hdfsRename(fs, dstPath, srcPath)) ? "Failed!" : "Success!"));
  141. totalResult += result;
  142. fprintf(stderr, "hdfsCopy(remote-remote): %s\n", ((result = hdfsCopy(fs, srcPath, fs, dstPath)) ? "Failed!" : "Success!"));
  143. totalResult += result;
  144. const char* slashTmp = "/tmp";
  145. const char* newDirectory = "/tmp/newdir";
  146. fprintf(stderr, "hdfsCreateDirectory: %s\n", ((result = hdfsCreateDirectory(fs, newDirectory)) ? "Failed!" : "Success!"));
  147. totalResult += result;
  148. fprintf(stderr, "hdfsSetReplication: %s\n", ((result = hdfsSetReplication(fs, srcPath, 2)) ? "Failed!" : "Success!"));
  149. totalResult += result;
  150. char buffer[256];
  151. const char *resp;
  152. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
  153. totalResult += (resp ? 0 : 1);
  154. fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", ((result = hdfsSetWorkingDirectory(fs, slashTmp)) ? "Failed!" : "Success!"));
  155. totalResult += result;
  156. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
  157. totalResult += (resp ? 0 : 1);
  158. fprintf(stderr, "hdfsGetDefaultBlockSize: %ld\n", hdfsGetDefaultBlockSize(fs));
  159. fprintf(stderr, "hdfsGetCapacity: %ld\n", hdfsGetCapacity(fs));
  160. fprintf(stderr, "hdfsGetUsed: %ld\n", hdfsGetUsed(fs));
  161. hdfsFileInfo *fileInfo = NULL;
  162. if((fileInfo = hdfsGetPathInfo(fs, slashTmp)) != NULL) {
  163. fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
  164. fprintf(stderr, "Name: %s, ", fileInfo->mName);
  165. fprintf(stderr, "Type: %c, ", (char)(fileInfo->mKind));
  166. fprintf(stderr, "Replication: %d, ", fileInfo->mReplication);
  167. fprintf(stderr, "BlockSize: %ld, ", fileInfo->mBlockSize);
  168. fprintf(stderr, "Size: %ld, ", fileInfo->mSize);
  169. fprintf(stderr, "LastMod: %s", ctime(&fileInfo->mLastMod));
  170. fprintf(stderr, "Owner: %s, ", fileInfo->mOwner);
  171. fprintf(stderr, "Group: %s, ", fileInfo->mGroup);
  172. char permissions[10];
  173. permission_disp(fileInfo->mPermissions, permissions);
  174. fprintf(stderr, "Permissions: %d (%s)\n", fileInfo->mPermissions, permissions);
  175. hdfsFreeFileInfo(fileInfo, 1);
  176. } else {
  177. totalResult++;
  178. fprintf(stderr, "waah! hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
  179. }
  180. hdfsFileInfo *fileList = 0;
  181. int numEntries = 0;
  182. if((fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) != NULL) {
  183. int i = 0;
  184. for(i=0; i < numEntries; ++i) {
  185. fprintf(stderr, "Name: %s, ", fileList[i].mName);
  186. fprintf(stderr, "Type: %c, ", (char)fileList[i].mKind);
  187. fprintf(stderr, "Replication: %d, ", fileList[i].mReplication);
  188. fprintf(stderr, "BlockSize: %ld, ", fileList[i].mBlockSize);
  189. fprintf(stderr, "Size: %ld, ", fileList[i].mSize);
  190. fprintf(stderr, "LastMod: %s", ctime(&fileList[i].mLastMod));
  191. fprintf(stderr, "Owner: %s, ", fileList[i].mOwner);
  192. fprintf(stderr, "Group: %s, ", fileList[i].mGroup);
  193. char permissions[10];
  194. permission_disp(fileList[i].mPermissions, permissions);
  195. fprintf(stderr, "Permissions: %d (%s)\n", fileList[i].mPermissions, permissions);
  196. }
  197. hdfsFreeFileInfo(fileList, numEntries);
  198. } else {
  199. if (errno) {
  200. totalResult++;
  201. fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
  202. } else {
  203. fprintf(stderr, "Empty directory!\n");
  204. }
  205. }
  206. char*** hosts = hdfsGetHosts(fs, srcPath, 0, 1);
  207. if(hosts) {
  208. fprintf(stderr, "hdfsGetHosts - SUCCESS! ... \n");
  209. int i=0;
  210. while(hosts[i]) {
  211. int j = 0;
  212. while(hosts[i][j]) {
  213. fprintf(stderr,
  214. "\thosts[%d][%d] - %s\n", i, j, hosts[i][j]);
  215. ++j;
  216. }
  217. ++i;
  218. }
  219. } else {
  220. totalResult++;
  221. fprintf(stderr, "waah! hdfsGetHosts - FAILED!\n");
  222. }
  223. char *newOwner = "root";
  224. // setting tmp dir to 777 so later when connectAsUser nobody, we can write to it
  225. short newPerm = 0666;
  226. // chown write
  227. fprintf(stderr, "hdfsChown: %s\n", ((result = hdfsChown(fs, writePath, NULL, "users")) ? "Failed!" : "Success!"));
  228. totalResult += result;
  229. fprintf(stderr, "hdfsChown: %s\n", ((result = hdfsChown(fs, writePath, newOwner, NULL)) ? "Failed!" : "Success!"));
  230. totalResult += result;
  231. // chmod write
  232. fprintf(stderr, "hdfsChmod: %s\n", ((result = hdfsChmod(fs, writePath, newPerm)) ? "Failed!" : "Success!"));
  233. totalResult += result;
  234. sleep(2);
  235. tTime newMtime = time(NULL);
  236. tTime newAtime = time(NULL);
  237. // utime write
  238. fprintf(stderr, "hdfsUtime: %s\n", ((result = hdfsUtime(fs, writePath, newMtime, newAtime)) ? "Failed!" : "Success!"));
  239. totalResult += result;
  240. // chown/chmod/utime read
  241. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, writePath);
  242. fprintf(stderr, "hdfsChown read: %s\n", ((result = (strcmp(finfo->mOwner, newOwner) != 0)) ? "Failed!" : "Success!"));
  243. totalResult += result;
  244. fprintf(stderr, "hdfsChmod read: %s\n", ((result = (finfo->mPermissions != newPerm)) ? "Failed!" : "Success!"));
  245. totalResult += result;
  246. // will later use /tmp/ as a different user so enable it
  247. fprintf(stderr, "hdfsChmod: %s\n", ((result = hdfsChmod(fs, "/tmp/", 0777)) ? "Failed!" : "Success!"));
  248. totalResult += result;
  249. fprintf(stderr,"newMTime=%ld\n",newMtime);
  250. fprintf(stderr,"curMTime=%ld\n",finfo->mLastMod);
  251. fprintf(stderr, "hdfsUtime read (mtime): %s\n", ((result = (finfo->mLastMod != newMtime)) ? "Failed!" : "Success!"));
  252. totalResult += result;
  253. // No easy way to turn on access times from hdfs_test right now
  254. // fprintf(stderr, "hdfsUtime read (atime): %s\n", ((result = (finfo->mLastAccess != newAtime)) ? "Failed!" : "Success!"));
  255. // totalResult += result;
  256. hdfsFreeFileInfo(finfo, 1);
  257. // Clean up
  258. fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(fs, newDirectory)) ? "Failed!" : "Success!"));
  259. totalResult += result;
  260. fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(fs, srcPath)) ? "Failed!" : "Success!"));
  261. totalResult += result;
  262. fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(lfs, srcPath)) ? "Failed!" : "Success!"));
  263. totalResult += result;
  264. fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(lfs, dstPath)) ? "Failed!" : "Success!"));
  265. totalResult += result;
  266. fprintf(stderr, "hdfsExists: %s\n", ((result = hdfsExists(fs, newDirectory)) ? "Success!" : "Failed!"));
  267. totalResult += (result ? 0 : 1);
  268. }
  269. {
  270. // TEST APPENDS
  271. const char *writePath = "/tmp/appends";
  272. // CREATE
  273. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY, 0, 0, 0);
  274. if(!writeFile) {
  275. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  276. exit(-1);
  277. }
  278. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  279. char* buffer = "Hello,";
  280. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer));
  281. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  282. if (hdfsFlush(fs, writeFile)) {
  283. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  284. exit(-1);
  285. }
  286. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  287. hdfsCloseFile(fs, writeFile);
  288. // RE-OPEN
  289. writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_APPEND, 0, 0, 0);
  290. if(!writeFile) {
  291. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  292. exit(-1);
  293. }
  294. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  295. buffer = " World";
  296. num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer) + 1);
  297. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  298. if (hdfsFlush(fs, writeFile)) {
  299. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  300. exit(-1);
  301. }
  302. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  303. hdfsCloseFile(fs, writeFile);
  304. // CHECK size
  305. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, writePath);
  306. fprintf(stderr, "fileinfo->mSize: == total %s\n", ((result = (finfo->mSize == strlen("Hello, World") + 1)) ? "Success!" : "Failed!"));
  307. totalResult += (result ? 0 : 1);
  308. // READ and check data
  309. hdfsFile readFile = hdfsOpenFile(fs, writePath, O_RDONLY, 0, 0, 0);
  310. if (!readFile) {
  311. fprintf(stderr, "Failed to open %s for reading!\n", writePath);
  312. exit(-1);
  313. }
  314. char rdbuffer[32];
  315. tSize num_read_bytes = hdfsRead(fs, readFile, (void*)rdbuffer, sizeof(rdbuffer));
  316. fprintf(stderr, "Read following %d bytes:\n%s\n",
  317. num_read_bytes, rdbuffer);
  318. fprintf(stderr, "read == Hello, World %s\n", (result = (strcmp(rdbuffer, "Hello, World") == 0)) ? "Success!" : "Failed!");
  319. hdfsCloseFile(fs, readFile);
  320. // DONE test appends
  321. }
  322. totalResult += (hdfsDisconnect(fs) != 0);
  323. {
  324. //
  325. // Now test as connecting as a specific user
  326. // This is only meant to test that we connected as that user, not to test
  327. // the actual fs user capabilities. Thus just create a file and read
  328. // the owner is correct.
  329. const char *tuser = "nobody";
  330. const char* writePath = "/tmp/usertestfile.txt";
  331. fs = hdfsConnectAsUser("default", 0, tuser);
  332. if(!fs) {
  333. fprintf(stderr, "Oops! Failed to connect to hdfs as user %s!\n",tuser);
  334. exit(-1);
  335. }
  336. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
  337. if(!writeFile) {
  338. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  339. exit(-1);
  340. }
  341. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  342. char* buffer = "Hello, World!";
  343. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
  344. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  345. if (hdfsFlush(fs, writeFile)) {
  346. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  347. exit(-1);
  348. }
  349. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  350. hdfsCloseFile(fs, writeFile);
  351. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, writePath);
  352. fprintf(stderr, "hdfs new file user is correct: %s\n", ((result = (strcmp(finfo->mOwner, tuser) != 0)) ? "Failed!" : "Success!"));
  353. totalResult += result;
  354. }
  355. totalResult += (hdfsDisconnect(fs) != 0);
  356. if (totalResult != 0) {
  357. return -1;
  358. } else {
  359. return 0;
  360. }
  361. }
  362. /**
  363. * vim: ts=4: sw=4: et:
  364. */