12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349 |
- /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
- #include "uv.h"
- #include "task.h"
- #include <errno.h>
- #include <string.h> /* memset */
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <limits.h> /* INT_MAX, PATH_MAX, IOV_MAX */
- /* FIXME we shouldn't need to branch in this file */
- #if defined(__unix__) || defined(__POSIX__) || \
- defined(__APPLE__) || defined(__sun) || \
- defined(_AIX) || defined(__MVS__) || \
- defined(__HAIKU__)
- #include <unistd.h> /* unlink, rmdir, etc. */
- #else
- # include <winioctl.h>
- # include <direct.h>
- # include <io.h>
- # ifndef ERROR_SYMLINK_NOT_SUPPORTED
- # define ERROR_SYMLINK_NOT_SUPPORTED 1464
- # endif
- # define unlink _unlink
- # define rmdir _rmdir
- # define open _open
- # define write _write
- # define close _close
- # ifndef stat
- # define stat _stati64
- # endif
- # ifndef lseek
- # define lseek _lseek
- # endif
- #endif
- #define TOO_LONG_NAME_LENGTH 65536
- #define PATHMAX 4096
- typedef struct {
- const char* path;
- double atime;
- double mtime;
- } utime_check_t;
- static int dummy_cb_count;
- static int close_cb_count;
- static int create_cb_count;
- static int open_cb_count;
- static int read_cb_count;
- static int write_cb_count;
- static int unlink_cb_count;
- static int mkdir_cb_count;
- static int mkdtemp_cb_count;
- static int mkstemp_cb_count;
- static int rmdir_cb_count;
- static int scandir_cb_count;
- static int stat_cb_count;
- static int rename_cb_count;
- static int fsync_cb_count;
- static int fdatasync_cb_count;
- static int ftruncate_cb_count;
- static int sendfile_cb_count;
- static int fstat_cb_count;
- static int access_cb_count;
- static int chmod_cb_count;
- static int fchmod_cb_count;
- static int chown_cb_count;
- static int fchown_cb_count;
- static int lchown_cb_count;
- static int link_cb_count;
- static int symlink_cb_count;
- static int readlink_cb_count;
- static int realpath_cb_count;
- static int utime_cb_count;
- static int futime_cb_count;
- static int lutime_cb_count;
- static int statfs_cb_count;
- static uv_loop_t* loop;
- static uv_fs_t open_req1;
- static uv_fs_t open_req2;
- static uv_fs_t read_req;
- static uv_fs_t write_req;
- static uv_fs_t unlink_req;
- static uv_fs_t close_req;
- static uv_fs_t mkdir_req;
- static uv_fs_t mkdtemp_req1;
- static uv_fs_t mkdtemp_req2;
- static uv_fs_t mkstemp_req1;
- static uv_fs_t mkstemp_req2;
- static uv_fs_t mkstemp_req3;
- static uv_fs_t rmdir_req;
- static uv_fs_t scandir_req;
- static uv_fs_t stat_req;
- static uv_fs_t rename_req;
- static uv_fs_t fsync_req;
- static uv_fs_t fdatasync_req;
- static uv_fs_t ftruncate_req;
- static uv_fs_t sendfile_req;
- static uv_fs_t utime_req;
- static uv_fs_t futime_req;
- static char buf[32];
- static char buf2[32];
- static char test_buf[] = "test-buffer\n";
- static char test_buf2[] = "second-buffer\n";
- static uv_buf_t iov;
- #ifdef _WIN32
- int uv_test_getiovmax(void) {
- return INT32_MAX; /* Emulated by libuv, so no real limit. */
- }
- #else
- int uv_test_getiovmax(void) {
- #if defined(IOV_MAX)
- return IOV_MAX;
- #elif defined(_SC_IOV_MAX)
- static int iovmax = -1;
- if (iovmax == -1) {
- iovmax = sysconf(_SC_IOV_MAX);
- /* On some embedded devices (arm-linux-uclibc based ip camera),
- * sysconf(_SC_IOV_MAX) can not get the correct value. The return
- * value is -1 and the errno is EINPROGRESS. Degrade the value to 1.
- */
- if (iovmax == -1) iovmax = 1;
- }
- return iovmax;
- #else
- return 1024;
- #endif
- }
- #endif
- #ifdef _WIN32
- /*
- * This tag and guid have no special meaning, and don't conflict with
- * reserved ids.
- */
- static unsigned REPARSE_TAG = 0x9913;
- static GUID REPARSE_GUID = {
- 0x1bf6205f, 0x46ae, 0x4527,
- { 0xb1, 0x0c, 0xc5, 0x09, 0xb7, 0x55, 0x22, 0x80 }};
- #endif
- static void check_permission(const char* filename, unsigned int mode) {
- int r;
- uv_fs_t req;
- uv_stat_t* s;
- r = uv_fs_stat(NULL, &req, filename, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- s = &req.statbuf;
- #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MSYS__)
- /*
- * On Windows, chmod can only modify S_IWUSR (_S_IWRITE) bit,
- * so only testing for the specified flags.
- */
- ASSERT((s->st_mode & 0777) & mode);
- #else
- ASSERT((s->st_mode & 0777) == mode);
- #endif
- uv_fs_req_cleanup(&req);
- }
- static void dummy_cb(uv_fs_t* req) {
- (void) req;
- dummy_cb_count++;
- }
- static void link_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_LINK);
- ASSERT(req->result == 0);
- link_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void symlink_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_SYMLINK);
- ASSERT(req->result == 0);
- symlink_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void readlink_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_READLINK);
- ASSERT(req->result == 0);
- ASSERT(strcmp(req->ptr, "test_file_symlink2") == 0);
- readlink_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void realpath_cb(uv_fs_t* req) {
- char test_file_abs_buf[PATHMAX];
- size_t test_file_abs_size = sizeof(test_file_abs_buf);
- ASSERT(req->fs_type == UV_FS_REALPATH);
- #ifdef _WIN32
- /*
- * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
- */
- if (req->result == UV_ENOSYS) {
- realpath_cb_count++;
- uv_fs_req_cleanup(req);
- return;
- }
- #endif
- ASSERT(req->result == 0);
- uv_cwd(test_file_abs_buf, &test_file_abs_size);
- #ifdef _WIN32
- strcat(test_file_abs_buf, "\\test_file");
- ASSERT(stricmp(req->ptr, test_file_abs_buf) == 0);
- #else
- strcat(test_file_abs_buf, "/test_file");
- ASSERT(strcmp(req->ptr, test_file_abs_buf) == 0);
- #endif
- realpath_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void access_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_ACCESS);
- access_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void fchmod_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_FCHMOD);
- ASSERT(req->result == 0);
- fchmod_cb_count++;
- uv_fs_req_cleanup(req);
- check_permission("test_file", *(int*)req->data);
- }
- static void chmod_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_CHMOD);
- ASSERT(req->result == 0);
- chmod_cb_count++;
- uv_fs_req_cleanup(req);
- check_permission("test_file", *(int*)req->data);
- }
- static void fchown_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_FCHOWN);
- ASSERT(req->result == 0);
- fchown_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void chown_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_CHOWN);
- ASSERT(req->result == 0);
- chown_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void lchown_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_LCHOWN);
- ASSERT(req->result == 0);
- lchown_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void chown_root_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_CHOWN);
- #if defined(_WIN32) || defined(__MSYS__)
- /* On windows, chown is a no-op and always succeeds. */
- ASSERT(req->result == 0);
- #else
- /* On unix, chown'ing the root directory is not allowed -
- * unless you're root, of course.
- */
- if (geteuid() == 0)
- ASSERT(req->result == 0);
- else
- # if defined(__CYGWIN__)
- /* On Cygwin, uid 0 is invalid (no root). */
- ASSERT(req->result == UV_EINVAL);
- # elif defined(__PASE__)
- /* On IBMi PASE, there is no root user. uid 0 is user qsecofr.
- * User may grant qsecofr's privileges, including changing
- * the file's ownership to uid 0.
- */
- ASSERT(req->result == 0);
- # else
- ASSERT(req->result == UV_EPERM);
- # endif
- #endif
- chown_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void unlink_cb(uv_fs_t* req) {
- ASSERT(req == &unlink_req);
- ASSERT(req->fs_type == UV_FS_UNLINK);
- ASSERT(req->result == 0);
- unlink_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void fstat_cb(uv_fs_t* req) {
- uv_stat_t* s = req->ptr;
- ASSERT(req->fs_type == UV_FS_FSTAT);
- ASSERT(req->result == 0);
- ASSERT(s->st_size == sizeof(test_buf));
- uv_fs_req_cleanup(req);
- fstat_cb_count++;
- }
- static void statfs_cb(uv_fs_t* req) {
- uv_statfs_t* stats;
- ASSERT(req->fs_type == UV_FS_STATFS);
- ASSERT(req->result == 0);
- ASSERT(req->ptr != NULL);
- stats = req->ptr;
- #if defined(_WIN32) || defined(__sun) || defined(_AIX) || defined(__MVS__) || \
- defined(__OpenBSD__) || defined(__NetBSD__)
- ASSERT(stats->f_type == 0);
- #else
- ASSERT(stats->f_type > 0);
- #endif
- ASSERT(stats->f_bsize > 0);
- ASSERT(stats->f_blocks > 0);
- ASSERT(stats->f_bfree <= stats->f_blocks);
- ASSERT(stats->f_bavail <= stats->f_bfree);
- #ifdef _WIN32
- ASSERT(stats->f_files == 0);
- ASSERT(stats->f_ffree == 0);
- #else
- /* There is no assertion for stats->f_files that makes sense, so ignore it. */
- ASSERT(stats->f_ffree <= stats->f_files);
- #endif
- uv_fs_req_cleanup(req);
- ASSERT(req->ptr == NULL);
- statfs_cb_count++;
- }
- static void close_cb(uv_fs_t* req) {
- int r;
- ASSERT(req == &close_req);
- ASSERT(req->fs_type == UV_FS_CLOSE);
- ASSERT(req->result == 0);
- close_cb_count++;
- uv_fs_req_cleanup(req);
- if (close_cb_count == 3) {
- r = uv_fs_unlink(loop, &unlink_req, "test_file2", unlink_cb);
- ASSERT(r == 0);
- }
- }
- static void ftruncate_cb(uv_fs_t* req) {
- int r;
- ASSERT(req == &ftruncate_req);
- ASSERT(req->fs_type == UV_FS_FTRUNCATE);
- ASSERT(req->result == 0);
- ftruncate_cb_count++;
- uv_fs_req_cleanup(req);
- r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
- ASSERT(r == 0);
- }
- static void fail_cb(uv_fs_t* req) {
- FATAL("fail_cb should not have been called");
- }
- static void read_cb(uv_fs_t* req) {
- int r;
- ASSERT(req == &read_req);
- ASSERT(req->fs_type == UV_FS_READ);
- ASSERT(req->result >= 0); /* FIXME(bnoordhuis) Check if requested size? */
- read_cb_count++;
- uv_fs_req_cleanup(req);
- if (read_cb_count == 1) {
- ASSERT(strcmp(buf, test_buf) == 0);
- r = uv_fs_ftruncate(loop, &ftruncate_req, open_req1.result, 7,
- ftruncate_cb);
- } else {
- ASSERT(strcmp(buf, "test-bu") == 0);
- r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
- }
- ASSERT(r == 0);
- }
- static void open_cb(uv_fs_t* req) {
- int r;
- ASSERT(req == &open_req1);
- ASSERT(req->fs_type == UV_FS_OPEN);
- if (req->result < 0) {
- fprintf(stderr, "async open error: %d\n", (int) req->result);
- ASSERT(0);
- }
- open_cb_count++;
- ASSERT(req->path);
- ASSERT(memcmp(req->path, "test_file2\0", 11) == 0);
- uv_fs_req_cleanup(req);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(loop, &read_req, open_req1.result, &iov, 1, -1,
- read_cb);
- ASSERT(r == 0);
- }
- static void open_cb_simple(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_OPEN);
- if (req->result < 0) {
- fprintf(stderr, "async open error: %d\n", (int) req->result);
- ASSERT(0);
- }
- open_cb_count++;
- ASSERT(req->path);
- uv_fs_req_cleanup(req);
- }
- static void fsync_cb(uv_fs_t* req) {
- int r;
- ASSERT(req == &fsync_req);
- ASSERT(req->fs_type == UV_FS_FSYNC);
- ASSERT(req->result == 0);
- fsync_cb_count++;
- uv_fs_req_cleanup(req);
- r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
- ASSERT(r == 0);
- }
- static void fdatasync_cb(uv_fs_t* req) {
- int r;
- ASSERT(req == &fdatasync_req);
- ASSERT(req->fs_type == UV_FS_FDATASYNC);
- ASSERT(req->result == 0);
- fdatasync_cb_count++;
- uv_fs_req_cleanup(req);
- r = uv_fs_fsync(loop, &fsync_req, open_req1.result, fsync_cb);
- ASSERT(r == 0);
- }
- static void write_cb(uv_fs_t* req) {
- int r;
- ASSERT(req == &write_req);
- ASSERT(req->fs_type == UV_FS_WRITE);
- ASSERT(req->result >= 0); /* FIXME(bnoordhuis) Check if requested size? */
- write_cb_count++;
- uv_fs_req_cleanup(req);
- r = uv_fs_fdatasync(loop, &fdatasync_req, open_req1.result, fdatasync_cb);
- ASSERT(r == 0);
- }
- static void create_cb(uv_fs_t* req) {
- int r;
- ASSERT(req == &open_req1);
- ASSERT(req->fs_type == UV_FS_OPEN);
- ASSERT(req->result >= 0);
- create_cb_count++;
- uv_fs_req_cleanup(req);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(loop, &write_req, req->result, &iov, 1, -1, write_cb);
- ASSERT(r == 0);
- }
- static void rename_cb(uv_fs_t* req) {
- ASSERT(req == &rename_req);
- ASSERT(req->fs_type == UV_FS_RENAME);
- ASSERT(req->result == 0);
- rename_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void mkdir_cb(uv_fs_t* req) {
- ASSERT(req == &mkdir_req);
- ASSERT(req->fs_type == UV_FS_MKDIR);
- ASSERT(req->result == 0);
- mkdir_cb_count++;
- ASSERT(req->path);
- ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
- uv_fs_req_cleanup(req);
- }
- static void check_mkdtemp_result(uv_fs_t* req) {
- int r;
- ASSERT(req->fs_type == UV_FS_MKDTEMP);
- ASSERT(req->result == 0);
- ASSERT(req->path);
- ASSERT(strlen(req->path) == 15);
- ASSERT(memcmp(req->path, "test_dir_", 9) == 0);
- ASSERT(memcmp(req->path + 9, "XXXXXX", 6) != 0);
- check_permission(req->path, 0700);
- /* Check if req->path is actually a directory */
- r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
- ASSERT(r == 0);
- ASSERT(((uv_stat_t*)stat_req.ptr)->st_mode & S_IFDIR);
- uv_fs_req_cleanup(&stat_req);
- }
- static void mkdtemp_cb(uv_fs_t* req) {
- ASSERT(req == &mkdtemp_req1);
- check_mkdtemp_result(req);
- mkdtemp_cb_count++;
- }
- static void check_mkstemp_result(uv_fs_t* req) {
- int r;
- ASSERT(req->fs_type == UV_FS_MKSTEMP);
- ASSERT(req->result >= 0);
- ASSERT(req->path);
- ASSERT(strlen(req->path) == 16);
- ASSERT(memcmp(req->path, "test_file_", 10) == 0);
- ASSERT(memcmp(req->path + 10, "XXXXXX", 6) != 0);
- check_permission(req->path, 0600);
- /* Check if req->path is actually a file */
- r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
- ASSERT(r == 0);
- ASSERT(stat_req.statbuf.st_mode & S_IFREG);
- uv_fs_req_cleanup(&stat_req);
- }
- static void mkstemp_cb(uv_fs_t* req) {
- ASSERT(req == &mkstemp_req1);
- check_mkstemp_result(req);
- mkstemp_cb_count++;
- }
- static void rmdir_cb(uv_fs_t* req) {
- ASSERT(req == &rmdir_req);
- ASSERT(req->fs_type == UV_FS_RMDIR);
- ASSERT(req->result == 0);
- rmdir_cb_count++;
- ASSERT(req->path);
- ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
- uv_fs_req_cleanup(req);
- }
- static void assert_is_file_type(uv_dirent_t dent) {
- #ifdef HAVE_DIRENT_TYPES
- /*
- * For Apple and Windows, we know getdents is expected to work but for other
- * environments, the filesystem dictates whether or not getdents supports
- * returning the file type.
- *
- * See:
- * http://man7.org/linux/man-pages/man2/getdents.2.html
- * https://github.com/libuv/libuv/issues/501
- */
- #if defined(__APPLE__) || defined(_WIN32)
- ASSERT(dent.type == UV_DIRENT_FILE);
- #else
- ASSERT(dent.type == UV_DIRENT_FILE || dent.type == UV_DIRENT_UNKNOWN);
- #endif
- #else
- ASSERT(dent.type == UV_DIRENT_UNKNOWN);
- #endif
- }
- static void scandir_cb(uv_fs_t* req) {
- uv_dirent_t dent;
- ASSERT(req == &scandir_req);
- ASSERT(req->fs_type == UV_FS_SCANDIR);
- ASSERT(req->result == 2);
- ASSERT(req->ptr);
- while (UV_EOF != uv_fs_scandir_next(req, &dent)) {
- ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
- assert_is_file_type(dent);
- }
- scandir_cb_count++;
- ASSERT(req->path);
- ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
- uv_fs_req_cleanup(req);
- ASSERT(!req->ptr);
- }
- static void empty_scandir_cb(uv_fs_t* req) {
- uv_dirent_t dent;
- ASSERT(req == &scandir_req);
- ASSERT(req->fs_type == UV_FS_SCANDIR);
- ASSERT(req->result == 0);
- ASSERT(req->ptr == NULL);
- ASSERT(UV_EOF == uv_fs_scandir_next(req, &dent));
- uv_fs_req_cleanup(req);
- scandir_cb_count++;
- }
- static void non_existent_scandir_cb(uv_fs_t* req) {
- uv_dirent_t dent;
- ASSERT(req == &scandir_req);
- ASSERT(req->fs_type == UV_FS_SCANDIR);
- ASSERT(req->result == UV_ENOENT);
- ASSERT(req->ptr == NULL);
- ASSERT(UV_ENOENT == uv_fs_scandir_next(req, &dent));
- uv_fs_req_cleanup(req);
- scandir_cb_count++;
- }
- static void file_scandir_cb(uv_fs_t* req) {
- ASSERT(req == &scandir_req);
- ASSERT(req->fs_type == UV_FS_SCANDIR);
- ASSERT(req->result == UV_ENOTDIR);
- ASSERT(req->ptr == NULL);
- uv_fs_req_cleanup(req);
- scandir_cb_count++;
- }
- static void stat_cb(uv_fs_t* req) {
- ASSERT(req == &stat_req);
- ASSERT(req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_LSTAT);
- ASSERT(req->result == 0);
- ASSERT(req->ptr);
- stat_cb_count++;
- uv_fs_req_cleanup(req);
- ASSERT(!req->ptr);
- }
- static void sendfile_cb(uv_fs_t* req) {
- ASSERT(req == &sendfile_req);
- ASSERT(req->fs_type == UV_FS_SENDFILE);
- ASSERT(req->result == 65546);
- sendfile_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void sendfile_nodata_cb(uv_fs_t* req) {
- ASSERT(req == &sendfile_req);
- ASSERT(req->fs_type == UV_FS_SENDFILE);
- ASSERT(req->result == 0);
- sendfile_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void open_noent_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_OPEN);
- ASSERT(req->result == UV_ENOENT);
- open_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void open_nametoolong_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_OPEN);
- ASSERT(req->result == UV_ENAMETOOLONG);
- open_cb_count++;
- uv_fs_req_cleanup(req);
- }
- static void open_loop_cb(uv_fs_t* req) {
- ASSERT(req->fs_type == UV_FS_OPEN);
- ASSERT(req->result == UV_ELOOP);
- open_cb_count++;
- uv_fs_req_cleanup(req);
- }
- TEST_IMPL(fs_file_noent) {
- uv_fs_t req;
- int r;
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &req, "does_not_exist", O_RDONLY, 0, NULL);
- ASSERT(r == UV_ENOENT);
- ASSERT(req.result == UV_ENOENT);
- uv_fs_req_cleanup(&req);
- r = uv_fs_open(loop, &req, "does_not_exist", O_RDONLY, 0, open_noent_cb);
- ASSERT(r == 0);
- ASSERT(open_cb_count == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(open_cb_count == 1);
- /* TODO add EACCES test */
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_file_nametoolong) {
- uv_fs_t req;
- int r;
- char name[TOO_LONG_NAME_LENGTH + 1];
- loop = uv_default_loop();
- memset(name, 'a', TOO_LONG_NAME_LENGTH);
- name[TOO_LONG_NAME_LENGTH] = 0;
- r = uv_fs_open(NULL, &req, name, O_RDONLY, 0, NULL);
- ASSERT(r == UV_ENAMETOOLONG);
- ASSERT(req.result == UV_ENAMETOOLONG);
- uv_fs_req_cleanup(&req);
- r = uv_fs_open(loop, &req, name, O_RDONLY, 0, open_nametoolong_cb);
- ASSERT(r == 0);
- ASSERT(open_cb_count == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(open_cb_count == 1);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_file_loop) {
- uv_fs_t req;
- int r;
- loop = uv_default_loop();
- unlink("test_symlink");
- r = uv_fs_symlink(NULL, &req, "test_symlink", "test_symlink", 0, NULL);
- #ifdef _WIN32
- /*
- * Windows XP and Server 2003 don't support symlinks; we'll get UV_ENOTSUP.
- * Starting with vista they are supported, but only when elevated, otherwise
- * we'll see UV_EPERM.
- */
- if (r == UV_ENOTSUP || r == UV_EPERM)
- return 0;
- #elif defined(__MSYS__)
- /* MSYS2's approximation of symlinks with copies does not work for broken
- links. */
- if (r == UV_ENOENT)
- return 0;
- #endif
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_open(NULL, &req, "test_symlink", O_RDONLY, 0, NULL);
- ASSERT(r == UV_ELOOP);
- ASSERT(req.result == UV_ELOOP);
- uv_fs_req_cleanup(&req);
- r = uv_fs_open(loop, &req, "test_symlink", O_RDONLY, 0, open_loop_cb);
- ASSERT(r == 0);
- ASSERT(open_cb_count == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(open_cb_count == 1);
- unlink("test_symlink");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void check_utime(const char* path,
- double atime,
- double mtime,
- int test_lutime) {
- uv_stat_t* s;
- uv_fs_t req;
- int r;
- if (test_lutime)
- r = uv_fs_lstat(loop, &req, path, NULL);
- else
- r = uv_fs_stat(loop, &req, path, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- s = &req.statbuf;
- ASSERT(s->st_atim.tv_sec + (s->st_atim.tv_nsec / 1000000000.0) == atime);
- ASSERT(s->st_mtim.tv_sec + (s->st_mtim.tv_nsec / 1000000000.0) == mtime);
- uv_fs_req_cleanup(&req);
- }
- static void utime_cb(uv_fs_t* req) {
- utime_check_t* c;
- ASSERT(req == &utime_req);
- ASSERT(req->result == 0);
- ASSERT(req->fs_type == UV_FS_UTIME);
- c = req->data;
- check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 0);
- uv_fs_req_cleanup(req);
- utime_cb_count++;
- }
- static void futime_cb(uv_fs_t* req) {
- utime_check_t* c;
- ASSERT(req == &futime_req);
- ASSERT(req->result == 0);
- ASSERT(req->fs_type == UV_FS_FUTIME);
- c = req->data;
- check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 0);
- uv_fs_req_cleanup(req);
- futime_cb_count++;
- }
- static void lutime_cb(uv_fs_t* req) {
- utime_check_t* c;
- ASSERT(req->result == 0);
- ASSERT(req->fs_type == UV_FS_LUTIME);
- c = req->data;
- check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 1);
- uv_fs_req_cleanup(req);
- lutime_cb_count++;
- }
- TEST_IMPL(fs_file_async) {
- int r;
- /* Setup. */
- unlink("test_file");
- unlink("test_file2");
- loop = uv_default_loop();
- r = uv_fs_open(loop, &open_req1, "test_file", O_WRONLY | O_CREAT,
- S_IRUSR | S_IWUSR, create_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(create_cb_count == 1);
- ASSERT(write_cb_count == 1);
- ASSERT(fsync_cb_count == 1);
- ASSERT(fdatasync_cb_count == 1);
- ASSERT(close_cb_count == 1);
- r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", rename_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(create_cb_count == 1);
- ASSERT(write_cb_count == 1);
- ASSERT(close_cb_count == 1);
- ASSERT(rename_cb_count == 1);
- r = uv_fs_open(loop, &open_req1, "test_file2", O_RDWR, 0, open_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(open_cb_count == 1);
- ASSERT(read_cb_count == 1);
- ASSERT(close_cb_count == 2);
- ASSERT(rename_cb_count == 1);
- ASSERT(create_cb_count == 1);
- ASSERT(write_cb_count == 1);
- ASSERT(ftruncate_cb_count == 1);
- r = uv_fs_open(loop, &open_req1, "test_file2", O_RDONLY, 0, open_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(open_cb_count == 2);
- ASSERT(read_cb_count == 2);
- ASSERT(close_cb_count == 3);
- ASSERT(rename_cb_count == 1);
- ASSERT(unlink_cb_count == 1);
- ASSERT(create_cb_count == 1);
- ASSERT(write_cb_count == 1);
- ASSERT(ftruncate_cb_count == 1);
- /* Cleanup. */
- unlink("test_file");
- unlink("test_file2");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_file_sync(int add_flags) {
- int r;
- /* Setup. */
- unlink("test_file");
- unlink("test_file2");
- loop = uv_default_loop();
- r = uv_fs_open(loop, &open_req1, "test_file",
- O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(write_req.result >= 0);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_file", O_RDWR | add_flags, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(read_req.result >= 0);
- ASSERT(strcmp(buf, test_buf) == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_ftruncate(NULL, &ftruncate_req, open_req1.result, 7, NULL);
- ASSERT(r == 0);
- ASSERT(ftruncate_req.result == 0);
- uv_fs_req_cleanup(&ftruncate_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_rename(NULL, &rename_req, "test_file", "test_file2", NULL);
- ASSERT(r == 0);
- ASSERT(rename_req.result == 0);
- uv_fs_req_cleanup(&rename_req);
- r = uv_fs_open(NULL, &open_req1, "test_file2", O_RDONLY | add_flags, 0,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(read_req.result >= 0);
- ASSERT(strcmp(buf, "test-bu") == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_unlink(NULL, &unlink_req, "test_file2", NULL);
- ASSERT(r == 0);
- ASSERT(unlink_req.result == 0);
- uv_fs_req_cleanup(&unlink_req);
- /* Cleanup */
- unlink("test_file");
- unlink("test_file2");
- }
- TEST_IMPL(fs_file_sync) {
- fs_file_sync(0);
- fs_file_sync(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_file_write_null_buffer(int add_flags) {
- int r;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &open_req1, "test_file",
- O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(NULL, 0);
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == 0);
- ASSERT(write_req.result == 0);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- unlink("test_file");
- }
- TEST_IMPL(fs_file_write_null_buffer) {
- fs_file_write_null_buffer(0);
- fs_file_write_null_buffer(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_async_dir) {
- int r;
- uv_dirent_t dent;
- /* Setup */
- unlink("test_dir/file1");
- unlink("test_dir/file2");
- rmdir("test_dir");
- loop = uv_default_loop();
- r = uv_fs_mkdir(loop, &mkdir_req, "test_dir", 0755, mkdir_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(mkdir_cb_count == 1);
- /* Create 2 files synchronously. */
- r = uv_fs_open(NULL, &open_req1, "test_dir/file1", O_WRONLY | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_dir/file2", O_WRONLY | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_scandir(loop, &scandir_req, "test_dir", 0, scandir_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(scandir_cb_count == 1);
- /* sync uv_fs_scandir */
- r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
- ASSERT(r == 2);
- ASSERT(scandir_req.result == 2);
- ASSERT(scandir_req.ptr);
- while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
- ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
- assert_is_file_type(dent);
- }
- uv_fs_req_cleanup(&scandir_req);
- ASSERT(!scandir_req.ptr);
- r = uv_fs_stat(loop, &stat_req, "test_dir", stat_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- r = uv_fs_stat(loop, &stat_req, "test_dir/", stat_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- r = uv_fs_lstat(loop, &stat_req, "test_dir", stat_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- r = uv_fs_lstat(loop, &stat_req, "test_dir/", stat_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(stat_cb_count == 4);
- r = uv_fs_unlink(loop, &unlink_req, "test_dir/file1", unlink_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(unlink_cb_count == 1);
- r = uv_fs_unlink(loop, &unlink_req, "test_dir/file2", unlink_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(unlink_cb_count == 2);
- r = uv_fs_rmdir(loop, &rmdir_req, "test_dir", rmdir_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(rmdir_cb_count == 1);
- /* Cleanup */
- unlink("test_dir/file1");
- unlink("test_dir/file2");
- rmdir("test_dir");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static int test_sendfile(void (*setup)(int), uv_fs_cb cb, off_t expected_size) {
- int f, r;
- struct stat s1, s2;
- loop = uv_default_loop();
- /* Setup. */
- unlink("test_file");
- unlink("test_file2");
- f = open("test_file", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
- ASSERT(f != -1);
- if (setup != NULL)
- setup(f);
- r = close(f);
- ASSERT(r == 0);
- /* Test starts here. */
- r = uv_fs_open(NULL, &open_req1, "test_file", O_RDWR, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_open(NULL, &open_req2, "test_file2", O_WRONLY | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req2.result >= 0);
- uv_fs_req_cleanup(&open_req2);
- r = uv_fs_sendfile(loop, &sendfile_req, open_req2.result, open_req1.result,
- 0, 131072, cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(sendfile_cb_count == 1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_close(NULL, &close_req, open_req2.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- ASSERT(0 == stat("test_file", &s1));
- ASSERT(0 == stat("test_file2", &s2));
- ASSERT(s1.st_size == s2.st_size);
- ASSERT(s2.st_size == expected_size);
- /* Cleanup. */
- unlink("test_file");
- unlink("test_file2");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void sendfile_setup(int f) {
- ASSERT(6 == write(f, "begin\n", 6));
- ASSERT(65542 == lseek(f, 65536, SEEK_CUR));
- ASSERT(4 == write(f, "end\n", 4));
- }
- TEST_IMPL(fs_async_sendfile) {
- return test_sendfile(sendfile_setup, sendfile_cb, 65546);
- }
- TEST_IMPL(fs_async_sendfile_nodata) {
- return test_sendfile(NULL, sendfile_nodata_cb, 0);
- }
- TEST_IMPL(fs_mkdtemp) {
- int r;
- const char* path_template = "test_dir_XXXXXX";
- loop = uv_default_loop();
- r = uv_fs_mkdtemp(loop, &mkdtemp_req1, path_template, mkdtemp_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(mkdtemp_cb_count == 1);
- /* sync mkdtemp */
- r = uv_fs_mkdtemp(NULL, &mkdtemp_req2, path_template, NULL);
- ASSERT(r == 0);
- check_mkdtemp_result(&mkdtemp_req2);
- /* mkdtemp return different values on subsequent calls */
- ASSERT(strcmp(mkdtemp_req1.path, mkdtemp_req2.path) != 0);
- /* Cleanup */
- rmdir(mkdtemp_req1.path);
- rmdir(mkdtemp_req2.path);
- uv_fs_req_cleanup(&mkdtemp_req1);
- uv_fs_req_cleanup(&mkdtemp_req2);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_mkstemp) {
- int r;
- int fd;
- const char path_template[] = "test_file_XXXXXX";
- uv_fs_t req;
- loop = uv_default_loop();
- r = uv_fs_mkstemp(loop, &mkstemp_req1, path_template, mkstemp_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(mkstemp_cb_count == 1);
- /* sync mkstemp */
- r = uv_fs_mkstemp(NULL, &mkstemp_req2, path_template, NULL);
- ASSERT(r >= 0);
- check_mkstemp_result(&mkstemp_req2);
- /* mkstemp return different values on subsequent calls */
- ASSERT(strcmp(mkstemp_req1.path, mkstemp_req2.path) != 0);
- /* invalid template returns EINVAL */
- ASSERT(uv_fs_mkstemp(NULL, &mkstemp_req3, "test_file", NULL) == UV_EINVAL);
- /* We can write to the opened file */
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &req, mkstemp_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(req.result == sizeof(test_buf));
- uv_fs_req_cleanup(&req);
- /* Cleanup */
- uv_fs_close(NULL, &req, mkstemp_req1.result, NULL);
- uv_fs_req_cleanup(&req);
- uv_fs_close(NULL, &req, mkstemp_req2.result, NULL);
- uv_fs_req_cleanup(&req);
- fd = uv_fs_open(NULL, &req, mkstemp_req1.path , O_RDONLY, 0, NULL);
- ASSERT(fd >= 0);
- uv_fs_req_cleanup(&req);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &req, fd, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- ASSERT(strcmp(buf, test_buf) == 0);
- uv_fs_req_cleanup(&req);
- uv_fs_close(NULL, &req, fd, NULL);
- uv_fs_req_cleanup(&req);
- unlink(mkstemp_req1.path);
- unlink(mkstemp_req2.path);
- uv_fs_req_cleanup(&mkstemp_req1);
- uv_fs_req_cleanup(&mkstemp_req2);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_fstat) {
- int r;
- uv_fs_t req;
- uv_file file;
- uv_stat_t* s;
- #ifndef _WIN32
- struct stat t;
- #endif
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- #ifndef _WIN32
- ASSERT(0 == fstat(file, &t));
- ASSERT(0 == uv_fs_fstat(NULL, &req, file, NULL));
- ASSERT(req.result == 0);
- s = req.ptr;
- # if defined(__APPLE__)
- ASSERT(s->st_birthtim.tv_sec == t.st_birthtimespec.tv_sec);
- ASSERT(s->st_birthtim.tv_nsec == t.st_birthtimespec.tv_nsec);
- # elif defined(__linux__)
- /* If statx() is supported, the birth time should be equal to the change time
- * because we just created the file. On older kernels, it's set to zero.
- */
- ASSERT(s->st_birthtim.tv_sec == 0 ||
- s->st_birthtim.tv_sec == t.st_ctim.tv_sec);
- ASSERT(s->st_birthtim.tv_nsec == 0 ||
- s->st_birthtim.tv_nsec == t.st_ctim.tv_nsec);
- # endif
- #endif
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(req.result == sizeof(test_buf));
- uv_fs_req_cleanup(&req);
- memset(&req.statbuf, 0xaa, sizeof(req.statbuf));
- r = uv_fs_fstat(NULL, &req, file, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- s = req.ptr;
- ASSERT(s->st_size == sizeof(test_buf));
- #ifndef _WIN32
- r = fstat(file, &t);
- ASSERT(r == 0);
- ASSERT(s->st_dev == (uint64_t) t.st_dev);
- ASSERT(s->st_mode == (uint64_t) t.st_mode);
- ASSERT(s->st_nlink == (uint64_t) t.st_nlink);
- ASSERT(s->st_uid == (uint64_t) t.st_uid);
- ASSERT(s->st_gid == (uint64_t) t.st_gid);
- ASSERT(s->st_rdev == (uint64_t) t.st_rdev);
- ASSERT(s->st_ino == (uint64_t) t.st_ino);
- ASSERT(s->st_size == (uint64_t) t.st_size);
- ASSERT(s->st_blksize == (uint64_t) t.st_blksize);
- ASSERT(s->st_blocks == (uint64_t) t.st_blocks);
- #if defined(__APPLE__)
- ASSERT(s->st_atim.tv_sec == t.st_atimespec.tv_sec);
- ASSERT(s->st_atim.tv_nsec == t.st_atimespec.tv_nsec);
- ASSERT(s->st_mtim.tv_sec == t.st_mtimespec.tv_sec);
- ASSERT(s->st_mtim.tv_nsec == t.st_mtimespec.tv_nsec);
- ASSERT(s->st_ctim.tv_sec == t.st_ctimespec.tv_sec);
- ASSERT(s->st_ctim.tv_nsec == t.st_ctimespec.tv_nsec);
- #elif defined(_AIX)
- ASSERT(s->st_atim.tv_sec == t.st_atime);
- ASSERT(s->st_atim.tv_nsec == 0);
- ASSERT(s->st_mtim.tv_sec == t.st_mtime);
- ASSERT(s->st_mtim.tv_nsec == 0);
- ASSERT(s->st_ctim.tv_sec == t.st_ctime);
- ASSERT(s->st_ctim.tv_nsec == 0);
- #elif defined(__ANDROID__)
- ASSERT(s->st_atim.tv_sec == t.st_atime);
- ASSERT(s->st_atim.tv_nsec == t.st_atimensec);
- ASSERT(s->st_mtim.tv_sec == t.st_mtime);
- ASSERT(s->st_mtim.tv_nsec == t.st_mtimensec);
- ASSERT(s->st_ctim.tv_sec == t.st_ctime);
- ASSERT(s->st_ctim.tv_nsec == t.st_ctimensec);
- #elif defined(__sun) || \
- defined(__DragonFly__) || \
- defined(__FreeBSD__) || \
- defined(__OpenBSD__) || \
- defined(__NetBSD__) || \
- defined(_GNU_SOURCE) || \
- defined(_BSD_SOURCE) || \
- defined(_SVID_SOURCE) || \
- defined(_XOPEN_SOURCE) || \
- defined(_DEFAULT_SOURCE)
- ASSERT(s->st_atim.tv_sec == t.st_atim.tv_sec);
- ASSERT(s->st_atim.tv_nsec == t.st_atim.tv_nsec);
- ASSERT(s->st_mtim.tv_sec == t.st_mtim.tv_sec);
- ASSERT(s->st_mtim.tv_nsec == t.st_mtim.tv_nsec);
- ASSERT(s->st_ctim.tv_sec == t.st_ctim.tv_sec);
- ASSERT(s->st_ctim.tv_nsec == t.st_ctim.tv_nsec);
- # if defined(__FreeBSD__) || \
- defined(__NetBSD__)
- ASSERT(s->st_birthtim.tv_sec == t.st_birthtim.tv_sec);
- ASSERT(s->st_birthtim.tv_nsec == t.st_birthtim.tv_nsec);
- # endif
- #else
- ASSERT(s->st_atim.tv_sec == t.st_atime);
- ASSERT(s->st_atim.tv_nsec == 0);
- ASSERT(s->st_mtim.tv_sec == t.st_mtime);
- ASSERT(s->st_mtim.tv_nsec == 0);
- ASSERT(s->st_ctim.tv_sec == t.st_ctime);
- ASSERT(s->st_ctim.tv_nsec == 0);
- #endif
- #endif
- #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
- ASSERT(s->st_flags == t.st_flags);
- ASSERT(s->st_gen == t.st_gen);
- #else
- ASSERT(s->st_flags == 0);
- ASSERT(s->st_gen == 0);
- #endif
- uv_fs_req_cleanup(&req);
- /* Now do the uv_fs_fstat call asynchronously */
- r = uv_fs_fstat(loop, &req, file, fstat_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(fstat_cb_count == 1);
- r = uv_fs_close(NULL, &req, file, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /*
- * Run the loop just to check we don't have make any extraneous uv_ref()
- * calls. This should drop out immediately.
- */
- uv_run(loop, UV_RUN_DEFAULT);
- /* Cleanup. */
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_access) {
- int r;
- uv_fs_t req;
- uv_file file;
- /* Setup. */
- unlink("test_file");
- rmdir("test_dir");
- loop = uv_default_loop();
- /* File should not exist */
- r = uv_fs_access(NULL, &req, "test_file", F_OK, NULL);
- ASSERT(r < 0);
- ASSERT(req.result < 0);
- uv_fs_req_cleanup(&req);
- /* File should not exist */
- r = uv_fs_access(loop, &req, "test_file", F_OK, access_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(access_cb_count == 1);
- access_cb_count = 0; /* reset for the next test */
- /* Create file */
- r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- /* File should exist */
- r = uv_fs_access(NULL, &req, "test_file", F_OK, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /* File should exist */
- r = uv_fs_access(loop, &req, "test_file", F_OK, access_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(access_cb_count == 1);
- access_cb_count = 0; /* reset for the next test */
- /* Close file */
- r = uv_fs_close(NULL, &req, file, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /* Directory access */
- r = uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_access(NULL, &req, "test_dir", W_OK, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /*
- * Run the loop just to check we don't have make any extraneous uv_ref()
- * calls. This should drop out immediately.
- */
- uv_run(loop, UV_RUN_DEFAULT);
- /* Cleanup. */
- unlink("test_file");
- rmdir("test_dir");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_chmod) {
- int r;
- uv_fs_t req;
- uv_file file;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(req.result == sizeof(test_buf));
- uv_fs_req_cleanup(&req);
- #ifndef _WIN32
- /* Make the file write-only */
- r = uv_fs_chmod(NULL, &req, "test_file", 0200, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- check_permission("test_file", 0200);
- #endif
- /* Make the file read-only */
- r = uv_fs_chmod(NULL, &req, "test_file", 0400, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- check_permission("test_file", 0400);
- /* Make the file read+write with sync uv_fs_fchmod */
- r = uv_fs_fchmod(NULL, &req, file, 0600, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- check_permission("test_file", 0600);
- #ifndef _WIN32
- /* async chmod */
- {
- static int mode = 0200;
- req.data = &mode;
- }
- r = uv_fs_chmod(loop, &req, "test_file", 0200, chmod_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(chmod_cb_count == 1);
- chmod_cb_count = 0; /* reset for the next test */
- #endif
- /* async chmod */
- {
- static int mode = 0400;
- req.data = &mode;
- }
- r = uv_fs_chmod(loop, &req, "test_file", 0400, chmod_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(chmod_cb_count == 1);
- /* async fchmod */
- {
- static int mode = 0600;
- req.data = &mode;
- }
- r = uv_fs_fchmod(loop, &req, file, 0600, fchmod_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(fchmod_cb_count == 1);
- uv_fs_close(loop, &req, file, NULL);
- /*
- * Run the loop just to check we don't have make any extraneous uv_ref()
- * calls. This should drop out immediately.
- */
- uv_run(loop, UV_RUN_DEFAULT);
- /* Cleanup. */
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_unlink_readonly) {
- int r;
- uv_fs_t req;
- uv_file file;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL,
- &req,
- "test_file",
- O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(req.result == sizeof(test_buf));
- uv_fs_req_cleanup(&req);
- uv_fs_close(loop, &req, file, NULL);
- /* Make the file read-only */
- r = uv_fs_chmod(NULL, &req, "test_file", 0400, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- check_permission("test_file", 0400);
- /* Try to unlink the file */
- r = uv_fs_unlink(NULL, &req, "test_file", NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /*
- * Run the loop just to check we don't have make any extraneous uv_ref()
- * calls. This should drop out immediately.
- */
- uv_run(loop, UV_RUN_DEFAULT);
- /* Cleanup. */
- uv_fs_chmod(NULL, &req, "test_file", 0600, NULL);
- uv_fs_req_cleanup(&req);
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #ifdef _WIN32
- TEST_IMPL(fs_unlink_archive_readonly) {
- int r;
- uv_fs_t req;
- uv_file file;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL,
- &req,
- "test_file",
- O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(req.result == sizeof(test_buf));
- uv_fs_req_cleanup(&req);
- uv_fs_close(loop, &req, file, NULL);
- /* Make the file read-only and clear archive flag */
- r = SetFileAttributes("test_file", FILE_ATTRIBUTE_READONLY);
- ASSERT(r != 0);
- uv_fs_req_cleanup(&req);
- check_permission("test_file", 0400);
- /* Try to unlink the file */
- r = uv_fs_unlink(NULL, &req, "test_file", NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /*
- * Run the loop just to check we don't have make any extraneous uv_ref()
- * calls. This should drop out immediately.
- */
- uv_run(loop, UV_RUN_DEFAULT);
- /* Cleanup. */
- uv_fs_chmod(NULL, &req, "test_file", 0600, NULL);
- uv_fs_req_cleanup(&req);
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif
- TEST_IMPL(fs_chown) {
- int r;
- uv_fs_t req;
- uv_file file;
- /* Setup. */
- unlink("test_file");
- unlink("test_file_link");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- /* sync chown */
- r = uv_fs_chown(NULL, &req, "test_file", -1, -1, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /* sync fchown */
- r = uv_fs_fchown(NULL, &req, file, -1, -1, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /* async chown */
- r = uv_fs_chown(loop, &req, "test_file", -1, -1, chown_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(chown_cb_count == 1);
- #ifndef __MVS__
- /* chown to root (fail) */
- chown_cb_count = 0;
- r = uv_fs_chown(loop, &req, "test_file", 0, 0, chown_root_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(chown_cb_count == 1);
- #endif
- /* async fchown */
- r = uv_fs_fchown(loop, &req, file, -1, -1, fchown_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(fchown_cb_count == 1);
- #ifndef __HAIKU__
- /* Haiku doesn't support hardlink */
- /* sync link */
- r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /* sync lchown */
- r = uv_fs_lchown(NULL, &req, "test_file_link", -1, -1, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /* async lchown */
- r = uv_fs_lchown(loop, &req, "test_file_link", -1, -1, lchown_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(lchown_cb_count == 1);
- #endif
- /* Close file */
- r = uv_fs_close(NULL, &req, file, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /*
- * Run the loop just to check we don't have make any extraneous uv_ref()
- * calls. This should drop out immediately.
- */
- uv_run(loop, UV_RUN_DEFAULT);
- /* Cleanup. */
- unlink("test_file");
- unlink("test_file_link");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_link) {
- int r;
- uv_fs_t req;
- uv_file file;
- uv_file link;
- /* Setup. */
- unlink("test_file");
- unlink("test_file_link");
- unlink("test_file_link2");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(req.result == sizeof(test_buf));
- uv_fs_req_cleanup(&req);
- uv_fs_close(loop, &req, file, NULL);
- /* sync link */
- r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_open(NULL, &req, "test_file_link", O_RDWR, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- link = req.result;
- uv_fs_req_cleanup(&req);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- ASSERT(strcmp(buf, test_buf) == 0);
- close(link);
- /* async link */
- r = uv_fs_link(loop, &req, "test_file", "test_file_link2", link_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(link_cb_count == 1);
- r = uv_fs_open(NULL, &req, "test_file_link2", O_RDWR, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- link = req.result;
- uv_fs_req_cleanup(&req);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- ASSERT(strcmp(buf, test_buf) == 0);
- uv_fs_close(loop, &req, link, NULL);
- /*
- * Run the loop just to check we don't have make any extraneous uv_ref()
- * calls. This should drop out immediately.
- */
- uv_run(loop, UV_RUN_DEFAULT);
- /* Cleanup. */
- unlink("test_file");
- unlink("test_file_link");
- unlink("test_file_link2");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_readlink) {
- uv_fs_t req;
- loop = uv_default_loop();
- ASSERT(0 == uv_fs_readlink(loop, &req, "no_such_file", dummy_cb));
- ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
- ASSERT(dummy_cb_count == 1);
- ASSERT(req.ptr == NULL);
- ASSERT(req.result == UV_ENOENT);
- uv_fs_req_cleanup(&req);
- ASSERT(UV_ENOENT == uv_fs_readlink(NULL, &req, "no_such_file", NULL));
- ASSERT(req.ptr == NULL);
- ASSERT(req.result == UV_ENOENT);
- uv_fs_req_cleanup(&req);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_realpath) {
- uv_fs_t req;
- loop = uv_default_loop();
- ASSERT(0 == uv_fs_realpath(loop, &req, "no_such_file", dummy_cb));
- ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
- ASSERT(dummy_cb_count == 1);
- ASSERT(req.ptr == NULL);
- #ifdef _WIN32
- /*
- * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
- */
- if (req.result == UV_ENOSYS) {
- uv_fs_req_cleanup(&req);
- RETURN_SKIP("realpath is not supported on Windows XP");
- }
- #endif
- ASSERT(req.result == UV_ENOENT);
- uv_fs_req_cleanup(&req);
- ASSERT(UV_ENOENT == uv_fs_realpath(NULL, &req, "no_such_file", NULL));
- ASSERT(req.ptr == NULL);
- ASSERT(req.result == UV_ENOENT);
- uv_fs_req_cleanup(&req);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_symlink) {
- int r;
- uv_fs_t req;
- uv_file file;
- uv_file link;
- char test_file_abs_buf[PATHMAX];
- size_t test_file_abs_size;
- /* Setup. */
- unlink("test_file");
- unlink("test_file_symlink");
- unlink("test_file_symlink2");
- unlink("test_file_symlink_symlink");
- unlink("test_file_symlink2_symlink");
- test_file_abs_size = sizeof(test_file_abs_buf);
- #ifdef _WIN32
- uv_cwd(test_file_abs_buf, &test_file_abs_size);
- strcat(test_file_abs_buf, "\\test_file");
- #else
- uv_cwd(test_file_abs_buf, &test_file_abs_size);
- strcat(test_file_abs_buf, "/test_file");
- #endif
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &req, "test_file", O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(req.result == sizeof(test_buf));
- uv_fs_req_cleanup(&req);
- uv_fs_close(loop, &req, file, NULL);
- /* sync symlink */
- r = uv_fs_symlink(NULL, &req, "test_file", "test_file_symlink", 0, NULL);
- #ifdef _WIN32
- if (r < 0) {
- if (r == UV_ENOTSUP) {
- /*
- * Windows doesn't support symlinks on older versions.
- * We just pass the test and bail out early if we get ENOTSUP.
- */
- return 0;
- } else if (r == UV_EPERM) {
- /*
- * Creating a symlink is only allowed when running elevated.
- * We pass the test and bail out early if we get UV_EPERM.
- */
- return 0;
- }
- }
- #endif
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_open(NULL, &req, "test_file_symlink", O_RDWR, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- link = req.result;
- uv_fs_req_cleanup(&req);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- ASSERT(strcmp(buf, test_buf) == 0);
- uv_fs_close(loop, &req, link, NULL);
- r = uv_fs_symlink(NULL,
- &req,
- "test_file_symlink",
- "test_file_symlink_symlink",
- 0,
- NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- #if defined(__MSYS__)
- RETURN_SKIP("symlink reading is not supported on MSYS2");
- #endif
- r = uv_fs_readlink(NULL, &req, "test_file_symlink_symlink", NULL);
- ASSERT(r == 0);
- ASSERT(strcmp(req.ptr, "test_file_symlink") == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_realpath(NULL, &req, "test_file_symlink_symlink", NULL);
- #ifdef _WIN32
- /*
- * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
- */
- if (r == UV_ENOSYS) {
- uv_fs_req_cleanup(&req);
- RETURN_SKIP("realpath is not supported on Windows XP");
- }
- #endif
- ASSERT(r == 0);
- #ifdef _WIN32
- ASSERT(stricmp(req.ptr, test_file_abs_buf) == 0);
- #else
- ASSERT(strcmp(req.ptr, test_file_abs_buf) == 0);
- #endif
- uv_fs_req_cleanup(&req);
- /* async link */
- r = uv_fs_symlink(loop,
- &req,
- "test_file",
- "test_file_symlink2",
- 0,
- symlink_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(symlink_cb_count == 1);
- r = uv_fs_open(NULL, &req, "test_file_symlink2", O_RDWR, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- link = req.result;
- uv_fs_req_cleanup(&req);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- ASSERT(strcmp(buf, test_buf) == 0);
- uv_fs_close(loop, &req, link, NULL);
- r = uv_fs_symlink(NULL,
- &req,
- "test_file_symlink2",
- "test_file_symlink2_symlink",
- 0,
- NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_readlink(loop, &req, "test_file_symlink2_symlink", readlink_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(readlink_cb_count == 1);
- r = uv_fs_realpath(loop, &req, "test_file", realpath_cb);
- #ifdef _WIN32
- /*
- * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
- */
- if (r == UV_ENOSYS) {
- uv_fs_req_cleanup(&req);
- RETURN_SKIP("realpath is not supported on Windows XP");
- }
- #endif
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(realpath_cb_count == 1);
- /*
- * Run the loop just to check we don't have make any extraneous uv_ref()
- * calls. This should drop out immediately.
- */
- uv_run(loop, UV_RUN_DEFAULT);
- /* Cleanup. */
- unlink("test_file");
- unlink("test_file_symlink");
- unlink("test_file_symlink_symlink");
- unlink("test_file_symlink2");
- unlink("test_file_symlink2_symlink");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- int test_symlink_dir_impl(int type) {
- uv_fs_t req;
- int r;
- char* test_dir;
- uv_dirent_t dent;
- static char test_dir_abs_buf[PATHMAX];
- size_t test_dir_abs_size;
- /* set-up */
- unlink("test_dir/file1");
- unlink("test_dir/file2");
- rmdir("test_dir");
- rmdir("test_dir_symlink");
- test_dir_abs_size = sizeof(test_dir_abs_buf);
- loop = uv_default_loop();
- uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
- uv_fs_req_cleanup(&req);
- #ifdef _WIN32
- strcpy(test_dir_abs_buf, "\\\\?\\");
- uv_cwd(test_dir_abs_buf + 4, &test_dir_abs_size);
- test_dir_abs_size += 4;
- strcat(test_dir_abs_buf, "\\test_dir\\");
- test_dir_abs_size += strlen("\\test_dir\\");
- test_dir = test_dir_abs_buf;
- #else
- uv_cwd(test_dir_abs_buf, &test_dir_abs_size);
- strcat(test_dir_abs_buf, "/test_dir");
- test_dir_abs_size += strlen("/test_dir");
- test_dir = "test_dir";
- #endif
- r = uv_fs_symlink(NULL, &req, test_dir, "test_dir_symlink", type, NULL);
- if (type == UV_FS_SYMLINK_DIR && (r == UV_ENOTSUP || r == UV_EPERM)) {
- uv_fs_req_cleanup(&req);
- RETURN_SKIP("this version of Windows doesn't support unprivileged "
- "creation of directory symlinks");
- }
- fprintf(stderr, "r == %i\n", r);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_stat(NULL, &req, "test_dir_symlink", NULL);
- ASSERT(r == 0);
- ASSERT(((uv_stat_t*)req.ptr)->st_mode & S_IFDIR);
- uv_fs_req_cleanup(&req);
- r = uv_fs_lstat(NULL, &req, "test_dir_symlink", NULL);
- ASSERT(r == 0);
- #if defined(__MSYS__)
- RETURN_SKIP("symlink reading is not supported on MSYS2");
- #endif
- ASSERT(((uv_stat_t*)req.ptr)->st_mode & S_IFLNK);
- #ifdef _WIN32
- ASSERT(((uv_stat_t*)req.ptr)->st_size == strlen(test_dir + 4));
- #else
- # ifdef __PASE__
- /* On IBMi PASE, st_size returns the length of the symlink itself. */
- ASSERT(((uv_stat_t*)req.ptr)->st_size == strlen("test_dir_symlink"));
- # else
- ASSERT(((uv_stat_t*)req.ptr)->st_size == strlen(test_dir));
- # endif
- #endif
- uv_fs_req_cleanup(&req);
- r = uv_fs_readlink(NULL, &req, "test_dir_symlink", NULL);
- ASSERT(r == 0);
- #ifdef _WIN32
- ASSERT(strcmp(req.ptr, test_dir + 4) == 0);
- #else
- ASSERT(strcmp(req.ptr, test_dir) == 0);
- #endif
- uv_fs_req_cleanup(&req);
- r = uv_fs_realpath(NULL, &req, "test_dir_symlink", NULL);
- #ifdef _WIN32
- /*
- * Windows XP and Server 2003 don't support GetFinalPathNameByHandleW()
- */
- if (r == UV_ENOSYS) {
- uv_fs_req_cleanup(&req);
- RETURN_SKIP("realpath is not supported on Windows XP");
- }
- #endif
- ASSERT(r == 0);
- #ifdef _WIN32
- ASSERT(strlen(req.ptr) == test_dir_abs_size - 5);
- ASSERT(strnicmp(req.ptr, test_dir + 4, test_dir_abs_size - 5) == 0);
- #else
- ASSERT(strcmp(req.ptr, test_dir_abs_buf) == 0);
- #endif
- uv_fs_req_cleanup(&req);
- r = uv_fs_open(NULL, &open_req1, "test_dir/file1", O_WRONLY | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_dir/file2", O_WRONLY | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_scandir(NULL, &scandir_req, "test_dir_symlink", 0, NULL);
- ASSERT(r == 2);
- ASSERT(scandir_req.result == 2);
- ASSERT(scandir_req.ptr);
- while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
- ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
- assert_is_file_type(dent);
- }
- uv_fs_req_cleanup(&scandir_req);
- ASSERT(!scandir_req.ptr);
- /* unlink will remove the directory symlink */
- r = uv_fs_unlink(NULL, &req, "test_dir_symlink", NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_scandir(NULL, &scandir_req, "test_dir_symlink", 0, NULL);
- ASSERT(r == UV_ENOENT);
- uv_fs_req_cleanup(&scandir_req);
- r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
- ASSERT(r == 2);
- ASSERT(scandir_req.result == 2);
- ASSERT(scandir_req.ptr);
- while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
- ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
- assert_is_file_type(dent);
- }
- uv_fs_req_cleanup(&scandir_req);
- ASSERT(!scandir_req.ptr);
- /* clean-up */
- unlink("test_dir/file1");
- unlink("test_dir/file2");
- rmdir("test_dir");
- rmdir("test_dir_symlink");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_symlink_dir) {
- return test_symlink_dir_impl(UV_FS_SYMLINK_DIR);
- }
- TEST_IMPL(fs_symlink_junction) {
- return test_symlink_dir_impl(UV_FS_SYMLINK_JUNCTION);
- }
- #ifdef _WIN32
- TEST_IMPL(fs_non_symlink_reparse_point) {
- uv_fs_t req;
- int r;
- HANDLE file_handle;
- REPARSE_GUID_DATA_BUFFER reparse_buffer;
- DWORD bytes_returned;
- uv_dirent_t dent;
- /* set-up */
- unlink("test_dir/test_file");
- rmdir("test_dir");
- loop = uv_default_loop();
- uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
- uv_fs_req_cleanup(&req);
- file_handle = CreateFile("test_dir/test_file",
- GENERIC_WRITE | FILE_WRITE_ATTRIBUTES,
- 0,
- NULL,
- CREATE_ALWAYS,
- FILE_FLAG_OPEN_REPARSE_POINT |
- FILE_FLAG_BACKUP_SEMANTICS,
- NULL);
- ASSERT(file_handle != INVALID_HANDLE_VALUE);
- memset(&reparse_buffer, 0, REPARSE_GUID_DATA_BUFFER_HEADER_SIZE);
- reparse_buffer.ReparseTag = REPARSE_TAG;
- reparse_buffer.ReparseDataLength = 0;
- reparse_buffer.ReparseGuid = REPARSE_GUID;
- r = DeviceIoControl(file_handle,
- FSCTL_SET_REPARSE_POINT,
- &reparse_buffer,
- REPARSE_GUID_DATA_BUFFER_HEADER_SIZE,
- NULL,
- 0,
- &bytes_returned,
- NULL);
- ASSERT(r != 0);
- CloseHandle(file_handle);
- r = uv_fs_readlink(NULL, &req, "test_dir/test_file", NULL);
- ASSERT(r == UV_EINVAL && GetLastError() == ERROR_SYMLINK_NOT_SUPPORTED);
- uv_fs_req_cleanup(&req);
- /*
- Placeholder tests for exercising the behavior fixed in issue #995.
- To run, update the path with the IP address of a Mac with the hard drive
- shared via SMB as "Macintosh HD".
- r = uv_fs_stat(NULL, &req, "\\\\<mac_ip>\\Macintosh HD\\.DS_Store", NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_lstat(NULL, &req, "\\\\<mac_ip>\\Macintosh HD\\.DS_Store", NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- */
- /*
- uv_fs_stat and uv_fs_lstat can only work on non-symlink reparse
- points when a minifilter driver is registered which intercepts
- associated filesystem requests. Installing a driver is beyond
- the scope of this test.
- r = uv_fs_stat(NULL, &req, "test_dir/test_file", NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_lstat(NULL, &req, "test_dir/test_file", NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- */
- r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
- ASSERT(r == 1);
- ASSERT(scandir_req.result == 1);
- ASSERT(scandir_req.ptr);
- while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
- ASSERT(strcmp(dent.name, "test_file") == 0);
- /* uv_fs_scandir incorrectly identifies non-symlink reparse points
- as links because it doesn't open the file and verify the reparse
- point tag. The PowerShell Get-ChildItem command shares this
- behavior, so it's reasonable to leave it as is. */
- ASSERT(dent.type == UV_DIRENT_LINK);
- }
- uv_fs_req_cleanup(&scandir_req);
- ASSERT(!scandir_req.ptr);
- /* clean-up */
- unlink("test_dir/test_file");
- rmdir("test_dir");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif
- TEST_IMPL(fs_utime) {
- utime_check_t checkme;
- const char* path = "test_file";
- double atime;
- double mtime;
- uv_fs_t req;
- int r;
- /* Setup. */
- loop = uv_default_loop();
- unlink(path);
- r = uv_fs_open(NULL, &req, path, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- uv_fs_req_cleanup(&req);
- uv_fs_close(loop, &req, r, NULL);
- atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
- /*
- * Test sub-second timestamps only on Windows (assuming NTFS). Some other
- * platforms support sub-second timestamps, but that support is filesystem-
- * dependent. Notably OS X (HFS Plus) does NOT support sub-second timestamps.
- */
- #ifdef _WIN32
- mtime += 0.444; /* 1982-09-10 11:22:33.444 */
- #endif
- r = uv_fs_utime(NULL, &req, path, atime, mtime, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_stat(NULL, &req, path, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- check_utime(path, atime, mtime, /* test_lutime */ 0);
- uv_fs_req_cleanup(&req);
- atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
- checkme.path = path;
- checkme.atime = atime;
- checkme.mtime = mtime;
- /* async utime */
- utime_req.data = &checkme;
- r = uv_fs_utime(loop, &utime_req, path, atime, mtime, utime_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(utime_cb_count == 1);
- /* Cleanup. */
- unlink(path);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #ifdef _WIN32
- TEST_IMPL(fs_stat_root) {
- int r;
- r = uv_fs_stat(NULL, &stat_req, "\\", NULL);
- ASSERT(r == 0);
- r = uv_fs_stat(NULL, &stat_req, "..\\..\\..\\..\\..\\..\\..", NULL);
- ASSERT(r == 0);
- r = uv_fs_stat(NULL, &stat_req, "..", NULL);
- ASSERT(r == 0);
- r = uv_fs_stat(NULL, &stat_req, "..\\", NULL);
- ASSERT(r == 0);
- /* stats the current directory on c: */
- r = uv_fs_stat(NULL, &stat_req, "c:", NULL);
- ASSERT(r == 0);
- r = uv_fs_stat(NULL, &stat_req, "c:\\", NULL);
- ASSERT(r == 0);
- r = uv_fs_stat(NULL, &stat_req, "\\\\?\\C:\\", NULL);
- ASSERT(r == 0);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif
- TEST_IMPL(fs_futime) {
- utime_check_t checkme;
- const char* path = "test_file";
- double atime;
- double mtime;
- uv_file file;
- uv_fs_t req;
- int r;
- #if defined(_AIX) && !defined(_AIX71)
- RETURN_SKIP("futime is not implemented for AIX versions below 7.1");
- #endif
- /* Setup. */
- loop = uv_default_loop();
- unlink(path);
- r = uv_fs_open(NULL, &req, path, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- uv_fs_req_cleanup(&req);
- uv_fs_close(loop, &req, r, NULL);
- atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
- /*
- * Test sub-second timestamps only on Windows (assuming NTFS). Some other
- * platforms support sub-second timestamps, but that support is filesystem-
- * dependent. Notably OS X (HFS Plus) does NOT support sub-second timestamps.
- */
- #ifdef _WIN32
- mtime += 0.444; /* 1982-09-10 11:22:33.444 */
- #endif
- r = uv_fs_open(NULL, &req, path, O_RDWR, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result; /* FIXME probably not how it's supposed to be used */
- uv_fs_req_cleanup(&req);
- r = uv_fs_futime(NULL, &req, file, atime, mtime, NULL);
- #if defined(__CYGWIN__) || defined(__MSYS__)
- ASSERT(r == UV_ENOSYS);
- RETURN_SKIP("futime not supported on Cygwin");
- #else
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- #endif
- uv_fs_req_cleanup(&req);
- r = uv_fs_stat(NULL, &req, path, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- check_utime(path, atime, mtime, /* test_lutime */ 0);
- uv_fs_req_cleanup(&req);
- atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
- checkme.atime = atime;
- checkme.mtime = mtime;
- checkme.path = path;
- /* async futime */
- futime_req.data = &checkme;
- r = uv_fs_futime(loop, &futime_req, file, atime, mtime, futime_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(futime_cb_count == 1);
- /* Cleanup. */
- unlink(path);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_lutime) {
- utime_check_t checkme;
- const char* path = "test_file";
- const char* symlink_path = "test_file_symlink";
- double atime;
- double mtime;
- uv_fs_t req;
- int r, s;
- /* Setup */
- loop = uv_default_loop();
- unlink(path);
- r = uv_fs_open(NULL, &req, path, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- uv_fs_req_cleanup(&req);
- uv_fs_close(loop, &req, r, NULL);
- unlink(symlink_path);
- s = uv_fs_symlink(NULL, &req, path, symlink_path, 0, NULL);
- #ifdef _WIN32
- if (s == UV_EPERM) {
- /*
- * Creating a symlink before Windows 10 Creators Update was only allowed
- * when running elevated console (with admin rights)
- */
- RETURN_SKIP(
- "Symlink creation requires elevated console (with admin rights)");
- }
- #endif
- ASSERT(s == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- /* Test the synchronous version. */
- atime = mtime = 400497753; /* 1982-09-10 11:22:33 */
- #ifdef _WIN32
- mtime += 0.444; /* 1982-09-10 11:22:33.444 */
- #endif
- checkme.atime = atime;
- checkme.mtime = mtime;
- checkme.path = symlink_path;
- req.data = &checkme;
- r = uv_fs_lutime(NULL, &req, symlink_path, atime, mtime, NULL);
- #if (defined(_AIX) && !defined(_AIX71)) || \
- defined(__MVS__)
- ASSERT(r == UV_ENOSYS);
- RETURN_SKIP("lutime is not implemented for z/OS and AIX versions below 7.1");
- #endif
- ASSERT(r == 0);
- lutime_cb(&req);
- ASSERT(lutime_cb_count == 1);
- /* Test the asynchronous version. */
- atime = mtime = 1291404900; /* 2010-12-03 20:35:00 */
- checkme.atime = atime;
- checkme.mtime = mtime;
- checkme.path = symlink_path;
- r = uv_fs_lutime(loop, &req, symlink_path, atime, mtime, lutime_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(lutime_cb_count == 2);
- /* Cleanup. */
- unlink(path);
- unlink(symlink_path);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_stat_missing_path) {
- uv_fs_t req;
- int r;
- loop = uv_default_loop();
- r = uv_fs_stat(NULL, &req, "non_existent_file", NULL);
- ASSERT(r == UV_ENOENT);
- ASSERT(req.result == UV_ENOENT);
- uv_fs_req_cleanup(&req);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_scandir_empty_dir) {
- const char* path;
- uv_fs_t req;
- uv_dirent_t dent;
- int r;
- path = "./empty_dir/";
- loop = uv_default_loop();
- uv_fs_mkdir(NULL, &req, path, 0777, NULL);
- uv_fs_req_cleanup(&req);
- /* Fill the req to ensure that required fields are cleaned up */
- memset(&req, 0xdb, sizeof(req));
- r = uv_fs_scandir(NULL, &req, path, 0, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- ASSERT(req.ptr == NULL);
- ASSERT(UV_EOF == uv_fs_scandir_next(&req, &dent));
- uv_fs_req_cleanup(&req);
- r = uv_fs_scandir(loop, &scandir_req, path, 0, empty_scandir_cb);
- ASSERT(r == 0);
- ASSERT(scandir_cb_count == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(scandir_cb_count == 1);
- uv_fs_rmdir(NULL, &req, path, NULL);
- uv_fs_req_cleanup(&req);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_scandir_non_existent_dir) {
- const char* path;
- uv_fs_t req;
- uv_dirent_t dent;
- int r;
- path = "./non_existent_dir/";
- loop = uv_default_loop();
- uv_fs_rmdir(NULL, &req, path, NULL);
- uv_fs_req_cleanup(&req);
- /* Fill the req to ensure that required fields are cleaned up */
- memset(&req, 0xdb, sizeof(req));
- r = uv_fs_scandir(NULL, &req, path, 0, NULL);
- ASSERT(r == UV_ENOENT);
- ASSERT(req.result == UV_ENOENT);
- ASSERT(req.ptr == NULL);
- ASSERT(UV_ENOENT == uv_fs_scandir_next(&req, &dent));
- uv_fs_req_cleanup(&req);
- r = uv_fs_scandir(loop, &scandir_req, path, 0, non_existent_scandir_cb);
- ASSERT(r == 0);
- ASSERT(scandir_cb_count == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(scandir_cb_count == 1);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_scandir_file) {
- const char* path;
- int r;
- path = "test/fixtures/empty_file";
- loop = uv_default_loop();
- r = uv_fs_scandir(NULL, &scandir_req, path, 0, NULL);
- ASSERT(r == UV_ENOTDIR);
- uv_fs_req_cleanup(&scandir_req);
- r = uv_fs_scandir(loop, &scandir_req, path, 0, file_scandir_cb);
- ASSERT(r == 0);
- ASSERT(scandir_cb_count == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(scandir_cb_count == 1);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_open_dir) {
- const char* path;
- uv_fs_t req;
- int r, file;
- path = ".";
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &req, path, O_RDONLY, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- ASSERT(req.ptr == NULL);
- file = r;
- uv_fs_req_cleanup(&req);
- r = uv_fs_close(NULL, &req, file, NULL);
- ASSERT(r == 0);
- r = uv_fs_open(loop, &req, path, O_RDONLY, 0, open_cb_simple);
- ASSERT(r == 0);
- ASSERT(open_cb_count == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(open_cb_count == 1);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_file_open_append(int add_flags) {
- int r;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &open_req1, "test_file",
- O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(write_req.result >= 0);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_file",
- O_RDWR | O_APPEND | add_flags, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(write_req.result >= 0);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY | add_flags,
- S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- printf("read = %d\n", r);
- ASSERT(r == 26);
- ASSERT(read_req.result == 26);
- ASSERT(memcmp(buf,
- "test-buffer\n\0test-buffer\n\0",
- sizeof("test-buffer\n\0test-buffer\n\0") - 1) == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- }
- TEST_IMPL(fs_file_open_append) {
- fs_file_open_append(0);
- fs_file_open_append(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_rename_to_existing_file) {
- int r;
- /* Setup. */
- unlink("test_file");
- unlink("test_file2");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &open_req1, "test_file", O_WRONLY | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(write_req.result >= 0);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_file2", O_WRONLY | O_CREAT,
- S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_rename(NULL, &rename_req, "test_file", "test_file2", NULL);
- ASSERT(r == 0);
- ASSERT(rename_req.result == 0);
- uv_fs_req_cleanup(&rename_req);
- r = uv_fs_open(NULL, &open_req1, "test_file2", O_RDONLY, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(read_req.result >= 0);
- ASSERT(strcmp(buf, test_buf) == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- unlink("test_file2");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_read_bufs(int add_flags) {
- char scratch[768];
- uv_buf_t bufs[4];
- ASSERT(0 <= uv_fs_open(NULL, &open_req1,
- "test/fixtures/lorem_ipsum.txt",
- O_RDONLY | add_flags, 0, NULL));
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- ASSERT(UV_EINVAL == uv_fs_read(NULL, &read_req, open_req1.result,
- NULL, 0, 0, NULL));
- ASSERT(UV_EINVAL == uv_fs_read(NULL, &read_req, open_req1.result,
- NULL, 1, 0, NULL));
- ASSERT(UV_EINVAL == uv_fs_read(NULL, &read_req, open_req1.result,
- bufs, 0, 0, NULL));
- bufs[0] = uv_buf_init(scratch + 0, 256);
- bufs[1] = uv_buf_init(scratch + 256, 256);
- bufs[2] = uv_buf_init(scratch + 512, 128);
- bufs[3] = uv_buf_init(scratch + 640, 128);
- ASSERT(446 == uv_fs_read(NULL,
- &read_req,
- open_req1.result,
- bufs + 0,
- 2, /* 2x 256 bytes. */
- 0, /* Positional read. */
- NULL));
- ASSERT(read_req.result == 446);
- uv_fs_req_cleanup(&read_req);
- ASSERT(190 == uv_fs_read(NULL,
- &read_req,
- open_req1.result,
- bufs + 2,
- 2, /* 2x 128 bytes. */
- 256, /* Positional read. */
- NULL));
- ASSERT(read_req.result == /* 446 - 256 */ 190);
- uv_fs_req_cleanup(&read_req);
- ASSERT(0 == memcmp(bufs[1].base + 0, bufs[2].base, 128));
- ASSERT(0 == memcmp(bufs[1].base + 128, bufs[3].base, 190 - 128));
- ASSERT(0 == uv_fs_close(NULL, &close_req, open_req1.result, NULL));
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- }
- TEST_IMPL(fs_read_bufs) {
- fs_read_bufs(0);
- fs_read_bufs(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_read_file_eof(int add_flags) {
- #if defined(__CYGWIN__) || defined(__MSYS__)
- RETURN_SKIP("Cygwin pread at EOF may (incorrectly) return data!");
- #endif
- int r;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &open_req1, "test_file",
- O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(write_req.result >= 0);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY | add_flags, 0,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- memset(buf, 0, sizeof(buf));
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(read_req.result >= 0);
- ASSERT(strcmp(buf, test_buf) == 0);
- uv_fs_req_cleanup(&read_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1,
- read_req.result, NULL);
- ASSERT(r == 0);
- ASSERT(read_req.result == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- }
- TEST_IMPL(fs_read_file_eof) {
- fs_read_file_eof(0);
- fs_read_file_eof(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_write_multiple_bufs(int add_flags) {
- uv_buf_t iovs[2];
- int r;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL, &open_req1, "test_file",
- O_WRONLY | O_CREAT | add_flags, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iovs[0] = uv_buf_init(test_buf, sizeof(test_buf));
- iovs[1] = uv_buf_init(test_buf2, sizeof(test_buf2));
- r = uv_fs_write(NULL, &write_req, open_req1.result, iovs, 2, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(write_req.result >= 0);
- uv_fs_req_cleanup(&write_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY | add_flags, 0,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- memset(buf, 0, sizeof(buf));
- memset(buf2, 0, sizeof(buf2));
- /* Read the strings back to separate buffers. */
- iovs[0] = uv_buf_init(buf, sizeof(test_buf));
- iovs[1] = uv_buf_init(buf2, sizeof(test_buf2));
- ASSERT(lseek(open_req1.result, 0, SEEK_CUR) == 0);
- r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, 2, -1, NULL);
- ASSERT(r >= 0);
- ASSERT(read_req.result == sizeof(test_buf) + sizeof(test_buf2));
- ASSERT(strcmp(buf, test_buf) == 0);
- ASSERT(strcmp(buf2, test_buf2) == 0);
- uv_fs_req_cleanup(&read_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == 0);
- ASSERT(read_req.result == 0);
- uv_fs_req_cleanup(&read_req);
- /* Read the strings back to separate buffers. */
- iovs[0] = uv_buf_init(buf, sizeof(test_buf));
- iovs[1] = uv_buf_init(buf2, sizeof(test_buf2));
- r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, 2, 0, NULL);
- ASSERT(r >= 0);
- if (read_req.result == sizeof(test_buf)) {
- /* Infer that preadv is not available. */
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iovs[1], 1, read_req.result, NULL);
- ASSERT(r >= 0);
- ASSERT(read_req.result == sizeof(test_buf2));
- } else {
- ASSERT(read_req.result == sizeof(test_buf) + sizeof(test_buf2));
- }
- ASSERT(strcmp(buf, test_buf) == 0);
- ASSERT(strcmp(buf2, test_buf2) == 0);
- uv_fs_req_cleanup(&read_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1,
- sizeof(test_buf) + sizeof(test_buf2), NULL);
- ASSERT(r == 0);
- ASSERT(read_req.result == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- }
- TEST_IMPL(fs_write_multiple_bufs) {
- fs_write_multiple_bufs(0);
- fs_write_multiple_bufs(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_write_alotof_bufs(int add_flags) {
- size_t iovcount;
- size_t iovmax;
- uv_buf_t* iovs;
- char* buffer;
- size_t index;
- int r;
- iovcount = 54321;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- iovs = malloc(sizeof(*iovs) * iovcount);
- ASSERT(iovs != NULL);
- iovmax = uv_test_getiovmax();
- r = uv_fs_open(NULL,
- &open_req1,
- "test_file",
- O_RDWR | O_CREAT | add_flags,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- for (index = 0; index < iovcount; ++index)
- iovs[index] = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL,
- &write_req,
- open_req1.result,
- iovs,
- iovcount,
- -1,
- NULL);
- ASSERT(r >= 0);
- ASSERT((size_t)write_req.result == sizeof(test_buf) * iovcount);
- uv_fs_req_cleanup(&write_req);
- /* Read the strings back to separate buffers. */
- buffer = malloc(sizeof(test_buf) * iovcount);
- ASSERT(buffer != NULL);
- for (index = 0; index < iovcount; ++index)
- iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf),
- sizeof(test_buf));
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY | add_flags, 0,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, iovcount, -1, NULL);
- if (iovcount > iovmax)
- iovcount = iovmax;
- ASSERT(r >= 0);
- ASSERT((size_t)read_req.result == sizeof(test_buf) * iovcount);
- for (index = 0; index < iovcount; ++index)
- ASSERT(strncmp(buffer + index * sizeof(test_buf),
- test_buf,
- sizeof(test_buf)) == 0);
- uv_fs_req_cleanup(&read_req);
- free(buffer);
- ASSERT(lseek(open_req1.result, write_req.result, SEEK_SET) == write_req.result);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL,
- &read_req,
- open_req1.result,
- &iov,
- 1,
- -1,
- NULL);
- ASSERT(r == 0);
- ASSERT(read_req.result == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- free(iovs);
- }
- TEST_IMPL(fs_write_alotof_bufs) {
- fs_write_alotof_bufs(0);
- fs_write_alotof_bufs(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_write_alotof_bufs_with_offset(int add_flags) {
- size_t iovcount;
- size_t iovmax;
- uv_buf_t* iovs;
- char* buffer;
- size_t index;
- int r;
- int64_t offset;
- char* filler;
- int filler_len;
- filler = "0123456789";
- filler_len = strlen(filler);
- iovcount = 54321;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- iovs = malloc(sizeof(*iovs) * iovcount);
- ASSERT(iovs != NULL);
- iovmax = uv_test_getiovmax();
- r = uv_fs_open(NULL,
- &open_req1,
- "test_file",
- O_RDWR | O_CREAT | add_flags,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(filler, filler_len);
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == filler_len);
- ASSERT(write_req.result == filler_len);
- uv_fs_req_cleanup(&write_req);
- offset = (int64_t)r;
- for (index = 0; index < iovcount; ++index)
- iovs[index] = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL,
- &write_req,
- open_req1.result,
- iovs,
- iovcount,
- offset,
- NULL);
- ASSERT(r >= 0);
- ASSERT((size_t)write_req.result == sizeof(test_buf) * iovcount);
- uv_fs_req_cleanup(&write_req);
- /* Read the strings back to separate buffers. */
- buffer = malloc(sizeof(test_buf) * iovcount);
- ASSERT(buffer != NULL);
- for (index = 0; index < iovcount; ++index)
- iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf),
- sizeof(test_buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result,
- iovs, iovcount, offset, NULL);
- ASSERT(r >= 0);
- if (r == sizeof(test_buf))
- iovcount = 1; /* Infer that preadv is not available. */
- else if (iovcount > iovmax)
- iovcount = iovmax;
- ASSERT((size_t)read_req.result == sizeof(test_buf) * iovcount);
- for (index = 0; index < iovcount; ++index)
- ASSERT(strncmp(buffer + index * sizeof(test_buf),
- test_buf,
- sizeof(test_buf)) == 0);
- uv_fs_req_cleanup(&read_req);
- free(buffer);
- r = uv_fs_stat(NULL, &stat_req, "test_file", NULL);
- ASSERT(r == 0);
- ASSERT((int64_t)((uv_stat_t*)stat_req.ptr)->st_size ==
- offset + (int64_t)write_req.result);
- uv_fs_req_cleanup(&stat_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL,
- &read_req,
- open_req1.result,
- &iov,
- 1,
- offset + write_req.result,
- NULL);
- ASSERT(r == 0);
- ASSERT(read_req.result == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- free(iovs);
- }
- TEST_IMPL(fs_write_alotof_bufs_with_offset) {
- fs_write_alotof_bufs_with_offset(0);
- fs_write_alotof_bufs_with_offset(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_read_dir) {
- int r;
- char buf[2];
- loop = uv_default_loop();
- /* Setup */
- rmdir("test_dir");
- r = uv_fs_mkdir(loop, &mkdir_req, "test_dir", 0755, mkdir_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(mkdir_cb_count == 1);
- /* Setup Done Here */
- /* Get a file descriptor for the directory */
- r = uv_fs_open(loop,
- &open_req1,
- "test_dir",
- UV_FS_O_RDONLY | UV_FS_O_DIRECTORY,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- uv_fs_req_cleanup(&open_req1);
- /* Try to read data from the directory */
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 0, NULL);
- #if defined(__FreeBSD__) || \
- defined(__OpenBSD__) || \
- defined(__NetBSD__) || \
- defined(__DragonFly__) || \
- defined(_AIX) || \
- defined(__sun) || \
- defined(__MVS__)
- /*
- * As of now, these operating systems support reading from a directory,
- * that too depends on the filesystem this temporary test directory is
- * created on. That is why this assertion is a bit lenient.
- */
- ASSERT((r >= 0) || (r == UV_EISDIR));
- #else
- ASSERT(r == UV_EISDIR);
- #endif
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- rmdir("test_dir");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #ifdef _WIN32
- TEST_IMPL(fs_partial_read) {
- RETURN_SKIP("Test not implemented on Windows.");
- }
- TEST_IMPL(fs_partial_write) {
- RETURN_SKIP("Test not implemented on Windows.");
- }
- #else /* !_WIN32 */
- struct thread_ctx {
- pthread_t pid;
- int fd;
- char* data;
- int size;
- int interval;
- int doread;
- };
- static void thread_main(void* arg) {
- const struct thread_ctx* ctx;
- int size;
- char* data;
- ctx = (struct thread_ctx*)arg;
- size = ctx->size;
- data = ctx->data;
- while (size > 0) {
- ssize_t result;
- int nbytes;
- nbytes = size < ctx->interval ? size : ctx->interval;
- if (ctx->doread) {
- result = write(ctx->fd, data, nbytes);
- /* Should not see EINTR (or other errors) */
- ASSERT(result == nbytes);
- } else {
- result = read(ctx->fd, data, nbytes);
- /* Should not see EINTR (or other errors),
- * but might get a partial read if we are faster than the writer
- */
- ASSERT(result > 0 && result <= nbytes);
- }
- pthread_kill(ctx->pid, SIGUSR1);
- size -= result;
- data += result;
- }
- }
- static void sig_func(uv_signal_t* handle, int signum) {
- uv_signal_stop(handle);
- }
- static size_t uv_test_fs_buf_offset(uv_buf_t* bufs, size_t size) {
- size_t offset;
- /* Figure out which bufs are done */
- for (offset = 0; size > 0 && bufs[offset].len <= size; ++offset)
- size -= bufs[offset].len;
- /* Fix a partial read/write */
- if (size > 0) {
- bufs[offset].base += size;
- bufs[offset].len -= size;
- }
- return offset;
- }
- static void test_fs_partial(int doread) {
- struct thread_ctx ctx;
- uv_thread_t thread;
- uv_signal_t signal;
- int pipe_fds[2];
- size_t iovcount;
- uv_buf_t* iovs;
- char* buffer;
- size_t index;
- iovcount = 54321;
- iovs = malloc(sizeof(*iovs) * iovcount);
- ASSERT(iovs != NULL);
- ctx.pid = pthread_self();
- ctx.doread = doread;
- ctx.interval = 1000;
- ctx.size = sizeof(test_buf) * iovcount;
- ctx.data = malloc(ctx.size);
- ASSERT(ctx.data != NULL);
- buffer = malloc(ctx.size);
- ASSERT(buffer != NULL);
- for (index = 0; index < iovcount; ++index)
- iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf), sizeof(test_buf));
- loop = uv_default_loop();
- ASSERT(0 == uv_signal_init(loop, &signal));
- ASSERT(0 == uv_signal_start(&signal, sig_func, SIGUSR1));
- ASSERT(0 == pipe(pipe_fds));
- ctx.fd = pipe_fds[doread];
- ASSERT(0 == uv_thread_create(&thread, thread_main, &ctx));
- if (doread) {
- uv_buf_t* read_iovs;
- int nread;
- read_iovs = iovs;
- nread = 0;
- while (nread < ctx.size) {
- int result;
- result = uv_fs_read(loop, &read_req, pipe_fds[0], read_iovs, iovcount, -1, NULL);
- if (result > 0) {
- size_t read_iovcount;
- read_iovcount = uv_test_fs_buf_offset(read_iovs, result);
- read_iovs += read_iovcount;
- iovcount -= read_iovcount;
- nread += result;
- } else {
- ASSERT(result == UV_EINTR);
- }
- uv_fs_req_cleanup(&read_req);
- }
- } else {
- int result;
- result = uv_fs_write(loop, &write_req, pipe_fds[1], iovs, iovcount, -1, NULL);
- ASSERT(write_req.result == result);
- ASSERT(result == ctx.size);
- uv_fs_req_cleanup(&write_req);
- }
- ASSERT(0 == memcmp(buffer, ctx.data, ctx.size));
- ASSERT(0 == uv_thread_join(&thread));
- ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
- ASSERT(0 == close(pipe_fds[1]));
- uv_close((uv_handle_t*) &signal, NULL);
- { /* Make sure we read everything that we wrote. */
- int result;
- result = uv_fs_read(loop, &read_req, pipe_fds[0], iovs, 1, -1, NULL);
- ASSERT(result == 0);
- uv_fs_req_cleanup(&read_req);
- }
- ASSERT(0 == close(pipe_fds[0]));
- free(iovs);
- free(buffer);
- free(ctx.data);
- MAKE_VALGRIND_HAPPY();
- }
- TEST_IMPL(fs_partial_read) {
- test_fs_partial(1);
- return 0;
- }
- TEST_IMPL(fs_partial_write) {
- test_fs_partial(0);
- return 0;
- }
- #endif/* _WIN32 */
- TEST_IMPL(fs_read_write_null_arguments) {
- int r;
- r = uv_fs_read(NULL, &read_req, 0, NULL, 0, -1, NULL);
- ASSERT(r == UV_EINVAL);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_write(NULL, &write_req, 0, NULL, 0, -1, NULL);
- /* Validate some memory management on failed input validation before sending
- fs work to the thread pool. */
- ASSERT(r == UV_EINVAL);
- ASSERT(write_req.path == NULL);
- ASSERT(write_req.ptr == NULL);
- #ifdef _WIN32
- ASSERT(write_req.file.pathw == NULL);
- ASSERT(write_req.fs.info.new_pathw == NULL);
- ASSERT(write_req.fs.info.bufs == NULL);
- #else
- ASSERT(write_req.new_path == NULL);
- ASSERT(write_req.bufs == NULL);
- #endif
- uv_fs_req_cleanup(&write_req);
- iov = uv_buf_init(NULL, 0);
- r = uv_fs_read(NULL, &read_req, 0, &iov, 0, -1, NULL);
- ASSERT(r == UV_EINVAL);
- uv_fs_req_cleanup(&read_req);
- iov = uv_buf_init(NULL, 0);
- r = uv_fs_write(NULL, &write_req, 0, &iov, 0, -1, NULL);
- ASSERT(r == UV_EINVAL);
- uv_fs_req_cleanup(&write_req);
- /* If the arguments are invalid, the loop should not be kept open */
- loop = uv_default_loop();
- r = uv_fs_read(loop, &read_req, 0, NULL, 0, -1, fail_cb);
- ASSERT(r == UV_EINVAL);
- uv_run(loop, UV_RUN_DEFAULT);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_write(loop, &write_req, 0, NULL, 0, -1, fail_cb);
- ASSERT(r == UV_EINVAL);
- uv_run(loop, UV_RUN_DEFAULT);
- uv_fs_req_cleanup(&write_req);
- iov = uv_buf_init(NULL, 0);
- r = uv_fs_read(loop, &read_req, 0, &iov, 0, -1, fail_cb);
- ASSERT(r == UV_EINVAL);
- uv_run(loop, UV_RUN_DEFAULT);
- uv_fs_req_cleanup(&read_req);
- iov = uv_buf_init(NULL, 0);
- r = uv_fs_write(loop, &write_req, 0, &iov, 0, -1, fail_cb);
- ASSERT(r == UV_EINVAL);
- uv_run(loop, UV_RUN_DEFAULT);
- uv_fs_req_cleanup(&write_req);
- return 0;
- }
- TEST_IMPL(get_osfhandle_valid_handle) {
- int r;
- uv_os_fd_t fd;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL,
- &open_req1,
- "test_file",
- O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- fd = uv_get_osfhandle(open_req1.result);
- #ifdef _WIN32
- ASSERT(fd != INVALID_HANDLE_VALUE);
- #else
- ASSERT(fd >= 0);
- #endif
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup. */
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(open_osfhandle_valid_handle) {
- int r;
- uv_os_fd_t handle;
- int fd;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(NULL,
- &open_req1,
- "test_file",
- O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- handle = uv_get_osfhandle(open_req1.result);
- #ifdef _WIN32
- ASSERT(handle != INVALID_HANDLE_VALUE);
- #else
- ASSERT(handle >= 0);
- #endif
- fd = uv_open_osfhandle(handle);
- #ifdef _WIN32
- ASSERT(fd > 0);
- #else
- ASSERT(fd == open_req1.result);
- #endif
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup. */
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_file_pos_after_op_with_offset) {
- int r;
- /* Setup. */
- unlink("test_file");
- loop = uv_default_loop();
- r = uv_fs_open(loop,
- &open_req1,
- "test_file",
- O_RDWR | O_CREAT,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r > 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(test_buf, sizeof(test_buf));
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 0, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(lseek(open_req1.result, 0, SEEK_CUR) == 0);
- uv_fs_req_cleanup(&write_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 0, NULL);
- ASSERT(r == sizeof(test_buf));
- ASSERT(strcmp(buf, test_buf) == 0);
- ASSERT(lseek(open_req1.result, 0, SEEK_CUR) == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #ifdef _WIN32
- static void fs_file_pos_common() {
- int r;
- iov = uv_buf_init("abc", 3);
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == 3);
- uv_fs_req_cleanup(&write_req);
- /* Read with offset should not change the position */
- iov = uv_buf_init(buf, 1);
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 1, NULL);
- ASSERT(r == 1);
- ASSERT(buf[0] == 'b');
- uv_fs_req_cleanup(&read_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&read_req);
- /* Write without offset should change the position */
- iov = uv_buf_init("d", 1);
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == 1);
- uv_fs_req_cleanup(&write_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&read_req);
- }
- static void fs_file_pos_close_check(const char *contents, int size) {
- int r;
- /* Close */
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- /* Confirm file contents */
- r = uv_fs_open(NULL, &open_req1, "test_file", O_RDONLY, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == size);
- ASSERT(strncmp(buf, contents, size) == 0);
- uv_fs_req_cleanup(&read_req);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- }
- static void fs_file_pos_write(int add_flags) {
- int r;
- /* Setup. */
- unlink("test_file");
- r = uv_fs_open(NULL,
- &open_req1,
- "test_file",
- O_TRUNC | O_CREAT | O_RDWR | add_flags,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r > 0);
- uv_fs_req_cleanup(&open_req1);
- fs_file_pos_common();
- /* Write with offset should not change the position */
- iov = uv_buf_init("e", 1);
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 1, NULL);
- ASSERT(r == 1);
- uv_fs_req_cleanup(&write_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&read_req);
- fs_file_pos_close_check("aecd", 4);
- }
- TEST_IMPL(fs_file_pos_write) {
- fs_file_pos_write(0);
- fs_file_pos_write(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void fs_file_pos_append(int add_flags) {
- int r;
- /* Setup. */
- unlink("test_file");
- r = uv_fs_open(NULL,
- &open_req1,
- "test_file",
- O_APPEND | O_CREAT | O_RDWR | add_flags,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r > 0);
- uv_fs_req_cleanup(&open_req1);
- fs_file_pos_common();
- /* Write with offset appends (ignoring offset)
- * but does not change the position */
- iov = uv_buf_init("e", 1);
- r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 1, NULL);
- ASSERT(r == 1);
- uv_fs_req_cleanup(&write_req);
- iov = uv_buf_init(buf, sizeof(buf));
- r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
- ASSERT(r == 1);
- ASSERT(buf[0] == 'e');
- uv_fs_req_cleanup(&read_req);
- fs_file_pos_close_check("abcde", 5);
- }
- TEST_IMPL(fs_file_pos_append) {
- fs_file_pos_append(0);
- fs_file_pos_append(UV_FS_O_FILEMAP);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif
- TEST_IMPL(fs_null_req) {
- /* Verify that all fs functions return UV_EINVAL when the request is NULL. */
- int r;
- r = uv_fs_open(NULL, NULL, NULL, 0, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_close(NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_read(NULL, NULL, 0, NULL, 0, -1, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_write(NULL, NULL, 0, NULL, 0, -1, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_unlink(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_mkdir(NULL, NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_mkdtemp(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_mkstemp(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_rmdir(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_scandir(NULL, NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_link(NULL, NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_symlink(NULL, NULL, NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_readlink(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_realpath(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_chown(NULL, NULL, NULL, 0, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_fchown(NULL, NULL, 0, 0, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_stat(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_lstat(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_fstat(NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_rename(NULL, NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_fsync(NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_fdatasync(NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_ftruncate(NULL, NULL, 0, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_copyfile(NULL, NULL, NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_sendfile(NULL, NULL, 0, 0, 0, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_access(NULL, NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_chmod(NULL, NULL, NULL, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_fchmod(NULL, NULL, 0, 0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_utime(NULL, NULL, NULL, 0.0, 0.0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_futime(NULL, NULL, 0, 0.0, 0.0, NULL);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_statfs(NULL, NULL, NULL, NULL);
- ASSERT(r == UV_EINVAL);
- /* This should be a no-op. */
- uv_fs_req_cleanup(NULL);
- return 0;
- }
- #ifdef _WIN32
- TEST_IMPL(fs_exclusive_sharing_mode) {
- int r;
- /* Setup. */
- unlink("test_file");
- ASSERT(UV_FS_O_EXLOCK > 0);
- r = uv_fs_open(NULL,
- &open_req1,
- "test_file",
- O_RDWR | O_CREAT | UV_FS_O_EXLOCK,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_open(NULL,
- &open_req2,
- "test_file",
- O_RDONLY | UV_FS_O_EXLOCK,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r < 0);
- ASSERT(open_req2.result < 0);
- uv_fs_req_cleanup(&open_req2);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- r = uv_fs_open(NULL,
- &open_req2,
- "test_file",
- O_RDONLY | UV_FS_O_EXLOCK,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req2.result >= 0);
- uv_fs_req_cleanup(&open_req2);
- r = uv_fs_close(NULL, &close_req, open_req2.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Cleanup */
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif
- #ifdef _WIN32
- TEST_IMPL(fs_file_flag_no_buffering) {
- int r;
- /* Setup. */
- unlink("test_file");
- ASSERT(UV_FS_O_APPEND > 0);
- ASSERT(UV_FS_O_CREAT > 0);
- ASSERT(UV_FS_O_DIRECT > 0);
- ASSERT(UV_FS_O_RDWR > 0);
- /* FILE_APPEND_DATA must be excluded from FILE_GENERIC_WRITE: */
- r = uv_fs_open(NULL,
- &open_req1,
- "test_file",
- UV_FS_O_RDWR | UV_FS_O_CREAT | UV_FS_O_DIRECT,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* FILE_APPEND_DATA and FILE_FLAG_NO_BUFFERING are mutually exclusive: */
- r = uv_fs_open(NULL,
- &open_req2,
- "test_file",
- UV_FS_O_APPEND | UV_FS_O_DIRECT,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r == UV_EINVAL);
- ASSERT(open_req2.result == UV_EINVAL);
- uv_fs_req_cleanup(&open_req2);
- /* Cleanup */
- unlink("test_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif
- #ifdef _WIN32
- int call_icacls(const char* command, ...) {
- char icacls_command[1024];
- va_list args;
- va_start(args, command);
- vsnprintf(icacls_command, ARRAYSIZE(icacls_command), command, args);
- va_end(args);
- return system(icacls_command);
- }
- TEST_IMPL(fs_open_readonly_acl) {
- uv_passwd_t pwd;
- uv_fs_t req;
- int r;
- /*
- Based on Node.js test from
- https://github.com/nodejs/node/commit/3ba81e34e86a5c32658e218cb6e65b13e8326bc5
- If anything goes wrong, you can delte the test_fle_icacls with:
- icacls test_file_icacls /remove "%USERNAME%" /inheritance:e
- attrib -r test_file_icacls
- del test_file_icacls
- */
- /* Setup - clear the ACL and remove the file */
- loop = uv_default_loop();
- r = uv_os_get_passwd(&pwd);
- ASSERT(r == 0);
- call_icacls("icacls test_file_icacls /remove \"%s\" /inheritance:e",
- pwd.username);
- uv_fs_chmod(loop, &req, "test_file_icacls", S_IWUSR, NULL);
- unlink("test_file_icacls");
- /* Create the file */
- r = uv_fs_open(loop,
- &open_req1,
- "test_file_icacls",
- O_RDONLY | O_CREAT,
- S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(open_req1.result >= 0);
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- ASSERT(r == 0);
- ASSERT(close_req.result == 0);
- uv_fs_req_cleanup(&close_req);
- /* Set up ACL */
- r = call_icacls("icacls test_file_icacls /inheritance:r /remove \"%s\"",
- pwd.username);
- if (r != 0) {
- goto acl_cleanup;
- }
- r = call_icacls("icacls test_file_icacls /grant \"%s\":RX", pwd.username);
- if (r != 0) {
- goto acl_cleanup;
- }
- /* Try opening the file */
- r = uv_fs_open(NULL, &open_req1, "test_file_icacls", O_RDONLY, 0, NULL);
- if (r < 0) {
- goto acl_cleanup;
- }
- uv_fs_req_cleanup(&open_req1);
- r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
- if (r != 0) {
- goto acl_cleanup;
- }
- uv_fs_req_cleanup(&close_req);
- acl_cleanup:
- /* Cleanup */
- call_icacls("icacls test_file_icacls /remove \"%s\" /inheritance:e",
- pwd.username);
- unlink("test_file_icacls");
- uv_os_free_passwd(&pwd);
- ASSERT(r == 0);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif
- #ifdef _WIN32
- TEST_IMPL(fs_fchmod_archive_readonly) {
- uv_fs_t req;
- uv_file file;
- int r;
- /* Test clearing read-only flag from files with Archive flag cleared */
- /* Setup*/
- unlink("test_file");
- r = uv_fs_open(NULL,
- &req,
- "test_file",
- O_WRONLY | O_CREAT,
- S_IWUSR | S_IRUSR,
- NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- r = uv_fs_close(NULL, &req, file, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- /* Make the file read-only and clear archive flag */
- r = SetFileAttributes("test_file", FILE_ATTRIBUTE_READONLY);
- ASSERT(r != 0);
- check_permission("test_file", 0400);
- /* Try fchmod */
- r = uv_fs_open(NULL, &req, "test_file", O_RDONLY, 0, NULL);
- ASSERT(r >= 0);
- ASSERT(req.result >= 0);
- file = req.result;
- uv_fs_req_cleanup(&req);
- r = uv_fs_fchmod(NULL, &req, file, S_IWUSR, NULL);
- ASSERT(r == 0);
- ASSERT(req.result == 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_close(NULL, &req, file, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- check_permission("test_file", S_IWUSR);
- /* Restore Archive flag for rest of the tests */
- r = SetFileAttributes("test_file", FILE_ATTRIBUTE_ARCHIVE);
- ASSERT(r != 0);
- return 0;
- }
- TEST_IMPL(fs_invalid_mkdir_name) {
- uv_loop_t* loop;
- uv_fs_t req;
- int r;
- loop = uv_default_loop();
- r = uv_fs_mkdir(loop, &req, "invalid>", 0, NULL);
- ASSERT(r == UV_EINVAL);
- return 0;
- }
- #endif
- TEST_IMPL(fs_statfs) {
- uv_fs_t req;
- int r;
- loop = uv_default_loop();
- /* Test the synchronous version. */
- r = uv_fs_statfs(NULL, &req, ".", NULL);
- ASSERT(r == 0);
- statfs_cb(&req);
- ASSERT(statfs_cb_count == 1);
- /* Test the asynchronous version. */
- r = uv_fs_statfs(loop, &req, ".", statfs_cb);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(statfs_cb_count == 2);
- return 0;
- }
|