test-fs.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349
  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. #include <limits.h> /* INT_MAX, PATH_MAX, IOV_MAX */
  28. /* FIXME we shouldn't need to branch in this file */
  29. #if defined(__unix__) || defined(__POSIX__) || \
  30. defined(__APPLE__) || defined(__sun) || \
  31. defined(_AIX) || defined(__MVS__) || \
  32. defined(__HAIKU__)
  33. #include <unistd.h> /* unlink, rmdir, etc. */
  34. #else
  35. # include <winioctl.h>
  36. # include <direct.h>
  37. # include <io.h>
  38. # ifndef ERROR_SYMLINK_NOT_SUPPORTED
  39. # define ERROR_SYMLINK_NOT_SUPPORTED 1464
  40. # endif
  41. # define unlink _unlink
  42. # define rmdir _rmdir
  43. # define open _open
  44. # define write _write
  45. # define close _close
  46. # ifndef stat
  47. # define stat _stati64
  48. # endif
  49. # ifndef lseek
  50. # define lseek _lseek
  51. # endif
  52. #endif
  53. #define TOO_LONG_NAME_LENGTH 65536
  54. #define PATHMAX 4096
  55. typedef struct {
  56. const char* path;
  57. double atime;
  58. double mtime;
  59. } utime_check_t;
  60. static int dummy_cb_count;
  61. static int close_cb_count;
  62. static int create_cb_count;
  63. static int open_cb_count;
  64. static int read_cb_count;
  65. static int write_cb_count;
  66. static int unlink_cb_count;
  67. static int mkdir_cb_count;
  68. static int mkdtemp_cb_count;
  69. static int mkstemp_cb_count;
  70. static int rmdir_cb_count;
  71. static int scandir_cb_count;
  72. static int stat_cb_count;
  73. static int rename_cb_count;
  74. static int fsync_cb_count;
  75. static int fdatasync_cb_count;
  76. static int ftruncate_cb_count;
  77. static int sendfile_cb_count;
  78. static int fstat_cb_count;
  79. static int access_cb_count;
  80. static int chmod_cb_count;
  81. static int fchmod_cb_count;
  82. static int chown_cb_count;
  83. static int fchown_cb_count;
  84. static int lchown_cb_count;
  85. static int link_cb_count;
  86. static int symlink_cb_count;
  87. static int readlink_cb_count;
  88. static int realpath_cb_count;
  89. static int utime_cb_count;
  90. static int futime_cb_count;
  91. static int lutime_cb_count;
  92. static int statfs_cb_count;
  93. static uv_loop_t* loop;
  94. static uv_fs_t open_req1;
  95. static uv_fs_t open_req2;
  96. static uv_fs_t read_req;
  97. static uv_fs_t write_req;
  98. static uv_fs_t unlink_req;
  99. static uv_fs_t close_req;
  100. static uv_fs_t mkdir_req;
  101. static uv_fs_t mkdtemp_req1;
  102. static uv_fs_t mkdtemp_req2;
  103. static uv_fs_t mkstemp_req1;
  104. static uv_fs_t mkstemp_req2;
  105. static uv_fs_t mkstemp_req3;
  106. static uv_fs_t rmdir_req;
  107. static uv_fs_t scandir_req;
  108. static uv_fs_t stat_req;
  109. static uv_fs_t rename_req;
  110. static uv_fs_t fsync_req;
  111. static uv_fs_t fdatasync_req;
  112. static uv_fs_t ftruncate_req;
  113. static uv_fs_t sendfile_req;
  114. static uv_fs_t utime_req;
  115. static uv_fs_t futime_req;
  116. static char buf[32];
  117. static char buf2[32];
  118. static char test_buf[] = "test-buffer\n";
  119. static char test_buf2[] = "second-buffer\n";
  120. static uv_buf_t iov;
  121. #ifdef _WIN32
  122. int uv_test_getiovmax(void) {
  123. return INT32_MAX; /* Emulated by libuv, so no real limit. */
  124. }
  125. #else
  126. int uv_test_getiovmax(void) {
  127. #if defined(IOV_MAX)
  128. return IOV_MAX;
  129. #elif defined(_SC_IOV_MAX)
  130. static int iovmax = -1;
  131. if (iovmax == -1) {
  132. iovmax = sysconf(_SC_IOV_MAX);
  133. /* On some embedded devices (arm-linux-uclibc based ip camera),
  134. * sysconf(_SC_IOV_MAX) can not get the correct value. The return
  135. * value is -1 and the errno is EINPROGRESS. Degrade the value to 1.
  136. */
  137. if (iovmax == -1) iovmax = 1;
  138. }
  139. return iovmax;
  140. #else
  141. return 1024;
  142. #endif
  143. }
  144. #endif
  145. #ifdef _WIN32
  146. /*
  147. * This tag and guid have no special meaning, and don't conflict with
  148. * reserved ids.
  149. */
  150. static unsigned REPARSE_TAG = 0x9913;
  151. static GUID REPARSE_GUID = {
  152. 0x1bf6205f, 0x46ae, 0x4527,
  153. { 0xb1, 0x0c, 0xc5, 0x09, 0xb7, 0x55, 0x22, 0x80 }};
  154. #endif
  155. static void check_permission(const char* filename, unsigned int mode) {
  156. int r;
  157. uv_fs_t req;
  158. uv_stat_t* s;
  159. r = uv_fs_stat(NULL, &req, filename, NULL);
  160. ASSERT(r == 0);
  161. ASSERT(req.result == 0);
  162. s = &req.statbuf;
  163. #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MSYS__)
  164. /*
  165. * On Windows, chmod can only modify S_IWUSR (_S_IWRITE) bit,
  166. * so only testing for the specified flags.
  167. */
  168. ASSERT((s->st_mode & 0777) & mode);
  169. #else
  170. ASSERT((s->st_mode & 0777) == mode);
  171. #endif
  172. uv_fs_req_cleanup(&req);
  173. }
  174. static void dummy_cb(uv_fs_t* req) {
  175. (void) req;
  176. dummy_cb_count++;
  177. }
  178. static void link_cb(uv_fs_t* req) {
  179. ASSERT(req->fs_type == UV_FS_LINK);
  180. ASSERT(req->result == 0);
  181. link_cb_count++;
  182. uv_fs_req_cleanup(req);
  183. }
  184. static void symlink_cb(uv_fs_t* req) {
  185. ASSERT(req->fs_type == UV_FS_SYMLINK);
  186. ASSERT(req->result == 0);
  187. symlink_cb_count++;
  188. uv_fs_req_cleanup(req);
  189. }
  190. static void readlink_cb(uv_fs_t* req) {
  191. ASSERT(req->fs_type == UV_FS_READLINK);
  192. ASSERT(req->result == 0);
  193. ASSERT(strcmp(req->ptr, "test_file_symlink2") == 0);
  194. readlink_cb_count++;
  195. uv_fs_req_cleanup(req);
  196. }
  197. static void realpath_cb(uv_fs_t* req) {
  198. char test_file_abs_buf[PATHMAX];
  199. size_t test_file_abs_size = sizeof(test_file_abs_buf);
  200. ASSERT(req->fs_type == UV_FS_REALPATH);
  201. #ifdef _WIN32
  202. /*
  203. * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
  204. */
  205. if (req->result == UV_ENOSYS) {
  206. realpath_cb_count++;
  207. uv_fs_req_cleanup(req);
  208. return;
  209. }
  210. #endif
  211. ASSERT(req->result == 0);
  212. uv_cwd(test_file_abs_buf, &test_file_abs_size);
  213. #ifdef _WIN32
  214. strcat(test_file_abs_buf, "\\test_file");
  215. ASSERT(stricmp(req->ptr, test_file_abs_buf) == 0);
  216. #else
  217. strcat(test_file_abs_buf, "/test_file");
  218. ASSERT(strcmp(req->ptr, test_file_abs_buf) == 0);
  219. #endif
  220. realpath_cb_count++;
  221. uv_fs_req_cleanup(req);
  222. }
  223. static void access_cb(uv_fs_t* req) {
  224. ASSERT(req->fs_type == UV_FS_ACCESS);
  225. access_cb_count++;
  226. uv_fs_req_cleanup(req);
  227. }
  228. static void fchmod_cb(uv_fs_t* req) {
  229. ASSERT(req->fs_type == UV_FS_FCHMOD);
  230. ASSERT(req->result == 0);
  231. fchmod_cb_count++;
  232. uv_fs_req_cleanup(req);
  233. check_permission("test_file", *(int*)req->data);
  234. }
  235. static void chmod_cb(uv_fs_t* req) {
  236. ASSERT(req->fs_type == UV_FS_CHMOD);
  237. ASSERT(req->result == 0);
  238. chmod_cb_count++;
  239. uv_fs_req_cleanup(req);
  240. check_permission("test_file", *(int*)req->data);
  241. }
  242. static void fchown_cb(uv_fs_t* req) {
  243. ASSERT(req->fs_type == UV_FS_FCHOWN);
  244. ASSERT(req->result == 0);
  245. fchown_cb_count++;
  246. uv_fs_req_cleanup(req);
  247. }
  248. static void chown_cb(uv_fs_t* req) {
  249. ASSERT(req->fs_type == UV_FS_CHOWN);
  250. ASSERT(req->result == 0);
  251. chown_cb_count++;
  252. uv_fs_req_cleanup(req);
  253. }
  254. static void lchown_cb(uv_fs_t* req) {
  255. ASSERT(req->fs_type == UV_FS_LCHOWN);
  256. ASSERT(req->result == 0);
  257. lchown_cb_count++;
  258. uv_fs_req_cleanup(req);
  259. }
  260. static void chown_root_cb(uv_fs_t* req) {
  261. ASSERT(req->fs_type == UV_FS_CHOWN);
  262. #if defined(_WIN32) || defined(__MSYS__)
  263. /* On windows, chown is a no-op and always succeeds. */
  264. ASSERT(req->result == 0);
  265. #else
  266. /* On unix, chown'ing the root directory is not allowed -
  267. * unless you're root, of course.
  268. */
  269. if (geteuid() == 0)
  270. ASSERT(req->result == 0);
  271. else
  272. # if defined(__CYGWIN__)
  273. /* On Cygwin, uid 0 is invalid (no root). */
  274. ASSERT(req->result == UV_EINVAL);
  275. # elif defined(__PASE__)
  276. /* On IBMi PASE, there is no root user. uid 0 is user qsecofr.
  277. * User may grant qsecofr's privileges, including changing
  278. * the file's ownership to uid 0.
  279. */
  280. ASSERT(req->result == 0);
  281. # else
  282. ASSERT(req->result == UV_EPERM);
  283. # endif
  284. #endif
  285. chown_cb_count++;
  286. uv_fs_req_cleanup(req);
  287. }
  288. static void unlink_cb(uv_fs_t* req) {
  289. ASSERT(req == &unlink_req);
  290. ASSERT(req->fs_type == UV_FS_UNLINK);
  291. ASSERT(req->result == 0);
  292. unlink_cb_count++;
  293. uv_fs_req_cleanup(req);
  294. }
  295. static void fstat_cb(uv_fs_t* req) {
  296. uv_stat_t* s = req->ptr;
  297. ASSERT(req->fs_type == UV_FS_FSTAT);
  298. ASSERT(req->result == 0);
  299. ASSERT(s->st_size == sizeof(test_buf));
  300. uv_fs_req_cleanup(req);
  301. fstat_cb_count++;
  302. }
  303. static void statfs_cb(uv_fs_t* req) {
  304. uv_statfs_t* stats;
  305. ASSERT(req->fs_type == UV_FS_STATFS);
  306. ASSERT(req->result == 0);
  307. ASSERT(req->ptr != NULL);
  308. stats = req->ptr;
  309. #if defined(_WIN32) || defined(__sun) || defined(_AIX) || defined(__MVS__) || \
  310. defined(__OpenBSD__) || defined(__NetBSD__)
  311. ASSERT(stats->f_type == 0);
  312. #else
  313. ASSERT(stats->f_type > 0);
  314. #endif
  315. ASSERT(stats->f_bsize > 0);
  316. ASSERT(stats->f_blocks > 0);
  317. ASSERT(stats->f_bfree <= stats->f_blocks);
  318. ASSERT(stats->f_bavail <= stats->f_bfree);
  319. #ifdef _WIN32
  320. ASSERT(stats->f_files == 0);
  321. ASSERT(stats->f_ffree == 0);
  322. #else
  323. /* There is no assertion for stats->f_files that makes sense, so ignore it. */
  324. ASSERT(stats->f_ffree <= stats->f_files);
  325. #endif
  326. uv_fs_req_cleanup(req);
  327. ASSERT(req->ptr == NULL);
  328. statfs_cb_count++;
  329. }
  330. static void close_cb(uv_fs_t* req) {
  331. int r;
  332. ASSERT(req == &close_req);
  333. ASSERT(req->fs_type == UV_FS_CLOSE);
  334. ASSERT(req->result == 0);
  335. close_cb_count++;
  336. uv_fs_req_cleanup(req);
  337. if (close_cb_count == 3) {
  338. r = uv_fs_unlink(loop, &unlink_req, "test_file2", unlink_cb);
  339. ASSERT(r == 0);
  340. }
  341. }
  342. static void ftruncate_cb(uv_fs_t* req) {
  343. int r;
  344. ASSERT(req == &ftruncate_req);
  345. ASSERT(req->fs_type == UV_FS_FTRUNCATE);
  346. ASSERT(req->result == 0);
  347. ftruncate_cb_count++;
  348. uv_fs_req_cleanup(req);
  349. r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
  350. ASSERT(r == 0);
  351. }
  352. static void fail_cb(uv_fs_t* req) {
  353. FATAL("fail_cb should not have been called");
  354. }
  355. static void read_cb(uv_fs_t* req) {
  356. int r;
  357. ASSERT(req == &read_req);
  358. ASSERT(req->fs_type == UV_FS_READ);
  359. ASSERT(req->result >= 0); /* FIXME(bnoordhuis) Check if requested size? */
  360. read_cb_count++;
  361. uv_fs_req_cleanup(req);
  362. if (read_cb_count == 1) {
  363. ASSERT(strcmp(buf, test_buf) == 0);
  364. r = uv_fs_ftruncate(loop, &ftruncate_req, open_req1.result, 7,
  365. ftruncate_cb);
  366. } else {
  367. ASSERT(strcmp(buf, "test-bu") == 0);
  368. r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
  369. }
  370. ASSERT(r == 0);
  371. }
  372. static void open_cb(uv_fs_t* req) {
  373. int r;
  374. ASSERT(req == &open_req1);
  375. ASSERT(req->fs_type == UV_FS_OPEN);
  376. if (req->result < 0) {
  377. fprintf(stderr, "async open error: %d\n", (int) req->result);
  378. ASSERT(0);
  379. }
  380. open_cb_count++;
  381. ASSERT(req->path);
  382. ASSERT(memcmp(req->path, "test_file2\0", 11) == 0);
  383. uv_fs_req_cleanup(req);
  384. memset(buf, 0, sizeof(buf));
  385. iov = uv_buf_init(buf, sizeof(buf));
  386. r = uv_fs_read(loop, &read_req, open_req1.result, &iov, 1, -1,
  387. read_cb);
  388. ASSERT(r == 0);
  389. }
  390. static void open_cb_simple(uv_fs_t* req) {
  391. ASSERT(req->fs_type == UV_FS_OPEN);
  392. if (req->result < 0) {
  393. fprintf(stderr, "async open error: %d\n", (int) req->result);
  394. ASSERT(0);
  395. }
  396. open_cb_count++;
  397. ASSERT(req->path);
  398. uv_fs_req_cleanup(req);
  399. }
  400. static void fsync_cb(uv_fs_t* req) {
  401. int r;
  402. ASSERT(req == &fsync_req);
  403. ASSERT(req->fs_type == UV_FS_FSYNC);
  404. ASSERT(req->result == 0);
  405. fsync_cb_count++;
  406. uv_fs_req_cleanup(req);
  407. r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
  408. ASSERT(r == 0);
  409. }
  410. static void fdatasync_cb(uv_fs_t* req) {
  411. int r;
  412. ASSERT(req == &fdatasync_req);
  413. ASSERT(req->fs_type == UV_FS_FDATASYNC);
  414. ASSERT(req->result == 0);
  415. fdatasync_cb_count++;
  416. uv_fs_req_cleanup(req);
  417. r = uv_fs_fsync(loop, &fsync_req, open_req1.result, fsync_cb);
  418. ASSERT(r == 0);
  419. }
  420. static void write_cb(uv_fs_t* req) {
  421. int r;
  422. ASSERT(req == &write_req);
  423. ASSERT(req->fs_type == UV_FS_WRITE);
  424. ASSERT(req->result >= 0); /* FIXME(bnoordhuis) Check if requested size? */
  425. write_cb_count++;
  426. uv_fs_req_cleanup(req);
  427. r = uv_fs_fdatasync(loop, &fdatasync_req, open_req1.result, fdatasync_cb);
  428. ASSERT(r == 0);
  429. }
  430. static void create_cb(uv_fs_t* req) {
  431. int r;
  432. ASSERT(req == &open_req1);
  433. ASSERT(req->fs_type == UV_FS_OPEN);
  434. ASSERT(req->result >= 0);
  435. create_cb_count++;
  436. uv_fs_req_cleanup(req);
  437. iov = uv_buf_init(test_buf, sizeof(test_buf));
  438. r = uv_fs_write(loop, &write_req, req->result, &iov, 1, -1, write_cb);
  439. ASSERT(r == 0);
  440. }
  441. static void rename_cb(uv_fs_t* req) {
  442. ASSERT(req == &rename_req);
  443. ASSERT(req->fs_type == UV_FS_RENAME);
  444. ASSERT(req->result == 0);
  445. rename_cb_count++;
  446. uv_fs_req_cleanup(req);
  447. }
  448. static void mkdir_cb(uv_fs_t* req) {
  449. ASSERT(req == &mkdir_req);
  450. ASSERT(req->fs_type == UV_FS_MKDIR);
  451. ASSERT(req->result == 0);
  452. mkdir_cb_count++;
  453. ASSERT(req->path);
  454. ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
  455. uv_fs_req_cleanup(req);
  456. }
  457. static void check_mkdtemp_result(uv_fs_t* req) {
  458. int r;
  459. ASSERT(req->fs_type == UV_FS_MKDTEMP);
  460. ASSERT(req->result == 0);
  461. ASSERT(req->path);
  462. ASSERT(strlen(req->path) == 15);
  463. ASSERT(memcmp(req->path, "test_dir_", 9) == 0);
  464. ASSERT(memcmp(req->path + 9, "XXXXXX", 6) != 0);
  465. check_permission(req->path, 0700);
  466. /* Check if req->path is actually a directory */
  467. r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
  468. ASSERT(r == 0);
  469. ASSERT(((uv_stat_t*)stat_req.ptr)->st_mode & S_IFDIR);
  470. uv_fs_req_cleanup(&stat_req);
  471. }
  472. static void mkdtemp_cb(uv_fs_t* req) {
  473. ASSERT(req == &mkdtemp_req1);
  474. check_mkdtemp_result(req);
  475. mkdtemp_cb_count++;
  476. }
  477. static void check_mkstemp_result(uv_fs_t* req) {
  478. int r;
  479. ASSERT(req->fs_type == UV_FS_MKSTEMP);
  480. ASSERT(req->result >= 0);
  481. ASSERT(req->path);
  482. ASSERT(strlen(req->path) == 16);
  483. ASSERT(memcmp(req->path, "test_file_", 10) == 0);
  484. ASSERT(memcmp(req->path + 10, "XXXXXX", 6) != 0);
  485. check_permission(req->path, 0600);
  486. /* Check if req->path is actually a file */
  487. r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
  488. ASSERT(r == 0);
  489. ASSERT(stat_req.statbuf.st_mode & S_IFREG);
  490. uv_fs_req_cleanup(&stat_req);
  491. }
  492. static void mkstemp_cb(uv_fs_t* req) {
  493. ASSERT(req == &mkstemp_req1);
  494. check_mkstemp_result(req);
  495. mkstemp_cb_count++;
  496. }
  497. static void rmdir_cb(uv_fs_t* req) {
  498. ASSERT(req == &rmdir_req);
  499. ASSERT(req->fs_type == UV_FS_RMDIR);
  500. ASSERT(req->result == 0);
  501. rmdir_cb_count++;
  502. ASSERT(req->path);
  503. ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
  504. uv_fs_req_cleanup(req);
  505. }
  506. static void assert_is_file_type(uv_dirent_t dent) {
  507. #ifdef HAVE_DIRENT_TYPES
  508. /*
  509. * For Apple and Windows, we know getdents is expected to work but for other
  510. * environments, the filesystem dictates whether or not getdents supports
  511. * returning the file type.
  512. *
  513. * See:
  514. * http://man7.org/linux/man-pages/man2/getdents.2.html
  515. * https://github.com/libuv/libuv/issues/501
  516. */
  517. #if defined(__APPLE__) || defined(_WIN32)
  518. ASSERT(dent.type == UV_DIRENT_FILE);
  519. #else
  520. ASSERT(dent.type == UV_DIRENT_FILE || dent.type == UV_DIRENT_UNKNOWN);
  521. #endif
  522. #else
  523. ASSERT(dent.type == UV_DIRENT_UNKNOWN);
  524. #endif
  525. }
  526. static void scandir_cb(uv_fs_t* req) {
  527. uv_dirent_t dent;
  528. ASSERT(req == &scandir_req);
  529. ASSERT(req->fs_type == UV_FS_SCANDIR);
  530. ASSERT(req->result == 2);
  531. ASSERT(req->ptr);
  532. while (UV_EOF != uv_fs_scandir_next(req, &dent)) {
  533. ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
  534. assert_is_file_type(dent);
  535. }
  536. scandir_cb_count++;
  537. ASSERT(req->path);
  538. ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
  539. uv_fs_req_cleanup(req);
  540. ASSERT(!req->ptr);
  541. }
  542. static void empty_scandir_cb(uv_fs_t* req) {
  543. uv_dirent_t dent;
  544. ASSERT(req == &scandir_req);
  545. ASSERT(req->fs_type == UV_FS_SCANDIR);
  546. ASSERT(req->result == 0);
  547. ASSERT(req->ptr == NULL);
  548. ASSERT(UV_EOF == uv_fs_scandir_next(req, &dent));
  549. uv_fs_req_cleanup(req);
  550. scandir_cb_count++;
  551. }
  552. static void non_existent_scandir_cb(uv_fs_t* req) {
  553. uv_dirent_t dent;
  554. ASSERT(req == &scandir_req);
  555. ASSERT(req->fs_type == UV_FS_SCANDIR);
  556. ASSERT(req->result == UV_ENOENT);
  557. ASSERT(req->ptr == NULL);
  558. ASSERT(UV_ENOENT == uv_fs_scandir_next(req, &dent));
  559. uv_fs_req_cleanup(req);
  560. scandir_cb_count++;
  561. }
  562. static void file_scandir_cb(uv_fs_t* req) {
  563. ASSERT(req == &scandir_req);
  564. ASSERT(req->fs_type == UV_FS_SCANDIR);
  565. ASSERT(req->result == UV_ENOTDIR);
  566. ASSERT(req->ptr == NULL);
  567. uv_fs_req_cleanup(req);
  568. scandir_cb_count++;
  569. }
  570. static void stat_cb(uv_fs_t* req) {
  571. ASSERT(req == &stat_req);
  572. ASSERT(req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_LSTAT);
  573. ASSERT(req->result == 0);
  574. ASSERT(req->ptr);
  575. stat_cb_count++;
  576. uv_fs_req_cleanup(req);
  577. ASSERT(!req->ptr);
  578. }
  579. static void sendfile_cb(uv_fs_t* req) {
  580. ASSERT(req == &sendfile_req);
  581. ASSERT(req->fs_type == UV_FS_SENDFILE);
  582. ASSERT(req->result == 65546);
  583. sendfile_cb_count++;
  584. uv_fs_req_cleanup(req);
  585. }
  586. static void sendfile_nodata_cb(uv_fs_t* req) {
  587. ASSERT(req == &sendfile_req);
  588. ASSERT(req->fs_type == UV_FS_SENDFILE);
  589. ASSERT(req->result == 0);
  590. sendfile_cb_count++;
  591. uv_fs_req_cleanup(req);
  592. }
  593. static void open_noent_cb(uv_fs_t* req) {
  594. ASSERT(req->fs_type == UV_FS_OPEN);
  595. ASSERT(req->result == UV_ENOENT);
  596. open_cb_count++;
  597. uv_fs_req_cleanup(req);
  598. }
  599. static void open_nametoolong_cb(uv_fs_t* req) {
  600. ASSERT(req->fs_type == UV_FS_OPEN);
  601. ASSERT(req->result == UV_ENAMETOOLONG);
  602. open_cb_count++;
  603. uv_fs_req_cleanup(req);
  604. }
  605. static void open_loop_cb(uv_fs_t* req) {
  606. ASSERT(req->fs_type == UV_FS_OPEN);
  607. ASSERT(req->result == UV_ELOOP);
  608. open_cb_count++;
  609. uv_fs_req_cleanup(req);
  610. }
  611. TEST_IMPL(fs_file_noent) {
  612. uv_fs_t req;
  613. int r;
  614. loop = uv_default_loop();
  615. r = uv_fs_open(NULL, &req, "does_not_exist", O_RDONLY, 0, NULL);
  616. ASSERT(r == UV_ENOENT);
  617. ASSERT(req.result == UV_ENOENT);
  618. uv_fs_req_cleanup(&req);
  619. r = uv_fs_open(loop, &req, "does_not_exist", O_RDONLY, 0, open_noent_cb);
  620. ASSERT(r == 0);
  621. ASSERT(open_cb_count == 0);
  622. uv_run(loop, UV_RUN_DEFAULT);
  623. ASSERT(open_cb_count == 1);
  624. /* TODO add EACCES test */
  625. MAKE_VALGRIND_HAPPY();
  626. return 0;
  627. }
  628. TEST_IMPL(fs_file_nametoolong) {
  629. uv_fs_t req;
  630. int r;
  631. char name[TOO_LONG_NAME_LENGTH + 1];
  632. loop = uv_default_loop();
  633. memset(name, 'a', TOO_LONG_NAME_LENGTH);
  634. name[TOO_LONG_NAME_LENGTH] = 0;
  635. r = uv_fs_open(NULL, &req, name, O_RDONLY, 0, NULL);
  636. ASSERT(r == UV_ENAMETOOLONG);
  637. ASSERT(req.result == UV_ENAMETOOLONG);
  638. uv_fs_req_cleanup(&req);
  639. r = uv_fs_open(loop, &req, name, O_RDONLY, 0, open_nametoolong_cb);
  640. ASSERT(r == 0);
  641. ASSERT(open_cb_count == 0);
  642. uv_run(loop, UV_RUN_DEFAULT);
  643. ASSERT(open_cb_count == 1);
  644. MAKE_VALGRIND_HAPPY();
  645. return 0;
  646. }
  647. TEST_IMPL(fs_file_loop) {
  648. uv_fs_t req;
  649. int r;
  650. loop = uv_default_loop();
  651. unlink("test_symlink");
  652. r = uv_fs_symlink(NULL, &req, "test_symlink", "test_symlink", 0, NULL);
  653. #ifdef _WIN32
  654. /*
  655. * Windows XP and Server 2003 don't support symlinks; we'll get UV_ENOTSUP.
  656. * Starting with vista they are supported, but only when elevated, otherwise
  657. * we'll see UV_EPERM.
  658. */
  659. if (r == UV_ENOTSUP || r == UV_EPERM)
  660. return 0;
  661. #elif defined(__MSYS__)
  662. /* MSYS2's approximation of symlinks with copies does not work for broken
  663. links. */
  664. if (r == UV_ENOENT)
  665. return 0;
  666. #endif
  667. ASSERT(r == 0);
  668. uv_fs_req_cleanup(&req);
  669. r = uv_fs_open(NULL, &req, "test_symlink", O_RDONLY, 0, NULL);
  670. ASSERT(r == UV_ELOOP);
  671. ASSERT(req.result == UV_ELOOP);
  672. uv_fs_req_cleanup(&req);
  673. r = uv_fs_open(loop, &req, "test_symlink", O_RDONLY, 0, open_loop_cb);
  674. ASSERT(r == 0);
  675. ASSERT(open_cb_count == 0);
  676. uv_run(loop, UV_RUN_DEFAULT);
  677. ASSERT(open_cb_count == 1);
  678. unlink("test_symlink");
  679. MAKE_VALGRIND_HAPPY();
  680. return 0;
  681. }
  682. static void check_utime(const char* path,
  683. double atime,
  684. double mtime,
  685. int test_lutime) {
  686. uv_stat_t* s;
  687. uv_fs_t req;
  688. int r;
  689. if (test_lutime)
  690. r = uv_fs_lstat(loop, &req, path, NULL);
  691. else
  692. r = uv_fs_stat(loop, &req, path, NULL);
  693. ASSERT(r == 0);
  694. ASSERT(req.result == 0);
  695. s = &req.statbuf;
  696. ASSERT(s->st_atim.tv_sec + (s->st_atim.tv_nsec / 1000000000.0) == atime);
  697. ASSERT(s->st_mtim.tv_sec + (s->st_mtim.tv_nsec / 1000000000.0) == mtime);
  698. uv_fs_req_cleanup(&req);
  699. }
  700. static void utime_cb(uv_fs_t* req) {
  701. utime_check_t* c;
  702. ASSERT(req == &utime_req);
  703. ASSERT(req->result == 0);
  704. ASSERT(req->fs_type == UV_FS_UTIME);
  705. c = req->data;
  706. check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 0);
  707. uv_fs_req_cleanup(req);
  708. utime_cb_count++;
  709. }
  710. static void futime_cb(uv_fs_t* req) {
  711. utime_check_t* c;
  712. ASSERT(req == &futime_req);
  713. ASSERT(req->result == 0);
  714. ASSERT(req->fs_type == UV_FS_FUTIME);
  715. c = req->data;
  716. check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 0);
  717. uv_fs_req_cleanup(req);
  718. futime_cb_count++;
  719. }
  720. static void lutime_cb(uv_fs_t* req) {
  721. utime_check_t* c;
  722. ASSERT(req->result == 0);
  723. ASSERT(req->fs_type == UV_FS_LUTIME);
  724. c = req->data;
  725. check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 1);
  726. uv_fs_req_cleanup(req);
  727. lutime_cb_count++;
  728. }
  729. TEST_IMPL(fs_file_async) {
  730. int r;
  731. /* Setup. */
  732. unlink("test_file");
  733. unlink("test_file2");
  734. loop = uv_default_loop();
  735. r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
  736. S_IRUSR | S_IWUSR, create_cb);
  737. ASSERT(r == 0);
  738. uv_run(loop, UV_RUN_DEFAULT);
  739. ASSERT(create_cb_count == 1);
  740. ASSERT(write_cb_count == 1);
  741. ASSERT(fsync_cb_count == 1);
  742. ASSERT(fdatasync_cb_count == 1);
  743. ASSERT(close_cb_count == 1);
  744. r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", rename_cb);
  745. ASSERT(r == 0);
  746. uv_run(loop, UV_RUN_DEFAULT);
  747. ASSERT(create_cb_count == 1);
  748. ASSERT(write_cb_count == 1);
  749. ASSERT(close_cb_count == 1);
  750. ASSERT(rename_cb_count == 1);
  751. r = uv_fs_open(loop, &open_req1, "test_file2", O_RDWR, 0, open_cb);
  752. ASSERT(r == 0);
  753. uv_run(loop, UV_RUN_DEFAULT);
  754. ASSERT(open_cb_count == 1);
  755. ASSERT(read_cb_count == 1);
  756. ASSERT(close_cb_count == 2);
  757. ASSERT(rename_cb_count == 1);
  758. ASSERT(create_cb_count == 1);
  759. ASSERT(write_cb_count == 1);
  760. ASSERT(ftruncate_cb_count == 1);
  761. r = uv_fs_open(loop, &open_req1, "test_file2", O_RDONLY, 0, open_cb);
  762. ASSERT(r == 0);
  763. uv_run(loop, UV_RUN_DEFAULT);
  764. ASSERT(open_cb_count == 2);
  765. ASSERT(read_cb_count == 2);
  766. ASSERT(close_cb_count == 3);
  767. ASSERT(rename_cb_count == 1);
  768. ASSERT(unlink_cb_count == 1);
  769. ASSERT(create_cb_count == 1);
  770. ASSERT(write_cb_count == 1);
  771. ASSERT(ftruncate_cb_count == 1);
  772. /* Cleanup. */
  773. unlink("test_file");
  774. unlink("test_file2");
  775. MAKE_VALGRIND_HAPPY();
  776. return 0;
  777. }
  778. static void fs_file_sync(int add_flags) {
  779. int r;
  780. /* Setup. */
  781. unlink("test_file");
  782. unlink("test_file2");
  783. loop = uv_default_loop();
  784. r = uv_fs_open(loop, &open_req1, "test_file",
  785. O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
  786. ASSERT(r >= 0);
  787. ASSERT(open_req1.result >= 0);
  788. uv_fs_req_cleanup(&open_req1);
  789. iov = uv_buf_init(test_buf, sizeof(test_buf));
  790. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  791. ASSERT(r >= 0);
  792. ASSERT(write_req.result >= 0);
  793. uv_fs_req_cleanup(&write_req);
  794. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  795. ASSERT(r == 0);
  796. ASSERT(close_req.result == 0);
  797. uv_fs_req_cleanup(&close_req);
  798. r = uv_fs_open(NULL, &open_req1, "test_file", O_RDWR | add_flags, 0, NULL);
  799. ASSERT(r >= 0);
  800. ASSERT(open_req1.result >= 0);
  801. uv_fs_req_cleanup(&open_req1);
  802. iov = uv_buf_init(buf, sizeof(buf));
  803. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  804. ASSERT(r >= 0);
  805. ASSERT(read_req.result >= 0);
  806. ASSERT(strcmp(buf, test_buf) == 0);
  807. uv_fs_req_cleanup(&read_req);
  808. r = uv_fs_ftruncate(NULL, &ftruncate_req, open_req1.result, 7, NULL);
  809. ASSERT(r == 0);
  810. ASSERT(ftruncate_req.result == 0);
  811. uv_fs_req_cleanup(&ftruncate_req);
  812. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  813. ASSERT(r == 0);
  814. ASSERT(close_req.result == 0);
  815. uv_fs_req_cleanup(&close_req);
  816. r = uv_fs_rename(NULL, &rename_req, "test_file", "test_file2", NULL);
  817. ASSERT(r == 0);
  818. ASSERT(rename_req.result == 0);
  819. uv_fs_req_cleanup(&rename_req);
  820. r = uv_fs_open(NULL, &open_req1, "test_file2", O_RDONLY | add_flags, 0,
  821. NULL);
  822. ASSERT(r >= 0);
  823. ASSERT(open_req1.result >= 0);
  824. uv_fs_req_cleanup(&open_req1);
  825. memset(buf, 0, sizeof(buf));
  826. iov = uv_buf_init(buf, sizeof(buf));
  827. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  828. ASSERT(r >= 0);
  829. ASSERT(read_req.result >= 0);
  830. ASSERT(strcmp(buf, "test-bu") == 0);
  831. uv_fs_req_cleanup(&read_req);
  832. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  833. ASSERT(r == 0);
  834. ASSERT(close_req.result == 0);
  835. uv_fs_req_cleanup(&close_req);
  836. r = uv_fs_unlink(NULL, &unlink_req, "test_file2", NULL);
  837. ASSERT(r == 0);
  838. ASSERT(unlink_req.result == 0);
  839. uv_fs_req_cleanup(&unlink_req);
  840. /* Cleanup */
  841. unlink("test_file");
  842. unlink("test_file2");
  843. }
  844. TEST_IMPL(fs_file_sync) {
  845. fs_file_sync(0);
  846. fs_file_sync(UV_FS_O_FILEMAP);
  847. MAKE_VALGRIND_HAPPY();
  848. return 0;
  849. }
  850. static void fs_file_write_null_buffer(int add_flags) {
  851. int r;
  852. /* Setup. */
  853. unlink("test_file");
  854. loop = uv_default_loop();
  855. r = uv_fs_open(NULL, &open_req1, "test_file",
  856. O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
  857. ASSERT(r >= 0);
  858. ASSERT(open_req1.result >= 0);
  859. uv_fs_req_cleanup(&open_req1);
  860. iov = uv_buf_init(NULL, 0);
  861. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  862. ASSERT(r == 0);
  863. ASSERT(write_req.result == 0);
  864. uv_fs_req_cleanup(&write_req);
  865. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  866. ASSERT(r == 0);
  867. ASSERT(close_req.result == 0);
  868. uv_fs_req_cleanup(&close_req);
  869. unlink("test_file");
  870. }
  871. TEST_IMPL(fs_file_write_null_buffer) {
  872. fs_file_write_null_buffer(0);
  873. fs_file_write_null_buffer(UV_FS_O_FILEMAP);
  874. MAKE_VALGRIND_HAPPY();
  875. return 0;
  876. }
  877. TEST_IMPL(fs_async_dir) {
  878. int r;
  879. uv_dirent_t dent;
  880. /* Setup */
  881. unlink("test_dir/file1");
  882. unlink("test_dir/file2");
  883. rmdir("test_dir");
  884. loop = uv_default_loop();
  885. r = uv_fs_mkdir(loop, &mkdir_req, "test_dir", 0755, mkdir_cb);
  886. ASSERT(r == 0);
  887. uv_run(loop, UV_RUN_DEFAULT);
  888. ASSERT(mkdir_cb_count == 1);
  889. /* Create 2 files synchronously. */
  890. r = uv_fs_open(NULL, &open_req1, "test_dir/file1", O_WRONLY | O_CREAT,
  891. S_IWUSR | S_IRUSR, NULL);
  892. ASSERT(r >= 0);
  893. uv_fs_req_cleanup(&open_req1);
  894. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  895. ASSERT(r == 0);
  896. uv_fs_req_cleanup(&close_req);
  897. r = uv_fs_open(NULL, &open_req1, "test_dir/file2", O_WRONLY | O_CREAT,
  898. S_IWUSR | S_IRUSR, NULL);
  899. ASSERT(r >= 0);
  900. uv_fs_req_cleanup(&open_req1);
  901. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  902. ASSERT(r == 0);
  903. uv_fs_req_cleanup(&close_req);
  904. r = uv_fs_scandir(loop, &scandir_req, "test_dir", 0, scandir_cb);
  905. ASSERT(r == 0);
  906. uv_run(loop, UV_RUN_DEFAULT);
  907. ASSERT(scandir_cb_count == 1);
  908. /* sync uv_fs_scandir */
  909. r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
  910. ASSERT(r == 2);
  911. ASSERT(scandir_req.result == 2);
  912. ASSERT(scandir_req.ptr);
  913. while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
  914. ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
  915. assert_is_file_type(dent);
  916. }
  917. uv_fs_req_cleanup(&scandir_req);
  918. ASSERT(!scandir_req.ptr);
  919. r = uv_fs_stat(loop, &stat_req, "test_dir", stat_cb);
  920. ASSERT(r == 0);
  921. uv_run(loop, UV_RUN_DEFAULT);
  922. r = uv_fs_stat(loop, &stat_req, "test_dir/", stat_cb);
  923. ASSERT(r == 0);
  924. uv_run(loop, UV_RUN_DEFAULT);
  925. r = uv_fs_lstat(loop, &stat_req, "test_dir", stat_cb);
  926. ASSERT(r == 0);
  927. uv_run(loop, UV_RUN_DEFAULT);
  928. r = uv_fs_lstat(loop, &stat_req, "test_dir/", stat_cb);
  929. ASSERT(r == 0);
  930. uv_run(loop, UV_RUN_DEFAULT);
  931. ASSERT(stat_cb_count == 4);
  932. r = uv_fs_unlink(loop, &unlink_req, "test_dir/file1", unlink_cb);
  933. ASSERT(r == 0);
  934. uv_run(loop, UV_RUN_DEFAULT);
  935. ASSERT(unlink_cb_count == 1);
  936. r = uv_fs_unlink(loop, &unlink_req, "test_dir/file2", unlink_cb);
  937. ASSERT(r == 0);
  938. uv_run(loop, UV_RUN_DEFAULT);
  939. ASSERT(unlink_cb_count == 2);
  940. r = uv_fs_rmdir(loop, &rmdir_req, "test_dir", rmdir_cb);
  941. ASSERT(r == 0);
  942. uv_run(loop, UV_RUN_DEFAULT);
  943. ASSERT(rmdir_cb_count == 1);
  944. /* Cleanup */
  945. unlink("test_dir/file1");
  946. unlink("test_dir/file2");
  947. rmdir("test_dir");
  948. MAKE_VALGRIND_HAPPY();
  949. return 0;
  950. }
  951. static int test_sendfile(void (*setup)(int), uv_fs_cb cb, off_t expected_size) {
  952. int f, r;
  953. struct stat s1, s2;
  954. loop = uv_default_loop();
  955. /* Setup. */
  956. unlink("test_file");
  957. unlink("test_file2");
  958. f = open("test_file", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
  959. ASSERT(f != -1);
  960. if (setup != NULL)
  961. setup(f);
  962. r = close(f);
  963. ASSERT(r == 0);
  964. /* Test starts here. */
  965. r = uv_fs_open(NULL, &open_req1, "test_file", O_RDWR, 0, NULL);
  966. ASSERT(r >= 0);
  967. ASSERT(open_req1.result >= 0);
  968. uv_fs_req_cleanup(&open_req1);
  969. r = uv_fs_open(NULL, &open_req2, "test_file2", O_WRONLY | O_CREAT,
  970. S_IWUSR | S_IRUSR, NULL);
  971. ASSERT(r >= 0);
  972. ASSERT(open_req2.result >= 0);
  973. uv_fs_req_cleanup(&open_req2);
  974. r = uv_fs_sendfile(loop, &sendfile_req, open_req2.result, open_req1.result,
  975. 0, 131072, cb);
  976. ASSERT(r == 0);
  977. uv_run(loop, UV_RUN_DEFAULT);
  978. ASSERT(sendfile_cb_count == 1);
  979. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  980. ASSERT(r == 0);
  981. uv_fs_req_cleanup(&close_req);
  982. r = uv_fs_close(NULL, &close_req, open_req2.result, NULL);
  983. ASSERT(r == 0);
  984. uv_fs_req_cleanup(&close_req);
  985. ASSERT(0 == stat("test_file", &s1));
  986. ASSERT(0 == stat("test_file2", &s2));
  987. ASSERT(s1.st_size == s2.st_size);
  988. ASSERT(s2.st_size == expected_size);
  989. /* Cleanup. */
  990. unlink("test_file");
  991. unlink("test_file2");
  992. MAKE_VALGRIND_HAPPY();
  993. return 0;
  994. }
  995. static void sendfile_setup(int f) {
  996. ASSERT(6 == write(f, "begin\n", 6));
  997. ASSERT(65542 == lseek(f, 65536, SEEK_CUR));
  998. ASSERT(4 == write(f, "end\n", 4));
  999. }
  1000. TEST_IMPL(fs_async_sendfile) {
  1001. return test_sendfile(sendfile_setup, sendfile_cb, 65546);
  1002. }
  1003. TEST_IMPL(fs_async_sendfile_nodata) {
  1004. return test_sendfile(NULL, sendfile_nodata_cb, 0);
  1005. }
  1006. TEST_IMPL(fs_mkdtemp) {
  1007. int r;
  1008. const char* path_template = "test_dir_XXXXXX";
  1009. loop = uv_default_loop();
  1010. r = uv_fs_mkdtemp(loop, &mkdtemp_req1, path_template, mkdtemp_cb);
  1011. ASSERT(r == 0);
  1012. uv_run(loop, UV_RUN_DEFAULT);
  1013. ASSERT(mkdtemp_cb_count == 1);
  1014. /* sync mkdtemp */
  1015. r = uv_fs_mkdtemp(NULL, &mkdtemp_req2, path_template, NULL);
  1016. ASSERT(r == 0);
  1017. check_mkdtemp_result(&mkdtemp_req2);
  1018. /* mkdtemp return different values on subsequent calls */
  1019. ASSERT(strcmp(mkdtemp_req1.path, mkdtemp_req2.path) != 0);
  1020. /* Cleanup */
  1021. rmdir(mkdtemp_req1.path);
  1022. rmdir(mkdtemp_req2.path);
  1023. uv_fs_req_cleanup(&mkdtemp_req1);
  1024. uv_fs_req_cleanup(&mkdtemp_req2);
  1025. MAKE_VALGRIND_HAPPY();
  1026. return 0;
  1027. }
  1028. TEST_IMPL(fs_mkstemp) {
  1029. int r;
  1030. int fd;
  1031. const char path_template[] = "test_file_XXXXXX";
  1032. uv_fs_t req;
  1033. loop = uv_default_loop();
  1034. r = uv_fs_mkstemp(loop, &mkstemp_req1, path_template, mkstemp_cb);
  1035. ASSERT(r == 0);
  1036. uv_run(loop, UV_RUN_DEFAULT);
  1037. ASSERT(mkstemp_cb_count == 1);
  1038. /* sync mkstemp */
  1039. r = uv_fs_mkstemp(NULL, &mkstemp_req2, path_template, NULL);
  1040. ASSERT(r >= 0);
  1041. check_mkstemp_result(&mkstemp_req2);
  1042. /* mkstemp return different values on subsequent calls */
  1043. ASSERT(strcmp(mkstemp_req1.path, mkstemp_req2.path) != 0);
  1044. /* invalid template returns EINVAL */
  1045. ASSERT(uv_fs_mkstemp(NULL, &mkstemp_req3, "test_file", NULL) == UV_EINVAL);
  1046. /* We can write to the opened file */
  1047. iov = uv_buf_init(test_buf, sizeof(test_buf));
  1048. r = uv_fs_write(NULL, &req, mkstemp_req1.result, &iov, 1, -1, NULL);
  1049. ASSERT(r == sizeof(test_buf));
  1050. ASSERT(req.result == sizeof(test_buf));
  1051. uv_fs_req_cleanup(&req);
  1052. /* Cleanup */
  1053. uv_fs_close(NULL, &req, mkstemp_req1.result, NULL);
  1054. uv_fs_req_cleanup(&req);
  1055. uv_fs_close(NULL, &req, mkstemp_req2.result, NULL);
  1056. uv_fs_req_cleanup(&req);
  1057. fd = uv_fs_open(NULL, &req, mkstemp_req1.path , O_RDONLY, 0, NULL);
  1058. ASSERT(fd >= 0);
  1059. uv_fs_req_cleanup(&req);
  1060. memset(buf, 0, sizeof(buf));
  1061. iov = uv_buf_init(buf, sizeof(buf));
  1062. r = uv_fs_read(NULL, &req, fd, &iov, 1, -1, NULL);
  1063. ASSERT(r >= 0);
  1064. ASSERT(req.result >= 0);
  1065. ASSERT(strcmp(buf, test_buf) == 0);
  1066. uv_fs_req_cleanup(&req);
  1067. uv_fs_close(NULL, &req, fd, NULL);
  1068. uv_fs_req_cleanup(&req);
  1069. unlink(mkstemp_req1.path);
  1070. unlink(mkstemp_req2.path);
  1071. uv_fs_req_cleanup(&mkstemp_req1);
  1072. uv_fs_req_cleanup(&mkstemp_req2);
  1073. MAKE_VALGRIND_HAPPY();
  1074. return 0;
  1075. }
  1076. TEST_IMPL(fs_fstat) {
  1077. int r;
  1078. uv_fs_t req;
  1079. uv_file file;
  1080. uv_stat_t* s;
  1081. #ifndef _WIN32
  1082. struct stat t;
  1083. #endif
  1084. /* Setup. */
  1085. unlink("test_file");
  1086. loop = uv_default_loop();
  1087. r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
  1088. S_IWUSR | S_IRUSR, NULL);
  1089. ASSERT(r >= 0);
  1090. ASSERT(req.result >= 0);
  1091. file = req.result;
  1092. uv_fs_req_cleanup(&req);
  1093. #ifndef _WIN32
  1094. ASSERT(0 == fstat(file, &t));
  1095. ASSERT(0 == uv_fs_fstat(NULL, &req, file, NULL));
  1096. ASSERT(req.result == 0);
  1097. s = req.ptr;
  1098. # if defined(__APPLE__)
  1099. ASSERT(s->st_birthtim.tv_sec == t.st_birthtimespec.tv_sec);
  1100. ASSERT(s->st_birthtim.tv_nsec == t.st_birthtimespec.tv_nsec);
  1101. # elif defined(__linux__)
  1102. /* If statx() is supported, the birth time should be equal to the change time
  1103. * because we just created the file. On older kernels, it's set to zero.
  1104. */
  1105. ASSERT(s->st_birthtim.tv_sec == 0 ||
  1106. s->st_birthtim.tv_sec == t.st_ctim.tv_sec);
  1107. ASSERT(s->st_birthtim.tv_nsec == 0 ||
  1108. s->st_birthtim.tv_nsec == t.st_ctim.tv_nsec);
  1109. # endif
  1110. #endif
  1111. iov = uv_buf_init(test_buf, sizeof(test_buf));
  1112. r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
  1113. ASSERT(r == sizeof(test_buf));
  1114. ASSERT(req.result == sizeof(test_buf));
  1115. uv_fs_req_cleanup(&req);
  1116. memset(&req.statbuf, 0xaa, sizeof(req.statbuf));
  1117. r = uv_fs_fstat(NULL, &req, file, NULL);
  1118. ASSERT(r == 0);
  1119. ASSERT(req.result == 0);
  1120. s = req.ptr;
  1121. ASSERT(s->st_size == sizeof(test_buf));
  1122. #ifndef _WIN32
  1123. r = fstat(file, &t);
  1124. ASSERT(r == 0);
  1125. ASSERT(s->st_dev == (uint64_t) t.st_dev);
  1126. ASSERT(s->st_mode == (uint64_t) t.st_mode);
  1127. ASSERT(s->st_nlink == (uint64_t) t.st_nlink);
  1128. ASSERT(s->st_uid == (uint64_t) t.st_uid);
  1129. ASSERT(s->st_gid == (uint64_t) t.st_gid);
  1130. ASSERT(s->st_rdev == (uint64_t) t.st_rdev);
  1131. ASSERT(s->st_ino == (uint64_t) t.st_ino);
  1132. ASSERT(s->st_size == (uint64_t) t.st_size);
  1133. ASSERT(s->st_blksize == (uint64_t) t.st_blksize);
  1134. ASSERT(s->st_blocks == (uint64_t) t.st_blocks);
  1135. #if defined(__APPLE__)
  1136. ASSERT(s->st_atim.tv_sec == t.st_atimespec.tv_sec);
  1137. ASSERT(s->st_atim.tv_nsec == t.st_atimespec.tv_nsec);
  1138. ASSERT(s->st_mtim.tv_sec == t.st_mtimespec.tv_sec);
  1139. ASSERT(s->st_mtim.tv_nsec == t.st_mtimespec.tv_nsec);
  1140. ASSERT(s->st_ctim.tv_sec == t.st_ctimespec.tv_sec);
  1141. ASSERT(s->st_ctim.tv_nsec == t.st_ctimespec.tv_nsec);
  1142. #elif defined(_AIX)
  1143. ASSERT(s->st_atim.tv_sec == t.st_atime);
  1144. ASSERT(s->st_atim.tv_nsec == 0);
  1145. ASSERT(s->st_mtim.tv_sec == t.st_mtime);
  1146. ASSERT(s->st_mtim.tv_nsec == 0);
  1147. ASSERT(s->st_ctim.tv_sec == t.st_ctime);
  1148. ASSERT(s->st_ctim.tv_nsec == 0);
  1149. #elif defined(__ANDROID__)
  1150. ASSERT(s->st_atim.tv_sec == t.st_atime);
  1151. ASSERT(s->st_atim.tv_nsec == t.st_atimensec);
  1152. ASSERT(s->st_mtim.tv_sec == t.st_mtime);
  1153. ASSERT(s->st_mtim.tv_nsec == t.st_mtimensec);
  1154. ASSERT(s->st_ctim.tv_sec == t.st_ctime);
  1155. ASSERT(s->st_ctim.tv_nsec == t.st_ctimensec);
  1156. #elif defined(__sun) || \
  1157. defined(__DragonFly__) || \
  1158. defined(__FreeBSD__) || \
  1159. defined(__OpenBSD__) || \
  1160. defined(__NetBSD__) || \
  1161. defined(_GNU_SOURCE) || \
  1162. defined(_BSD_SOURCE) || \
  1163. defined(_SVID_SOURCE) || \
  1164. defined(_XOPEN_SOURCE) || \
  1165. defined(_DEFAULT_SOURCE)
  1166. ASSERT(s->st_atim.tv_sec == t.st_atim.tv_sec);
  1167. ASSERT(s->st_atim.tv_nsec == t.st_atim.tv_nsec);
  1168. ASSERT(s->st_mtim.tv_sec == t.st_mtim.tv_sec);
  1169. ASSERT(s->st_mtim.tv_nsec == t.st_mtim.tv_nsec);
  1170. ASSERT(s->st_ctim.tv_sec == t.st_ctim.tv_sec);
  1171. ASSERT(s->st_ctim.tv_nsec == t.st_ctim.tv_nsec);
  1172. # if defined(__FreeBSD__) || \
  1173. defined(__NetBSD__)
  1174. ASSERT(s->st_birthtim.tv_sec == t.st_birthtim.tv_sec);
  1175. ASSERT(s->st_birthtim.tv_nsec == t.st_birthtim.tv_nsec);
  1176. # endif
  1177. #else
  1178. ASSERT(s->st_atim.tv_sec == t.st_atime);
  1179. ASSERT(s->st_atim.tv_nsec == 0);
  1180. ASSERT(s->st_mtim.tv_sec == t.st_mtime);
  1181. ASSERT(s->st_mtim.tv_nsec == 0);
  1182. ASSERT(s->st_ctim.tv_sec == t.st_ctime);
  1183. ASSERT(s->st_ctim.tv_nsec == 0);
  1184. #endif
  1185. #endif
  1186. #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
  1187. ASSERT(s->st_flags == t.st_flags);
  1188. ASSERT(s->st_gen == t.st_gen);
  1189. #else
  1190. ASSERT(s->st_flags == 0);
  1191. ASSERT(s->st_gen == 0);
  1192. #endif
  1193. uv_fs_req_cleanup(&req);
  1194. /* Now do the uv_fs_fstat call asynchronously */
  1195. r = uv_fs_fstat(loop, &req, file, fstat_cb);
  1196. ASSERT(r == 0);
  1197. uv_run(loop, UV_RUN_DEFAULT);
  1198. ASSERT(fstat_cb_count == 1);
  1199. r = uv_fs_close(NULL, &req, file, NULL);
  1200. ASSERT(r == 0);
  1201. ASSERT(req.result == 0);
  1202. uv_fs_req_cleanup(&req);
  1203. /*
  1204. * Run the loop just to check we don't have make any extraneous uv_ref()
  1205. * calls. This should drop out immediately.
  1206. */
  1207. uv_run(loop, UV_RUN_DEFAULT);
  1208. /* Cleanup. */
  1209. unlink("test_file");
  1210. MAKE_VALGRIND_HAPPY();
  1211. return 0;
  1212. }
  1213. TEST_IMPL(fs_access) {
  1214. int r;
  1215. uv_fs_t req;
  1216. uv_file file;
  1217. /* Setup. */
  1218. unlink("test_file");
  1219. rmdir("test_dir");
  1220. loop = uv_default_loop();
  1221. /* File should not exist */
  1222. r = uv_fs_access(NULL, &req, "test_file", F_OK, NULL);
  1223. ASSERT(r < 0);
  1224. ASSERT(req.result < 0);
  1225. uv_fs_req_cleanup(&req);
  1226. /* File should not exist */
  1227. r = uv_fs_access(loop, &req, "test_file", F_OK, access_cb);
  1228. ASSERT(r == 0);
  1229. uv_run(loop, UV_RUN_DEFAULT);
  1230. ASSERT(access_cb_count == 1);
  1231. access_cb_count = 0; /* reset for the next test */
  1232. /* Create file */
  1233. r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
  1234. S_IWUSR | S_IRUSR, NULL);
  1235. ASSERT(r >= 0);
  1236. ASSERT(req.result >= 0);
  1237. file = req.result;
  1238. uv_fs_req_cleanup(&req);
  1239. /* File should exist */
  1240. r = uv_fs_access(NULL, &req, "test_file", F_OK, NULL);
  1241. ASSERT(r == 0);
  1242. ASSERT(req.result == 0);
  1243. uv_fs_req_cleanup(&req);
  1244. /* File should exist */
  1245. r = uv_fs_access(loop, &req, "test_file", F_OK, access_cb);
  1246. ASSERT(r == 0);
  1247. uv_run(loop, UV_RUN_DEFAULT);
  1248. ASSERT(access_cb_count == 1);
  1249. access_cb_count = 0; /* reset for the next test */
  1250. /* Close file */
  1251. r = uv_fs_close(NULL, &req, file, NULL);
  1252. ASSERT(r == 0);
  1253. ASSERT(req.result == 0);
  1254. uv_fs_req_cleanup(&req);
  1255. /* Directory access */
  1256. r = uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
  1257. ASSERT(r == 0);
  1258. uv_fs_req_cleanup(&req);
  1259. r = uv_fs_access(NULL, &req, "test_dir", W_OK, NULL);
  1260. ASSERT(r == 0);
  1261. ASSERT(req.result == 0);
  1262. uv_fs_req_cleanup(&req);
  1263. /*
  1264. * Run the loop just to check we don't have make any extraneous uv_ref()
  1265. * calls. This should drop out immediately.
  1266. */
  1267. uv_run(loop, UV_RUN_DEFAULT);
  1268. /* Cleanup. */
  1269. unlink("test_file");
  1270. rmdir("test_dir");
  1271. MAKE_VALGRIND_HAPPY();
  1272. return 0;
  1273. }
  1274. TEST_IMPL(fs_chmod) {
  1275. int r;
  1276. uv_fs_t req;
  1277. uv_file file;
  1278. /* Setup. */
  1279. unlink("test_file");
  1280. loop = uv_default_loop();
  1281. r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
  1282. S_IWUSR | S_IRUSR, NULL);
  1283. ASSERT(r >= 0);
  1284. ASSERT(req.result >= 0);
  1285. file = req.result;
  1286. uv_fs_req_cleanup(&req);
  1287. iov = uv_buf_init(test_buf, sizeof(test_buf));
  1288. r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
  1289. ASSERT(r == sizeof(test_buf));
  1290. ASSERT(req.result == sizeof(test_buf));
  1291. uv_fs_req_cleanup(&req);
  1292. #ifndef _WIN32
  1293. /* Make the file write-only */
  1294. r = uv_fs_chmod(NULL, &req, "test_file", 0200, NULL);
  1295. ASSERT(r == 0);
  1296. ASSERT(req.result == 0);
  1297. uv_fs_req_cleanup(&req);
  1298. check_permission("test_file", 0200);
  1299. #endif
  1300. /* Make the file read-only */
  1301. r = uv_fs_chmod(NULL, &req, "test_file", 0400, NULL);
  1302. ASSERT(r == 0);
  1303. ASSERT(req.result == 0);
  1304. uv_fs_req_cleanup(&req);
  1305. check_permission("test_file", 0400);
  1306. /* Make the file read+write with sync uv_fs_fchmod */
  1307. r = uv_fs_fchmod(NULL, &req, file, 0600, NULL);
  1308. ASSERT(r == 0);
  1309. ASSERT(req.result == 0);
  1310. uv_fs_req_cleanup(&req);
  1311. check_permission("test_file", 0600);
  1312. #ifndef _WIN32
  1313. /* async chmod */
  1314. {
  1315. static int mode = 0200;
  1316. req.data = &mode;
  1317. }
  1318. r = uv_fs_chmod(loop, &req, "test_file", 0200, chmod_cb);
  1319. ASSERT(r == 0);
  1320. uv_run(loop, UV_RUN_DEFAULT);
  1321. ASSERT(chmod_cb_count == 1);
  1322. chmod_cb_count = 0; /* reset for the next test */
  1323. #endif
  1324. /* async chmod */
  1325. {
  1326. static int mode = 0400;
  1327. req.data = &mode;
  1328. }
  1329. r = uv_fs_chmod(loop, &req, "test_file", 0400, chmod_cb);
  1330. ASSERT(r == 0);
  1331. uv_run(loop, UV_RUN_DEFAULT);
  1332. ASSERT(chmod_cb_count == 1);
  1333. /* async fchmod */
  1334. {
  1335. static int mode = 0600;
  1336. req.data = &mode;
  1337. }
  1338. r = uv_fs_fchmod(loop, &req, file, 0600, fchmod_cb);
  1339. ASSERT(r == 0);
  1340. uv_run(loop, UV_RUN_DEFAULT);
  1341. ASSERT(fchmod_cb_count == 1);
  1342. uv_fs_close(loop, &req, file, NULL);
  1343. /*
  1344. * Run the loop just to check we don't have make any extraneous uv_ref()
  1345. * calls. This should drop out immediately.
  1346. */
  1347. uv_run(loop, UV_RUN_DEFAULT);
  1348. /* Cleanup. */
  1349. unlink("test_file");
  1350. MAKE_VALGRIND_HAPPY();
  1351. return 0;
  1352. }
  1353. TEST_IMPL(fs_unlink_readonly) {
  1354. int r;
  1355. uv_fs_t req;
  1356. uv_file file;
  1357. /* Setup. */
  1358. unlink("test_file");
  1359. loop = uv_default_loop();
  1360. r = uv_fs_open(NULL,
  1361. &req,
  1362. "test_file",
  1363. O_RDWR | O_CREAT,
  1364. S_IWUSR | S_IRUSR,
  1365. NULL);
  1366. ASSERT(r >= 0);
  1367. ASSERT(req.result >= 0);
  1368. file = req.result;
  1369. uv_fs_req_cleanup(&req);
  1370. iov = uv_buf_init(test_buf, sizeof(test_buf));
  1371. r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
  1372. ASSERT(r == sizeof(test_buf));
  1373. ASSERT(req.result == sizeof(test_buf));
  1374. uv_fs_req_cleanup(&req);
  1375. uv_fs_close(loop, &req, file, NULL);
  1376. /* Make the file read-only */
  1377. r = uv_fs_chmod(NULL, &req, "test_file", 0400, NULL);
  1378. ASSERT(r == 0);
  1379. ASSERT(req.result == 0);
  1380. uv_fs_req_cleanup(&req);
  1381. check_permission("test_file", 0400);
  1382. /* Try to unlink the file */
  1383. r = uv_fs_unlink(NULL, &req, "test_file", NULL);
  1384. ASSERT(r == 0);
  1385. ASSERT(req.result == 0);
  1386. uv_fs_req_cleanup(&req);
  1387. /*
  1388. * Run the loop just to check we don't have make any extraneous uv_ref()
  1389. * calls. This should drop out immediately.
  1390. */
  1391. uv_run(loop, UV_RUN_DEFAULT);
  1392. /* Cleanup. */
  1393. uv_fs_chmod(NULL, &req, "test_file", 0600, NULL);
  1394. uv_fs_req_cleanup(&req);
  1395. unlink("test_file");
  1396. MAKE_VALGRIND_HAPPY();
  1397. return 0;
  1398. }
  1399. #ifdef _WIN32
  1400. TEST_IMPL(fs_unlink_archive_readonly) {
  1401. int r;
  1402. uv_fs_t req;
  1403. uv_file file;
  1404. /* Setup. */
  1405. unlink("test_file");
  1406. loop = uv_default_loop();
  1407. r = uv_fs_open(NULL,
  1408. &req,
  1409. "test_file",
  1410. O_RDWR | O_CREAT,
  1411. S_IWUSR | S_IRUSR,
  1412. NULL);
  1413. ASSERT(r >= 0);
  1414. ASSERT(req.result >= 0);
  1415. file = req.result;
  1416. uv_fs_req_cleanup(&req);
  1417. iov = uv_buf_init(test_buf, sizeof(test_buf));
  1418. r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
  1419. ASSERT(r == sizeof(test_buf));
  1420. ASSERT(req.result == sizeof(test_buf));
  1421. uv_fs_req_cleanup(&req);
  1422. uv_fs_close(loop, &req, file, NULL);
  1423. /* Make the file read-only and clear archive flag */
  1424. r = SetFileAttributes("test_file", FILE_ATTRIBUTE_READONLY);
  1425. ASSERT(r != 0);
  1426. uv_fs_req_cleanup(&req);
  1427. check_permission("test_file", 0400);
  1428. /* Try to unlink the file */
  1429. r = uv_fs_unlink(NULL, &req, "test_file", NULL);
  1430. ASSERT(r == 0);
  1431. ASSERT(req.result == 0);
  1432. uv_fs_req_cleanup(&req);
  1433. /*
  1434. * Run the loop just to check we don't have make any extraneous uv_ref()
  1435. * calls. This should drop out immediately.
  1436. */
  1437. uv_run(loop, UV_RUN_DEFAULT);
  1438. /* Cleanup. */
  1439. uv_fs_chmod(NULL, &req, "test_file", 0600, NULL);
  1440. uv_fs_req_cleanup(&req);
  1441. unlink("test_file");
  1442. MAKE_VALGRIND_HAPPY();
  1443. return 0;
  1444. }
  1445. #endif
  1446. TEST_IMPL(fs_chown) {
  1447. int r;
  1448. uv_fs_t req;
  1449. uv_file file;
  1450. /* Setup. */
  1451. unlink("test_file");
  1452. unlink("test_file_link");
  1453. loop = uv_default_loop();
  1454. r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
  1455. S_IWUSR | S_IRUSR, NULL);
  1456. ASSERT(r >= 0);
  1457. ASSERT(req.result >= 0);
  1458. file = req.result;
  1459. uv_fs_req_cleanup(&req);
  1460. /* sync chown */
  1461. r = uv_fs_chown(NULL, &req, "test_file", -1, -1, NULL);
  1462. ASSERT(r == 0);
  1463. ASSERT(req.result == 0);
  1464. uv_fs_req_cleanup(&req);
  1465. /* sync fchown */
  1466. r = uv_fs_fchown(NULL, &req, file, -1, -1, NULL);
  1467. ASSERT(r == 0);
  1468. ASSERT(req.result == 0);
  1469. uv_fs_req_cleanup(&req);
  1470. /* async chown */
  1471. r = uv_fs_chown(loop, &req, "test_file", -1, -1, chown_cb);
  1472. ASSERT(r == 0);
  1473. uv_run(loop, UV_RUN_DEFAULT);
  1474. ASSERT(chown_cb_count == 1);
  1475. #ifndef __MVS__
  1476. /* chown to root (fail) */
  1477. chown_cb_count = 0;
  1478. r = uv_fs_chown(loop, &req, "test_file", 0, 0, chown_root_cb);
  1479. ASSERT(r == 0);
  1480. uv_run(loop, UV_RUN_DEFAULT);
  1481. ASSERT(chown_cb_count == 1);
  1482. #endif
  1483. /* async fchown */
  1484. r = uv_fs_fchown(loop, &req, file, -1, -1, fchown_cb);
  1485. ASSERT(r == 0);
  1486. uv_run(loop, UV_RUN_DEFAULT);
  1487. ASSERT(fchown_cb_count == 1);
  1488. #ifndef __HAIKU__
  1489. /* Haiku doesn't support hardlink */
  1490. /* sync link */
  1491. r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
  1492. ASSERT(r == 0);
  1493. ASSERT(req.result == 0);
  1494. uv_fs_req_cleanup(&req);
  1495. /* sync lchown */
  1496. r = uv_fs_lchown(NULL, &req, "test_file_link", -1, -1, NULL);
  1497. ASSERT(r == 0);
  1498. ASSERT(req.result == 0);
  1499. uv_fs_req_cleanup(&req);
  1500. /* async lchown */
  1501. r = uv_fs_lchown(loop, &req, "test_file_link", -1, -1, lchown_cb);
  1502. ASSERT(r == 0);
  1503. uv_run(loop, UV_RUN_DEFAULT);
  1504. ASSERT(lchown_cb_count == 1);
  1505. #endif
  1506. /* Close file */
  1507. r = uv_fs_close(NULL, &req, file, NULL);
  1508. ASSERT(r == 0);
  1509. ASSERT(req.result == 0);
  1510. uv_fs_req_cleanup(&req);
  1511. /*
  1512. * Run the loop just to check we don't have make any extraneous uv_ref()
  1513. * calls. This should drop out immediately.
  1514. */
  1515. uv_run(loop, UV_RUN_DEFAULT);
  1516. /* Cleanup. */
  1517. unlink("test_file");
  1518. unlink("test_file_link");
  1519. MAKE_VALGRIND_HAPPY();
  1520. return 0;
  1521. }
  1522. TEST_IMPL(fs_link) {
  1523. int r;
  1524. uv_fs_t req;
  1525. uv_file file;
  1526. uv_file link;
  1527. /* Setup. */
  1528. unlink("test_file");
  1529. unlink("test_file_link");
  1530. unlink("test_file_link2");
  1531. loop = uv_default_loop();
  1532. r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
  1533. S_IWUSR | S_IRUSR, NULL);
  1534. ASSERT(r >= 0);
  1535. ASSERT(req.result >= 0);
  1536. file = req.result;
  1537. uv_fs_req_cleanup(&req);
  1538. iov = uv_buf_init(test_buf, sizeof(test_buf));
  1539. r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
  1540. ASSERT(r == sizeof(test_buf));
  1541. ASSERT(req.result == sizeof(test_buf));
  1542. uv_fs_req_cleanup(&req);
  1543. uv_fs_close(loop, &req, file, NULL);
  1544. /* sync link */
  1545. r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
  1546. ASSERT(r == 0);
  1547. ASSERT(req.result == 0);
  1548. uv_fs_req_cleanup(&req);
  1549. r = uv_fs_open(NULL, &req, "test_file_link", O_RDWR, 0, NULL);
  1550. ASSERT(r >= 0);
  1551. ASSERT(req.result >= 0);
  1552. link = req.result;
  1553. uv_fs_req_cleanup(&req);
  1554. memset(buf, 0, sizeof(buf));
  1555. iov = uv_buf_init(buf, sizeof(buf));
  1556. r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
  1557. ASSERT(r >= 0);
  1558. ASSERT(req.result >= 0);
  1559. ASSERT(strcmp(buf, test_buf) == 0);
  1560. close(link);
  1561. /* async link */
  1562. r = uv_fs_link(loop, &req, "test_file", "test_file_link2", link_cb);
  1563. ASSERT(r == 0);
  1564. uv_run(loop, UV_RUN_DEFAULT);
  1565. ASSERT(link_cb_count == 1);
  1566. r = uv_fs_open(NULL, &req, "test_file_link2", O_RDWR, 0, NULL);
  1567. ASSERT(r >= 0);
  1568. ASSERT(req.result >= 0);
  1569. link = req.result;
  1570. uv_fs_req_cleanup(&req);
  1571. memset(buf, 0, sizeof(buf));
  1572. iov = uv_buf_init(buf, sizeof(buf));
  1573. r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
  1574. ASSERT(r >= 0);
  1575. ASSERT(req.result >= 0);
  1576. ASSERT(strcmp(buf, test_buf) == 0);
  1577. uv_fs_close(loop, &req, link, NULL);
  1578. /*
  1579. * Run the loop just to check we don't have make any extraneous uv_ref()
  1580. * calls. This should drop out immediately.
  1581. */
  1582. uv_run(loop, UV_RUN_DEFAULT);
  1583. /* Cleanup. */
  1584. unlink("test_file");
  1585. unlink("test_file_link");
  1586. unlink("test_file_link2");
  1587. MAKE_VALGRIND_HAPPY();
  1588. return 0;
  1589. }
  1590. TEST_IMPL(fs_readlink) {
  1591. uv_fs_t req;
  1592. loop = uv_default_loop();
  1593. ASSERT(0 == uv_fs_readlink(loop, &req, "no_such_file", dummy_cb));
  1594. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  1595. ASSERT(dummy_cb_count == 1);
  1596. ASSERT(req.ptr == NULL);
  1597. ASSERT(req.result == UV_ENOENT);
  1598. uv_fs_req_cleanup(&req);
  1599. ASSERT(UV_ENOENT == uv_fs_readlink(NULL, &req, "no_such_file", NULL));
  1600. ASSERT(req.ptr == NULL);
  1601. ASSERT(req.result == UV_ENOENT);
  1602. uv_fs_req_cleanup(&req);
  1603. MAKE_VALGRIND_HAPPY();
  1604. return 0;
  1605. }
  1606. TEST_IMPL(fs_realpath) {
  1607. uv_fs_t req;
  1608. loop = uv_default_loop();
  1609. ASSERT(0 == uv_fs_realpath(loop, &req, "no_such_file", dummy_cb));
  1610. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  1611. ASSERT(dummy_cb_count == 1);
  1612. ASSERT(req.ptr == NULL);
  1613. #ifdef _WIN32
  1614. /*
  1615. * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
  1616. */
  1617. if (req.result == UV_ENOSYS) {
  1618. uv_fs_req_cleanup(&req);
  1619. RETURN_SKIP("realpath is not supported on Windows XP");
  1620. }
  1621. #endif
  1622. ASSERT(req.result == UV_ENOENT);
  1623. uv_fs_req_cleanup(&req);
  1624. ASSERT(UV_ENOENT == uv_fs_realpath(NULL, &req, "no_such_file", NULL));
  1625. ASSERT(req.ptr == NULL);
  1626. ASSERT(req.result == UV_ENOENT);
  1627. uv_fs_req_cleanup(&req);
  1628. MAKE_VALGRIND_HAPPY();
  1629. return 0;
  1630. }
  1631. TEST_IMPL(fs_symlink) {
  1632. int r;
  1633. uv_fs_t req;
  1634. uv_file file;
  1635. uv_file link;
  1636. char test_file_abs_buf[PATHMAX];
  1637. size_t test_file_abs_size;
  1638. /* Setup. */
  1639. unlink("test_file");
  1640. unlink("test_file_symlink");
  1641. unlink("test_file_symlink2");
  1642. unlink("test_file_symlink_symlink");
  1643. unlink("test_file_symlink2_symlink");
  1644. test_file_abs_size = sizeof(test_file_abs_buf);
  1645. #ifdef _WIN32
  1646. uv_cwd(test_file_abs_buf, &test_file_abs_size);
  1647. strcat(test_file_abs_buf, "\\test_file");
  1648. #else
  1649. uv_cwd(test_file_abs_buf, &test_file_abs_size);
  1650. strcat(test_file_abs_buf, "/test_file");
  1651. #endif
  1652. loop = uv_default_loop();
  1653. r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
  1654. S_IWUSR | S_IRUSR, NULL);
  1655. ASSERT(r >= 0);
  1656. ASSERT(req.result >= 0);
  1657. file = req.result;
  1658. uv_fs_req_cleanup(&req);
  1659. iov = uv_buf_init(test_buf, sizeof(test_buf));
  1660. r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
  1661. ASSERT(r == sizeof(test_buf));
  1662. ASSERT(req.result == sizeof(test_buf));
  1663. uv_fs_req_cleanup(&req);
  1664. uv_fs_close(loop, &req, file, NULL);
  1665. /* sync symlink */
  1666. r = uv_fs_symlink(NULL, &req, "test_file", "test_file_symlink", 0, NULL);
  1667. #ifdef _WIN32
  1668. if (r < 0) {
  1669. if (r == UV_ENOTSUP) {
  1670. /*
  1671. * Windows doesn't support symlinks on older versions.
  1672. * We just pass the test and bail out early if we get ENOTSUP.
  1673. */
  1674. return 0;
  1675. } else if (r == UV_EPERM) {
  1676. /*
  1677. * Creating a symlink is only allowed when running elevated.
  1678. * We pass the test and bail out early if we get UV_EPERM.
  1679. */
  1680. return 0;
  1681. }
  1682. }
  1683. #endif
  1684. ASSERT(r == 0);
  1685. ASSERT(req.result == 0);
  1686. uv_fs_req_cleanup(&req);
  1687. r = uv_fs_open(NULL, &req, "test_file_symlink", O_RDWR, 0, NULL);
  1688. ASSERT(r >= 0);
  1689. ASSERT(req.result >= 0);
  1690. link = req.result;
  1691. uv_fs_req_cleanup(&req);
  1692. memset(buf, 0, sizeof(buf));
  1693. iov = uv_buf_init(buf, sizeof(buf));
  1694. r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
  1695. ASSERT(r >= 0);
  1696. ASSERT(req.result >= 0);
  1697. ASSERT(strcmp(buf, test_buf) == 0);
  1698. uv_fs_close(loop, &req, link, NULL);
  1699. r = uv_fs_symlink(NULL,
  1700. &req,
  1701. "test_file_symlink",
  1702. "test_file_symlink_symlink",
  1703. 0,
  1704. NULL);
  1705. ASSERT(r == 0);
  1706. uv_fs_req_cleanup(&req);
  1707. #if defined(__MSYS__)
  1708. RETURN_SKIP("symlink reading is not supported on MSYS2");
  1709. #endif
  1710. r = uv_fs_readlink(NULL, &req, "test_file_symlink_symlink", NULL);
  1711. ASSERT(r == 0);
  1712. ASSERT(strcmp(req.ptr, "test_file_symlink") == 0);
  1713. uv_fs_req_cleanup(&req);
  1714. r = uv_fs_realpath(NULL, &req, "test_file_symlink_symlink", NULL);
  1715. #ifdef _WIN32
  1716. /*
  1717. * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
  1718. */
  1719. if (r == UV_ENOSYS) {
  1720. uv_fs_req_cleanup(&req);
  1721. RETURN_SKIP("realpath is not supported on Windows XP");
  1722. }
  1723. #endif
  1724. ASSERT(r == 0);
  1725. #ifdef _WIN32
  1726. ASSERT(stricmp(req.ptr, test_file_abs_buf) == 0);
  1727. #else
  1728. ASSERT(strcmp(req.ptr, test_file_abs_buf) == 0);
  1729. #endif
  1730. uv_fs_req_cleanup(&req);
  1731. /* async link */
  1732. r = uv_fs_symlink(loop,
  1733. &req,
  1734. "test_file",
  1735. "test_file_symlink2",
  1736. 0,
  1737. symlink_cb);
  1738. ASSERT(r == 0);
  1739. uv_run(loop, UV_RUN_DEFAULT);
  1740. ASSERT(symlink_cb_count == 1);
  1741. r = uv_fs_open(NULL, &req, "test_file_symlink2", O_RDWR, 0, NULL);
  1742. ASSERT(r >= 0);
  1743. ASSERT(req.result >= 0);
  1744. link = req.result;
  1745. uv_fs_req_cleanup(&req);
  1746. memset(buf, 0, sizeof(buf));
  1747. iov = uv_buf_init(buf, sizeof(buf));
  1748. r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
  1749. ASSERT(r >= 0);
  1750. ASSERT(req.result >= 0);
  1751. ASSERT(strcmp(buf, test_buf) == 0);
  1752. uv_fs_close(loop, &req, link, NULL);
  1753. r = uv_fs_symlink(NULL,
  1754. &req,
  1755. "test_file_symlink2",
  1756. "test_file_symlink2_symlink",
  1757. 0,
  1758. NULL);
  1759. ASSERT(r == 0);
  1760. uv_fs_req_cleanup(&req);
  1761. r = uv_fs_readlink(loop, &req, "test_file_symlink2_symlink", readlink_cb);
  1762. ASSERT(r == 0);
  1763. uv_run(loop, UV_RUN_DEFAULT);
  1764. ASSERT(readlink_cb_count == 1);
  1765. r = uv_fs_realpath(loop, &req, "test_file", realpath_cb);
  1766. #ifdef _WIN32
  1767. /*
  1768. * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
  1769. */
  1770. if (r == UV_ENOSYS) {
  1771. uv_fs_req_cleanup(&req);
  1772. RETURN_SKIP("realpath is not supported on Windows XP");
  1773. }
  1774. #endif
  1775. ASSERT(r == 0);
  1776. uv_run(loop, UV_RUN_DEFAULT);
  1777. ASSERT(realpath_cb_count == 1);
  1778. /*
  1779. * Run the loop just to check we don't have make any extraneous uv_ref()
  1780. * calls. This should drop out immediately.
  1781. */
  1782. uv_run(loop, UV_RUN_DEFAULT);
  1783. /* Cleanup. */
  1784. unlink("test_file");
  1785. unlink("test_file_symlink");
  1786. unlink("test_file_symlink_symlink");
  1787. unlink("test_file_symlink2");
  1788. unlink("test_file_symlink2_symlink");
  1789. MAKE_VALGRIND_HAPPY();
  1790. return 0;
  1791. }
  1792. int test_symlink_dir_impl(int type) {
  1793. uv_fs_t req;
  1794. int r;
  1795. char* test_dir;
  1796. uv_dirent_t dent;
  1797. static char test_dir_abs_buf[PATHMAX];
  1798. size_t test_dir_abs_size;
  1799. /* set-up */
  1800. unlink("test_dir/file1");
  1801. unlink("test_dir/file2");
  1802. rmdir("test_dir");
  1803. rmdir("test_dir_symlink");
  1804. test_dir_abs_size = sizeof(test_dir_abs_buf);
  1805. loop = uv_default_loop();
  1806. uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
  1807. uv_fs_req_cleanup(&req);
  1808. #ifdef _WIN32
  1809. strcpy(test_dir_abs_buf, "\\\\?\\");
  1810. uv_cwd(test_dir_abs_buf + 4, &test_dir_abs_size);
  1811. test_dir_abs_size += 4;
  1812. strcat(test_dir_abs_buf, "\\test_dir\\");
  1813. test_dir_abs_size += strlen("\\test_dir\\");
  1814. test_dir = test_dir_abs_buf;
  1815. #else
  1816. uv_cwd(test_dir_abs_buf, &test_dir_abs_size);
  1817. strcat(test_dir_abs_buf, "/test_dir");
  1818. test_dir_abs_size += strlen("/test_dir");
  1819. test_dir = "test_dir";
  1820. #endif
  1821. r = uv_fs_symlink(NULL, &req, test_dir, "test_dir_symlink", type, NULL);
  1822. if (type == UV_FS_SYMLINK_DIR && (r == UV_ENOTSUP || r == UV_EPERM)) {
  1823. uv_fs_req_cleanup(&req);
  1824. RETURN_SKIP("this version of Windows doesn't support unprivileged "
  1825. "creation of directory symlinks");
  1826. }
  1827. fprintf(stderr, "r == %i\n", r);
  1828. ASSERT(r == 0);
  1829. ASSERT(req.result == 0);
  1830. uv_fs_req_cleanup(&req);
  1831. r = uv_fs_stat(NULL, &req, "test_dir_symlink", NULL);
  1832. ASSERT(r == 0);
  1833. ASSERT(((uv_stat_t*)req.ptr)->st_mode & S_IFDIR);
  1834. uv_fs_req_cleanup(&req);
  1835. r = uv_fs_lstat(NULL, &req, "test_dir_symlink", NULL);
  1836. ASSERT(r == 0);
  1837. #if defined(__MSYS__)
  1838. RETURN_SKIP("symlink reading is not supported on MSYS2");
  1839. #endif
  1840. ASSERT(((uv_stat_t*)req.ptr)->st_mode & S_IFLNK);
  1841. #ifdef _WIN32
  1842. ASSERT(((uv_stat_t*)req.ptr)->st_size == strlen(test_dir + 4));
  1843. #else
  1844. # ifdef __PASE__
  1845. /* On IBMi PASE, st_size returns the length of the symlink itself. */
  1846. ASSERT(((uv_stat_t*)req.ptr)->st_size == strlen("test_dir_symlink"));
  1847. # else
  1848. ASSERT(((uv_stat_t*)req.ptr)->st_size == strlen(test_dir));
  1849. # endif
  1850. #endif
  1851. uv_fs_req_cleanup(&req);
  1852. r = uv_fs_readlink(NULL, &req, "test_dir_symlink", NULL);
  1853. ASSERT(r == 0);
  1854. #ifdef _WIN32
  1855. ASSERT(strcmp(req.ptr, test_dir + 4) == 0);
  1856. #else
  1857. ASSERT(strcmp(req.ptr, test_dir) == 0);
  1858. #endif
  1859. uv_fs_req_cleanup(&req);
  1860. r = uv_fs_realpath(NULL, &req, "test_dir_symlink", NULL);
  1861. #ifdef _WIN32
  1862. /*
  1863. * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
  1864. */
  1865. if (r == UV_ENOSYS) {
  1866. uv_fs_req_cleanup(&req);
  1867. RETURN_SKIP("realpath is not supported on Windows XP");
  1868. }
  1869. #endif
  1870. ASSERT(r == 0);
  1871. #ifdef _WIN32
  1872. ASSERT(strlen(req.ptr) == test_dir_abs_size - 5);
  1873. ASSERT(strnicmp(req.ptr, test_dir + 4, test_dir_abs_size - 5) == 0);
  1874. #else
  1875. ASSERT(strcmp(req.ptr, test_dir_abs_buf) == 0);
  1876. #endif
  1877. uv_fs_req_cleanup(&req);
  1878. r = uv_fs_open(NULL, &open_req1, "test_dir/file1", O_WRONLY | O_CREAT,
  1879. S_IWUSR | S_IRUSR, NULL);
  1880. ASSERT(r >= 0);
  1881. uv_fs_req_cleanup(&open_req1);
  1882. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  1883. ASSERT(r == 0);
  1884. uv_fs_req_cleanup(&close_req);
  1885. r = uv_fs_open(NULL, &open_req1, "test_dir/file2", O_WRONLY | O_CREAT,
  1886. S_IWUSR | S_IRUSR, NULL);
  1887. ASSERT(r >= 0);
  1888. uv_fs_req_cleanup(&open_req1);
  1889. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  1890. ASSERT(r == 0);
  1891. uv_fs_req_cleanup(&close_req);
  1892. r = uv_fs_scandir(NULL, &scandir_req, "test_dir_symlink", 0, NULL);
  1893. ASSERT(r == 2);
  1894. ASSERT(scandir_req.result == 2);
  1895. ASSERT(scandir_req.ptr);
  1896. while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
  1897. ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
  1898. assert_is_file_type(dent);
  1899. }
  1900. uv_fs_req_cleanup(&scandir_req);
  1901. ASSERT(!scandir_req.ptr);
  1902. /* unlink will remove the directory symlink */
  1903. r = uv_fs_unlink(NULL, &req, "test_dir_symlink", NULL);
  1904. ASSERT(r == 0);
  1905. uv_fs_req_cleanup(&req);
  1906. r = uv_fs_scandir(NULL, &scandir_req, "test_dir_symlink", 0, NULL);
  1907. ASSERT(r == UV_ENOENT);
  1908. uv_fs_req_cleanup(&scandir_req);
  1909. r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
  1910. ASSERT(r == 2);
  1911. ASSERT(scandir_req.result == 2);
  1912. ASSERT(scandir_req.ptr);
  1913. while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
  1914. ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
  1915. assert_is_file_type(dent);
  1916. }
  1917. uv_fs_req_cleanup(&scandir_req);
  1918. ASSERT(!scandir_req.ptr);
  1919. /* clean-up */
  1920. unlink("test_dir/file1");
  1921. unlink("test_dir/file2");
  1922. rmdir("test_dir");
  1923. rmdir("test_dir_symlink");
  1924. MAKE_VALGRIND_HAPPY();
  1925. return 0;
  1926. }
  1927. TEST_IMPL(fs_symlink_dir) {
  1928. return test_symlink_dir_impl(UV_FS_SYMLINK_DIR);
  1929. }
  1930. TEST_IMPL(fs_symlink_junction) {
  1931. return test_symlink_dir_impl(UV_FS_SYMLINK_JUNCTION);
  1932. }
  1933. #ifdef _WIN32
  1934. TEST_IMPL(fs_non_symlink_reparse_point) {
  1935. uv_fs_t req;
  1936. int r;
  1937. HANDLE file_handle;
  1938. REPARSE_GUID_DATA_BUFFER reparse_buffer;
  1939. DWORD bytes_returned;
  1940. uv_dirent_t dent;
  1941. /* set-up */
  1942. unlink("test_dir/test_file");
  1943. rmdir("test_dir");
  1944. loop = uv_default_loop();
  1945. uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
  1946. uv_fs_req_cleanup(&req);
  1947. file_handle = CreateFile("test_dir/test_file",
  1948. GENERIC_WRITE | FILE_WRITE_ATTRIBUTES,
  1949. 0,
  1950. NULL,
  1951. CREATE_ALWAYS,
  1952. FILE_FLAG_OPEN_REPARSE_POINT |
  1953. FILE_FLAG_BACKUP_SEMANTICS,
  1954. NULL);
  1955. ASSERT(file_handle != INVALID_HANDLE_VALUE);
  1956. memset(&reparse_buffer, 0, REPARSE_GUID_DATA_BUFFER_HEADER_SIZE);
  1957. reparse_buffer.ReparseTag = REPARSE_TAG;
  1958. reparse_buffer.ReparseDataLength = 0;
  1959. reparse_buffer.ReparseGuid = REPARSE_GUID;
  1960. r = DeviceIoControl(file_handle,
  1961. FSCTL_SET_REPARSE_POINT,
  1962. &reparse_buffer,
  1963. REPARSE_GUID_DATA_BUFFER_HEADER_SIZE,
  1964. NULL,
  1965. 0,
  1966. &bytes_returned,
  1967. NULL);
  1968. ASSERT(r != 0);
  1969. CloseHandle(file_handle);
  1970. r = uv_fs_readlink(NULL, &req, "test_dir/test_file", NULL);
  1971. ASSERT(r == UV_EINVAL && GetLastError() == ERROR_SYMLINK_NOT_SUPPORTED);
  1972. uv_fs_req_cleanup(&req);
  1973. /*
  1974. Placeholder tests for exercising the behavior fixed in issue #995.
  1975. To run, update the path with the IP address of a Mac with the hard drive
  1976. shared via SMB as "Macintosh HD".
  1977. r = uv_fs_stat(NULL, &req, "\\\\<mac_ip>\\Macintosh HD\\.DS_Store", NULL);
  1978. ASSERT(r == 0);
  1979. uv_fs_req_cleanup(&req);
  1980. r = uv_fs_lstat(NULL, &req, "\\\\<mac_ip>\\Macintosh HD\\.DS_Store", NULL);
  1981. ASSERT(r == 0);
  1982. uv_fs_req_cleanup(&req);
  1983. */
  1984. /*
  1985. uv_fs_stat and uv_fs_lstat can only work on non-symlink reparse
  1986. points when a minifilter driver is registered which intercepts
  1987. associated filesystem requests. Installing a driver is beyond
  1988. the scope of this test.
  1989. r = uv_fs_stat(NULL, &req, "test_dir/test_file", NULL);
  1990. ASSERT(r == 0);
  1991. uv_fs_req_cleanup(&req);
  1992. r = uv_fs_lstat(NULL, &req, "test_dir/test_file", NULL);
  1993. ASSERT(r == 0);
  1994. uv_fs_req_cleanup(&req);
  1995. */
  1996. r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
  1997. ASSERT(r == 1);
  1998. ASSERT(scandir_req.result == 1);
  1999. ASSERT(scandir_req.ptr);
  2000. while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
  2001. ASSERT(strcmp(dent.name, "test_file") == 0);
  2002. /* uv_fs_scandir incorrectly identifies non-symlink reparse points
  2003. as links because it doesn't open the file and verify the reparse
  2004. point tag. The PowerShell Get-ChildItem command shares this
  2005. behavior, so it's reasonable to leave it as is. */
  2006. ASSERT(dent.type == UV_DIRENT_LINK);
  2007. }
  2008. uv_fs_req_cleanup(&scandir_req);
  2009. ASSERT(!scandir_req.ptr);
  2010. /* clean-up */
  2011. unlink("test_dir/test_file");
  2012. rmdir("test_dir");
  2013. MAKE_VALGRIND_HAPPY();
  2014. return 0;
  2015. }
  2016. #endif
  2017. TEST_IMPL(fs_utime) {
  2018. utime_check_t checkme;
  2019. const char* path = "test_file";
  2020. double atime;
  2021. double mtime;
  2022. uv_fs_t req;
  2023. int r;
  2024. /* Setup. */
  2025. loop = uv_default_loop();
  2026. unlink(path);
  2027. r = uv_fs_open(NULL, &req, path, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL);
  2028. ASSERT(r >= 0);
  2029. ASSERT(req.result >= 0);
  2030. uv_fs_req_cleanup(&req);
  2031. uv_fs_close(loop, &req, r, NULL);
  2032. atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
  2033. /*
  2034. * Test sub-second timestamps only on Windows (assuming NTFS). Some other
  2035. * platforms support sub-second timestamps, but that support is filesystem-
  2036. * dependent. Notably OS X (HFS Plus) does NOT support sub-second timestamps.
  2037. */
  2038. #ifdef _WIN32
  2039. mtime += 0.444; /* 1982-09-10 11:22:33.444 */
  2040. #endif
  2041. r = uv_fs_utime(NULL, &req, path, atime, mtime, NULL);
  2042. ASSERT(r == 0);
  2043. ASSERT(req.result == 0);
  2044. uv_fs_req_cleanup(&req);
  2045. r = uv_fs_stat(NULL, &req, path, NULL);
  2046. ASSERT(r == 0);
  2047. ASSERT(req.result == 0);
  2048. check_utime(path, atime, mtime, /* test_lutime */ 0);
  2049. uv_fs_req_cleanup(&req);
  2050. atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
  2051. checkme.path = path;
  2052. checkme.atime = atime;
  2053. checkme.mtime = mtime;
  2054. /* async utime */
  2055. utime_req.data = &checkme;
  2056. r = uv_fs_utime(loop, &utime_req, path, atime, mtime, utime_cb);
  2057. ASSERT(r == 0);
  2058. uv_run(loop, UV_RUN_DEFAULT);
  2059. ASSERT(utime_cb_count == 1);
  2060. /* Cleanup. */
  2061. unlink(path);
  2062. MAKE_VALGRIND_HAPPY();
  2063. return 0;
  2064. }
  2065. #ifdef _WIN32
  2066. TEST_IMPL(fs_stat_root) {
  2067. int r;
  2068. r = uv_fs_stat(NULL, &stat_req, "\\", NULL);
  2069. ASSERT(r == 0);
  2070. r = uv_fs_stat(NULL, &stat_req, "..\\..\\..\\..\\..\\..\\..", NULL);
  2071. ASSERT(r == 0);
  2072. r = uv_fs_stat(NULL, &stat_req, "..", NULL);
  2073. ASSERT(r == 0);
  2074. r = uv_fs_stat(NULL, &stat_req, "..\\", NULL);
  2075. ASSERT(r == 0);
  2076. /* stats the current directory on c: */
  2077. r = uv_fs_stat(NULL, &stat_req, "c:", NULL);
  2078. ASSERT(r == 0);
  2079. r = uv_fs_stat(NULL, &stat_req, "c:\\", NULL);
  2080. ASSERT(r == 0);
  2081. r = uv_fs_stat(NULL, &stat_req, "\\\\?\\C:\\", NULL);
  2082. ASSERT(r == 0);
  2083. MAKE_VALGRIND_HAPPY();
  2084. return 0;
  2085. }
  2086. #endif
  2087. TEST_IMPL(fs_futime) {
  2088. utime_check_t checkme;
  2089. const char* path = "test_file";
  2090. double atime;
  2091. double mtime;
  2092. uv_file file;
  2093. uv_fs_t req;
  2094. int r;
  2095. #if defined(_AIX) && !defined(_AIX71)
  2096. RETURN_SKIP("futime is not implemented for AIX versions below 7.1");
  2097. #endif
  2098. /* Setup. */
  2099. loop = uv_default_loop();
  2100. unlink(path);
  2101. r = uv_fs_open(NULL, &req, path, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL);
  2102. ASSERT(r >= 0);
  2103. ASSERT(req.result >= 0);
  2104. uv_fs_req_cleanup(&req);
  2105. uv_fs_close(loop, &req, r, NULL);
  2106. atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
  2107. /*
  2108. * Test sub-second timestamps only on Windows (assuming NTFS). Some other
  2109. * platforms support sub-second timestamps, but that support is filesystem-
  2110. * dependent. Notably OS X (HFS Plus) does NOT support sub-second timestamps.
  2111. */
  2112. #ifdef _WIN32
  2113. mtime += 0.444; /* 1982-09-10 11:22:33.444 */
  2114. #endif
  2115. r = uv_fs_open(NULL, &req, path, O_RDWR, 0, NULL);
  2116. ASSERT(r >= 0);
  2117. ASSERT(req.result >= 0);
  2118. file = req.result; /* FIXME probably not how it's supposed to be used */
  2119. uv_fs_req_cleanup(&req);
  2120. r = uv_fs_futime(NULL, &req, file, atime, mtime, NULL);
  2121. #if defined(__CYGWIN__) || defined(__MSYS__)
  2122. ASSERT(r == UV_ENOSYS);
  2123. RETURN_SKIP("futime not supported on Cygwin");
  2124. #else
  2125. ASSERT(r == 0);
  2126. ASSERT(req.result == 0);
  2127. #endif
  2128. uv_fs_req_cleanup(&req);
  2129. r = uv_fs_stat(NULL, &req, path, NULL);
  2130. ASSERT(r == 0);
  2131. ASSERT(req.result == 0);
  2132. check_utime(path, atime, mtime, /* test_lutime */ 0);
  2133. uv_fs_req_cleanup(&req);
  2134. atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
  2135. checkme.atime = atime;
  2136. checkme.mtime = mtime;
  2137. checkme.path = path;
  2138. /* async futime */
  2139. futime_req.data = &checkme;
  2140. r = uv_fs_futime(loop, &futime_req, file, atime, mtime, futime_cb);
  2141. ASSERT(r == 0);
  2142. uv_run(loop, UV_RUN_DEFAULT);
  2143. ASSERT(futime_cb_count == 1);
  2144. /* Cleanup. */
  2145. unlink(path);
  2146. MAKE_VALGRIND_HAPPY();
  2147. return 0;
  2148. }
  2149. TEST_IMPL(fs_lutime) {
  2150. utime_check_t checkme;
  2151. const char* path = "test_file";
  2152. const char* symlink_path = "test_file_symlink";
  2153. double atime;
  2154. double mtime;
  2155. uv_fs_t req;
  2156. int r, s;
  2157. /* Setup */
  2158. loop = uv_default_loop();
  2159. unlink(path);
  2160. r = uv_fs_open(NULL, &req, path, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL);
  2161. ASSERT(r >= 0);
  2162. ASSERT(req.result >= 0);
  2163. uv_fs_req_cleanup(&req);
  2164. uv_fs_close(loop, &req, r, NULL);
  2165. unlink(symlink_path);
  2166. s = uv_fs_symlink(NULL, &req, path, symlink_path, 0, NULL);
  2167. #ifdef _WIN32
  2168. if (s == UV_EPERM) {
  2169. /*
  2170. * Creating a symlink before Windows 10 Creators Update was only allowed
  2171. * when running elevated console (with admin rights)
  2172. */
  2173. RETURN_SKIP(
  2174. "Symlink creation requires elevated console (with admin rights)");
  2175. }
  2176. #endif
  2177. ASSERT(s == 0);
  2178. ASSERT(req.result == 0);
  2179. uv_fs_req_cleanup(&req);
  2180. /* Test the synchronous version. */
  2181. atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
  2182. #ifdef _WIN32
  2183. mtime += 0.444; /* 1982-09-10 11:22:33.444 */
  2184. #endif
  2185. checkme.atime = atime;
  2186. checkme.mtime = mtime;
  2187. checkme.path = symlink_path;
  2188. req.data = &checkme;
  2189. r = uv_fs_lutime(NULL, &req, symlink_path, atime, mtime, NULL);
  2190. #if (defined(_AIX) && !defined(_AIX71)) || \
  2191. defined(__MVS__)
  2192. ASSERT(r == UV_ENOSYS);
  2193. RETURN_SKIP("lutime is not implemented for z/OS and AIX versions below 7.1");
  2194. #endif
  2195. ASSERT(r == 0);
  2196. lutime_cb(&req);
  2197. ASSERT(lutime_cb_count == 1);
  2198. /* Test the asynchronous version. */
  2199. atime = mtime = 1291404900; /* 2010-12-03 20:35:00 */
  2200. checkme.atime = atime;
  2201. checkme.mtime = mtime;
  2202. checkme.path = symlink_path;
  2203. r = uv_fs_lutime(loop, &req, symlink_path, atime, mtime, lutime_cb);
  2204. ASSERT(r == 0);
  2205. uv_run(loop, UV_RUN_DEFAULT);
  2206. ASSERT(lutime_cb_count == 2);
  2207. /* Cleanup. */
  2208. unlink(path);
  2209. unlink(symlink_path);
  2210. MAKE_VALGRIND_HAPPY();
  2211. return 0;
  2212. }
  2213. TEST_IMPL(fs_stat_missing_path) {
  2214. uv_fs_t req;
  2215. int r;
  2216. loop = uv_default_loop();
  2217. r = uv_fs_stat(NULL, &req, "non_existent_file", NULL);
  2218. ASSERT(r == UV_ENOENT);
  2219. ASSERT(req.result == UV_ENOENT);
  2220. uv_fs_req_cleanup(&req);
  2221. MAKE_VALGRIND_HAPPY();
  2222. return 0;
  2223. }
  2224. TEST_IMPL(fs_scandir_empty_dir) {
  2225. const char* path;
  2226. uv_fs_t req;
  2227. uv_dirent_t dent;
  2228. int r;
  2229. path = "./empty_dir/";
  2230. loop = uv_default_loop();
  2231. uv_fs_mkdir(NULL, &req, path, 0777, NULL);
  2232. uv_fs_req_cleanup(&req);
  2233. /* Fill the req to ensure that required fields are cleaned up */
  2234. memset(&req, 0xdb, sizeof(req));
  2235. r = uv_fs_scandir(NULL, &req, path, 0, NULL);
  2236. ASSERT(r == 0);
  2237. ASSERT(req.result == 0);
  2238. ASSERT(req.ptr == NULL);
  2239. ASSERT(UV_EOF == uv_fs_scandir_next(&req, &dent));
  2240. uv_fs_req_cleanup(&req);
  2241. r = uv_fs_scandir(loop, &scandir_req, path, 0, empty_scandir_cb);
  2242. ASSERT(r == 0);
  2243. ASSERT(scandir_cb_count == 0);
  2244. uv_run(loop, UV_RUN_DEFAULT);
  2245. ASSERT(scandir_cb_count == 1);
  2246. uv_fs_rmdir(NULL, &req, path, NULL);
  2247. uv_fs_req_cleanup(&req);
  2248. MAKE_VALGRIND_HAPPY();
  2249. return 0;
  2250. }
  2251. TEST_IMPL(fs_scandir_non_existent_dir) {
  2252. const char* path;
  2253. uv_fs_t req;
  2254. uv_dirent_t dent;
  2255. int r;
  2256. path = "./non_existent_dir/";
  2257. loop = uv_default_loop();
  2258. uv_fs_rmdir(NULL, &req, path, NULL);
  2259. uv_fs_req_cleanup(&req);
  2260. /* Fill the req to ensure that required fields are cleaned up */
  2261. memset(&req, 0xdb, sizeof(req));
  2262. r = uv_fs_scandir(NULL, &req, path, 0, NULL);
  2263. ASSERT(r == UV_ENOENT);
  2264. ASSERT(req.result == UV_ENOENT);
  2265. ASSERT(req.ptr == NULL);
  2266. ASSERT(UV_ENOENT == uv_fs_scandir_next(&req, &dent));
  2267. uv_fs_req_cleanup(&req);
  2268. r = uv_fs_scandir(loop, &scandir_req, path, 0, non_existent_scandir_cb);
  2269. ASSERT(r == 0);
  2270. ASSERT(scandir_cb_count == 0);
  2271. uv_run(loop, UV_RUN_DEFAULT);
  2272. ASSERT(scandir_cb_count == 1);
  2273. MAKE_VALGRIND_HAPPY();
  2274. return 0;
  2275. }
  2276. TEST_IMPL(fs_scandir_file) {
  2277. const char* path;
  2278. int r;
  2279. path = "test/fixtures/empty_file";
  2280. loop = uv_default_loop();
  2281. r = uv_fs_scandir(NULL, &scandir_req, path, 0, NULL);
  2282. ASSERT(r == UV_ENOTDIR);
  2283. uv_fs_req_cleanup(&scandir_req);
  2284. r = uv_fs_scandir(loop, &scandir_req, path, 0, file_scandir_cb);
  2285. ASSERT(r == 0);
  2286. ASSERT(scandir_cb_count == 0);
  2287. uv_run(loop, UV_RUN_DEFAULT);
  2288. ASSERT(scandir_cb_count == 1);
  2289. MAKE_VALGRIND_HAPPY();
  2290. return 0;
  2291. }
  2292. TEST_IMPL(fs_open_dir) {
  2293. const char* path;
  2294. uv_fs_t req;
  2295. int r, file;
  2296. path = ".";
  2297. loop = uv_default_loop();
  2298. r = uv_fs_open(NULL, &req, path, O_RDONLY, 0, NULL);
  2299. ASSERT(r >= 0);
  2300. ASSERT(req.result >= 0);
  2301. ASSERT(req.ptr == NULL);
  2302. file = r;
  2303. uv_fs_req_cleanup(&req);
  2304. r = uv_fs_close(NULL, &req, file, NULL);
  2305. ASSERT(r == 0);
  2306. r = uv_fs_open(loop, &req, path, O_RDONLY, 0, open_cb_simple);
  2307. ASSERT(r == 0);
  2308. ASSERT(open_cb_count == 0);
  2309. uv_run(loop, UV_RUN_DEFAULT);
  2310. ASSERT(open_cb_count == 1);
  2311. MAKE_VALGRIND_HAPPY();
  2312. return 0;
  2313. }
  2314. static void fs_file_open_append(int add_flags) {
  2315. int r;
  2316. /* Setup. */
  2317. unlink("test_file");
  2318. loop = uv_default_loop();
  2319. r = uv_fs_open(NULL, &open_req1, "test_file",
  2320. O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
  2321. ASSERT(r >= 0);
  2322. ASSERT(open_req1.result >= 0);
  2323. uv_fs_req_cleanup(&open_req1);
  2324. iov = uv_buf_init(test_buf, sizeof(test_buf));
  2325. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  2326. ASSERT(r >= 0);
  2327. ASSERT(write_req.result >= 0);
  2328. uv_fs_req_cleanup(&write_req);
  2329. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2330. ASSERT(r == 0);
  2331. ASSERT(close_req.result == 0);
  2332. uv_fs_req_cleanup(&close_req);
  2333. r = uv_fs_open(NULL, &open_req1, "test_file",
  2334. O_RDWR | O_APPEND | add_flags, 0, NULL);
  2335. ASSERT(r >= 0);
  2336. ASSERT(open_req1.result >= 0);
  2337. uv_fs_req_cleanup(&open_req1);
  2338. iov = uv_buf_init(test_buf, sizeof(test_buf));
  2339. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  2340. ASSERT(r >= 0);
  2341. ASSERT(write_req.result >= 0);
  2342. uv_fs_req_cleanup(&write_req);
  2343. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2344. ASSERT(r == 0);
  2345. ASSERT(close_req.result == 0);
  2346. uv_fs_req_cleanup(&close_req);
  2347. r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY | add_flags,
  2348. S_IRUSR, NULL);
  2349. ASSERT(r >= 0);
  2350. ASSERT(open_req1.result >= 0);
  2351. uv_fs_req_cleanup(&open_req1);
  2352. iov = uv_buf_init(buf, sizeof(buf));
  2353. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  2354. printf("read = %d\n", r);
  2355. ASSERT(r == 26);
  2356. ASSERT(read_req.result == 26);
  2357. ASSERT(memcmp(buf,
  2358. "test-buffer\n\0test-buffer\n\0",
  2359. sizeof("test-buffer\n\0test-buffer\n\0") - 1) == 0);
  2360. uv_fs_req_cleanup(&read_req);
  2361. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2362. ASSERT(r == 0);
  2363. ASSERT(close_req.result == 0);
  2364. uv_fs_req_cleanup(&close_req);
  2365. /* Cleanup */
  2366. unlink("test_file");
  2367. }
  2368. TEST_IMPL(fs_file_open_append) {
  2369. fs_file_open_append(0);
  2370. fs_file_open_append(UV_FS_O_FILEMAP);
  2371. MAKE_VALGRIND_HAPPY();
  2372. return 0;
  2373. }
  2374. TEST_IMPL(fs_rename_to_existing_file) {
  2375. int r;
  2376. /* Setup. */
  2377. unlink("test_file");
  2378. unlink("test_file2");
  2379. loop = uv_default_loop();
  2380. r = uv_fs_open(NULL, &open_req1, "test_file", O_WRONLY | O_CREAT,
  2381. S_IWUSR | S_IRUSR, NULL);
  2382. ASSERT(r >= 0);
  2383. ASSERT(open_req1.result >= 0);
  2384. uv_fs_req_cleanup(&open_req1);
  2385. iov = uv_buf_init(test_buf, sizeof(test_buf));
  2386. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  2387. ASSERT(r >= 0);
  2388. ASSERT(write_req.result >= 0);
  2389. uv_fs_req_cleanup(&write_req);
  2390. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2391. ASSERT(r == 0);
  2392. ASSERT(close_req.result == 0);
  2393. uv_fs_req_cleanup(&close_req);
  2394. r = uv_fs_open(NULL, &open_req1, "test_file2", O_WRONLY | O_CREAT,
  2395. S_IWUSR | S_IRUSR, NULL);
  2396. ASSERT(r >= 0);
  2397. ASSERT(open_req1.result >= 0);
  2398. uv_fs_req_cleanup(&open_req1);
  2399. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2400. ASSERT(r == 0);
  2401. ASSERT(close_req.result == 0);
  2402. uv_fs_req_cleanup(&close_req);
  2403. r = uv_fs_rename(NULL, &rename_req, "test_file", "test_file2", NULL);
  2404. ASSERT(r == 0);
  2405. ASSERT(rename_req.result == 0);
  2406. uv_fs_req_cleanup(&rename_req);
  2407. r = uv_fs_open(NULL, &open_req1, "test_file2", O_RDONLY, 0, NULL);
  2408. ASSERT(r >= 0);
  2409. ASSERT(open_req1.result >= 0);
  2410. uv_fs_req_cleanup(&open_req1);
  2411. memset(buf, 0, sizeof(buf));
  2412. iov = uv_buf_init(buf, sizeof(buf));
  2413. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  2414. ASSERT(r >= 0);
  2415. ASSERT(read_req.result >= 0);
  2416. ASSERT(strcmp(buf, test_buf) == 0);
  2417. uv_fs_req_cleanup(&read_req);
  2418. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2419. ASSERT(r == 0);
  2420. ASSERT(close_req.result == 0);
  2421. uv_fs_req_cleanup(&close_req);
  2422. /* Cleanup */
  2423. unlink("test_file");
  2424. unlink("test_file2");
  2425. MAKE_VALGRIND_HAPPY();
  2426. return 0;
  2427. }
  2428. static void fs_read_bufs(int add_flags) {
  2429. char scratch[768];
  2430. uv_buf_t bufs[4];
  2431. ASSERT(0 <= uv_fs_open(NULL, &open_req1,
  2432. "test/fixtures/lorem_ipsum.txt",
  2433. O_RDONLY | add_flags, 0, NULL));
  2434. ASSERT(open_req1.result >= 0);
  2435. uv_fs_req_cleanup(&open_req1);
  2436. ASSERT(UV_EINVAL == uv_fs_read(NULL, &read_req, open_req1.result,
  2437. NULL, 0, 0, NULL));
  2438. ASSERT(UV_EINVAL == uv_fs_read(NULL, &read_req, open_req1.result,
  2439. NULL, 1, 0, NULL));
  2440. ASSERT(UV_EINVAL == uv_fs_read(NULL, &read_req, open_req1.result,
  2441. bufs, 0, 0, NULL));
  2442. bufs[0] = uv_buf_init(scratch + 0, 256);
  2443. bufs[1] = uv_buf_init(scratch + 256, 256);
  2444. bufs[2] = uv_buf_init(scratch + 512, 128);
  2445. bufs[3] = uv_buf_init(scratch + 640, 128);
  2446. ASSERT(446 == uv_fs_read(NULL,
  2447. &read_req,
  2448. open_req1.result,
  2449. bufs + 0,
  2450. 2, /* 2x 256 bytes. */
  2451. 0, /* Positional read. */
  2452. NULL));
  2453. ASSERT(read_req.result == 446);
  2454. uv_fs_req_cleanup(&read_req);
  2455. ASSERT(190 == uv_fs_read(NULL,
  2456. &read_req,
  2457. open_req1.result,
  2458. bufs + 2,
  2459. 2, /* 2x 128 bytes. */
  2460. 256, /* Positional read. */
  2461. NULL));
  2462. ASSERT(read_req.result == /* 446 - 256 */ 190);
  2463. uv_fs_req_cleanup(&read_req);
  2464. ASSERT(0 == memcmp(bufs[1].base + 0, bufs[2].base, 128));
  2465. ASSERT(0 == memcmp(bufs[1].base + 128, bufs[3].base, 190 - 128));
  2466. ASSERT(0 == uv_fs_close(NULL, &close_req, open_req1.result, NULL));
  2467. ASSERT(close_req.result == 0);
  2468. uv_fs_req_cleanup(&close_req);
  2469. }
  2470. TEST_IMPL(fs_read_bufs) {
  2471. fs_read_bufs(0);
  2472. fs_read_bufs(UV_FS_O_FILEMAP);
  2473. MAKE_VALGRIND_HAPPY();
  2474. return 0;
  2475. }
  2476. static void fs_read_file_eof(int add_flags) {
  2477. #if defined(__CYGWIN__) || defined(__MSYS__)
  2478. RETURN_SKIP("Cygwin pread at EOF may (incorrectly) return data!");
  2479. #endif
  2480. int r;
  2481. /* Setup. */
  2482. unlink("test_file");
  2483. loop = uv_default_loop();
  2484. r = uv_fs_open(NULL, &open_req1, "test_file",
  2485. O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
  2486. ASSERT(r >= 0);
  2487. ASSERT(open_req1.result >= 0);
  2488. uv_fs_req_cleanup(&open_req1);
  2489. iov = uv_buf_init(test_buf, sizeof(test_buf));
  2490. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  2491. ASSERT(r >= 0);
  2492. ASSERT(write_req.result >= 0);
  2493. uv_fs_req_cleanup(&write_req);
  2494. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2495. ASSERT(r == 0);
  2496. ASSERT(close_req.result == 0);
  2497. uv_fs_req_cleanup(&close_req);
  2498. r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY | add_flags, 0,
  2499. NULL);
  2500. ASSERT(r >= 0);
  2501. ASSERT(open_req1.result >= 0);
  2502. uv_fs_req_cleanup(&open_req1);
  2503. memset(buf, 0, sizeof(buf));
  2504. iov = uv_buf_init(buf, sizeof(buf));
  2505. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  2506. ASSERT(r >= 0);
  2507. ASSERT(read_req.result >= 0);
  2508. ASSERT(strcmp(buf, test_buf) == 0);
  2509. uv_fs_req_cleanup(&read_req);
  2510. iov = uv_buf_init(buf, sizeof(buf));
  2511. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1,
  2512. read_req.result, NULL);
  2513. ASSERT(r == 0);
  2514. ASSERT(read_req.result == 0);
  2515. uv_fs_req_cleanup(&read_req);
  2516. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2517. ASSERT(r == 0);
  2518. ASSERT(close_req.result == 0);
  2519. uv_fs_req_cleanup(&close_req);
  2520. /* Cleanup */
  2521. unlink("test_file");
  2522. }
  2523. TEST_IMPL(fs_read_file_eof) {
  2524. fs_read_file_eof(0);
  2525. fs_read_file_eof(UV_FS_O_FILEMAP);
  2526. MAKE_VALGRIND_HAPPY();
  2527. return 0;
  2528. }
  2529. static void fs_write_multiple_bufs(int add_flags) {
  2530. uv_buf_t iovs[2];
  2531. int r;
  2532. /* Setup. */
  2533. unlink("test_file");
  2534. loop = uv_default_loop();
  2535. r = uv_fs_open(NULL, &open_req1, "test_file",
  2536. O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
  2537. ASSERT(r >= 0);
  2538. ASSERT(open_req1.result >= 0);
  2539. uv_fs_req_cleanup(&open_req1);
  2540. iovs[0] = uv_buf_init(test_buf, sizeof(test_buf));
  2541. iovs[1] = uv_buf_init(test_buf2, sizeof(test_buf2));
  2542. r = uv_fs_write(NULL, &write_req, open_req1.result, iovs, 2, 0, NULL);
  2543. ASSERT(r >= 0);
  2544. ASSERT(write_req.result >= 0);
  2545. uv_fs_req_cleanup(&write_req);
  2546. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2547. ASSERT(r == 0);
  2548. ASSERT(close_req.result == 0);
  2549. uv_fs_req_cleanup(&close_req);
  2550. r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY | add_flags, 0,
  2551. NULL);
  2552. ASSERT(r >= 0);
  2553. ASSERT(open_req1.result >= 0);
  2554. uv_fs_req_cleanup(&open_req1);
  2555. memset(buf, 0, sizeof(buf));
  2556. memset(buf2, 0, sizeof(buf2));
  2557. /* Read the strings back to separate buffers. */
  2558. iovs[0] = uv_buf_init(buf, sizeof(test_buf));
  2559. iovs[1] = uv_buf_init(buf2, sizeof(test_buf2));
  2560. ASSERT(lseek(open_req1.result, 0, SEEK_CUR) == 0);
  2561. r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, 2, -1, NULL);
  2562. ASSERT(r >= 0);
  2563. ASSERT(read_req.result == sizeof(test_buf) + sizeof(test_buf2));
  2564. ASSERT(strcmp(buf, test_buf) == 0);
  2565. ASSERT(strcmp(buf2, test_buf2) == 0);
  2566. uv_fs_req_cleanup(&read_req);
  2567. iov = uv_buf_init(buf, sizeof(buf));
  2568. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  2569. ASSERT(r == 0);
  2570. ASSERT(read_req.result == 0);
  2571. uv_fs_req_cleanup(&read_req);
  2572. /* Read the strings back to separate buffers. */
  2573. iovs[0] = uv_buf_init(buf, sizeof(test_buf));
  2574. iovs[1] = uv_buf_init(buf2, sizeof(test_buf2));
  2575. r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, 2, 0, NULL);
  2576. ASSERT(r >= 0);
  2577. if (read_req.result == sizeof(test_buf)) {
  2578. /* Infer that preadv is not available. */
  2579. uv_fs_req_cleanup(&read_req);
  2580. r = uv_fs_read(NULL, &read_req, open_req1.result, &iovs[1], 1, read_req.result, NULL);
  2581. ASSERT(r >= 0);
  2582. ASSERT(read_req.result == sizeof(test_buf2));
  2583. } else {
  2584. ASSERT(read_req.result == sizeof(test_buf) + sizeof(test_buf2));
  2585. }
  2586. ASSERT(strcmp(buf, test_buf) == 0);
  2587. ASSERT(strcmp(buf2, test_buf2) == 0);
  2588. uv_fs_req_cleanup(&read_req);
  2589. iov = uv_buf_init(buf, sizeof(buf));
  2590. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1,
  2591. sizeof(test_buf) + sizeof(test_buf2), NULL);
  2592. ASSERT(r == 0);
  2593. ASSERT(read_req.result == 0);
  2594. uv_fs_req_cleanup(&read_req);
  2595. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2596. ASSERT(r == 0);
  2597. ASSERT(close_req.result == 0);
  2598. uv_fs_req_cleanup(&close_req);
  2599. /* Cleanup */
  2600. unlink("test_file");
  2601. }
  2602. TEST_IMPL(fs_write_multiple_bufs) {
  2603. fs_write_multiple_bufs(0);
  2604. fs_write_multiple_bufs(UV_FS_O_FILEMAP);
  2605. MAKE_VALGRIND_HAPPY();
  2606. return 0;
  2607. }
  2608. static void fs_write_alotof_bufs(int add_flags) {
  2609. size_t iovcount;
  2610. size_t iovmax;
  2611. uv_buf_t* iovs;
  2612. char* buffer;
  2613. size_t index;
  2614. int r;
  2615. iovcount = 54321;
  2616. /* Setup. */
  2617. unlink("test_file");
  2618. loop = uv_default_loop();
  2619. iovs = malloc(sizeof(*iovs) * iovcount);
  2620. ASSERT(iovs != NULL);
  2621. iovmax = uv_test_getiovmax();
  2622. r = uv_fs_open(NULL,
  2623. &open_req1,
  2624. "test_file",
  2625. O_RDWR | O_CREAT | add_flags,
  2626. S_IWUSR | S_IRUSR,
  2627. NULL);
  2628. ASSERT(r >= 0);
  2629. ASSERT(open_req1.result >= 0);
  2630. uv_fs_req_cleanup(&open_req1);
  2631. for (index = 0; index < iovcount; ++index)
  2632. iovs[index] = uv_buf_init(test_buf, sizeof(test_buf));
  2633. r = uv_fs_write(NULL,
  2634. &write_req,
  2635. open_req1.result,
  2636. iovs,
  2637. iovcount,
  2638. -1,
  2639. NULL);
  2640. ASSERT(r >= 0);
  2641. ASSERT((size_t)write_req.result == sizeof(test_buf) * iovcount);
  2642. uv_fs_req_cleanup(&write_req);
  2643. /* Read the strings back to separate buffers. */
  2644. buffer = malloc(sizeof(test_buf) * iovcount);
  2645. ASSERT(buffer != NULL);
  2646. for (index = 0; index < iovcount; ++index)
  2647. iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf),
  2648. sizeof(test_buf));
  2649. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2650. ASSERT(r == 0);
  2651. ASSERT(close_req.result == 0);
  2652. uv_fs_req_cleanup(&close_req);
  2653. r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY | add_flags, 0,
  2654. NULL);
  2655. ASSERT(r >= 0);
  2656. ASSERT(open_req1.result >= 0);
  2657. uv_fs_req_cleanup(&open_req1);
  2658. r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, iovcount, -1, NULL);
  2659. if (iovcount > iovmax)
  2660. iovcount = iovmax;
  2661. ASSERT(r >= 0);
  2662. ASSERT((size_t)read_req.result == sizeof(test_buf) * iovcount);
  2663. for (index = 0; index < iovcount; ++index)
  2664. ASSERT(strncmp(buffer + index * sizeof(test_buf),
  2665. test_buf,
  2666. sizeof(test_buf)) == 0);
  2667. uv_fs_req_cleanup(&read_req);
  2668. free(buffer);
  2669. ASSERT(lseek(open_req1.result, write_req.result, SEEK_SET) == write_req.result);
  2670. iov = uv_buf_init(buf, sizeof(buf));
  2671. r = uv_fs_read(NULL,
  2672. &read_req,
  2673. open_req1.result,
  2674. &iov,
  2675. 1,
  2676. -1,
  2677. NULL);
  2678. ASSERT(r == 0);
  2679. ASSERT(read_req.result == 0);
  2680. uv_fs_req_cleanup(&read_req);
  2681. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2682. ASSERT(r == 0);
  2683. ASSERT(close_req.result == 0);
  2684. uv_fs_req_cleanup(&close_req);
  2685. /* Cleanup */
  2686. unlink("test_file");
  2687. free(iovs);
  2688. }
  2689. TEST_IMPL(fs_write_alotof_bufs) {
  2690. fs_write_alotof_bufs(0);
  2691. fs_write_alotof_bufs(UV_FS_O_FILEMAP);
  2692. MAKE_VALGRIND_HAPPY();
  2693. return 0;
  2694. }
  2695. static void fs_write_alotof_bufs_with_offset(int add_flags) {
  2696. size_t iovcount;
  2697. size_t iovmax;
  2698. uv_buf_t* iovs;
  2699. char* buffer;
  2700. size_t index;
  2701. int r;
  2702. int64_t offset;
  2703. char* filler;
  2704. int filler_len;
  2705. filler = "0123456789";
  2706. filler_len = strlen(filler);
  2707. iovcount = 54321;
  2708. /* Setup. */
  2709. unlink("test_file");
  2710. loop = uv_default_loop();
  2711. iovs = malloc(sizeof(*iovs) * iovcount);
  2712. ASSERT(iovs != NULL);
  2713. iovmax = uv_test_getiovmax();
  2714. r = uv_fs_open(NULL,
  2715. &open_req1,
  2716. "test_file",
  2717. O_RDWR | O_CREAT | add_flags,
  2718. S_IWUSR | S_IRUSR,
  2719. NULL);
  2720. ASSERT(r >= 0);
  2721. ASSERT(open_req1.result >= 0);
  2722. uv_fs_req_cleanup(&open_req1);
  2723. iov = uv_buf_init(filler, filler_len);
  2724. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  2725. ASSERT(r == filler_len);
  2726. ASSERT(write_req.result == filler_len);
  2727. uv_fs_req_cleanup(&write_req);
  2728. offset = (int64_t)r;
  2729. for (index = 0; index < iovcount; ++index)
  2730. iovs[index] = uv_buf_init(test_buf, sizeof(test_buf));
  2731. r = uv_fs_write(NULL,
  2732. &write_req,
  2733. open_req1.result,
  2734. iovs,
  2735. iovcount,
  2736. offset,
  2737. NULL);
  2738. ASSERT(r >= 0);
  2739. ASSERT((size_t)write_req.result == sizeof(test_buf) * iovcount);
  2740. uv_fs_req_cleanup(&write_req);
  2741. /* Read the strings back to separate buffers. */
  2742. buffer = malloc(sizeof(test_buf) * iovcount);
  2743. ASSERT(buffer != NULL);
  2744. for (index = 0; index < iovcount; ++index)
  2745. iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf),
  2746. sizeof(test_buf));
  2747. r = uv_fs_read(NULL, &read_req, open_req1.result,
  2748. iovs, iovcount, offset, NULL);
  2749. ASSERT(r >= 0);
  2750. if (r == sizeof(test_buf))
  2751. iovcount = 1; /* Infer that preadv is not available. */
  2752. else if (iovcount > iovmax)
  2753. iovcount = iovmax;
  2754. ASSERT((size_t)read_req.result == sizeof(test_buf) * iovcount);
  2755. for (index = 0; index < iovcount; ++index)
  2756. ASSERT(strncmp(buffer + index * sizeof(test_buf),
  2757. test_buf,
  2758. sizeof(test_buf)) == 0);
  2759. uv_fs_req_cleanup(&read_req);
  2760. free(buffer);
  2761. r = uv_fs_stat(NULL, &stat_req, "test_file", NULL);
  2762. ASSERT(r == 0);
  2763. ASSERT((int64_t)((uv_stat_t*)stat_req.ptr)->st_size ==
  2764. offset + (int64_t)write_req.result);
  2765. uv_fs_req_cleanup(&stat_req);
  2766. iov = uv_buf_init(buf, sizeof(buf));
  2767. r = uv_fs_read(NULL,
  2768. &read_req,
  2769. open_req1.result,
  2770. &iov,
  2771. 1,
  2772. offset + write_req.result,
  2773. NULL);
  2774. ASSERT(r == 0);
  2775. ASSERT(read_req.result == 0);
  2776. uv_fs_req_cleanup(&read_req);
  2777. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2778. ASSERT(r == 0);
  2779. ASSERT(close_req.result == 0);
  2780. uv_fs_req_cleanup(&close_req);
  2781. /* Cleanup */
  2782. unlink("test_file");
  2783. free(iovs);
  2784. }
  2785. TEST_IMPL(fs_write_alotof_bufs_with_offset) {
  2786. fs_write_alotof_bufs_with_offset(0);
  2787. fs_write_alotof_bufs_with_offset(UV_FS_O_FILEMAP);
  2788. MAKE_VALGRIND_HAPPY();
  2789. return 0;
  2790. }
  2791. TEST_IMPL(fs_read_dir) {
  2792. int r;
  2793. char buf[2];
  2794. loop = uv_default_loop();
  2795. /* Setup */
  2796. rmdir("test_dir");
  2797. r = uv_fs_mkdir(loop, &mkdir_req, "test_dir", 0755, mkdir_cb);
  2798. ASSERT(r == 0);
  2799. uv_run(loop, UV_RUN_DEFAULT);
  2800. ASSERT(mkdir_cb_count == 1);
  2801. /* Setup Done Here */
  2802. /* Get a file descriptor for the directory */
  2803. r = uv_fs_open(loop,
  2804. &open_req1,
  2805. "test_dir",
  2806. UV_FS_O_RDONLY | UV_FS_O_DIRECTORY,
  2807. S_IWUSR | S_IRUSR,
  2808. NULL);
  2809. ASSERT(r >= 0);
  2810. uv_fs_req_cleanup(&open_req1);
  2811. /* Try to read data from the directory */
  2812. iov = uv_buf_init(buf, sizeof(buf));
  2813. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 0, NULL);
  2814. #if defined(__FreeBSD__) || \
  2815. defined(__OpenBSD__) || \
  2816. defined(__NetBSD__) || \
  2817. defined(__DragonFly__) || \
  2818. defined(_AIX) || \
  2819. defined(__sun) || \
  2820. defined(__MVS__)
  2821. /*
  2822. * As of now, these operating systems support reading from a directory,
  2823. * that too depends on the filesystem this temporary test directory is
  2824. * created on. That is why this assertion is a bit lenient.
  2825. */
  2826. ASSERT((r >= 0) || (r == UV_EISDIR));
  2827. #else
  2828. ASSERT(r == UV_EISDIR);
  2829. #endif
  2830. uv_fs_req_cleanup(&read_req);
  2831. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  2832. ASSERT(r == 0);
  2833. uv_fs_req_cleanup(&close_req);
  2834. /* Cleanup */
  2835. rmdir("test_dir");
  2836. MAKE_VALGRIND_HAPPY();
  2837. return 0;
  2838. }
  2839. #ifdef _WIN32
  2840. TEST_IMPL(fs_partial_read) {
  2841. RETURN_SKIP("Test not implemented on Windows.");
  2842. }
  2843. TEST_IMPL(fs_partial_write) {
  2844. RETURN_SKIP("Test not implemented on Windows.");
  2845. }
  2846. #else /* !_WIN32 */
  2847. struct thread_ctx {
  2848. pthread_t pid;
  2849. int fd;
  2850. char* data;
  2851. int size;
  2852. int interval;
  2853. int doread;
  2854. };
  2855. static void thread_main(void* arg) {
  2856. const struct thread_ctx* ctx;
  2857. int size;
  2858. char* data;
  2859. ctx = (struct thread_ctx*)arg;
  2860. size = ctx->size;
  2861. data = ctx->data;
  2862. while (size > 0) {
  2863. ssize_t result;
  2864. int nbytes;
  2865. nbytes = size < ctx->interval ? size : ctx->interval;
  2866. if (ctx->doread) {
  2867. result = write(ctx->fd, data, nbytes);
  2868. /* Should not see EINTR (or other errors) */
  2869. ASSERT(result == nbytes);
  2870. } else {
  2871. result = read(ctx->fd, data, nbytes);
  2872. /* Should not see EINTR (or other errors),
  2873. * but might get a partial read if we are faster than the writer
  2874. */
  2875. ASSERT(result > 0 && result <= nbytes);
  2876. }
  2877. pthread_kill(ctx->pid, SIGUSR1);
  2878. size -= result;
  2879. data += result;
  2880. }
  2881. }
  2882. static void sig_func(uv_signal_t* handle, int signum) {
  2883. uv_signal_stop(handle);
  2884. }
  2885. static size_t uv_test_fs_buf_offset(uv_buf_t* bufs, size_t size) {
  2886. size_t offset;
  2887. /* Figure out which bufs are done */
  2888. for (offset = 0; size > 0 && bufs[offset].len <= size; ++offset)
  2889. size -= bufs[offset].len;
  2890. /* Fix a partial read/write */
  2891. if (size > 0) {
  2892. bufs[offset].base += size;
  2893. bufs[offset].len -= size;
  2894. }
  2895. return offset;
  2896. }
  2897. static void test_fs_partial(int doread) {
  2898. struct thread_ctx ctx;
  2899. uv_thread_t thread;
  2900. uv_signal_t signal;
  2901. int pipe_fds[2];
  2902. size_t iovcount;
  2903. uv_buf_t* iovs;
  2904. char* buffer;
  2905. size_t index;
  2906. iovcount = 54321;
  2907. iovs = malloc(sizeof(*iovs) * iovcount);
  2908. ASSERT(iovs != NULL);
  2909. ctx.pid = pthread_self();
  2910. ctx.doread = doread;
  2911. ctx.interval = 1000;
  2912. ctx.size = sizeof(test_buf) * iovcount;
  2913. ctx.data = malloc(ctx.size);
  2914. ASSERT(ctx.data != NULL);
  2915. buffer = malloc(ctx.size);
  2916. ASSERT(buffer != NULL);
  2917. for (index = 0; index < iovcount; ++index)
  2918. iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf), sizeof(test_buf));
  2919. loop = uv_default_loop();
  2920. ASSERT(0 == uv_signal_init(loop, &signal));
  2921. ASSERT(0 == uv_signal_start(&signal, sig_func, SIGUSR1));
  2922. ASSERT(0 == pipe(pipe_fds));
  2923. ctx.fd = pipe_fds[doread];
  2924. ASSERT(0 == uv_thread_create(&thread, thread_main, &ctx));
  2925. if (doread) {
  2926. uv_buf_t* read_iovs;
  2927. int nread;
  2928. read_iovs = iovs;
  2929. nread = 0;
  2930. while (nread < ctx.size) {
  2931. int result;
  2932. result = uv_fs_read(loop, &read_req, pipe_fds[0], read_iovs, iovcount, -1, NULL);
  2933. if (result > 0) {
  2934. size_t read_iovcount;
  2935. read_iovcount = uv_test_fs_buf_offset(read_iovs, result);
  2936. read_iovs += read_iovcount;
  2937. iovcount -= read_iovcount;
  2938. nread += result;
  2939. } else {
  2940. ASSERT(result == UV_EINTR);
  2941. }
  2942. uv_fs_req_cleanup(&read_req);
  2943. }
  2944. } else {
  2945. int result;
  2946. result = uv_fs_write(loop, &write_req, pipe_fds[1], iovs, iovcount, -1, NULL);
  2947. ASSERT(write_req.result == result);
  2948. ASSERT(result == ctx.size);
  2949. uv_fs_req_cleanup(&write_req);
  2950. }
  2951. ASSERT(0 == memcmp(buffer, ctx.data, ctx.size));
  2952. ASSERT(0 == uv_thread_join(&thread));
  2953. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  2954. ASSERT(0 == close(pipe_fds[1]));
  2955. uv_close((uv_handle_t*) &signal, NULL);
  2956. { /* Make sure we read everything that we wrote. */
  2957. int result;
  2958. result = uv_fs_read(loop, &read_req, pipe_fds[0], iovs, 1, -1, NULL);
  2959. ASSERT(result == 0);
  2960. uv_fs_req_cleanup(&read_req);
  2961. }
  2962. ASSERT(0 == close(pipe_fds[0]));
  2963. free(iovs);
  2964. free(buffer);
  2965. free(ctx.data);
  2966. MAKE_VALGRIND_HAPPY();
  2967. }
  2968. TEST_IMPL(fs_partial_read) {
  2969. test_fs_partial(1);
  2970. return 0;
  2971. }
  2972. TEST_IMPL(fs_partial_write) {
  2973. test_fs_partial(0);
  2974. return 0;
  2975. }
  2976. #endif/* _WIN32 */
  2977. TEST_IMPL(fs_read_write_null_arguments) {
  2978. int r;
  2979. r = uv_fs_read(NULL, &read_req, 0, NULL, 0, -1, NULL);
  2980. ASSERT(r == UV_EINVAL);
  2981. uv_fs_req_cleanup(&read_req);
  2982. r = uv_fs_write(NULL, &write_req, 0, NULL, 0, -1, NULL);
  2983. /* Validate some memory management on failed input validation before sending
  2984. fs work to the thread pool. */
  2985. ASSERT(r == UV_EINVAL);
  2986. ASSERT(write_req.path == NULL);
  2987. ASSERT(write_req.ptr == NULL);
  2988. #ifdef _WIN32
  2989. ASSERT(write_req.file.pathw == NULL);
  2990. ASSERT(write_req.fs.info.new_pathw == NULL);
  2991. ASSERT(write_req.fs.info.bufs == NULL);
  2992. #else
  2993. ASSERT(write_req.new_path == NULL);
  2994. ASSERT(write_req.bufs == NULL);
  2995. #endif
  2996. uv_fs_req_cleanup(&write_req);
  2997. iov = uv_buf_init(NULL, 0);
  2998. r = uv_fs_read(NULL, &read_req, 0, &iov, 0, -1, NULL);
  2999. ASSERT(r == UV_EINVAL);
  3000. uv_fs_req_cleanup(&read_req);
  3001. iov = uv_buf_init(NULL, 0);
  3002. r = uv_fs_write(NULL, &write_req, 0, &iov, 0, -1, NULL);
  3003. ASSERT(r == UV_EINVAL);
  3004. uv_fs_req_cleanup(&write_req);
  3005. /* If the arguments are invalid, the loop should not be kept open */
  3006. loop = uv_default_loop();
  3007. r = uv_fs_read(loop, &read_req, 0, NULL, 0, -1, fail_cb);
  3008. ASSERT(r == UV_EINVAL);
  3009. uv_run(loop, UV_RUN_DEFAULT);
  3010. uv_fs_req_cleanup(&read_req);
  3011. r = uv_fs_write(loop, &write_req, 0, NULL, 0, -1, fail_cb);
  3012. ASSERT(r == UV_EINVAL);
  3013. uv_run(loop, UV_RUN_DEFAULT);
  3014. uv_fs_req_cleanup(&write_req);
  3015. iov = uv_buf_init(NULL, 0);
  3016. r = uv_fs_read(loop, &read_req, 0, &iov, 0, -1, fail_cb);
  3017. ASSERT(r == UV_EINVAL);
  3018. uv_run(loop, UV_RUN_DEFAULT);
  3019. uv_fs_req_cleanup(&read_req);
  3020. iov = uv_buf_init(NULL, 0);
  3021. r = uv_fs_write(loop, &write_req, 0, &iov, 0, -1, fail_cb);
  3022. ASSERT(r == UV_EINVAL);
  3023. uv_run(loop, UV_RUN_DEFAULT);
  3024. uv_fs_req_cleanup(&write_req);
  3025. return 0;
  3026. }
  3027. TEST_IMPL(get_osfhandle_valid_handle) {
  3028. int r;
  3029. uv_os_fd_t fd;
  3030. /* Setup. */
  3031. unlink("test_file");
  3032. loop = uv_default_loop();
  3033. r = uv_fs_open(NULL,
  3034. &open_req1,
  3035. "test_file",
  3036. O_RDWR | O_CREAT,
  3037. S_IWUSR | S_IRUSR,
  3038. NULL);
  3039. ASSERT(r >= 0);
  3040. ASSERT(open_req1.result >= 0);
  3041. uv_fs_req_cleanup(&open_req1);
  3042. fd = uv_get_osfhandle(open_req1.result);
  3043. #ifdef _WIN32
  3044. ASSERT(fd != INVALID_HANDLE_VALUE);
  3045. #else
  3046. ASSERT(fd >= 0);
  3047. #endif
  3048. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3049. ASSERT(r == 0);
  3050. ASSERT(close_req.result == 0);
  3051. uv_fs_req_cleanup(&close_req);
  3052. /* Cleanup. */
  3053. unlink("test_file");
  3054. MAKE_VALGRIND_HAPPY();
  3055. return 0;
  3056. }
  3057. TEST_IMPL(open_osfhandle_valid_handle) {
  3058. int r;
  3059. uv_os_fd_t handle;
  3060. int fd;
  3061. /* Setup. */
  3062. unlink("test_file");
  3063. loop = uv_default_loop();
  3064. r = uv_fs_open(NULL,
  3065. &open_req1,
  3066. "test_file",
  3067. O_RDWR | O_CREAT,
  3068. S_IWUSR | S_IRUSR,
  3069. NULL);
  3070. ASSERT(r >= 0);
  3071. ASSERT(open_req1.result >= 0);
  3072. uv_fs_req_cleanup(&open_req1);
  3073. handle = uv_get_osfhandle(open_req1.result);
  3074. #ifdef _WIN32
  3075. ASSERT(handle != INVALID_HANDLE_VALUE);
  3076. #else
  3077. ASSERT(handle >= 0);
  3078. #endif
  3079. fd = uv_open_osfhandle(handle);
  3080. #ifdef _WIN32
  3081. ASSERT(fd > 0);
  3082. #else
  3083. ASSERT(fd == open_req1.result);
  3084. #endif
  3085. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3086. ASSERT(r == 0);
  3087. ASSERT(close_req.result == 0);
  3088. uv_fs_req_cleanup(&close_req);
  3089. /* Cleanup. */
  3090. unlink("test_file");
  3091. MAKE_VALGRIND_HAPPY();
  3092. return 0;
  3093. }
  3094. TEST_IMPL(fs_file_pos_after_op_with_offset) {
  3095. int r;
  3096. /* Setup. */
  3097. unlink("test_file");
  3098. loop = uv_default_loop();
  3099. r = uv_fs_open(loop,
  3100. &open_req1,
  3101. "test_file",
  3102. O_RDWR | O_CREAT,
  3103. S_IWUSR | S_IRUSR,
  3104. NULL);
  3105. ASSERT(r > 0);
  3106. uv_fs_req_cleanup(&open_req1);
  3107. iov = uv_buf_init(test_buf, sizeof(test_buf));
  3108. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 0, NULL);
  3109. ASSERT(r == sizeof(test_buf));
  3110. ASSERT(lseek(open_req1.result, 0, SEEK_CUR) == 0);
  3111. uv_fs_req_cleanup(&write_req);
  3112. iov = uv_buf_init(buf, sizeof(buf));
  3113. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 0, NULL);
  3114. ASSERT(r == sizeof(test_buf));
  3115. ASSERT(strcmp(buf, test_buf) == 0);
  3116. ASSERT(lseek(open_req1.result, 0, SEEK_CUR) == 0);
  3117. uv_fs_req_cleanup(&read_req);
  3118. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3119. ASSERT(r == 0);
  3120. uv_fs_req_cleanup(&close_req);
  3121. /* Cleanup */
  3122. unlink("test_file");
  3123. MAKE_VALGRIND_HAPPY();
  3124. return 0;
  3125. }
  3126. #ifdef _WIN32
  3127. static void fs_file_pos_common() {
  3128. int r;
  3129. iov = uv_buf_init("abc", 3);
  3130. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  3131. ASSERT(r == 3);
  3132. uv_fs_req_cleanup(&write_req);
  3133. /* Read with offset should not change the position */
  3134. iov = uv_buf_init(buf, 1);
  3135. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 1, NULL);
  3136. ASSERT(r == 1);
  3137. ASSERT(buf[0] == 'b');
  3138. uv_fs_req_cleanup(&read_req);
  3139. iov = uv_buf_init(buf, sizeof(buf));
  3140. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  3141. ASSERT(r == 0);
  3142. uv_fs_req_cleanup(&read_req);
  3143. /* Write without offset should change the position */
  3144. iov = uv_buf_init("d", 1);
  3145. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  3146. ASSERT(r == 1);
  3147. uv_fs_req_cleanup(&write_req);
  3148. iov = uv_buf_init(buf, sizeof(buf));
  3149. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  3150. ASSERT(r == 0);
  3151. uv_fs_req_cleanup(&read_req);
  3152. }
  3153. static void fs_file_pos_close_check(const char *contents, int size) {
  3154. int r;
  3155. /* Close */
  3156. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3157. ASSERT(r == 0);
  3158. uv_fs_req_cleanup(&close_req);
  3159. /* Confirm file contents */
  3160. r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY, 0, NULL);
  3161. ASSERT(r >= 0);
  3162. ASSERT(open_req1.result >= 0);
  3163. uv_fs_req_cleanup(&open_req1);
  3164. iov = uv_buf_init(buf, sizeof(buf));
  3165. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  3166. ASSERT(r == size);
  3167. ASSERT(strncmp(buf, contents, size) == 0);
  3168. uv_fs_req_cleanup(&read_req);
  3169. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3170. ASSERT(r == 0);
  3171. uv_fs_req_cleanup(&close_req);
  3172. /* Cleanup */
  3173. unlink("test_file");
  3174. }
  3175. static void fs_file_pos_write(int add_flags) {
  3176. int r;
  3177. /* Setup. */
  3178. unlink("test_file");
  3179. r = uv_fs_open(NULL,
  3180. &open_req1,
  3181. "test_file",
  3182. O_TRUNC | O_CREAT | O_RDWR | add_flags,
  3183. S_IWUSR | S_IRUSR,
  3184. NULL);
  3185. ASSERT(r > 0);
  3186. uv_fs_req_cleanup(&open_req1);
  3187. fs_file_pos_common();
  3188. /* Write with offset should not change the position */
  3189. iov = uv_buf_init("e", 1);
  3190. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 1, NULL);
  3191. ASSERT(r == 1);
  3192. uv_fs_req_cleanup(&write_req);
  3193. iov = uv_buf_init(buf, sizeof(buf));
  3194. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  3195. ASSERT(r == 0);
  3196. uv_fs_req_cleanup(&read_req);
  3197. fs_file_pos_close_check("aecd", 4);
  3198. }
  3199. TEST_IMPL(fs_file_pos_write) {
  3200. fs_file_pos_write(0);
  3201. fs_file_pos_write(UV_FS_O_FILEMAP);
  3202. MAKE_VALGRIND_HAPPY();
  3203. return 0;
  3204. }
  3205. static void fs_file_pos_append(int add_flags) {
  3206. int r;
  3207. /* Setup. */
  3208. unlink("test_file");
  3209. r = uv_fs_open(NULL,
  3210. &open_req1,
  3211. "test_file",
  3212. O_APPEND | O_CREAT | O_RDWR | add_flags,
  3213. S_IWUSR | S_IRUSR,
  3214. NULL);
  3215. ASSERT(r > 0);
  3216. uv_fs_req_cleanup(&open_req1);
  3217. fs_file_pos_common();
  3218. /* Write with offset appends (ignoring offset)
  3219. * but does not change the position */
  3220. iov = uv_buf_init("e", 1);
  3221. r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 1, NULL);
  3222. ASSERT(r == 1);
  3223. uv_fs_req_cleanup(&write_req);
  3224. iov = uv_buf_init(buf, sizeof(buf));
  3225. r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
  3226. ASSERT(r == 1);
  3227. ASSERT(buf[0] == 'e');
  3228. uv_fs_req_cleanup(&read_req);
  3229. fs_file_pos_close_check("abcde", 5);
  3230. }
  3231. TEST_IMPL(fs_file_pos_append) {
  3232. fs_file_pos_append(0);
  3233. fs_file_pos_append(UV_FS_O_FILEMAP);
  3234. MAKE_VALGRIND_HAPPY();
  3235. return 0;
  3236. }
  3237. #endif
  3238. TEST_IMPL(fs_null_req) {
  3239. /* Verify that all fs functions return UV_EINVAL when the request is NULL. */
  3240. int r;
  3241. r = uv_fs_open(NULL, NULL, NULL, 0, 0, NULL);
  3242. ASSERT(r == UV_EINVAL);
  3243. r = uv_fs_close(NULL, NULL, 0, NULL);
  3244. ASSERT(r == UV_EINVAL);
  3245. r = uv_fs_read(NULL, NULL, 0, NULL, 0, -1, NULL);
  3246. ASSERT(r == UV_EINVAL);
  3247. r = uv_fs_write(NULL, NULL, 0, NULL, 0, -1, NULL);
  3248. ASSERT(r == UV_EINVAL);
  3249. r = uv_fs_unlink(NULL, NULL, NULL, NULL);
  3250. ASSERT(r == UV_EINVAL);
  3251. r = uv_fs_mkdir(NULL, NULL, NULL, 0, NULL);
  3252. ASSERT(r == UV_EINVAL);
  3253. r = uv_fs_mkdtemp(NULL, NULL, NULL, NULL);
  3254. ASSERT(r == UV_EINVAL);
  3255. r = uv_fs_mkstemp(NULL, NULL, NULL, NULL);
  3256. ASSERT(r == UV_EINVAL);
  3257. r = uv_fs_rmdir(NULL, NULL, NULL, NULL);
  3258. ASSERT(r == UV_EINVAL);
  3259. r = uv_fs_scandir(NULL, NULL, NULL, 0, NULL);
  3260. ASSERT(r == UV_EINVAL);
  3261. r = uv_fs_link(NULL, NULL, NULL, NULL, NULL);
  3262. ASSERT(r == UV_EINVAL);
  3263. r = uv_fs_symlink(NULL, NULL, NULL, NULL, 0, NULL);
  3264. ASSERT(r == UV_EINVAL);
  3265. r = uv_fs_readlink(NULL, NULL, NULL, NULL);
  3266. ASSERT(r == UV_EINVAL);
  3267. r = uv_fs_realpath(NULL, NULL, NULL, NULL);
  3268. ASSERT(r == UV_EINVAL);
  3269. r = uv_fs_chown(NULL, NULL, NULL, 0, 0, NULL);
  3270. ASSERT(r == UV_EINVAL);
  3271. r = uv_fs_fchown(NULL, NULL, 0, 0, 0, NULL);
  3272. ASSERT(r == UV_EINVAL);
  3273. r = uv_fs_stat(NULL, NULL, NULL, NULL);
  3274. ASSERT(r == UV_EINVAL);
  3275. r = uv_fs_lstat(NULL, NULL, NULL, NULL);
  3276. ASSERT(r == UV_EINVAL);
  3277. r = uv_fs_fstat(NULL, NULL, 0, NULL);
  3278. ASSERT(r == UV_EINVAL);
  3279. r = uv_fs_rename(NULL, NULL, NULL, NULL, NULL);
  3280. ASSERT(r == UV_EINVAL);
  3281. r = uv_fs_fsync(NULL, NULL, 0, NULL);
  3282. ASSERT(r == UV_EINVAL);
  3283. r = uv_fs_fdatasync(NULL, NULL, 0, NULL);
  3284. ASSERT(r == UV_EINVAL);
  3285. r = uv_fs_ftruncate(NULL, NULL, 0, 0, NULL);
  3286. ASSERT(r == UV_EINVAL);
  3287. r = uv_fs_copyfile(NULL, NULL, NULL, NULL, 0, NULL);
  3288. ASSERT(r == UV_EINVAL);
  3289. r = uv_fs_sendfile(NULL, NULL, 0, 0, 0, 0, NULL);
  3290. ASSERT(r == UV_EINVAL);
  3291. r = uv_fs_access(NULL, NULL, NULL, 0, NULL);
  3292. ASSERT(r == UV_EINVAL);
  3293. r = uv_fs_chmod(NULL, NULL, NULL, 0, NULL);
  3294. ASSERT(r == UV_EINVAL);
  3295. r = uv_fs_fchmod(NULL, NULL, 0, 0, NULL);
  3296. ASSERT(r == UV_EINVAL);
  3297. r = uv_fs_utime(NULL, NULL, NULL, 0.0, 0.0, NULL);
  3298. ASSERT(r == UV_EINVAL);
  3299. r = uv_fs_futime(NULL, NULL, 0, 0.0, 0.0, NULL);
  3300. ASSERT(r == UV_EINVAL);
  3301. r = uv_fs_statfs(NULL, NULL, NULL, NULL);
  3302. ASSERT(r == UV_EINVAL);
  3303. /* This should be a no-op. */
  3304. uv_fs_req_cleanup(NULL);
  3305. return 0;
  3306. }
  3307. #ifdef _WIN32
  3308. TEST_IMPL(fs_exclusive_sharing_mode) {
  3309. int r;
  3310. /* Setup. */
  3311. unlink("test_file");
  3312. ASSERT(UV_FS_O_EXLOCK > 0);
  3313. r = uv_fs_open(NULL,
  3314. &open_req1,
  3315. "test_file",
  3316. O_RDWR | O_CREAT | UV_FS_O_EXLOCK,
  3317. S_IWUSR | S_IRUSR,
  3318. NULL);
  3319. ASSERT(r >= 0);
  3320. ASSERT(open_req1.result >= 0);
  3321. uv_fs_req_cleanup(&open_req1);
  3322. r = uv_fs_open(NULL,
  3323. &open_req2,
  3324. "test_file",
  3325. O_RDONLY | UV_FS_O_EXLOCK,
  3326. S_IWUSR | S_IRUSR,
  3327. NULL);
  3328. ASSERT(r < 0);
  3329. ASSERT(open_req2.result < 0);
  3330. uv_fs_req_cleanup(&open_req2);
  3331. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3332. ASSERT(r == 0);
  3333. ASSERT(close_req.result == 0);
  3334. uv_fs_req_cleanup(&close_req);
  3335. r = uv_fs_open(NULL,
  3336. &open_req2,
  3337. "test_file",
  3338. O_RDONLY | UV_FS_O_EXLOCK,
  3339. S_IWUSR | S_IRUSR,
  3340. NULL);
  3341. ASSERT(r >= 0);
  3342. ASSERT(open_req2.result >= 0);
  3343. uv_fs_req_cleanup(&open_req2);
  3344. r = uv_fs_close(NULL, &close_req, open_req2.result, NULL);
  3345. ASSERT(r == 0);
  3346. ASSERT(close_req.result == 0);
  3347. uv_fs_req_cleanup(&close_req);
  3348. /* Cleanup */
  3349. unlink("test_file");
  3350. MAKE_VALGRIND_HAPPY();
  3351. return 0;
  3352. }
  3353. #endif
  3354. #ifdef _WIN32
  3355. TEST_IMPL(fs_file_flag_no_buffering) {
  3356. int r;
  3357. /* Setup. */
  3358. unlink("test_file");
  3359. ASSERT(UV_FS_O_APPEND > 0);
  3360. ASSERT(UV_FS_O_CREAT > 0);
  3361. ASSERT(UV_FS_O_DIRECT > 0);
  3362. ASSERT(UV_FS_O_RDWR > 0);
  3363. /* FILE_APPEND_DATA must be excluded from FILE_GENERIC_WRITE: */
  3364. r = uv_fs_open(NULL,
  3365. &open_req1,
  3366. "test_file",
  3367. UV_FS_O_RDWR | UV_FS_O_CREAT | UV_FS_O_DIRECT,
  3368. S_IWUSR | S_IRUSR,
  3369. NULL);
  3370. ASSERT(r >= 0);
  3371. ASSERT(open_req1.result >= 0);
  3372. uv_fs_req_cleanup(&open_req1);
  3373. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3374. ASSERT(r == 0);
  3375. ASSERT(close_req.result == 0);
  3376. uv_fs_req_cleanup(&close_req);
  3377. /* FILE_APPEND_DATA and FILE_FLAG_NO_BUFFERING are mutually exclusive: */
  3378. r = uv_fs_open(NULL,
  3379. &open_req2,
  3380. "test_file",
  3381. UV_FS_O_APPEND | UV_FS_O_DIRECT,
  3382. S_IWUSR | S_IRUSR,
  3383. NULL);
  3384. ASSERT(r == UV_EINVAL);
  3385. ASSERT(open_req2.result == UV_EINVAL);
  3386. uv_fs_req_cleanup(&open_req2);
  3387. /* Cleanup */
  3388. unlink("test_file");
  3389. MAKE_VALGRIND_HAPPY();
  3390. return 0;
  3391. }
  3392. #endif
  3393. #ifdef _WIN32
  3394. int call_icacls(const char* command, ...) {
  3395. char icacls_command[1024];
  3396. va_list args;
  3397. va_start(args, command);
  3398. vsnprintf(icacls_command, ARRAYSIZE(icacls_command), command, args);
  3399. va_end(args);
  3400. return system(icacls_command);
  3401. }
  3402. TEST_IMPL(fs_open_readonly_acl) {
  3403. uv_passwd_t pwd;
  3404. uv_fs_t req;
  3405. int r;
  3406. /*
  3407. Based on Node.js test from
  3408. https://github.com/nodejs/node/commit/3ba81e34e86a5c32658e218cb6e65b13e8326bc5
  3409. If anything goes wrong, you can delte the test_fle_icacls with:
  3410. icacls test_file_icacls /remove "%USERNAME%" /inheritance:e
  3411. attrib -r test_file_icacls
  3412. del test_file_icacls
  3413. */
  3414. /* Setup - clear the ACL and remove the file */
  3415. loop = uv_default_loop();
  3416. r = uv_os_get_passwd(&pwd);
  3417. ASSERT(r == 0);
  3418. call_icacls("icacls test_file_icacls /remove \"%s\" /inheritance:e",
  3419. pwd.username);
  3420. uv_fs_chmod(loop, &req, "test_file_icacls", S_IWUSR, NULL);
  3421. unlink("test_file_icacls");
  3422. /* Create the file */
  3423. r = uv_fs_open(loop,
  3424. &open_req1,
  3425. "test_file_icacls",
  3426. O_RDONLY | O_CREAT,
  3427. S_IRUSR,
  3428. NULL);
  3429. ASSERT(r >= 0);
  3430. ASSERT(open_req1.result >= 0);
  3431. uv_fs_req_cleanup(&open_req1);
  3432. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3433. ASSERT(r == 0);
  3434. ASSERT(close_req.result == 0);
  3435. uv_fs_req_cleanup(&close_req);
  3436. /* Set up ACL */
  3437. r = call_icacls("icacls test_file_icacls /inheritance:r /remove \"%s\"",
  3438. pwd.username);
  3439. if (r != 0) {
  3440. goto acl_cleanup;
  3441. }
  3442. r = call_icacls("icacls test_file_icacls /grant \"%s\":RX", pwd.username);
  3443. if (r != 0) {
  3444. goto acl_cleanup;
  3445. }
  3446. /* Try opening the file */
  3447. r = uv_fs_open(NULL, &open_req1, "test_file_icacls", O_RDONLY, 0, NULL);
  3448. if (r < 0) {
  3449. goto acl_cleanup;
  3450. }
  3451. uv_fs_req_cleanup(&open_req1);
  3452. r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
  3453. if (r != 0) {
  3454. goto acl_cleanup;
  3455. }
  3456. uv_fs_req_cleanup(&close_req);
  3457. acl_cleanup:
  3458. /* Cleanup */
  3459. call_icacls("icacls test_file_icacls /remove \"%s\" /inheritance:e",
  3460. pwd.username);
  3461. unlink("test_file_icacls");
  3462. uv_os_free_passwd(&pwd);
  3463. ASSERT(r == 0);
  3464. MAKE_VALGRIND_HAPPY();
  3465. return 0;
  3466. }
  3467. #endif
  3468. #ifdef _WIN32
  3469. TEST_IMPL(fs_fchmod_archive_readonly) {
  3470. uv_fs_t req;
  3471. uv_file file;
  3472. int r;
  3473. /* Test clearing read-only flag from files with Archive flag cleared */
  3474. /* Setup*/
  3475. unlink("test_file");
  3476. r = uv_fs_open(NULL,
  3477. &req,
  3478. "test_file",
  3479. O_WRONLY | O_CREAT,
  3480. S_IWUSR | S_IRUSR,
  3481. NULL);
  3482. ASSERT(r >= 0);
  3483. ASSERT(req.result >= 0);
  3484. file = req.result;
  3485. uv_fs_req_cleanup(&req);
  3486. r = uv_fs_close(NULL, &req, file, NULL);
  3487. ASSERT(r == 0);
  3488. uv_fs_req_cleanup(&req);
  3489. /* Make the file read-only and clear archive flag */
  3490. r = SetFileAttributes("test_file", FILE_ATTRIBUTE_READONLY);
  3491. ASSERT(r != 0);
  3492. check_permission("test_file", 0400);
  3493. /* Try fchmod */
  3494. r = uv_fs_open(NULL, &req, "test_file", O_RDONLY, 0, NULL);
  3495. ASSERT(r >= 0);
  3496. ASSERT(req.result >= 0);
  3497. file = req.result;
  3498. uv_fs_req_cleanup(&req);
  3499. r = uv_fs_fchmod(NULL, &req, file, S_IWUSR, NULL);
  3500. ASSERT(r == 0);
  3501. ASSERT(req.result == 0);
  3502. uv_fs_req_cleanup(&req);
  3503. r = uv_fs_close(NULL, &req, file, NULL);
  3504. ASSERT(r == 0);
  3505. uv_fs_req_cleanup(&req);
  3506. check_permission("test_file", S_IWUSR);
  3507. /* Restore Archive flag for rest of the tests */
  3508. r = SetFileAttributes("test_file", FILE_ATTRIBUTE_ARCHIVE);
  3509. ASSERT(r != 0);
  3510. return 0;
  3511. }
  3512. TEST_IMPL(fs_invalid_mkdir_name) {
  3513. uv_loop_t* loop;
  3514. uv_fs_t req;
  3515. int r;
  3516. loop = uv_default_loop();
  3517. r = uv_fs_mkdir(loop, &req, "invalid>", 0, NULL);
  3518. ASSERT(r == UV_EINVAL);
  3519. return 0;
  3520. }
  3521. #endif
  3522. TEST_IMPL(fs_statfs) {
  3523. uv_fs_t req;
  3524. int r;
  3525. loop = uv_default_loop();
  3526. /* Test the synchronous version. */
  3527. r = uv_fs_statfs(NULL, &req, ".", NULL);
  3528. ASSERT(r == 0);
  3529. statfs_cb(&req);
  3530. ASSERT(statfs_cb_count == 1);
  3531. /* Test the asynchronous version. */
  3532. r = uv_fs_statfs(loop, &req, ".", statfs_cb);
  3533. ASSERT(r == 0);
  3534. uv_run(loop, UV_RUN_DEFAULT);
  3535. ASSERT(statfs_cb_count == 2);
  3536. return 0;
  3537. }