test-fs.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <errno.h>
  24. #include <string.h> /* memset */
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. /* FIXME we shouldn't need to branch in this file */
  28. #if defined(__unix__) || defined(__POSIX__) || \
  29. defined(__APPLE__) || defined(_AIX)
  30. #include <unistd.h> /* unlink, rmdir, etc. */
  31. #else
  32. # include <direct.h>
  33. # include <io.h>
  34. # define unlink _unlink
  35. # define rmdir _rmdir
  36. # define stat _stati64
  37. # define open _open
  38. # define write _write
  39. # define lseek _lseek
  40. # define close _close
  41. #endif
  42. #define TOO_LONG_NAME_LENGTH 65536
  43. #define PATHMAX 1024
  44. typedef struct {
  45. const char* path;
  46. double atime;
  47. double mtime;
  48. } utime_check_t;
  49. static int dummy_cb_count;
  50. static int close_cb_count;
  51. static int create_cb_count;
  52. static int open_cb_count;
  53. static int read_cb_count;
  54. static int write_cb_count;
  55. static int unlink_cb_count;
  56. static int mkdir_cb_count;
  57. static int rmdir_cb_count;
  58. static int readdir_cb_count;
  59. static int stat_cb_count;
  60. static int rename_cb_count;
  61. static int fsync_cb_count;
  62. static int fdatasync_cb_count;
  63. static int ftruncate_cb_count;
  64. static int sendfile_cb_count;
  65. static int fstat_cb_count;
  66. static int chmod_cb_count;
  67. static int fchmod_cb_count;
  68. static int chown_cb_count;
  69. static int fchown_cb_count;
  70. static int link_cb_count;
  71. static int symlink_cb_count;
  72. static int readlink_cb_count;
  73. static int utime_cb_count;
  74. static int futime_cb_count;
  75. static uv_loop_t* loop;
  76. static uv_fs_t open_req1;
  77. static uv_fs_t open_req2;
  78. static uv_fs_t read_req;
  79. static uv_fs_t write_req;
  80. static uv_fs_t unlink_req;
  81. static uv_fs_t close_req;
  82. static uv_fs_t mkdir_req;
  83. static uv_fs_t rmdir_req;
  84. static uv_fs_t readdir_req;
  85. static uv_fs_t stat_req;
  86. static uv_fs_t rename_req;
  87. static uv_fs_t fsync_req;
  88. static uv_fs_t fdatasync_req;
  89. static uv_fs_t ftruncate_req;
  90. static uv_fs_t sendfile_req;
  91. static uv_fs_t utime_req;
  92. static uv_fs_t futime_req;
  93. static char buf[32];
  94. static char test_buf[] = "test-buffer\n";
  95. static void check_permission(const char* filename, unsigned int mode) {
  96. int r;
  97. uv_fs_t req;
  98. uv_stat_t* s;
  99. r = uv_fs_stat(uv_default_loop(), &req, filename, NULL);
  100. ASSERT(r == 0);
  101. ASSERT(req.result == 0);
  102. s = &req.statbuf;
  103. #ifdef _WIN32
  104. /*
  105. * On Windows, chmod can only modify S_IWUSR (_S_IWRITE) bit,
  106. * so only testing for the specified flags.
  107. */
  108. ASSERT((s->st_mode & 0777) & mode);
  109. #else
  110. ASSERT((s->st_mode & 0777) == mode);
  111. #endif
  112. uv_fs_req_cleanup(&req);
  113. }
  114. static void dummy_cb(uv_fs_t* req) {
  115. (void) req;
  116. dummy_cb_count++;
  117. }
  118. static void link_cb(uv_fs_t* req) {
  119. ASSERT(req->fs_type == UV_FS_LINK);
  120. ASSERT(req->result == 0);
  121. link_cb_count++;
  122. uv_fs_req_cleanup(req);
  123. }
  124. static void symlink_cb(uv_fs_t* req) {
  125. ASSERT(req->fs_type == UV_FS_SYMLINK);
  126. ASSERT(req->result == 0);
  127. symlink_cb_count++;
  128. uv_fs_req_cleanup(req);
  129. }
  130. static void readlink_cb(uv_fs_t* req) {
  131. ASSERT(req->fs_type == UV_FS_READLINK);
  132. ASSERT(req->result == 0);
  133. ASSERT(strcmp(req->ptr, "test_file_symlink2") == 0);
  134. readlink_cb_count++;
  135. uv_fs_req_cleanup(req);
  136. }
  137. static void fchmod_cb(uv_fs_t* req) {
  138. ASSERT(req->fs_type == UV_FS_FCHMOD);
  139. ASSERT(req->result == 0);
  140. fchmod_cb_count++;
  141. uv_fs_req_cleanup(req);
  142. check_permission("test_file", *(int*)req->data);
  143. }
  144. static void chmod_cb(uv_fs_t* req) {
  145. ASSERT(req->fs_type == UV_FS_CHMOD);
  146. ASSERT(req->result == 0);
  147. chmod_cb_count++;
  148. uv_fs_req_cleanup(req);
  149. check_permission("test_file", *(int*)req->data);
  150. }
  151. static void fchown_cb(uv_fs_t* req) {
  152. ASSERT(req->fs_type == UV_FS_FCHOWN);
  153. ASSERT(req->result == 0);
  154. fchown_cb_count++;
  155. uv_fs_req_cleanup(req);
  156. }
  157. static void chown_cb(uv_fs_t* req) {
  158. ASSERT(req->fs_type == UV_FS_CHOWN);
  159. ASSERT(req->result == 0);
  160. chown_cb_count++;
  161. uv_fs_req_cleanup(req);
  162. }
  163. static void chown_root_cb(uv_fs_t* req) {
  164. ASSERT(req->fs_type == UV_FS_CHOWN);
  165. #ifdef _WIN32
  166. /* On windows, chown is a no-op and always succeeds. */
  167. ASSERT(req->result == 0);
  168. #else
  169. /* On unix, chown'ing the root directory is not allowed -
  170. * unless you're root, of course.
  171. */
  172. if (geteuid() == 0)
  173. ASSERT(req->result == 0);
  174. else
  175. ASSERT(req->result == UV_EPERM);
  176. #endif
  177. chown_cb_count++;
  178. uv_fs_req_cleanup(req);
  179. }
  180. static void unlink_cb(uv_fs_t* req) {
  181. ASSERT(req == &unlink_req);
  182. ASSERT(req->fs_type == UV_FS_UNLINK);
  183. ASSERT(req->result == 0);
  184. unlink_cb_count++;
  185. uv_fs_req_cleanup(req);
  186. }
  187. static void fstat_cb(uv_fs_t* req) {
  188. uv_stat_t* s = req->ptr;
  189. ASSERT(req->fs_type == UV_FS_FSTAT);
  190. ASSERT(req->result == 0);
  191. ASSERT(s->st_size == sizeof(test_buf));
  192. uv_fs_req_cleanup(req);
  193. fstat_cb_count++;
  194. }
  195. static void close_cb(uv_fs_t* req) {
  196. int r;
  197. ASSERT(req == &close_req);
  198. ASSERT(req->fs_type == UV_FS_CLOSE);
  199. ASSERT(req->result == 0);
  200. close_cb_count++;
  201. uv_fs_req_cleanup(req);
  202. if (close_cb_count == 3) {
  203. r = uv_fs_unlink(loop, &unlink_req, "test_file2", unlink_cb);
  204. ASSERT(r == 0);
  205. }
  206. }
  207. static void ftruncate_cb(uv_fs_t* req) {
  208. int r;
  209. ASSERT(req == &ftruncate_req);
  210. ASSERT(req->fs_type == UV_FS_FTRUNCATE);
  211. ASSERT(req->result == 0);
  212. ftruncate_cb_count++;
  213. uv_fs_req_cleanup(req);
  214. r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
  215. ASSERT(r == 0);
  216. }
  217. static void read_cb(uv_fs_t* req) {
  218. int r;
  219. ASSERT(req == &read_req);
  220. ASSERT(req->fs_type == UV_FS_READ);
  221. ASSERT(req->result >= 0); /* FIXME(bnoordhuis) Check if requested size? */
  222. read_cb_count++;
  223. uv_fs_req_cleanup(req);
  224. if (read_cb_count == 1) {
  225. ASSERT(strcmp(buf, test_buf) == 0);
  226. r = uv_fs_ftruncate(loop, &ftruncate_req, open_req1.result, 7,
  227. ftruncate_cb);
  228. } else {
  229. ASSERT(strcmp(buf, "test-bu") == 0);
  230. r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
  231. }
  232. ASSERT(r == 0);
  233. }
  234. static void open_cb(uv_fs_t* req) {
  235. int r;
  236. ASSERT(req == &open_req1);
  237. ASSERT(req->fs_type == UV_FS_OPEN);
  238. if (req->result < 0) {
  239. fprintf(stderr, "async open error: %d\n", (int) req->result);
  240. ASSERT(0);
  241. }
  242. open_cb_count++;
  243. ASSERT(req->path);
  244. ASSERT(memcmp(req->path, "test_file2\0", 11) == 0);
  245. uv_fs_req_cleanup(req);
  246. memset(buf, 0, sizeof(buf));
  247. r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
  248. read_cb);
  249. ASSERT(r == 0);
  250. }
  251. static void open_cb_simple(uv_fs_t* req) {
  252. ASSERT(req->fs_type == UV_FS_OPEN);
  253. if (req->result < 0) {
  254. fprintf(stderr, "async open error: %d\n", (int) req->result);
  255. ASSERT(0);
  256. }
  257. open_cb_count++;
  258. ASSERT(req->path);
  259. uv_fs_req_cleanup(req);
  260. }
  261. static void fsync_cb(uv_fs_t* req) {
  262. int r;
  263. ASSERT(req == &fsync_req);
  264. ASSERT(req->fs_type == UV_FS_FSYNC);
  265. ASSERT(req->result == 0);
  266. fsync_cb_count++;
  267. uv_fs_req_cleanup(req);
  268. r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
  269. ASSERT(r == 0);
  270. }
  271. static void fdatasync_cb(uv_fs_t* req) {
  272. int r;
  273. ASSERT(req == &fdatasync_req);
  274. ASSERT(req->fs_type == UV_FS_FDATASYNC);
  275. ASSERT(req->result == 0);
  276. fdatasync_cb_count++;
  277. uv_fs_req_cleanup(req);
  278. r = uv_fs_fsync(loop, &fsync_req, open_req1.result, fsync_cb);
  279. ASSERT(r == 0);
  280. }
  281. static void write_cb(uv_fs_t* req) {
  282. int r;
  283. ASSERT(req == &write_req);
  284. ASSERT(req->fs_type == UV_FS_WRITE);
  285. ASSERT(req->result >= 0); /* FIXME(bnoordhuis) Check if requested size? */
  286. write_cb_count++;
  287. uv_fs_req_cleanup(req);
  288. r = uv_fs_fdatasync(loop, &fdatasync_req, open_req1.result, fdatasync_cb);
  289. ASSERT(r == 0);
  290. }
  291. static void create_cb(uv_fs_t* req) {
  292. int r;
  293. ASSERT(req == &open_req1);
  294. ASSERT(req->fs_type == UV_FS_OPEN);
  295. ASSERT(req->result >= 0);
  296. create_cb_count++;
  297. uv_fs_req_cleanup(req);
  298. r = uv_fs_write(loop, &write_req, req->result, test_buf, sizeof(test_buf),
  299. -1, write_cb);
  300. ASSERT(r == 0);
  301. }
  302. static void rename_cb(uv_fs_t* req) {
  303. ASSERT(req == &rename_req);
  304. ASSERT(req->fs_type == UV_FS_RENAME);
  305. ASSERT(req->result == 0);
  306. rename_cb_count++;
  307. uv_fs_req_cleanup(req);
  308. }
  309. static void mkdir_cb(uv_fs_t* req) {
  310. ASSERT(req == &mkdir_req);
  311. ASSERT(req->fs_type == UV_FS_MKDIR);
  312. ASSERT(req->result == 0);
  313. mkdir_cb_count++;
  314. ASSERT(req->path);
  315. ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
  316. uv_fs_req_cleanup(req);
  317. }
  318. static void rmdir_cb(uv_fs_t* req) {
  319. ASSERT(req == &rmdir_req);
  320. ASSERT(req->fs_type == UV_FS_RMDIR);
  321. ASSERT(req->result == 0);
  322. rmdir_cb_count++;
  323. ASSERT(req->path);
  324. ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
  325. uv_fs_req_cleanup(req);
  326. }
  327. static void readdir_cb(uv_fs_t* req) {
  328. ASSERT(req == &readdir_req);
  329. ASSERT(req->fs_type == UV_FS_READDIR);
  330. ASSERT(req->result == 2);
  331. ASSERT(req->ptr);
  332. ASSERT(memcmp(req->ptr, "file1\0file2\0", 12) == 0
  333. || memcmp(req->ptr, "file2\0file1\0", 12) == 0);
  334. readdir_cb_count++;
  335. ASSERT(req->path);
  336. ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
  337. uv_fs_req_cleanup(req);
  338. ASSERT(!req->ptr);
  339. }
  340. static void empty_readdir_cb(uv_fs_t* req) {
  341. ASSERT(req == &readdir_req);
  342. ASSERT(req->fs_type == UV_FS_READDIR);
  343. ASSERT(req->result == 0);
  344. ASSERT(req->ptr == NULL);
  345. uv_fs_req_cleanup(req);
  346. readdir_cb_count++;
  347. }
  348. static void file_readdir_cb(uv_fs_t* req) {
  349. ASSERT(req == &readdir_req);
  350. ASSERT(req->fs_type == UV_FS_READDIR);
  351. ASSERT(req->result == UV_ENOTDIR);
  352. ASSERT(req->ptr == NULL);
  353. uv_fs_req_cleanup(req);
  354. readdir_cb_count++;
  355. }
  356. static void stat_cb(uv_fs_t* req) {
  357. ASSERT(req == &stat_req);
  358. ASSERT(req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_LSTAT);
  359. ASSERT(req->result == 0);
  360. ASSERT(req->ptr);
  361. stat_cb_count++;
  362. uv_fs_req_cleanup(req);
  363. ASSERT(!req->ptr);
  364. }
  365. static void sendfile_cb(uv_fs_t* req) {
  366. ASSERT(req == &sendfile_req);
  367. ASSERT(req->fs_type == UV_FS_SENDFILE);
  368. ASSERT(req->result == 65546);
  369. sendfile_cb_count++;
  370. uv_fs_req_cleanup(req);
  371. }
  372. static void open_noent_cb(uv_fs_t* req) {
  373. ASSERT(req->fs_type == UV_FS_OPEN);
  374. ASSERT(req->result == UV_ENOENT);
  375. open_cb_count++;
  376. uv_fs_req_cleanup(req);
  377. }
  378. static void open_nametoolong_cb(uv_fs_t* req) {
  379. ASSERT(req->fs_type == UV_FS_OPEN);
  380. ASSERT(req->result == UV_ENAMETOOLONG);
  381. open_cb_count++;
  382. uv_fs_req_cleanup(req);
  383. }
  384. static void open_loop_cb(uv_fs_t* req) {
  385. ASSERT(req->fs_type == UV_FS_OPEN);
  386. ASSERT(req->result == UV_ELOOP);
  387. open_cb_count++;
  388. uv_fs_req_cleanup(req);
  389. }
  390. TEST_IMPL(fs_file_noent) {
  391. uv_fs_t req;
  392. int r;
  393. loop = uv_default_loop();
  394. r = uv_fs_open(loop, &req, "does_not_exist", O_RDONLY, 0, NULL);
  395. ASSERT(r == UV_ENOENT);
  396. ASSERT(req.result == UV_ENOENT);
  397. uv_fs_req_cleanup(&req);
  398. r = uv_fs_open(loop, &req, "does_not_exist", O_RDONLY, 0, open_noent_cb);
  399. ASSERT(r == 0);
  400. ASSERT(open_cb_count == 0);
  401. uv_run(loop, UV_RUN_DEFAULT);
  402. ASSERT(open_cb_count == 1);
  403. /* TODO add EACCES test */
  404. MAKE_VALGRIND_HAPPY();
  405. return 0;
  406. }
  407. TEST_IMPL(fs_file_nametoolong) {
  408. uv_fs_t req;
  409. int r;
  410. char name[TOO_LONG_NAME_LENGTH + 1];
  411. loop = uv_default_loop();
  412. memset(name, 'a', TOO_LONG_NAME_LENGTH);
  413. name[TOO_LONG_NAME_LENGTH] = 0;
  414. r = uv_fs_open(loop, &req, name, O_RDONLY, 0, NULL);
  415. ASSERT(r == UV_ENAMETOOLONG);
  416. ASSERT(req.result == UV_ENAMETOOLONG);
  417. uv_fs_req_cleanup(&req);
  418. r = uv_fs_open(loop, &req, name, O_RDONLY, 0, open_nametoolong_cb);
  419. ASSERT(r == 0);
  420. ASSERT(open_cb_count == 0);
  421. uv_run(loop, UV_RUN_DEFAULT);
  422. ASSERT(open_cb_count == 1);
  423. MAKE_VALGRIND_HAPPY();
  424. return 0;
  425. }
  426. TEST_IMPL(fs_file_loop) {
  427. uv_fs_t req;
  428. int r;
  429. loop = uv_default_loop();
  430. unlink("test_symlink");
  431. uv_fs_symlink(loop, &req, "test_symlink", "test_symlink", 0, NULL);
  432. uv_fs_req_cleanup(&req);
  433. r = uv_fs_open(loop, &req, "test_symlink", O_RDONLY, 0, NULL);
  434. ASSERT(r == UV_ELOOP);
  435. ASSERT(req.result == UV_ELOOP);
  436. uv_fs_req_cleanup(&req);
  437. r = uv_fs_open(loop, &req, "test_symlink", O_RDONLY, 0, open_loop_cb);
  438. ASSERT(r == 0);
  439. ASSERT(open_cb_count == 0);
  440. uv_run(loop, UV_RUN_DEFAULT);
  441. ASSERT(open_cb_count == 1);
  442. unlink("test_symlink");
  443. MAKE_VALGRIND_HAPPY();
  444. return 0;
  445. }
  446. static void check_utime(const char* path, double atime, double mtime) {
  447. uv_stat_t* s;
  448. uv_fs_t req;
  449. int r;
  450. r = uv_fs_stat(loop, &req, path, NULL);
  451. ASSERT(r == 0);
  452. ASSERT(req.result == 0);
  453. s = &req.statbuf;
  454. ASSERT(s->st_atim.tv_sec == atime);
  455. ASSERT(s->st_mtim.tv_sec == mtime);
  456. uv_fs_req_cleanup(&req);
  457. }
  458. static void utime_cb(uv_fs_t* req) {
  459. utime_check_t* c;
  460. ASSERT(req == &utime_req);
  461. ASSERT(req->result == 0);
  462. ASSERT(req->fs_type == UV_FS_UTIME);
  463. c = req->data;
  464. check_utime(c->path, c->atime, c->mtime);
  465. uv_fs_req_cleanup(req);
  466. utime_cb_count++;
  467. }
  468. static void futime_cb(uv_fs_t* req) {
  469. utime_check_t* c;
  470. ASSERT(req == &futime_req);
  471. ASSERT(req->result == 0);
  472. ASSERT(req->fs_type == UV_FS_FUTIME);
  473. c = req->data;
  474. check_utime(c->path, c->atime, c->mtime);
  475. uv_fs_req_cleanup(req);
  476. futime_cb_count++;
  477. }
  478. TEST_IMPL(fs_file_async) {
  479. int r;
  480. /* Setup. */
  481. unlink("test_file");
  482. unlink("test_file2");
  483. loop = uv_default_loop();
  484. r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
  485. S_IRUSR | S_IWUSR, create_cb);
  486. ASSERT(r == 0);
  487. uv_run(loop, UV_RUN_DEFAULT);
  488. ASSERT(create_cb_count == 1);
  489. ASSERT(write_cb_count == 1);
  490. ASSERT(fsync_cb_count == 1);
  491. ASSERT(fdatasync_cb_count == 1);
  492. ASSERT(close_cb_count == 1);
  493. r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", rename_cb);
  494. ASSERT(r == 0);
  495. uv_run(loop, UV_RUN_DEFAULT);
  496. ASSERT(create_cb_count == 1);
  497. ASSERT(write_cb_count == 1);
  498. ASSERT(close_cb_count == 1);
  499. ASSERT(rename_cb_count == 1);
  500. r = uv_fs_open(loop, &open_req1, "test_file2", O_RDWR, 0, open_cb);
  501. ASSERT(r == 0);
  502. uv_run(loop, UV_RUN_DEFAULT);
  503. ASSERT(open_cb_count == 1);
  504. ASSERT(read_cb_count == 1);
  505. ASSERT(close_cb_count == 2);
  506. ASSERT(rename_cb_count == 1);
  507. ASSERT(create_cb_count == 1);
  508. ASSERT(write_cb_count == 1);
  509. ASSERT(ftruncate_cb_count == 1);
  510. r = uv_fs_open(loop, &open_req1, "test_file2", O_RDONLY, 0, open_cb);
  511. ASSERT(r == 0);
  512. uv_run(loop, UV_RUN_DEFAULT);
  513. ASSERT(open_cb_count == 2);
  514. ASSERT(read_cb_count == 2);
  515. ASSERT(close_cb_count == 3);
  516. ASSERT(rename_cb_count == 1);
  517. ASSERT(unlink_cb_count == 1);
  518. ASSERT(create_cb_count == 1);
  519. ASSERT(write_cb_count == 1);
  520. ASSERT(ftruncate_cb_count == 1);
  521. /* Cleanup. */
  522. unlink("test_file");
  523. unlink("test_file2");
  524. MAKE_VALGRIND_HAPPY();
  525. return 0;
  526. }
  527. TEST_IMPL(fs_file_sync) {
  528. int r;
  529. /* Setup. */
  530. unlink("test_file");
  531. unlink("test_file2");
  532. loop = uv_default_loop();
  533. r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
  534. S_IWUSR | S_IRUSR, NULL);
  535. ASSERT(r >= 0);
  536. ASSERT(open_req1.result >= 0);
  537. uv_fs_req_cleanup(&open_req1);
  538. r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
  539. sizeof(test_buf), -1, NULL);
  540. ASSERT(r >= 0);
  541. ASSERT(write_req.result >= 0);
  542. uv_fs_req_cleanup(&write_req);
  543. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  544. ASSERT(r == 0);
  545. ASSERT(close_req.result == 0);
  546. uv_fs_req_cleanup(&close_req);
  547. r = uv_fs_open(loop, &open_req1, "test_file", O_RDWR, 0, NULL);
  548. ASSERT(r >= 0);
  549. ASSERT(open_req1.result >= 0);
  550. uv_fs_req_cleanup(&open_req1);
  551. r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
  552. NULL);
  553. ASSERT(r >= 0);
  554. ASSERT(read_req.result >= 0);
  555. ASSERT(strcmp(buf, test_buf) == 0);
  556. uv_fs_req_cleanup(&read_req);
  557. r = uv_fs_ftruncate(loop, &ftruncate_req, open_req1.result, 7, NULL);
  558. ASSERT(r == 0);
  559. ASSERT(ftruncate_req.result == 0);
  560. uv_fs_req_cleanup(&ftruncate_req);
  561. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  562. ASSERT(r == 0);
  563. ASSERT(close_req.result == 0);
  564. uv_fs_req_cleanup(&close_req);
  565. r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", NULL);
  566. ASSERT(r == 0);
  567. ASSERT(rename_req.result == 0);
  568. uv_fs_req_cleanup(&rename_req);
  569. r = uv_fs_open(loop, &open_req1, "test_file2", O_RDONLY, 0, NULL);
  570. ASSERT(r >= 0);
  571. ASSERT(open_req1.result >= 0);
  572. uv_fs_req_cleanup(&open_req1);
  573. memset(buf, 0, sizeof(buf));
  574. r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
  575. NULL);
  576. ASSERT(r >= 0);
  577. ASSERT(read_req.result >= 0);
  578. ASSERT(strcmp(buf, "test-bu") == 0);
  579. uv_fs_req_cleanup(&read_req);
  580. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  581. ASSERT(r == 0);
  582. ASSERT(close_req.result == 0);
  583. uv_fs_req_cleanup(&close_req);
  584. r = uv_fs_unlink(loop, &unlink_req, "test_file2", NULL);
  585. ASSERT(r == 0);
  586. ASSERT(unlink_req.result == 0);
  587. uv_fs_req_cleanup(&unlink_req);
  588. /* Cleanup */
  589. unlink("test_file");
  590. unlink("test_file2");
  591. MAKE_VALGRIND_HAPPY();
  592. return 0;
  593. }
  594. TEST_IMPL(fs_async_dir) {
  595. int r;
  596. /* Setup */
  597. unlink("test_dir/file1");
  598. unlink("test_dir/file2");
  599. rmdir("test_dir");
  600. loop = uv_default_loop();
  601. r = uv_fs_mkdir(loop, &mkdir_req, "test_dir", 0755, mkdir_cb);
  602. ASSERT(r == 0);
  603. uv_run(loop, UV_RUN_DEFAULT);
  604. ASSERT(mkdir_cb_count == 1);
  605. /* Create 2 files synchronously. */
  606. r = uv_fs_open(loop, &open_req1, "test_dir/file1", O_WRONLY | O_CREAT,
  607. S_IWUSR | S_IRUSR, NULL);
  608. ASSERT(r >= 0);
  609. uv_fs_req_cleanup(&open_req1);
  610. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  611. ASSERT(r == 0);
  612. uv_fs_req_cleanup(&close_req);
  613. r = uv_fs_open(loop, &open_req1, "test_dir/file2", O_WRONLY | O_CREAT,
  614. S_IWUSR | S_IRUSR, NULL);
  615. ASSERT(r >= 0);
  616. uv_fs_req_cleanup(&open_req1);
  617. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  618. ASSERT(r == 0);
  619. uv_fs_req_cleanup(&close_req);
  620. r = uv_fs_readdir(loop, &readdir_req, "test_dir", 0, readdir_cb);
  621. ASSERT(r == 0);
  622. uv_run(loop, UV_RUN_DEFAULT);
  623. ASSERT(readdir_cb_count == 1);
  624. /* sync uv_fs_readdir */
  625. r = uv_fs_readdir(loop, &readdir_req, "test_dir", 0, NULL);
  626. ASSERT(r == 2);
  627. ASSERT(readdir_req.result == 2);
  628. ASSERT(readdir_req.ptr);
  629. ASSERT(memcmp(readdir_req.ptr, "file1\0file2\0", 12) == 0
  630. || memcmp(readdir_req.ptr, "file2\0file1\0", 12) == 0);
  631. uv_fs_req_cleanup(&readdir_req);
  632. ASSERT(!readdir_req.ptr);
  633. r = uv_fs_stat(loop, &stat_req, "test_dir", stat_cb);
  634. ASSERT(r == 0);
  635. uv_run(loop, UV_RUN_DEFAULT);
  636. r = uv_fs_stat(loop, &stat_req, "test_dir/", stat_cb);
  637. ASSERT(r == 0);
  638. uv_run(loop, UV_RUN_DEFAULT);
  639. r = uv_fs_lstat(loop, &stat_req, "test_dir", stat_cb);
  640. ASSERT(r == 0);
  641. uv_run(loop, UV_RUN_DEFAULT);
  642. r = uv_fs_lstat(loop, &stat_req, "test_dir/", stat_cb);
  643. ASSERT(r == 0);
  644. uv_run(loop, UV_RUN_DEFAULT);
  645. ASSERT(stat_cb_count == 4);
  646. r = uv_fs_unlink(loop, &unlink_req, "test_dir/file1", unlink_cb);
  647. ASSERT(r == 0);
  648. uv_run(loop, UV_RUN_DEFAULT);
  649. ASSERT(unlink_cb_count == 1);
  650. r = uv_fs_unlink(loop, &unlink_req, "test_dir/file2", unlink_cb);
  651. ASSERT(r == 0);
  652. uv_run(loop, UV_RUN_DEFAULT);
  653. ASSERT(unlink_cb_count == 2);
  654. r = uv_fs_rmdir(loop, &rmdir_req, "test_dir", rmdir_cb);
  655. ASSERT(r == 0);
  656. uv_run(loop, UV_RUN_DEFAULT);
  657. ASSERT(rmdir_cb_count == 1);
  658. /* Cleanup */
  659. unlink("test_dir/file1");
  660. unlink("test_dir/file2");
  661. rmdir("test_dir");
  662. MAKE_VALGRIND_HAPPY();
  663. return 0;
  664. }
  665. TEST_IMPL(fs_async_sendfile) {
  666. int f, r;
  667. struct stat s1, s2;
  668. loop = uv_default_loop();
  669. /* Setup. */
  670. unlink("test_file");
  671. unlink("test_file2");
  672. f = open("test_file", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
  673. ASSERT(f != -1);
  674. r = write(f, "begin\n", 6);
  675. ASSERT(r == 6);
  676. r = lseek(f, 65536, SEEK_CUR);
  677. ASSERT(r == 65542);
  678. r = write(f, "end\n", 4);
  679. ASSERT(r != -1);
  680. r = close(f);
  681. ASSERT(r == 0);
  682. /* Test starts here. */
  683. r = uv_fs_open(loop, &open_req1, "test_file", O_RDWR, 0, NULL);
  684. ASSERT(r >= 0);
  685. ASSERT(open_req1.result >= 0);
  686. uv_fs_req_cleanup(&open_req1);
  687. r = uv_fs_open(loop, &open_req2, "test_file2", O_WRONLY | O_CREAT,
  688. S_IWUSR | S_IRUSR, NULL);
  689. ASSERT(r >= 0);
  690. ASSERT(open_req2.result >= 0);
  691. uv_fs_req_cleanup(&open_req2);
  692. r = uv_fs_sendfile(loop, &sendfile_req, open_req2.result, open_req1.result,
  693. 0, 131072, sendfile_cb);
  694. ASSERT(r == 0);
  695. uv_run(loop, UV_RUN_DEFAULT);
  696. ASSERT(sendfile_cb_count == 1);
  697. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  698. ASSERT(r == 0);
  699. uv_fs_req_cleanup(&close_req);
  700. r = uv_fs_close(loop, &close_req, open_req2.result, NULL);
  701. ASSERT(r == 0);
  702. uv_fs_req_cleanup(&close_req);
  703. stat("test_file", &s1);
  704. stat("test_file2", &s2);
  705. ASSERT(65546 == s2.st_size && s1.st_size == s2.st_size);
  706. /* Cleanup. */
  707. unlink("test_file");
  708. unlink("test_file2");
  709. MAKE_VALGRIND_HAPPY();
  710. return 0;
  711. }
  712. TEST_IMPL(fs_fstat) {
  713. int r;
  714. uv_fs_t req;
  715. uv_file file;
  716. uv_stat_t* s;
  717. #ifndef _WIN32
  718. struct stat t;
  719. #endif
  720. /* Setup. */
  721. unlink("test_file");
  722. loop = uv_default_loop();
  723. r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
  724. S_IWUSR | S_IRUSR, NULL);
  725. ASSERT(r >= 0);
  726. ASSERT(req.result >= 0);
  727. file = req.result;
  728. uv_fs_req_cleanup(&req);
  729. r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL);
  730. ASSERT(r == sizeof(test_buf));
  731. ASSERT(req.result == sizeof(test_buf));
  732. uv_fs_req_cleanup(&req);
  733. r = uv_fs_fstat(loop, &req, file, NULL);
  734. ASSERT(r == 0);
  735. ASSERT(req.result == 0);
  736. s = req.ptr;
  737. ASSERT(s->st_size == sizeof(test_buf));
  738. #ifndef _WIN32
  739. r = fstat(file, &t);
  740. ASSERT(r == 0);
  741. ASSERT(s->st_dev == (uint64_t) t.st_dev);
  742. ASSERT(s->st_mode == (uint64_t) t.st_mode);
  743. ASSERT(s->st_nlink == (uint64_t) t.st_nlink);
  744. ASSERT(s->st_uid == (uint64_t) t.st_uid);
  745. ASSERT(s->st_gid == (uint64_t) t.st_gid);
  746. ASSERT(s->st_rdev == (uint64_t) t.st_rdev);
  747. ASSERT(s->st_ino == (uint64_t) t.st_ino);
  748. ASSERT(s->st_size == (uint64_t) t.st_size);
  749. ASSERT(s->st_blksize == (uint64_t) t.st_blksize);
  750. ASSERT(s->st_blocks == (uint64_t) t.st_blocks);
  751. #if defined(__APPLE__)
  752. ASSERT(s->st_atim.tv_sec == t.st_atimespec.tv_sec);
  753. ASSERT(s->st_atim.tv_nsec == t.st_atimespec.tv_nsec);
  754. ASSERT(s->st_mtim.tv_sec == t.st_mtimespec.tv_sec);
  755. ASSERT(s->st_mtim.tv_nsec == t.st_mtimespec.tv_nsec);
  756. ASSERT(s->st_ctim.tv_sec == t.st_ctimespec.tv_sec);
  757. ASSERT(s->st_ctim.tv_nsec == t.st_ctimespec.tv_nsec);
  758. ASSERT(s->st_birthtim.tv_sec == t.st_birthtimespec.tv_sec);
  759. ASSERT(s->st_birthtim.tv_nsec == t.st_birthtimespec.tv_nsec);
  760. ASSERT(s->st_flags == t.st_flags);
  761. ASSERT(s->st_gen == t.st_gen);
  762. #elif !defined(__ANDROID__) && (defined(__sun) || \
  763. defined(_BSD_SOURCE) || \
  764. defined(_SVID_SOURCE) || \
  765. defined(_XOPEN_SOURCE))
  766. ASSERT(s->st_atim.tv_sec == t.st_atim.tv_sec);
  767. ASSERT(s->st_atim.tv_nsec == t.st_atim.tv_nsec);
  768. ASSERT(s->st_mtim.tv_sec == t.st_mtim.tv_sec);
  769. ASSERT(s->st_mtim.tv_nsec == t.st_mtim.tv_nsec);
  770. ASSERT(s->st_ctim.tv_sec == t.st_ctim.tv_sec);
  771. ASSERT(s->st_ctim.tv_nsec == t.st_ctim.tv_nsec);
  772. # if defined(__DragonFly__) || \
  773. defined(__FreeBSD__) || \
  774. defined(__OpenBSD__) || \
  775. defined(__NetBSD__)
  776. ASSERT(s->st_birthtim.tv_sec == t.st_birthtim.tv_sec);
  777. ASSERT(s->st_birthtim.tv_nsec == t.st_birthtim.tv_nsec);
  778. ASSERT(s->st_flags == t.st_flags);
  779. ASSERT(s->st_gen == t.st_gen);
  780. # endif
  781. #else
  782. ASSERT(s->st_atim.tv_sec == t.st_atime);
  783. ASSERT(s->st_atim.tv_nsec == 0);
  784. ASSERT(s->st_mtim.tv_sec == t.st_mtime);
  785. ASSERT(s->st_mtim.tv_nsec == 0);
  786. ASSERT(s->st_ctim.tv_sec == t.st_ctime);
  787. ASSERT(s->st_ctim.tv_nsec == 0);
  788. #endif
  789. #endif
  790. uv_fs_req_cleanup(&req);
  791. /* Now do the uv_fs_fstat call asynchronously */
  792. r = uv_fs_fstat(loop, &req, file, fstat_cb);
  793. ASSERT(r == 0);
  794. uv_run(loop, UV_RUN_DEFAULT);
  795. ASSERT(fstat_cb_count == 1);
  796. r = uv_fs_close(loop, &req, file, NULL);
  797. ASSERT(r == 0);
  798. ASSERT(req.result == 0);
  799. uv_fs_req_cleanup(&req);
  800. /*
  801. * Run the loop just to check we don't have make any extraneous uv_ref()
  802. * calls. This should drop out immediately.
  803. */
  804. uv_run(loop, UV_RUN_DEFAULT);
  805. /* Cleanup. */
  806. unlink("test_file");
  807. MAKE_VALGRIND_HAPPY();
  808. return 0;
  809. }
  810. TEST_IMPL(fs_chmod) {
  811. int r;
  812. uv_fs_t req;
  813. uv_file file;
  814. /* Setup. */
  815. unlink("test_file");
  816. loop = uv_default_loop();
  817. r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
  818. S_IWUSR | S_IRUSR, NULL);
  819. ASSERT(r >= 0);
  820. ASSERT(req.result >= 0);
  821. file = req.result;
  822. uv_fs_req_cleanup(&req);
  823. r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL);
  824. ASSERT(r == sizeof(test_buf));
  825. ASSERT(req.result == sizeof(test_buf));
  826. uv_fs_req_cleanup(&req);
  827. #ifndef _WIN32
  828. /* Make the file write-only */
  829. r = uv_fs_chmod(loop, &req, "test_file", 0200, NULL);
  830. ASSERT(r == 0);
  831. ASSERT(req.result == 0);
  832. uv_fs_req_cleanup(&req);
  833. check_permission("test_file", 0200);
  834. #endif
  835. /* Make the file read-only */
  836. r = uv_fs_chmod(loop, &req, "test_file", 0400, NULL);
  837. ASSERT(r == 0);
  838. ASSERT(req.result == 0);
  839. uv_fs_req_cleanup(&req);
  840. check_permission("test_file", 0400);
  841. /* Make the file read+write with sync uv_fs_fchmod */
  842. r = uv_fs_fchmod(loop, &req, file, 0600, NULL);
  843. ASSERT(r == 0);
  844. ASSERT(req.result == 0);
  845. uv_fs_req_cleanup(&req);
  846. check_permission("test_file", 0600);
  847. #ifndef _WIN32
  848. /* async chmod */
  849. {
  850. static int mode = 0200;
  851. req.data = &mode;
  852. }
  853. r = uv_fs_chmod(loop, &req, "test_file", 0200, chmod_cb);
  854. ASSERT(r == 0);
  855. uv_run(loop, UV_RUN_DEFAULT);
  856. ASSERT(chmod_cb_count == 1);
  857. chmod_cb_count = 0; /* reset for the next test */
  858. #endif
  859. /* async chmod */
  860. {
  861. static int mode = 0400;
  862. req.data = &mode;
  863. }
  864. r = uv_fs_chmod(loop, &req, "test_file", 0400, chmod_cb);
  865. ASSERT(r == 0);
  866. uv_run(loop, UV_RUN_DEFAULT);
  867. ASSERT(chmod_cb_count == 1);
  868. /* async fchmod */
  869. {
  870. static int mode = 0600;
  871. req.data = &mode;
  872. }
  873. r = uv_fs_fchmod(loop, &req, file, 0600, fchmod_cb);
  874. ASSERT(r == 0);
  875. uv_run(loop, UV_RUN_DEFAULT);
  876. ASSERT(fchmod_cb_count == 1);
  877. close(file);
  878. /*
  879. * Run the loop just to check we don't have make any extraneous uv_ref()
  880. * calls. This should drop out immediately.
  881. */
  882. uv_run(loop, UV_RUN_DEFAULT);
  883. /* Cleanup. */
  884. unlink("test_file");
  885. MAKE_VALGRIND_HAPPY();
  886. return 0;
  887. }
  888. TEST_IMPL(fs_chown) {
  889. int r;
  890. uv_fs_t req;
  891. uv_file file;
  892. /* Setup. */
  893. unlink("test_file");
  894. loop = uv_default_loop();
  895. r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
  896. S_IWUSR | S_IRUSR, NULL);
  897. ASSERT(r >= 0);
  898. ASSERT(req.result >= 0);
  899. file = req.result;
  900. uv_fs_req_cleanup(&req);
  901. /* sync chown */
  902. r = uv_fs_chown(loop, &req, "test_file", -1, -1, NULL);
  903. ASSERT(r == 0);
  904. ASSERT(req.result == 0);
  905. uv_fs_req_cleanup(&req);
  906. /* sync fchown */
  907. r = uv_fs_fchown(loop, &req, file, -1, -1, NULL);
  908. ASSERT(r == 0);
  909. ASSERT(req.result == 0);
  910. uv_fs_req_cleanup(&req);
  911. /* async chown */
  912. r = uv_fs_chown(loop, &req, "test_file", -1, -1, chown_cb);
  913. ASSERT(r == 0);
  914. uv_run(loop, UV_RUN_DEFAULT);
  915. ASSERT(chown_cb_count == 1);
  916. /* chown to root (fail) */
  917. chown_cb_count = 0;
  918. r = uv_fs_chown(loop, &req, "test_file", 0, 0, chown_root_cb);
  919. uv_run(loop, UV_RUN_DEFAULT);
  920. ASSERT(chown_cb_count == 1);
  921. /* async fchown */
  922. r = uv_fs_fchown(loop, &req, file, -1, -1, fchown_cb);
  923. ASSERT(r == 0);
  924. uv_run(loop, UV_RUN_DEFAULT);
  925. ASSERT(fchown_cb_count == 1);
  926. close(file);
  927. /*
  928. * Run the loop just to check we don't have make any extraneous uv_ref()
  929. * calls. This should drop out immediately.
  930. */
  931. uv_run(loop, UV_RUN_DEFAULT);
  932. /* Cleanup. */
  933. unlink("test_file");
  934. MAKE_VALGRIND_HAPPY();
  935. return 0;
  936. }
  937. TEST_IMPL(fs_link) {
  938. int r;
  939. uv_fs_t req;
  940. uv_file file;
  941. uv_file link;
  942. /* Setup. */
  943. unlink("test_file");
  944. unlink("test_file_link");
  945. unlink("test_file_link2");
  946. loop = uv_default_loop();
  947. r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
  948. S_IWUSR | S_IRUSR, NULL);
  949. ASSERT(r >= 0);
  950. ASSERT(req.result >= 0);
  951. file = req.result;
  952. uv_fs_req_cleanup(&req);
  953. r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL);
  954. ASSERT(r == sizeof(test_buf));
  955. ASSERT(req.result == sizeof(test_buf));
  956. uv_fs_req_cleanup(&req);
  957. close(file);
  958. /* sync link */
  959. r = uv_fs_link(loop, &req, "test_file", "test_file_link", NULL);
  960. ASSERT(r == 0);
  961. ASSERT(req.result == 0);
  962. uv_fs_req_cleanup(&req);
  963. r = uv_fs_open(loop, &req, "test_file_link", O_RDWR, 0, NULL);
  964. ASSERT(r >= 0);
  965. ASSERT(req.result >= 0);
  966. link = req.result;
  967. uv_fs_req_cleanup(&req);
  968. memset(buf, 0, sizeof(buf));
  969. r = uv_fs_read(loop, &req, link, buf, sizeof(buf), 0, NULL);
  970. ASSERT(r >= 0);
  971. ASSERT(req.result >= 0);
  972. ASSERT(strcmp(buf, test_buf) == 0);
  973. close(link);
  974. /* async link */
  975. r = uv_fs_link(loop, &req, "test_file", "test_file_link2", link_cb);
  976. ASSERT(r == 0);
  977. uv_run(loop, UV_RUN_DEFAULT);
  978. ASSERT(link_cb_count == 1);
  979. r = uv_fs_open(loop, &req, "test_file_link2", O_RDWR, 0, NULL);
  980. ASSERT(r >= 0);
  981. ASSERT(req.result >= 0);
  982. link = req.result;
  983. uv_fs_req_cleanup(&req);
  984. memset(buf, 0, sizeof(buf));
  985. r = uv_fs_read(loop, &req, link, buf, sizeof(buf), 0, NULL);
  986. ASSERT(r >= 0);
  987. ASSERT(req.result >= 0);
  988. ASSERT(strcmp(buf, test_buf) == 0);
  989. close(link);
  990. /*
  991. * Run the loop just to check we don't have make any extraneous uv_ref()
  992. * calls. This should drop out immediately.
  993. */
  994. uv_run(loop, UV_RUN_DEFAULT);
  995. /* Cleanup. */
  996. unlink("test_file");
  997. unlink("test_file_link");
  998. unlink("test_file_link2");
  999. MAKE_VALGRIND_HAPPY();
  1000. return 0;
  1001. }
  1002. TEST_IMPL(fs_readlink) {
  1003. uv_fs_t req;
  1004. loop = uv_default_loop();
  1005. ASSERT(0 == uv_fs_readlink(loop, &req, "no_such_file", dummy_cb));
  1006. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  1007. ASSERT(dummy_cb_count == 1);
  1008. ASSERT(req.ptr == NULL);
  1009. ASSERT(req.result == UV_ENOENT);
  1010. uv_fs_req_cleanup(&req);
  1011. ASSERT(UV_ENOENT == uv_fs_readlink(loop, &req, "no_such_file", NULL));
  1012. ASSERT(req.ptr == NULL);
  1013. ASSERT(req.result == UV_ENOENT);
  1014. uv_fs_req_cleanup(&req);
  1015. MAKE_VALGRIND_HAPPY();
  1016. return 0;
  1017. }
  1018. TEST_IMPL(fs_symlink) {
  1019. int r;
  1020. uv_fs_t req;
  1021. uv_file file;
  1022. uv_file link;
  1023. /* Setup. */
  1024. unlink("test_file");
  1025. unlink("test_file_symlink");
  1026. unlink("test_file_symlink2");
  1027. unlink("test_file_symlink_symlink");
  1028. unlink("test_file_symlink2_symlink");
  1029. loop = uv_default_loop();
  1030. r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT,
  1031. S_IWUSR | S_IRUSR, NULL);
  1032. ASSERT(r >= 0);
  1033. ASSERT(req.result >= 0);
  1034. file = req.result;
  1035. uv_fs_req_cleanup(&req);
  1036. r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL);
  1037. ASSERT(r == sizeof(test_buf));
  1038. ASSERT(req.result == sizeof(test_buf));
  1039. uv_fs_req_cleanup(&req);
  1040. close(file);
  1041. /* sync symlink */
  1042. r = uv_fs_symlink(loop, &req, "test_file", "test_file_symlink", 0, NULL);
  1043. #ifdef _WIN32
  1044. if (r < 0) {
  1045. if (r == UV_ENOTSUP) {
  1046. /*
  1047. * Windows doesn't support symlinks on older versions.
  1048. * We just pass the test and bail out early if we get ENOTSUP.
  1049. */
  1050. return 0;
  1051. } else if (r == UV_EPERM) {
  1052. /*
  1053. * Creating a symlink is only allowed when running elevated.
  1054. * We pass the test and bail out early if we get UV_EPERM.
  1055. */
  1056. return 0;
  1057. }
  1058. }
  1059. #endif
  1060. ASSERT(r == 0);
  1061. ASSERT(req.result == 0);
  1062. uv_fs_req_cleanup(&req);
  1063. r = uv_fs_open(loop, &req, "test_file_symlink", O_RDWR, 0, NULL);
  1064. ASSERT(r >= 0);
  1065. ASSERT(req.result >= 0);
  1066. link = req.result;
  1067. uv_fs_req_cleanup(&req);
  1068. memset(buf, 0, sizeof(buf));
  1069. r = uv_fs_read(loop, &req, link, buf, sizeof(buf), 0, NULL);
  1070. ASSERT(r >= 0);
  1071. ASSERT(req.result >= 0);
  1072. ASSERT(strcmp(buf, test_buf) == 0);
  1073. close(link);
  1074. r = uv_fs_symlink(loop,
  1075. &req,
  1076. "test_file_symlink",
  1077. "test_file_symlink_symlink",
  1078. 0,
  1079. NULL);
  1080. ASSERT(r == 0);
  1081. uv_fs_req_cleanup(&req);
  1082. r = uv_fs_readlink(loop, &req, "test_file_symlink_symlink", NULL);
  1083. ASSERT(r == 0);
  1084. ASSERT(strcmp(req.ptr, "test_file_symlink") == 0);
  1085. uv_fs_req_cleanup(&req);
  1086. /* async link */
  1087. r = uv_fs_symlink(loop,
  1088. &req,
  1089. "test_file",
  1090. "test_file_symlink2",
  1091. 0,
  1092. symlink_cb);
  1093. ASSERT(r == 0);
  1094. uv_run(loop, UV_RUN_DEFAULT);
  1095. ASSERT(symlink_cb_count == 1);
  1096. r = uv_fs_open(loop, &req, "test_file_symlink2", O_RDWR, 0, NULL);
  1097. ASSERT(r >= 0);
  1098. ASSERT(req.result >= 0);
  1099. link = req.result;
  1100. uv_fs_req_cleanup(&req);
  1101. memset(buf, 0, sizeof(buf));
  1102. r = uv_fs_read(loop, &req, link, buf, sizeof(buf), 0, NULL);
  1103. ASSERT(r >= 0);
  1104. ASSERT(req.result >= 0);
  1105. ASSERT(strcmp(buf, test_buf) == 0);
  1106. close(link);
  1107. r = uv_fs_symlink(loop,
  1108. &req,
  1109. "test_file_symlink2",
  1110. "test_file_symlink2_symlink",
  1111. 0,
  1112. NULL);
  1113. ASSERT(r == 0);
  1114. uv_fs_req_cleanup(&req);
  1115. r = uv_fs_readlink(loop, &req, "test_file_symlink2_symlink", readlink_cb);
  1116. ASSERT(r == 0);
  1117. uv_run(loop, UV_RUN_DEFAULT);
  1118. ASSERT(readlink_cb_count == 1);
  1119. /*
  1120. * Run the loop just to check we don't have make any extraneous uv_ref()
  1121. * calls. This should drop out immediately.
  1122. */
  1123. uv_run(loop, UV_RUN_DEFAULT);
  1124. /* Cleanup. */
  1125. unlink("test_file");
  1126. unlink("test_file_symlink");
  1127. unlink("test_file_symlink_symlink");
  1128. unlink("test_file_symlink2");
  1129. unlink("test_file_symlink2_symlink");
  1130. MAKE_VALGRIND_HAPPY();
  1131. return 0;
  1132. }
  1133. TEST_IMPL(fs_symlink_dir) {
  1134. uv_fs_t req;
  1135. int r;
  1136. char* test_dir;
  1137. /* set-up */
  1138. unlink("test_dir/file1");
  1139. unlink("test_dir/file2");
  1140. rmdir("test_dir");
  1141. rmdir("test_dir_symlink");
  1142. loop = uv_default_loop();
  1143. uv_fs_mkdir(loop, &req, "test_dir", 0777, NULL);
  1144. uv_fs_req_cleanup(&req);
  1145. #ifdef _WIN32
  1146. {
  1147. static char src_path_buf[PATHMAX];
  1148. strcpy(src_path_buf, "\\\\?\\");
  1149. uv_cwd(src_path_buf + 4, sizeof(src_path_buf));
  1150. strcat(src_path_buf, "\\test_dir\\");
  1151. test_dir = src_path_buf;
  1152. }
  1153. #else
  1154. test_dir = "test_dir";
  1155. #endif
  1156. r = uv_fs_symlink(loop, &req, test_dir, "test_dir_symlink",
  1157. UV_FS_SYMLINK_JUNCTION, NULL);
  1158. ASSERT(r == 0);
  1159. ASSERT(req.result == 0);
  1160. uv_fs_req_cleanup(&req);
  1161. r = uv_fs_stat(loop, &req, "test_dir_symlink", NULL);
  1162. ASSERT(r == 0);
  1163. ASSERT(((uv_stat_t*)req.ptr)->st_mode & S_IFDIR);
  1164. uv_fs_req_cleanup(&req);
  1165. r = uv_fs_lstat(loop, &req, "test_dir_symlink", NULL);
  1166. ASSERT(r == 0);
  1167. ASSERT(((uv_stat_t*)req.ptr)->st_mode & S_IFLNK);
  1168. #ifdef _WIN32
  1169. ASSERT(((uv_stat_t*)req.ptr)->st_size == strlen(test_dir + 4));
  1170. #else
  1171. ASSERT(((uv_stat_t*)req.ptr)->st_size == strlen(test_dir));
  1172. #endif
  1173. uv_fs_req_cleanup(&req);
  1174. r = uv_fs_readlink(loop, &req, "test_dir_symlink", NULL);
  1175. ASSERT(r == 0);
  1176. #ifdef _WIN32
  1177. ASSERT(strcmp(req.ptr, test_dir + 4) == 0);
  1178. #else
  1179. ASSERT(strcmp(req.ptr, test_dir) == 0);
  1180. #endif
  1181. uv_fs_req_cleanup(&req);
  1182. r = uv_fs_open(loop, &open_req1, "test_dir/file1", O_WRONLY | O_CREAT,
  1183. S_IWUSR | S_IRUSR, NULL);
  1184. ASSERT(r >= 0);
  1185. uv_fs_req_cleanup(&open_req1);
  1186. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1187. ASSERT(r == 0);
  1188. uv_fs_req_cleanup(&close_req);
  1189. r = uv_fs_open(loop, &open_req1, "test_dir/file2", O_WRONLY | O_CREAT,
  1190. S_IWUSR | S_IRUSR, NULL);
  1191. ASSERT(r >= 0);
  1192. uv_fs_req_cleanup(&open_req1);
  1193. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1194. ASSERT(r == 0);
  1195. uv_fs_req_cleanup(&close_req);
  1196. r = uv_fs_readdir(loop, &readdir_req, "test_dir_symlink", 0, NULL);
  1197. ASSERT(r == 2);
  1198. ASSERT(readdir_req.result == 2);
  1199. ASSERT(readdir_req.ptr);
  1200. ASSERT(memcmp(readdir_req.ptr, "file1\0file2\0", 12) == 0
  1201. || memcmp(readdir_req.ptr, "file2\0file1\0", 12) == 0);
  1202. uv_fs_req_cleanup(&readdir_req);
  1203. ASSERT(!readdir_req.ptr);
  1204. /* unlink will remove the directory symlink */
  1205. r = uv_fs_unlink(loop, &req, "test_dir_symlink", NULL);
  1206. ASSERT(r == 0);
  1207. uv_fs_req_cleanup(&req);
  1208. r = uv_fs_readdir(loop, &readdir_req, "test_dir_symlink", 0, NULL);
  1209. ASSERT(r == UV_ENOENT);
  1210. uv_fs_req_cleanup(&readdir_req);
  1211. r = uv_fs_readdir(loop, &readdir_req, "test_dir", 0, NULL);
  1212. ASSERT(r == 2);
  1213. ASSERT(readdir_req.result == 2);
  1214. ASSERT(readdir_req.ptr);
  1215. ASSERT(memcmp(readdir_req.ptr, "file1\0file2\0", 12) == 0
  1216. || memcmp(readdir_req.ptr, "file2\0file1\0", 12) == 0);
  1217. uv_fs_req_cleanup(&readdir_req);
  1218. ASSERT(!readdir_req.ptr);
  1219. /* clean-up */
  1220. unlink("test_dir/file1");
  1221. unlink("test_dir/file2");
  1222. rmdir("test_dir");
  1223. rmdir("test_dir_symlink");
  1224. MAKE_VALGRIND_HAPPY();
  1225. return 0;
  1226. }
  1227. TEST_IMPL(fs_utime) {
  1228. utime_check_t checkme;
  1229. const char* path = "test_file";
  1230. double atime;
  1231. double mtime;
  1232. uv_fs_t req;
  1233. int r;
  1234. /* Setup. */
  1235. loop = uv_default_loop();
  1236. unlink(path);
  1237. r = uv_fs_open(loop, &req, path, O_RDWR | O_CREAT,
  1238. S_IWUSR | S_IRUSR, NULL);
  1239. ASSERT(r >= 0);
  1240. ASSERT(req.result >= 0);
  1241. uv_fs_req_cleanup(&req);
  1242. close(r);
  1243. atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
  1244. r = uv_fs_utime(loop, &req, path, atime, mtime, NULL);
  1245. ASSERT(r == 0);
  1246. ASSERT(req.result == 0);
  1247. uv_fs_req_cleanup(&req);
  1248. r = uv_fs_stat(loop, &req, path, NULL);
  1249. ASSERT(r == 0);
  1250. ASSERT(req.result == 0);
  1251. check_utime(path, atime, mtime);
  1252. uv_fs_req_cleanup(&req);
  1253. atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
  1254. checkme.path = path;
  1255. checkme.atime = atime;
  1256. checkme.mtime = mtime;
  1257. /* async utime */
  1258. utime_req.data = &checkme;
  1259. r = uv_fs_utime(loop, &utime_req, path, atime, mtime, utime_cb);
  1260. ASSERT(r == 0);
  1261. uv_run(loop, UV_RUN_DEFAULT);
  1262. ASSERT(utime_cb_count == 1);
  1263. /* Cleanup. */
  1264. unlink(path);
  1265. MAKE_VALGRIND_HAPPY();
  1266. return 0;
  1267. }
  1268. #ifdef _WIN32
  1269. TEST_IMPL(fs_stat_root) {
  1270. int r;
  1271. uv_loop_t* loop = uv_default_loop();
  1272. r = uv_fs_stat(loop, &stat_req, "\\", NULL);
  1273. ASSERT(r == 0);
  1274. r = uv_fs_stat(loop, &stat_req, "..\\..\\..\\..\\..\\..\\..", NULL);
  1275. ASSERT(r == 0);
  1276. r = uv_fs_stat(loop, &stat_req, "..", NULL);
  1277. ASSERT(r == 0);
  1278. r = uv_fs_stat(loop, &stat_req, "..\\", NULL);
  1279. ASSERT(r == 0);
  1280. /* stats the current directory on c: */
  1281. r = uv_fs_stat(loop, &stat_req, "c:", NULL);
  1282. ASSERT(r == 0);
  1283. r = uv_fs_stat(loop, &stat_req, "c:\\", NULL);
  1284. ASSERT(r == 0);
  1285. r = uv_fs_stat(loop, &stat_req, "\\\\?\\C:\\", NULL);
  1286. ASSERT(r == 0);
  1287. MAKE_VALGRIND_HAPPY();
  1288. return 0;
  1289. }
  1290. #endif
  1291. TEST_IMPL(fs_futime) {
  1292. utime_check_t checkme;
  1293. const char* path = "test_file";
  1294. double atime;
  1295. double mtime;
  1296. uv_file file;
  1297. uv_fs_t req;
  1298. int r;
  1299. /* Setup. */
  1300. loop = uv_default_loop();
  1301. unlink(path);
  1302. r = uv_fs_open(loop, &req, path, O_RDWR | O_CREAT,
  1303. S_IWUSR | S_IRUSR, NULL);
  1304. ASSERT(r >= 0);
  1305. ASSERT(req.result >= 0);
  1306. uv_fs_req_cleanup(&req);
  1307. close(r);
  1308. atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
  1309. r = uv_fs_open(loop, &req, path, O_RDWR, 0, NULL);
  1310. ASSERT(r >= 0);
  1311. ASSERT(req.result >= 0);
  1312. file = req.result; /* FIXME probably not how it's supposed to be used */
  1313. uv_fs_req_cleanup(&req);
  1314. r = uv_fs_futime(loop, &req, file, atime, mtime, NULL);
  1315. ASSERT(r == 0);
  1316. ASSERT(req.result == 0);
  1317. uv_fs_req_cleanup(&req);
  1318. r = uv_fs_stat(loop, &req, path, NULL);
  1319. ASSERT(r == 0);
  1320. ASSERT(req.result == 0);
  1321. check_utime(path, atime, mtime);
  1322. uv_fs_req_cleanup(&req);
  1323. atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
  1324. checkme.atime = atime;
  1325. checkme.mtime = mtime;
  1326. checkme.path = path;
  1327. /* async futime */
  1328. futime_req.data = &checkme;
  1329. r = uv_fs_futime(loop, &futime_req, file, atime, mtime, futime_cb);
  1330. ASSERT(r == 0);
  1331. uv_run(loop, UV_RUN_DEFAULT);
  1332. ASSERT(futime_cb_count == 1);
  1333. /* Cleanup. */
  1334. unlink(path);
  1335. MAKE_VALGRIND_HAPPY();
  1336. return 0;
  1337. }
  1338. TEST_IMPL(fs_stat_missing_path) {
  1339. uv_fs_t req;
  1340. int r;
  1341. loop = uv_default_loop();
  1342. r = uv_fs_stat(loop, &req, "non_existent_file", NULL);
  1343. ASSERT(r == UV_ENOENT);
  1344. ASSERT(req.result == UV_ENOENT);
  1345. uv_fs_req_cleanup(&req);
  1346. MAKE_VALGRIND_HAPPY();
  1347. return 0;
  1348. }
  1349. TEST_IMPL(fs_readdir_empty_dir) {
  1350. const char* path;
  1351. uv_fs_t req;
  1352. int r;
  1353. path = "./empty_dir/";
  1354. loop = uv_default_loop();
  1355. uv_fs_mkdir(loop, &req, path, 0777, NULL);
  1356. uv_fs_req_cleanup(&req);
  1357. r = uv_fs_readdir(loop, &req, path, 0, NULL);
  1358. ASSERT(r == 0);
  1359. ASSERT(req.result == 0);
  1360. ASSERT(req.ptr == NULL);
  1361. uv_fs_req_cleanup(&req);
  1362. r = uv_fs_readdir(loop, &readdir_req, path, 0, empty_readdir_cb);
  1363. ASSERT(r == 0);
  1364. ASSERT(readdir_cb_count == 0);
  1365. uv_run(loop, UV_RUN_DEFAULT);
  1366. ASSERT(readdir_cb_count == 1);
  1367. uv_fs_rmdir(loop, &req, path, NULL);
  1368. uv_fs_req_cleanup(&req);
  1369. MAKE_VALGRIND_HAPPY();
  1370. return 0;
  1371. }
  1372. TEST_IMPL(fs_readdir_file) {
  1373. const char* path;
  1374. int r;
  1375. path = "test/fixtures/empty_file";
  1376. loop = uv_default_loop();
  1377. r = uv_fs_readdir(loop, &readdir_req, path, 0, NULL);
  1378. ASSERT(r == UV_ENOTDIR);
  1379. uv_fs_req_cleanup(&readdir_req);
  1380. r = uv_fs_readdir(loop, &readdir_req, path, 0, file_readdir_cb);
  1381. ASSERT(r == 0);
  1382. ASSERT(readdir_cb_count == 0);
  1383. uv_run(loop, UV_RUN_DEFAULT);
  1384. ASSERT(readdir_cb_count == 1);
  1385. MAKE_VALGRIND_HAPPY();
  1386. return 0;
  1387. }
  1388. TEST_IMPL(fs_open_dir) {
  1389. const char* path;
  1390. uv_fs_t req;
  1391. int r, file;
  1392. path = ".";
  1393. loop = uv_default_loop();
  1394. r = uv_fs_open(loop, &req, path, O_RDONLY, 0, NULL);
  1395. ASSERT(r >= 0);
  1396. ASSERT(req.result >= 0);
  1397. ASSERT(req.ptr == NULL);
  1398. file = r;
  1399. uv_fs_req_cleanup(&req);
  1400. r = uv_fs_close(loop, &req, file, NULL);
  1401. ASSERT(r == 0);
  1402. r = uv_fs_open(loop, &req, path, O_RDONLY, 0, open_cb_simple);
  1403. ASSERT(r == 0);
  1404. ASSERT(open_cb_count == 0);
  1405. uv_run(loop, UV_RUN_DEFAULT);
  1406. ASSERT(open_cb_count == 1);
  1407. MAKE_VALGRIND_HAPPY();
  1408. return 0;
  1409. }
  1410. TEST_IMPL(fs_file_open_append) {
  1411. int r;
  1412. /* Setup. */
  1413. unlink("test_file");
  1414. loop = uv_default_loop();
  1415. r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
  1416. S_IWUSR | S_IRUSR, NULL);
  1417. ASSERT(r >= 0);
  1418. ASSERT(open_req1.result >= 0);
  1419. uv_fs_req_cleanup(&open_req1);
  1420. r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
  1421. sizeof(test_buf), -1, NULL);
  1422. ASSERT(r >= 0);
  1423. ASSERT(write_req.result >= 0);
  1424. uv_fs_req_cleanup(&write_req);
  1425. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1426. ASSERT(r == 0);
  1427. ASSERT(close_req.result == 0);
  1428. uv_fs_req_cleanup(&close_req);
  1429. r = uv_fs_open(loop, &open_req1, "test_file", O_RDWR | O_APPEND, 0, NULL);
  1430. ASSERT(r >= 0);
  1431. ASSERT(open_req1.result >= 0);
  1432. uv_fs_req_cleanup(&open_req1);
  1433. r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
  1434. sizeof(test_buf), -1, NULL);
  1435. ASSERT(r >= 0);
  1436. ASSERT(write_req.result >= 0);
  1437. uv_fs_req_cleanup(&write_req);
  1438. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1439. ASSERT(r == 0);
  1440. ASSERT(close_req.result == 0);
  1441. uv_fs_req_cleanup(&close_req);
  1442. r = uv_fs_open(loop, &open_req1, "test_file", O_RDONLY, S_IRUSR, NULL);
  1443. ASSERT(r >= 0);
  1444. ASSERT(open_req1.result >= 0);
  1445. uv_fs_req_cleanup(&open_req1);
  1446. r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
  1447. NULL);
  1448. printf("read = %d\n", r);
  1449. ASSERT(r == 26);
  1450. ASSERT(read_req.result == 26);
  1451. ASSERT(memcmp(buf,
  1452. "test-buffer\n\0test-buffer\n\0",
  1453. sizeof("test-buffer\n\0test-buffer\n\0") - 1) == 0);
  1454. uv_fs_req_cleanup(&read_req);
  1455. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1456. ASSERT(r == 0);
  1457. ASSERT(close_req.result == 0);
  1458. uv_fs_req_cleanup(&close_req);
  1459. /* Cleanup */
  1460. unlink("test_file");
  1461. MAKE_VALGRIND_HAPPY();
  1462. return 0;
  1463. }
  1464. TEST_IMPL(fs_rename_to_existing_file) {
  1465. int r;
  1466. /* Setup. */
  1467. unlink("test_file");
  1468. unlink("test_file2");
  1469. loop = uv_default_loop();
  1470. r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
  1471. S_IWUSR | S_IRUSR, NULL);
  1472. ASSERT(r >= 0);
  1473. ASSERT(open_req1.result >= 0);
  1474. uv_fs_req_cleanup(&open_req1);
  1475. r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
  1476. sizeof(test_buf), -1, NULL);
  1477. ASSERT(r >= 0);
  1478. ASSERT(write_req.result >= 0);
  1479. uv_fs_req_cleanup(&write_req);
  1480. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1481. ASSERT(r == 0);
  1482. ASSERT(close_req.result == 0);
  1483. uv_fs_req_cleanup(&close_req);
  1484. r = uv_fs_open(loop, &open_req1, "test_file2", O_WRONLY | O_CREAT,
  1485. S_IWUSR | S_IRUSR, NULL);
  1486. ASSERT(r >= 0);
  1487. ASSERT(open_req1.result >= 0);
  1488. uv_fs_req_cleanup(&open_req1);
  1489. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1490. ASSERT(r == 0);
  1491. ASSERT(close_req.result == 0);
  1492. uv_fs_req_cleanup(&close_req);
  1493. r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", NULL);
  1494. ASSERT(r == 0);
  1495. ASSERT(rename_req.result == 0);
  1496. uv_fs_req_cleanup(&rename_req);
  1497. r = uv_fs_open(loop, &open_req1, "test_file2", O_RDONLY, 0, NULL);
  1498. ASSERT(r >= 0);
  1499. ASSERT(open_req1.result >= 0);
  1500. uv_fs_req_cleanup(&open_req1);
  1501. memset(buf, 0, sizeof(buf));
  1502. r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
  1503. NULL);
  1504. ASSERT(r >= 0);
  1505. ASSERT(read_req.result >= 0);
  1506. ASSERT(strcmp(buf, test_buf) == 0);
  1507. uv_fs_req_cleanup(&read_req);
  1508. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1509. ASSERT(r == 0);
  1510. ASSERT(close_req.result == 0);
  1511. uv_fs_req_cleanup(&close_req);
  1512. /* Cleanup */
  1513. unlink("test_file");
  1514. unlink("test_file2");
  1515. MAKE_VALGRIND_HAPPY();
  1516. return 0;
  1517. }
  1518. TEST_IMPL(fs_read_file_eof) {
  1519. int r;
  1520. /* Setup. */
  1521. unlink("test_file");
  1522. loop = uv_default_loop();
  1523. r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
  1524. S_IWUSR | S_IRUSR, NULL);
  1525. ASSERT(r >= 0);
  1526. ASSERT(open_req1.result >= 0);
  1527. uv_fs_req_cleanup(&open_req1);
  1528. r = uv_fs_write(loop, &write_req, open_req1.result, test_buf,
  1529. sizeof(test_buf), -1, NULL);
  1530. ASSERT(r >= 0);
  1531. ASSERT(write_req.result >= 0);
  1532. uv_fs_req_cleanup(&write_req);
  1533. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1534. ASSERT(r == 0);
  1535. ASSERT(close_req.result == 0);
  1536. uv_fs_req_cleanup(&close_req);
  1537. r = uv_fs_open(loop, &open_req1, "test_file", O_RDONLY, 0, NULL);
  1538. ASSERT(r >= 0);
  1539. ASSERT(open_req1.result >= 0);
  1540. uv_fs_req_cleanup(&open_req1);
  1541. memset(buf, 0, sizeof(buf));
  1542. r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
  1543. NULL);
  1544. ASSERT(r >= 0);
  1545. ASSERT(read_req.result >= 0);
  1546. ASSERT(strcmp(buf, test_buf) == 0);
  1547. uv_fs_req_cleanup(&read_req);
  1548. r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf),
  1549. read_req.result, NULL);
  1550. ASSERT(r == 0);
  1551. ASSERT(read_req.result == 0);
  1552. uv_fs_req_cleanup(&read_req);
  1553. r = uv_fs_close(loop, &close_req, open_req1.result, NULL);
  1554. ASSERT(r == 0);
  1555. ASSERT(close_req.result == 0);
  1556. uv_fs_req_cleanup(&close_req);
  1557. /* Cleanup */
  1558. unlink("test_file");
  1559. MAKE_VALGRIND_HAPPY();
  1560. return 0;
  1561. }