binding.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  1. #include <assert.h>
  2. #include <bare.h>
  3. #include <js.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <utf.h>
  8. #include <uv.h>
  9. /**
  10. * Per-thread singleton response handler that all file system requests
  11. * reference.
  12. */
  13. typedef struct {
  14. js_env_t *env;
  15. js_ref_t *ctx;
  16. js_ref_t *on_response;
  17. } bare_fs_t;
  18. typedef struct {
  19. uv_fs_t req;
  20. js_env_t *env;
  21. js_ref_t *ctx;
  22. js_ref_t *on_response;
  23. uint32_t id;
  24. js_ref_t *data;
  25. } bare_fs_req_t;
  26. typedef utf8_t bare_fs_path_t[4096 + 1 /* NULL */];
  27. typedef struct {
  28. uv_dir_t *dir;
  29. } bare_fs_dir_t;
  30. typedef struct {
  31. uv_fs_event_t handle;
  32. js_env_t *env;
  33. js_ref_t *ctx;
  34. js_ref_t *on_event;
  35. js_ref_t *on_close;
  36. } bare_fs_watcher_t;
  37. typedef uv_dirent_t bare_fs_dirent_t;
  38. static inline void
  39. on_fs_response (uv_fs_t *uv_req) {
  40. int err;
  41. bare_fs_req_t *req = (bare_fs_req_t *) uv_req;
  42. js_env_t *env = req->env;
  43. js_handle_scope_t *scope;
  44. err = js_open_handle_scope(env, &scope);
  45. assert(err == 0);
  46. js_value_t *ctx;
  47. err = js_get_reference_value(env, req->ctx, &ctx);
  48. assert(err == 0);
  49. js_value_t *on_response;
  50. err = js_get_reference_value(env, req->on_response, &on_response);
  51. assert(err == 0);
  52. js_value_t *args[3];
  53. err = js_create_uint32(env, req->id, &args[0]);
  54. assert(err == 0);
  55. if (uv_req->result < 0) {
  56. js_value_t *code;
  57. err = js_create_string_utf8(env, (utf8_t *) uv_err_name(uv_req->result), -1, &code);
  58. assert(err == 0);
  59. js_value_t *message;
  60. err = js_create_string_utf8(env, (utf8_t *) uv_strerror(uv_req->result), -1, &message);
  61. assert(err == 0);
  62. err = js_create_error(env, code, message, &args[1]);
  63. assert(err == 0);
  64. } else {
  65. err = js_get_null(env, &args[1]);
  66. assert(err == 0);
  67. }
  68. err = js_create_int32(env, uv_req->result, &args[2]);
  69. assert(err == 0);
  70. uv_fs_req_cleanup(uv_req);
  71. if (req->data) {
  72. err = js_delete_reference(env, req->data);
  73. assert(err == 0);
  74. req->data = NULL;
  75. }
  76. js_call_function(req->env, ctx, on_response, 3, args, NULL);
  77. err = js_close_handle_scope(req->env, scope);
  78. assert(err == 0);
  79. }
  80. static void
  81. on_fs_stat_response (uv_fs_t *uv_req) {
  82. int err;
  83. bare_fs_req_t *req = (bare_fs_req_t *) uv_req;
  84. js_env_t *env = req->env;
  85. if (uv_req->result == 0) {
  86. js_value_t *data;
  87. err = js_get_reference_value(env, req->data, &data);
  88. assert(err == 0);
  89. uint32_t i = 0;
  90. #define V(property) \
  91. { \
  92. js_value_t *value; \
  93. err = js_create_int64(env, uv_req->statbuf.st_##property, &value); \
  94. assert(err == 0); \
  95. \
  96. err = js_set_element(env, data, i++, value); \
  97. assert(err == 0); \
  98. }
  99. V(dev)
  100. V(mode)
  101. V(nlink)
  102. V(uid)
  103. V(gid)
  104. V(rdev)
  105. V(blksize)
  106. V(ino)
  107. V(size)
  108. V(blocks)
  109. #undef V
  110. #define V(property) \
  111. { \
  112. uv_timespec_t time = uv_req->statbuf.st_##property; \
  113. \
  114. js_value_t *value; \
  115. err = js_create_int64(env, time.tv_sec * 1e3 + time.tv_nsec / 1e6, &value); \
  116. assert(err == 0); \
  117. \
  118. err = js_set_element(env, data, i++, value); \
  119. assert(err == 0); \
  120. }
  121. V(atim)
  122. V(mtim)
  123. V(ctim)
  124. V(birthtim)
  125. #undef V
  126. }
  127. on_fs_response(uv_req);
  128. }
  129. static void
  130. on_fs_realpath_response (uv_fs_t *uv_req) {
  131. int err;
  132. bare_fs_req_t *req = (bare_fs_req_t *) uv_req;
  133. js_env_t *env = req->env;
  134. if (uv_req->result == 0) {
  135. js_value_t *data;
  136. err = js_get_reference_value(env, req->data, &data);
  137. assert(err == 0);
  138. char *path;
  139. err = js_get_typedarray_info(env, data, NULL, (void **) &path, NULL, NULL, NULL);
  140. assert(err == 0);
  141. strncpy(path, uv_req->ptr, sizeof(bare_fs_path_t));
  142. }
  143. on_fs_response(uv_req);
  144. }
  145. static void
  146. on_fs_readlink_response (uv_fs_t *uv_req) {
  147. int err;
  148. bare_fs_req_t *req = (bare_fs_req_t *) uv_req;
  149. js_env_t *env = req->env;
  150. if (uv_req->result == 0) {
  151. js_value_t *data;
  152. err = js_get_reference_value(env, req->data, &data);
  153. assert(err == 0);
  154. char *path;
  155. err = js_get_typedarray_info(env, data, NULL, (void **) &path, NULL, NULL, NULL);
  156. assert(err == 0);
  157. strncpy(path, uv_req->ptr, sizeof(bare_fs_path_t));
  158. }
  159. on_fs_response(uv_req);
  160. }
  161. static void
  162. on_fs_opendir_response (uv_fs_t *uv_req) {
  163. int err;
  164. bare_fs_req_t *req = (bare_fs_req_t *) uv_req;
  165. js_env_t *env = req->env;
  166. if (uv_req->result == 0) {
  167. js_value_t *data;
  168. err = js_get_reference_value(env, req->data, &data);
  169. assert(err == 0);
  170. bare_fs_dir_t *dir;
  171. err = js_get_typedarray_info(env, data, NULL, (void **) &dir, NULL, NULL, NULL);
  172. assert(err == 0);
  173. dir->dir = uv_req->ptr;
  174. }
  175. on_fs_response(uv_req);
  176. }
  177. static void
  178. on_fs_readdir_response (uv_fs_t *uv_req) {
  179. int err;
  180. bare_fs_req_t *req = (bare_fs_req_t *) uv_req;
  181. js_env_t *env = req->env;
  182. if (uv_req->result > 0) {
  183. js_value_t *data;
  184. err = js_get_reference_value(env, req->data, &data);
  185. assert(err == 0);
  186. uv_dir_t *dir = uv_req->ptr;
  187. for (size_t i = 0, n = uv_req->result; i < n; i++) {
  188. uv_dirent_t *dirent = &dir->dirents[i];
  189. js_value_t *entry;
  190. err = js_create_object(env, &entry);
  191. assert(err == 0);
  192. err = js_set_element(env, data, i, entry);
  193. assert(err == 0);
  194. size_t name_len = strlen(dirent->name);
  195. js_value_t *name;
  196. void *data;
  197. err = js_create_arraybuffer(env, name_len, &data, &name);
  198. assert(err == 0);
  199. memcpy(data, dirent->name, name_len);
  200. err = js_set_named_property(env, entry, "name", name);
  201. assert(err == 0);
  202. js_value_t *type;
  203. err = js_create_uint32(env, dirent->type, &type);
  204. assert(err == 0);
  205. err = js_set_named_property(env, entry, "type", type);
  206. assert(err == 0);
  207. }
  208. }
  209. on_fs_response(uv_req);
  210. }
  211. static void
  212. on_fs_watcher_event (uv_fs_event_t *handle, const char *filename, int events, int status) {
  213. int err;
  214. bare_fs_watcher_t *watcher = (bare_fs_watcher_t *) handle;
  215. js_env_t *env = watcher->env;
  216. js_handle_scope_t *scope;
  217. err = js_open_handle_scope(env, &scope);
  218. assert(err == 0);
  219. js_value_t *ctx;
  220. err = js_get_reference_value(env, watcher->ctx, &ctx);
  221. assert(err == 0);
  222. js_value_t *on_event;
  223. err = js_get_reference_value(env, watcher->on_event, &on_event);
  224. assert(err == 0);
  225. js_value_t *args[3];
  226. if (status < 0) {
  227. js_value_t *code;
  228. err = js_create_string_utf8(env, (utf8_t *) uv_err_name(status), -1, &code);
  229. assert(err == 0);
  230. js_value_t *message;
  231. err = js_create_string_utf8(env, (utf8_t *) uv_strerror(status), -1, &message);
  232. assert(err == 0);
  233. err = js_create_error(env, code, message, &args[0]);
  234. assert(err == 0);
  235. err = js_create_int32(env, 0, &args[1]);
  236. assert(err == 0);
  237. err = js_get_null(env, &args[2]);
  238. assert(err == 0);
  239. } else {
  240. err = js_get_null(env, &args[0]);
  241. assert(err == 0);
  242. err = js_create_int32(env, events, &args[1]);
  243. assert(err == 0);
  244. size_t len = strlen(filename);
  245. void *data;
  246. err = js_create_arraybuffer(env, len, &data, &args[2]);
  247. assert(err == 0);
  248. memcpy(data, (void *) filename, len);
  249. }
  250. js_call_function(env, ctx, on_event, 3, args, NULL);
  251. err = js_close_handle_scope(env, scope);
  252. assert(err == 0);
  253. }
  254. static void
  255. on_fs_watcher_close (uv_handle_t *handle) {
  256. int err;
  257. bare_fs_watcher_t *watcher = (bare_fs_watcher_t *) handle;
  258. js_env_t *env = watcher->env;
  259. js_handle_scope_t *scope;
  260. err = js_open_handle_scope(env, &scope);
  261. assert(err == 0);
  262. js_value_t *ctx;
  263. err = js_get_reference_value(env, watcher->ctx, &ctx);
  264. assert(err == 0);
  265. js_value_t *on_close;
  266. err = js_get_reference_value(env, watcher->on_close, &on_close);
  267. assert(err == 0);
  268. js_call_function(env, ctx, on_close, 0, NULL, NULL);
  269. err = js_delete_reference(env, watcher->on_event);
  270. assert(err == 0);
  271. err = js_delete_reference(env, watcher->on_close);
  272. assert(err == 0);
  273. err = js_delete_reference(env, watcher->ctx);
  274. assert(err == 0);
  275. err = js_close_handle_scope(env, scope);
  276. assert(err == 0);
  277. }
  278. static js_value_t *
  279. bare_fs_init (js_env_t *env, js_callback_info_t *info) {
  280. int err;
  281. size_t argc = 3;
  282. js_value_t *argv[3];
  283. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  284. assert(err == 0);
  285. assert(argc == 3);
  286. bare_fs_t *fs;
  287. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &fs, NULL, NULL, NULL);
  288. assert(err == 0);
  289. fs->env = env;
  290. err = js_create_reference(env, argv[1], 1, &fs->ctx);
  291. assert(err == 0);
  292. err = js_create_reference(env, argv[2], 1, &fs->on_response);
  293. assert(err == 0);
  294. return NULL;
  295. }
  296. static js_value_t *
  297. bare_fs_destroy (js_env_t *env, js_callback_info_t *info) {
  298. int err;
  299. size_t argc = 1;
  300. js_value_t *argv[1];
  301. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  302. assert(err == 0);
  303. assert(argc == 1);
  304. bare_fs_t *fs;
  305. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &fs, NULL, NULL, NULL);
  306. assert(err == 0);
  307. err = js_delete_reference(env, fs->on_response);
  308. assert(err == 0);
  309. err = js_delete_reference(env, fs->ctx);
  310. assert(err == 0);
  311. return NULL;
  312. }
  313. static js_value_t *
  314. bare_fs_req_init (js_env_t *env, js_callback_info_t *info) {
  315. int err;
  316. size_t argc = 2;
  317. js_value_t *argv[2];
  318. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  319. assert(err == 0);
  320. assert(argc == 2);
  321. bare_fs_t *fs;
  322. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &fs, NULL, NULL, NULL);
  323. assert(err == 0);
  324. bare_fs_req_t *req;
  325. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &req, NULL, NULL, NULL);
  326. assert(err == 0);
  327. // Copy the singleton response handler to the request.
  328. req->env = fs->env;
  329. req->ctx = fs->ctx;
  330. req->on_response = fs->on_response;
  331. return NULL;
  332. }
  333. static js_value_t *
  334. bare_fs_open (js_env_t *env, js_callback_info_t *info) {
  335. int err;
  336. size_t argc = 4;
  337. js_value_t *argv[4];
  338. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  339. assert(err == 0);
  340. assert(argc == 4);
  341. bare_fs_req_t *req;
  342. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  343. assert(err == 0);
  344. bare_fs_path_t path;
  345. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  346. assert(err == 0);
  347. int32_t flags;
  348. err = js_get_value_int32(env, argv[2], &flags);
  349. assert(err == 0);
  350. int32_t mode;
  351. err = js_get_value_int32(env, argv[3], &mode);
  352. assert(err == 0);
  353. uv_loop_t *loop;
  354. js_get_env_loop(env, &loop);
  355. uv_fs_open(loop, (uv_fs_t *) req, (char *) path, flags, mode, on_fs_response);
  356. return NULL;
  357. }
  358. static js_value_t *
  359. bare_fs_open_sync (js_env_t *env, js_callback_info_t *info) {
  360. int err;
  361. size_t argc = 3;
  362. js_value_t *argv[3];
  363. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  364. assert(err == 0);
  365. assert(argc == 3);
  366. bare_fs_path_t path;
  367. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  368. assert(err == 0);
  369. int32_t flags;
  370. err = js_get_value_int32(env, argv[1], &flags);
  371. assert(err == 0);
  372. int32_t mode;
  373. err = js_get_value_int32(env, argv[2], &mode);
  374. assert(err == 0);
  375. uv_loop_t *loop;
  376. js_get_env_loop(env, &loop);
  377. uv_fs_t req;
  378. uv_fs_open(loop, &req, (char *) path, flags, mode, NULL);
  379. js_value_t *res = NULL;
  380. if (req.result < 0) {
  381. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  382. } else {
  383. err = js_create_int32(env, req.result, &res);
  384. assert(err == 0);
  385. }
  386. uv_fs_req_cleanup(&req);
  387. return res;
  388. }
  389. static js_value_t *
  390. bare_fs_close (js_env_t *env, js_callback_info_t *info) {
  391. int err;
  392. size_t argc = 2;
  393. js_value_t *argv[2];
  394. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  395. assert(err == 0);
  396. assert(argc == 2);
  397. bare_fs_req_t *req;
  398. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  399. assert(err == 0);
  400. uint32_t fd;
  401. err = js_get_value_uint32(env, argv[1], &fd);
  402. assert(err == 0);
  403. uv_loop_t *loop;
  404. js_get_env_loop(env, &loop);
  405. uv_fs_close(loop, (uv_fs_t *) req, fd, on_fs_response);
  406. return NULL;
  407. }
  408. static js_value_t *
  409. bare_fs_close_sync (js_env_t *env, js_callback_info_t *info) {
  410. int err;
  411. size_t argc = 1;
  412. js_value_t *argv[1];
  413. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  414. assert(err == 0);
  415. assert(argc == 1);
  416. uint32_t fd;
  417. err = js_get_value_uint32(env, argv[0], &fd);
  418. assert(err == 0);
  419. uv_loop_t *loop;
  420. js_get_env_loop(env, &loop);
  421. uv_fs_t req;
  422. uv_fs_close(loop, &req, fd, NULL);
  423. if (req.result < 0) {
  424. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  425. }
  426. uv_fs_req_cleanup(&req);
  427. return NULL;
  428. }
  429. static js_value_t *
  430. bare_fs_access (js_env_t *env, js_callback_info_t *info) {
  431. int err;
  432. size_t argc = 3;
  433. js_value_t *argv[3];
  434. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  435. assert(err == 0);
  436. assert(argc == 3);
  437. bare_fs_req_t *req;
  438. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  439. assert(err == 0);
  440. bare_fs_path_t path;
  441. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  442. assert(err == 0);
  443. int32_t mode;
  444. err = js_get_value_int32(env, argv[2], &mode);
  445. assert(err == 0);
  446. uv_loop_t *loop;
  447. js_get_env_loop(env, &loop);
  448. uv_fs_access(loop, (uv_fs_t *) req, (char *) path, mode, on_fs_response);
  449. return NULL;
  450. }
  451. static js_value_t *
  452. bare_fs_access_sync (js_env_t *env, js_callback_info_t *info) {
  453. int err;
  454. size_t argc = 2;
  455. js_value_t *argv[2];
  456. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  457. assert(err == 0);
  458. assert(argc == 2);
  459. bare_fs_path_t path;
  460. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  461. assert(err == 0);
  462. int32_t mode;
  463. err = js_get_value_int32(env, argv[1], &mode);
  464. assert(err == 0);
  465. uv_loop_t *loop;
  466. js_get_env_loop(env, &loop);
  467. uv_fs_t req;
  468. uv_fs_access(loop, &req, (char *) path, mode, NULL);
  469. if (req.result < 0) {
  470. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  471. }
  472. uv_fs_req_cleanup(&req);
  473. return NULL;
  474. }
  475. static js_value_t *
  476. bare_fs_read (js_env_t *env, js_callback_info_t *info) {
  477. int err;
  478. size_t argc = 6;
  479. js_value_t *argv[6];
  480. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  481. assert(err == 0);
  482. assert(argc == 6);
  483. bare_fs_req_t *req;
  484. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  485. assert(err == 0);
  486. uint32_t fd;
  487. err = js_get_value_uint32(env, argv[1], &fd);
  488. assert(err == 0);
  489. uint8_t *data;
  490. size_t data_len;
  491. err = js_get_typedarray_info(env, argv[2], NULL, (void **) &data, &data_len, NULL, NULL);
  492. assert(err == 0);
  493. uint32_t offset;
  494. err = js_get_value_uint32(env, argv[3], &offset);
  495. assert(err == 0);
  496. uint32_t len;
  497. err = js_get_value_uint32(env, argv[4], &len);
  498. assert(err == 0);
  499. if (offset >= data_len) len = 0;
  500. else if (offset + len >= data_len) len = data_len - offset;
  501. int64_t pos;
  502. err = js_get_value_int64(env, argv[5], &pos);
  503. assert(err == 0);
  504. uv_loop_t *loop;
  505. js_get_env_loop(env, &loop);
  506. uv_buf_t buf = uv_buf_init((void *) (data + offset), len);
  507. uv_fs_read(loop, (uv_fs_t *) req, fd, &buf, 1, pos, on_fs_response);
  508. return NULL;
  509. }
  510. static js_value_t *
  511. bare_fs_read_sync (js_env_t *env, js_callback_info_t *info) {
  512. int err;
  513. size_t argc = 5;
  514. js_value_t *argv[5];
  515. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  516. assert(err == 0);
  517. assert(argc == 5);
  518. uint32_t fd;
  519. err = js_get_value_uint32(env, argv[0], &fd);
  520. assert(err == 0);
  521. uint8_t *data;
  522. size_t data_len;
  523. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &data, &data_len, NULL, NULL);
  524. assert(err == 0);
  525. uint32_t offset;
  526. err = js_get_value_uint32(env, argv[2], &offset);
  527. assert(err == 0);
  528. uint32_t len;
  529. err = js_get_value_uint32(env, argv[3], &len);
  530. assert(err == 0);
  531. if (offset >= data_len) len = 0;
  532. else if (offset + len >= data_len) len = data_len - offset;
  533. int64_t pos;
  534. err = js_get_value_int64(env, argv[4], &pos);
  535. assert(err == 0);
  536. uv_loop_t *loop;
  537. js_get_env_loop(env, &loop);
  538. uv_buf_t buf = uv_buf_init((void *) (data + offset), len);
  539. uv_fs_t req;
  540. uv_fs_read(loop, (uv_fs_t *) &req, fd, &buf, 1, pos, NULL);
  541. js_value_t *res;
  542. err = js_create_int32(env, req.result, &res);
  543. assert(err == 0);
  544. uv_fs_req_cleanup(&req);
  545. return res;
  546. }
  547. static js_value_t *
  548. bare_fs_readv (js_env_t *env, js_callback_info_t *info) {
  549. int err;
  550. size_t argc = 4;
  551. js_value_t *argv[4];
  552. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  553. assert(err == 0);
  554. assert(argc == 4);
  555. bare_fs_req_t *req;
  556. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  557. assert(err == 0);
  558. uint32_t fd;
  559. err = js_get_value_uint32(env, argv[1], &fd);
  560. assert(err == 0);
  561. js_value_t *arr = argv[2];
  562. js_value_t *item;
  563. int64_t pos;
  564. err = js_get_value_int64(env, argv[3], &pos);
  565. assert(err == 0);
  566. err = js_create_reference(env, arr, 1, &req->data);
  567. assert(err == 0);
  568. uv_loop_t *loop;
  569. js_get_env_loop(env, &loop);
  570. uint32_t bufs_len;
  571. err = js_get_array_length(env, arr, &bufs_len);
  572. assert(err == 0);
  573. uv_buf_t *bufs = malloc(sizeof(uv_buf_t) * bufs_len);
  574. for (uint32_t i = 0; i < bufs_len; i++) {
  575. err = js_get_element(env, arr, i, &item);
  576. assert(err == 0);
  577. uv_buf_t *buf = &bufs[i];
  578. err = js_get_typedarray_info(env, item, NULL, (void **) &buf->base, (size_t *) &buf->len, NULL, NULL);
  579. assert(err == 0);
  580. }
  581. uv_fs_read(loop, (uv_fs_t *) req, fd, bufs, bufs_len, pos, on_fs_response);
  582. free(bufs);
  583. return NULL;
  584. }
  585. static js_value_t *
  586. bare_fs_write (js_env_t *env, js_callback_info_t *info) {
  587. int err;
  588. size_t argc = 6;
  589. js_value_t *argv[6];
  590. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  591. assert(err == 0);
  592. assert(argc == 6);
  593. bare_fs_req_t *req;
  594. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  595. assert(err == 0);
  596. uint32_t fd;
  597. err = js_get_value_uint32(env, argv[1], &fd);
  598. assert(err == 0);
  599. uint8_t *data;
  600. size_t data_len;
  601. err = js_get_typedarray_info(env, argv[2], NULL, (void **) &data, &data_len, NULL, NULL);
  602. assert(err == 0);
  603. uint32_t offset;
  604. err = js_get_value_uint32(env, argv[3], &offset);
  605. assert(err == 0);
  606. uint32_t len;
  607. err = js_get_value_uint32(env, argv[4], &len);
  608. assert(err == 0);
  609. if (offset >= data_len) len = 0;
  610. else if (offset + len >= data_len) len = data_len - offset;
  611. int64_t pos;
  612. err = js_get_value_int64(env, argv[5], &pos);
  613. assert(err == 0);
  614. uv_loop_t *loop;
  615. js_get_env_loop(env, &loop);
  616. uv_buf_t buf = uv_buf_init((void *) (data + offset), len);
  617. uv_fs_write(loop, (uv_fs_t *) req, fd, &buf, 1, pos, on_fs_response);
  618. return NULL;
  619. }
  620. static js_value_t *
  621. bare_fs_write_sync (js_env_t *env, js_callback_info_t *info) {
  622. int err;
  623. size_t argc = 5;
  624. js_value_t *argv[5];
  625. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  626. assert(err == 0);
  627. assert(argc == 5);
  628. uint32_t fd;
  629. err = js_get_value_uint32(env, argv[0], &fd);
  630. assert(err == 0);
  631. uint8_t *data;
  632. size_t data_len;
  633. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &data, &data_len, NULL, NULL);
  634. assert(err == 0);
  635. uint32_t offset;
  636. err = js_get_value_uint32(env, argv[2], &offset);
  637. assert(err == 0);
  638. uint32_t len;
  639. err = js_get_value_uint32(env, argv[3], &len);
  640. assert(err == 0);
  641. if (offset >= data_len) len = 0;
  642. else if (offset + len >= data_len) len = data_len - offset;
  643. int64_t pos;
  644. err = js_get_value_int64(env, argv[4], &pos);
  645. assert(err == 0);
  646. uv_loop_t *loop;
  647. js_get_env_loop(env, &loop);
  648. uv_buf_t buf = uv_buf_init((void *) (data + offset), len);
  649. uv_fs_t req;
  650. uv_fs_write(loop, &req, fd, &buf, 1, pos, NULL);
  651. js_value_t *res;
  652. err = js_create_int32(env, req.result, &res);
  653. assert(err == 0);
  654. uv_fs_req_cleanup(&req);
  655. return res;
  656. }
  657. static js_value_t *
  658. bare_fs_writev (js_env_t *env, js_callback_info_t *info) {
  659. int err;
  660. size_t argc = 4;
  661. js_value_t *argv[4];
  662. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  663. assert(err == 0);
  664. assert(argc == 4);
  665. bare_fs_req_t *req;
  666. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  667. assert(err == 0);
  668. uint32_t fd;
  669. err = js_get_value_uint32(env, argv[1], &fd);
  670. assert(err == 0);
  671. js_value_t *arr = argv[2];
  672. js_value_t *item;
  673. int64_t pos;
  674. err = js_get_value_int64(env, argv[3], &pos);
  675. assert(err == 0);
  676. err = js_create_reference(env, arr, 1, &req->data);
  677. assert(err == 0);
  678. uv_loop_t *loop;
  679. js_get_env_loop(env, &loop);
  680. uint32_t bufs_len;
  681. err = js_get_array_length(env, arr, &bufs_len);
  682. assert(err == 0);
  683. uv_buf_t *bufs = malloc(sizeof(uv_buf_t) * bufs_len);
  684. for (uint32_t i = 0; i < bufs_len; i++) {
  685. err = js_get_element(env, arr, i, &item);
  686. assert(err == 0);
  687. uv_buf_t *buf = &bufs[i];
  688. err = js_get_typedarray_info(env, item, NULL, (void **) &buf->base, (size_t *) &buf->len, NULL, NULL);
  689. assert(err == 0);
  690. }
  691. uv_fs_write(loop, (uv_fs_t *) req, fd, bufs, bufs_len, pos, on_fs_response);
  692. free(bufs);
  693. return NULL;
  694. }
  695. static js_value_t *
  696. bare_fs_ftruncate (js_env_t *env, js_callback_info_t *info) {
  697. int err;
  698. size_t argc = 3;
  699. js_value_t *argv[3];
  700. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  701. assert(err == 0);
  702. assert(argc == 3);
  703. bare_fs_req_t *req;
  704. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  705. assert(err == 0);
  706. uint32_t fd;
  707. err = js_get_value_uint32(env, argv[1], &fd);
  708. assert(err == 0);
  709. int64_t len;
  710. err = js_get_value_int64(env, argv[2], &len);
  711. assert(err == 0);
  712. uv_loop_t *loop;
  713. js_get_env_loop(env, &loop);
  714. uv_fs_ftruncate(loop, (uv_fs_t *) req, fd, len, on_fs_response);
  715. return NULL;
  716. }
  717. static js_value_t *
  718. bare_fs_chmod (js_env_t *env, js_callback_info_t *info) {
  719. int err;
  720. size_t argc = 3;
  721. js_value_t *argv[3];
  722. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  723. assert(err == 0);
  724. assert(argc == 3);
  725. bare_fs_req_t *req;
  726. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  727. assert(err == 0);
  728. bare_fs_path_t path;
  729. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  730. assert(err == 0);
  731. int32_t mode;
  732. err = js_get_value_int32(env, argv[2], &mode);
  733. assert(err == 0);
  734. uv_loop_t *loop;
  735. js_get_env_loop(env, &loop);
  736. uv_fs_chmod(loop, (uv_fs_t *) req, (char *) path, mode, on_fs_response);
  737. return NULL;
  738. }
  739. static js_value_t *
  740. bare_fs_chmod_sync (js_env_t *env, js_callback_info_t *info) {
  741. int err;
  742. size_t argc = 2;
  743. js_value_t *argv[2];
  744. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  745. assert(err == 0);
  746. assert(argc == 2);
  747. bare_fs_path_t path;
  748. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  749. assert(err == 0);
  750. int32_t mode;
  751. err = js_get_value_int32(env, argv[1], &mode);
  752. assert(err == 0);
  753. uv_loop_t *loop;
  754. js_get_env_loop(env, &loop);
  755. uv_fs_t req;
  756. uv_fs_chmod(loop, &req, (char *) path, mode, NULL);
  757. if (req.result < 0) {
  758. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  759. }
  760. uv_fs_req_cleanup(&req);
  761. return NULL;
  762. }
  763. static js_value_t *
  764. bare_fs_fchmod (js_env_t *env, js_callback_info_t *info) {
  765. int err;
  766. size_t argc = 3;
  767. js_value_t *argv[3];
  768. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  769. assert(err == 0);
  770. assert(argc == 3);
  771. bare_fs_req_t *req;
  772. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  773. assert(err == 0);
  774. uint32_t fd;
  775. err = js_get_value_uint32(env, argv[1], &fd);
  776. assert(err == 0);
  777. int32_t mode;
  778. err = js_get_value_int32(env, argv[2], &mode);
  779. assert(err == 0);
  780. uv_loop_t *loop;
  781. js_get_env_loop(env, &loop);
  782. uv_fs_fchmod(loop, (uv_fs_t *) req, fd, mode, on_fs_response);
  783. return NULL;
  784. }
  785. static js_value_t *
  786. bare_fs_fchmod_sync (js_env_t *env, js_callback_info_t *info) {
  787. int err;
  788. size_t argc = 2;
  789. js_value_t *argv[2];
  790. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  791. assert(err == 0);
  792. assert(argc == 2);
  793. uint32_t fd;
  794. err = js_get_value_uint32(env, argv[0], &fd);
  795. assert(err == 0);
  796. int32_t mode;
  797. err = js_get_value_int32(env, argv[1], &mode);
  798. assert(err == 0);
  799. uv_loop_t *loop;
  800. js_get_env_loop(env, &loop);
  801. uv_fs_t req;
  802. uv_fs_fchmod(loop, &req, fd, mode, NULL);
  803. if (req.result < 0) {
  804. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  805. }
  806. uv_fs_req_cleanup(&req);
  807. return NULL;
  808. }
  809. static js_value_t *
  810. bare_fs_rename (js_env_t *env, js_callback_info_t *info) {
  811. int err;
  812. size_t argc = 3;
  813. js_value_t *argv[3];
  814. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  815. assert(err == 0);
  816. assert(argc == 3);
  817. bare_fs_req_t *req;
  818. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  819. assert(err == 0);
  820. bare_fs_path_t src;
  821. err = js_get_value_string_utf8(env, argv[1], src, sizeof(bare_fs_path_t), NULL);
  822. assert(err == 0);
  823. bare_fs_path_t dest;
  824. err = js_get_value_string_utf8(env, argv[2], dest, sizeof(bare_fs_path_t), NULL);
  825. assert(err == 0);
  826. uv_loop_t *loop;
  827. js_get_env_loop(env, &loop);
  828. uv_fs_rename(loop, (uv_fs_t *) req, (char *) src, (char *) dest, on_fs_response);
  829. return NULL;
  830. }
  831. static js_value_t *
  832. bare_fs_rename_sync (js_env_t *env, js_callback_info_t *info) {
  833. int err;
  834. size_t argc = 2;
  835. js_value_t *argv[2];
  836. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  837. assert(err == 0);
  838. assert(argc == 2);
  839. bare_fs_path_t src;
  840. err = js_get_value_string_utf8(env, argv[0], src, sizeof(bare_fs_path_t), NULL);
  841. assert(err == 0);
  842. bare_fs_path_t dest;
  843. err = js_get_value_string_utf8(env, argv[1], dest, sizeof(bare_fs_path_t), NULL);
  844. assert(err == 0);
  845. uv_loop_t *loop;
  846. js_get_env_loop(env, &loop);
  847. uv_fs_t req;
  848. uv_fs_rename(loop, &req, (char *) src, (char *) dest, NULL);
  849. if (req.result < 0) {
  850. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  851. }
  852. uv_fs_req_cleanup(&req);
  853. return NULL;
  854. }
  855. static js_value_t *
  856. bare_fs_mkdir (js_env_t *env, js_callback_info_t *info) {
  857. int err;
  858. size_t argc = 3;
  859. js_value_t *argv[3];
  860. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  861. assert(err == 0);
  862. assert(argc == 3);
  863. bare_fs_req_t *req;
  864. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  865. assert(err == 0);
  866. bare_fs_path_t path;
  867. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  868. assert(err == 0);
  869. int32_t mode;
  870. err = js_get_value_int32(env, argv[2], &mode);
  871. assert(err == 0);
  872. uv_loop_t *loop;
  873. js_get_env_loop(env, &loop);
  874. uv_fs_mkdir(loop, (uv_fs_t *) req, (char *) path, mode, on_fs_response);
  875. return NULL;
  876. }
  877. static js_value_t *
  878. bare_fs_mkdir_sync (js_env_t *env, js_callback_info_t *info) {
  879. int err;
  880. size_t argc = 2;
  881. js_value_t *argv[2];
  882. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  883. assert(err == 0);
  884. assert(argc == 2);
  885. bare_fs_path_t path;
  886. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  887. assert(err == 0);
  888. int32_t mode;
  889. err = js_get_value_int32(env, argv[1], &mode);
  890. assert(err == 0);
  891. uv_loop_t *loop;
  892. js_get_env_loop(env, &loop);
  893. uv_fs_t req;
  894. uv_fs_mkdir(loop, &req, (char *) path, mode, NULL);
  895. if (req.result < 0) {
  896. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  897. }
  898. uv_fs_req_cleanup(&req);
  899. return NULL;
  900. }
  901. static js_value_t *
  902. bare_fs_rmdir (js_env_t *env, js_callback_info_t *info) {
  903. int err;
  904. size_t argc = 2;
  905. js_value_t *argv[2];
  906. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  907. assert(err == 0);
  908. assert(argc == 2);
  909. bare_fs_req_t *req;
  910. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  911. assert(err == 0);
  912. bare_fs_path_t path;
  913. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  914. assert(err == 0);
  915. uv_loop_t *loop;
  916. js_get_env_loop(env, &loop);
  917. uv_fs_rmdir(loop, (uv_fs_t *) req, (char *) path, on_fs_response);
  918. return NULL;
  919. }
  920. static js_value_t *
  921. bare_fs_rmdir_sync (js_env_t *env, js_callback_info_t *info) {
  922. int err;
  923. size_t argc = 1;
  924. js_value_t *argv[1];
  925. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  926. assert(err == 0);
  927. assert(argc == 1);
  928. bare_fs_path_t path;
  929. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  930. assert(err == 0);
  931. uv_loop_t *loop;
  932. js_get_env_loop(env, &loop);
  933. uv_fs_t req;
  934. uv_fs_rmdir(loop, &req, (char *) path, NULL);
  935. if (req.result < 0) {
  936. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  937. }
  938. uv_fs_req_cleanup(&req);
  939. return NULL;
  940. }
  941. static js_value_t *
  942. bare_fs_stat (js_env_t *env, js_callback_info_t *info) {
  943. int err;
  944. size_t argc = 3;
  945. js_value_t *argv[3];
  946. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  947. assert(err == 0);
  948. assert(argc == 3);
  949. bare_fs_req_t *req;
  950. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  951. assert(err == 0);
  952. bare_fs_path_t path;
  953. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  954. assert(err == 0);
  955. err = js_create_reference(env, argv[2], 1, &req->data);
  956. assert(err == 0);
  957. uv_loop_t *loop;
  958. js_get_env_loop(env, &loop);
  959. uv_fs_stat(loop, (uv_fs_t *) req, (char *) path, on_fs_stat_response);
  960. return NULL;
  961. }
  962. static js_value_t *
  963. bare_fs_stat_sync (js_env_t *env, js_callback_info_t *info) {
  964. int err;
  965. size_t argc = 1;
  966. js_value_t *argv[1];
  967. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  968. assert(err == 0);
  969. assert(argc == 1);
  970. bare_fs_path_t path;
  971. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  972. assert(err == 0);
  973. uv_loop_t *loop;
  974. js_get_env_loop(env, &loop);
  975. uv_fs_t req;
  976. uv_fs_stat(loop, &req, (char *) path, NULL);
  977. js_value_t *res = NULL;
  978. if (req.result < 0) {
  979. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  980. } else {
  981. err = js_create_array_with_length(env, 14, &res);
  982. assert(err == 0);
  983. uint32_t i = 0;
  984. #define V(property) \
  985. { \
  986. js_value_t *value; \
  987. err = js_create_int64(env, req.statbuf.st_##property, &value); \
  988. assert(err == 0); \
  989. \
  990. err = js_set_element(env, res, i++, value); \
  991. assert(err == 0); \
  992. }
  993. V(dev)
  994. V(mode)
  995. V(nlink)
  996. V(uid)
  997. V(gid)
  998. V(rdev)
  999. V(blksize)
  1000. V(ino)
  1001. V(size)
  1002. V(blocks)
  1003. #undef V
  1004. #define V(property) \
  1005. { \
  1006. uv_timespec_t time = req.statbuf.st_##property; \
  1007. \
  1008. js_value_t *value; \
  1009. err = js_create_int64(env, time.tv_sec * 1e3 + time.tv_nsec / 1e6, &value); \
  1010. assert(err == 0); \
  1011. \
  1012. err = js_set_element(env, res, i++, value); \
  1013. assert(err == 0); \
  1014. }
  1015. V(atim)
  1016. V(mtim)
  1017. V(ctim)
  1018. V(birthtim)
  1019. #undef V
  1020. }
  1021. uv_fs_req_cleanup(&req);
  1022. return res;
  1023. }
  1024. static js_value_t *
  1025. bare_fs_lstat (js_env_t *env, js_callback_info_t *info) {
  1026. int err;
  1027. size_t argc = 3;
  1028. js_value_t *argv[3];
  1029. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1030. assert(err == 0);
  1031. assert(argc == 3);
  1032. bare_fs_req_t *req;
  1033. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1034. assert(err == 0);
  1035. bare_fs_path_t path;
  1036. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  1037. assert(err == 0);
  1038. err = js_create_reference(env, argv[2], 1, &req->data);
  1039. assert(err == 0);
  1040. uv_loop_t *loop;
  1041. js_get_env_loop(env, &loop);
  1042. uv_fs_lstat(loop, (uv_fs_t *) req, (char *) path, on_fs_stat_response);
  1043. return NULL;
  1044. }
  1045. static js_value_t *
  1046. bare_fs_lstat_sync (js_env_t *env, js_callback_info_t *info) {
  1047. int err;
  1048. size_t argc = 1;
  1049. js_value_t *argv[1];
  1050. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1051. assert(err == 0);
  1052. assert(argc == 1);
  1053. bare_fs_path_t path;
  1054. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  1055. assert(err == 0);
  1056. uv_loop_t *loop;
  1057. js_get_env_loop(env, &loop);
  1058. uv_fs_t req;
  1059. uv_fs_lstat(loop, &req, (char *) path, NULL);
  1060. js_value_t *res = NULL;
  1061. if (req.result < 0) {
  1062. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1063. } else {
  1064. err = js_create_array_with_length(env, 14, &res);
  1065. assert(err == 0);
  1066. uint32_t i = 0;
  1067. #define V(property) \
  1068. { \
  1069. js_value_t *value; \
  1070. err = js_create_int64(env, req.statbuf.st_##property, &value); \
  1071. assert(err == 0); \
  1072. \
  1073. err = js_set_element(env, res, i++, value); \
  1074. assert(err == 0); \
  1075. }
  1076. V(dev)
  1077. V(mode)
  1078. V(nlink)
  1079. V(uid)
  1080. V(gid)
  1081. V(rdev)
  1082. V(blksize)
  1083. V(ino)
  1084. V(size)
  1085. V(blocks)
  1086. #undef V
  1087. #define V(property) \
  1088. { \
  1089. uv_timespec_t time = req.statbuf.st_##property; \
  1090. \
  1091. js_value_t *value; \
  1092. err = js_create_int64(env, time.tv_sec * 1e3 + time.tv_nsec / 1e6, &value); \
  1093. assert(err == 0); \
  1094. \
  1095. err = js_set_element(env, res, i++, value); \
  1096. assert(err == 0); \
  1097. }
  1098. V(atim)
  1099. V(mtim)
  1100. V(ctim)
  1101. V(birthtim)
  1102. #undef V
  1103. }
  1104. uv_fs_req_cleanup(&req);
  1105. return res;
  1106. }
  1107. static js_value_t *
  1108. bare_fs_fstat (js_env_t *env, js_callback_info_t *info) {
  1109. int err;
  1110. size_t argc = 3;
  1111. js_value_t *argv[3];
  1112. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1113. assert(err == 0);
  1114. assert(argc == 3);
  1115. bare_fs_req_t *req;
  1116. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1117. assert(err == 0);
  1118. uint32_t fd;
  1119. err = js_get_value_uint32(env, argv[1], &fd);
  1120. assert(err == 0);
  1121. err = js_create_reference(env, argv[2], 1, &req->data);
  1122. assert(err == 0);
  1123. uv_loop_t *loop;
  1124. js_get_env_loop(env, &loop);
  1125. uv_fs_fstat(loop, (uv_fs_t *) req, fd, on_fs_stat_response);
  1126. return NULL;
  1127. }
  1128. static js_value_t *
  1129. bare_fs_fstat_sync (js_env_t *env, js_callback_info_t *info) {
  1130. int err;
  1131. size_t argc = 1;
  1132. js_value_t *argv[1];
  1133. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1134. assert(err == 0);
  1135. assert(argc == 1);
  1136. uint32_t fd;
  1137. err = js_get_value_uint32(env, argv[0], &fd);
  1138. assert(err == 0);
  1139. uv_loop_t *loop;
  1140. js_get_env_loop(env, &loop);
  1141. uv_fs_t req;
  1142. uv_fs_fstat(loop, &req, fd, NULL);
  1143. js_value_t *res = NULL;
  1144. if (req.result < 0) {
  1145. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1146. } else {
  1147. err = js_create_array_with_length(env, 14, &res);
  1148. assert(err == 0);
  1149. uint32_t i = 0;
  1150. #define V(property) \
  1151. { \
  1152. js_value_t *value; \
  1153. err = js_create_int64(env, req.statbuf.st_##property, &value); \
  1154. assert(err == 0); \
  1155. \
  1156. err = js_set_element(env, res, i++, value); \
  1157. assert(err == 0); \
  1158. }
  1159. V(dev)
  1160. V(mode)
  1161. V(nlink)
  1162. V(uid)
  1163. V(gid)
  1164. V(rdev)
  1165. V(blksize)
  1166. V(ino)
  1167. V(size)
  1168. V(blocks)
  1169. #undef V
  1170. #define V(property) \
  1171. { \
  1172. uv_timespec_t time = req.statbuf.st_##property; \
  1173. \
  1174. js_value_t *value; \
  1175. err = js_create_int64(env, time.tv_sec * 1e3 + time.tv_nsec / 1e6, &value); \
  1176. assert(err == 0); \
  1177. \
  1178. err = js_set_element(env, res, i++, value); \
  1179. assert(err == 0); \
  1180. }
  1181. V(atim)
  1182. V(mtim)
  1183. V(ctim)
  1184. V(birthtim)
  1185. #undef V
  1186. }
  1187. uv_fs_req_cleanup(&req);
  1188. return res;
  1189. }
  1190. static js_value_t *
  1191. bare_fs_unlink (js_env_t *env, js_callback_info_t *info) {
  1192. int err;
  1193. size_t argc = 2;
  1194. js_value_t *argv[2];
  1195. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1196. assert(err == 0);
  1197. assert(argc == 2);
  1198. bare_fs_req_t *req;
  1199. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1200. assert(err == 0);
  1201. bare_fs_path_t path;
  1202. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  1203. assert(err == 0);
  1204. uv_loop_t *loop;
  1205. js_get_env_loop(env, &loop);
  1206. uv_fs_unlink(loop, (uv_fs_t *) req, (char *) path, on_fs_response);
  1207. return NULL;
  1208. }
  1209. static js_value_t *
  1210. bare_fs_unlink_sync (js_env_t *env, js_callback_info_t *info) {
  1211. int err;
  1212. size_t argc = 1;
  1213. js_value_t *argv[1];
  1214. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1215. assert(err == 0);
  1216. assert(argc == 1);
  1217. bare_fs_path_t path;
  1218. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  1219. assert(err == 0);
  1220. uv_loop_t *loop;
  1221. js_get_env_loop(env, &loop);
  1222. uv_fs_t req;
  1223. uv_fs_unlink(loop, &req, (char *) path, NULL);
  1224. if (req.result < 0) {
  1225. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1226. }
  1227. uv_fs_req_cleanup(&req);
  1228. return NULL;
  1229. }
  1230. static js_value_t *
  1231. bare_fs_realpath (js_env_t *env, js_callback_info_t *info) {
  1232. int err;
  1233. size_t argc = 3;
  1234. js_value_t *argv[3];
  1235. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1236. assert(err == 0);
  1237. assert(argc == 3);
  1238. bare_fs_req_t *req;
  1239. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1240. assert(err == 0);
  1241. bare_fs_path_t path;
  1242. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  1243. assert(err == 0);
  1244. err = js_create_reference(env, argv[2], 1, &req->data);
  1245. assert(err == 0);
  1246. uv_loop_t *loop;
  1247. js_get_env_loop(env, &loop);
  1248. uv_fs_realpath(loop, (uv_fs_t *) req, (char *) path, on_fs_realpath_response);
  1249. return NULL;
  1250. }
  1251. static js_value_t *
  1252. bare_fs_realpath_sync (js_env_t *env, js_callback_info_t *info) {
  1253. int err;
  1254. size_t argc = 2;
  1255. js_value_t *argv[2];
  1256. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1257. assert(err == 0);
  1258. assert(argc == 2);
  1259. bare_fs_path_t path;
  1260. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  1261. assert(err == 0);
  1262. uv_loop_t *loop;
  1263. js_get_env_loop(env, &loop);
  1264. uv_fs_t req;
  1265. uv_fs_realpath(loop, &req, (char *) path, NULL);
  1266. js_value_t *res = NULL;
  1267. if (req.result < 0) {
  1268. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1269. } else {
  1270. char *path;
  1271. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &path, NULL, NULL, NULL);
  1272. assert(err == 0);
  1273. strncpy(path, req.ptr, sizeof(bare_fs_path_t));
  1274. }
  1275. uv_fs_req_cleanup(&req);
  1276. return res;
  1277. }
  1278. static js_value_t *
  1279. bare_fs_readlink (js_env_t *env, js_callback_info_t *info) {
  1280. int err;
  1281. size_t argc = 3;
  1282. js_value_t *argv[3];
  1283. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1284. assert(err == 0);
  1285. assert(argc == 3);
  1286. bare_fs_req_t *req;
  1287. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1288. assert(err == 0);
  1289. bare_fs_path_t path;
  1290. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  1291. assert(err == 0);
  1292. err = js_create_reference(env, argv[2], 1, &req->data);
  1293. assert(err == 0);
  1294. uv_loop_t *loop;
  1295. js_get_env_loop(env, &loop);
  1296. uv_fs_readlink(loop, (uv_fs_t *) req, (char *) path, on_fs_readlink_response);
  1297. return NULL;
  1298. }
  1299. static js_value_t *
  1300. bare_fs_readlink_sync (js_env_t *env, js_callback_info_t *info) {
  1301. int err;
  1302. size_t argc = 2;
  1303. js_value_t *argv[2];
  1304. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1305. assert(err == 0);
  1306. assert(argc == 2);
  1307. bare_fs_path_t path;
  1308. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  1309. assert(err == 0);
  1310. uv_loop_t *loop;
  1311. js_get_env_loop(env, &loop);
  1312. uv_fs_t req;
  1313. uv_fs_readlink(loop, &req, (char *) path, NULL);
  1314. if (req.result < 0) {
  1315. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1316. } else {
  1317. char *path;
  1318. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &path, NULL, NULL, NULL);
  1319. assert(err == 0);
  1320. strncpy(path, req.ptr, sizeof(bare_fs_path_t));
  1321. }
  1322. uv_fs_req_cleanup(&req);
  1323. return NULL;
  1324. }
  1325. static js_value_t *
  1326. bare_fs_symlink (js_env_t *env, js_callback_info_t *info) {
  1327. int err;
  1328. size_t argc = 4;
  1329. js_value_t *argv[4];
  1330. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1331. assert(err == 0);
  1332. assert(argc == 4);
  1333. bare_fs_req_t *req;
  1334. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1335. assert(err == 0);
  1336. bare_fs_path_t target;
  1337. err = js_get_value_string_utf8(env, argv[1], target, sizeof(bare_fs_path_t), NULL);
  1338. assert(err == 0);
  1339. bare_fs_path_t path;
  1340. err = js_get_value_string_utf8(env, argv[2], path, sizeof(bare_fs_path_t), NULL);
  1341. assert(err == 0);
  1342. int32_t flags;
  1343. err = js_get_value_int32(env, argv[3], &flags);
  1344. assert(err == 0);
  1345. uv_loop_t *loop;
  1346. js_get_env_loop(env, &loop);
  1347. uv_fs_symlink(loop, (uv_fs_t *) req, (char *) target, (char *) path, flags, on_fs_response);
  1348. return NULL;
  1349. }
  1350. static js_value_t *
  1351. bare_fs_symlink_sync (js_env_t *env, js_callback_info_t *info) {
  1352. int err;
  1353. size_t argc = 3;
  1354. js_value_t *argv[3];
  1355. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1356. assert(err == 0);
  1357. assert(argc == 3);
  1358. bare_fs_path_t target;
  1359. err = js_get_value_string_utf8(env, argv[0], target, sizeof(bare_fs_path_t), NULL);
  1360. assert(err == 0);
  1361. bare_fs_path_t path;
  1362. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  1363. assert(err == 0);
  1364. int32_t flags;
  1365. err = js_get_value_int32(env, argv[2], &flags);
  1366. assert(err == 0);
  1367. uv_loop_t *loop;
  1368. js_get_env_loop(env, &loop);
  1369. uv_fs_t req;
  1370. uv_fs_symlink(loop, &req, (char *) target, (char *) path, flags, NULL);
  1371. if (req.result < 0) {
  1372. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1373. }
  1374. uv_fs_req_cleanup(&req);
  1375. return NULL;
  1376. }
  1377. static js_value_t *
  1378. bare_fs_opendir (js_env_t *env, js_callback_info_t *info) {
  1379. int err;
  1380. size_t argc = 3;
  1381. js_value_t *argv[3];
  1382. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1383. assert(err == 0);
  1384. assert(argc == 3);
  1385. bare_fs_req_t *req;
  1386. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1387. assert(err == 0);
  1388. bare_fs_path_t path;
  1389. err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
  1390. assert(err == 0);
  1391. err = js_create_reference(env, argv[2], 1, &req->data);
  1392. assert(err == 0);
  1393. uv_loop_t *loop;
  1394. js_get_env_loop(env, &loop);
  1395. uv_fs_opendir(loop, (uv_fs_t *) req, (char *) path, on_fs_opendir_response);
  1396. return NULL;
  1397. }
  1398. static js_value_t *
  1399. bare_fs_opendir_sync (js_env_t *env, js_callback_info_t *info) {
  1400. int err;
  1401. size_t argc = 2;
  1402. js_value_t *argv[2];
  1403. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1404. assert(err == 0);
  1405. assert(argc == 2);
  1406. bare_fs_path_t path;
  1407. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  1408. assert(err == 0);
  1409. js_value_t *data = argv[1];
  1410. uv_loop_t *loop;
  1411. js_get_env_loop(env, &loop);
  1412. uv_fs_t req;
  1413. uv_fs_opendir(loop, &req, (char *) path, NULL);
  1414. if (req.result < 0) {
  1415. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1416. } else {
  1417. bare_fs_dir_t *dir;
  1418. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &dir, NULL, NULL, NULL);
  1419. assert(err == 0);
  1420. dir->dir = req.ptr;
  1421. }
  1422. uv_fs_req_cleanup(&req);
  1423. return NULL;
  1424. }
  1425. static js_value_t *
  1426. bare_fs_readdir (js_env_t *env, js_callback_info_t *info) {
  1427. int err;
  1428. size_t argc = 4;
  1429. js_value_t *argv[4];
  1430. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1431. assert(err == 0);
  1432. assert(argc == 4);
  1433. bare_fs_req_t *req;
  1434. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1435. assert(err == 0);
  1436. bare_fs_dir_t *dir;
  1437. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &dir, NULL, NULL, NULL);
  1438. assert(err == 0);
  1439. bare_fs_dirent_t *dirents;
  1440. size_t dirents_len;
  1441. err = js_get_typedarray_info(env, argv[2], NULL, (void **) &dirents, &dirents_len, NULL, NULL);
  1442. assert(err == 0);
  1443. err = js_create_reference(env, argv[3], 1, &req->data);
  1444. assert(err == 0);
  1445. uv_loop_t *loop;
  1446. js_get_env_loop(env, &loop);
  1447. dir->dir->dirents = dirents;
  1448. dir->dir->nentries = dirents_len / sizeof(bare_fs_dirent_t);
  1449. uv_fs_readdir(loop, (uv_fs_t *) req, dir->dir, on_fs_readdir_response);
  1450. return NULL;
  1451. }
  1452. static js_value_t *
  1453. bare_fs_readdir_sync (js_env_t *env, js_callback_info_t *info) {
  1454. int err;
  1455. size_t argc = 3;
  1456. js_value_t *argv[3];
  1457. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1458. assert(err == 0);
  1459. assert(argc == 3);
  1460. bare_fs_dir_t *dir;
  1461. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &dir, NULL, NULL, NULL);
  1462. assert(err == 0);
  1463. bare_fs_dirent_t *dirents;
  1464. size_t dirents_len;
  1465. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &dirents, &dirents_len, NULL, NULL);
  1466. assert(err == 0);
  1467. uv_loop_t *loop;
  1468. js_get_env_loop(env, &loop);
  1469. dir->dir->dirents = dirents;
  1470. dir->dir->nentries = dirents_len / sizeof(bare_fs_dirent_t);
  1471. uv_fs_t req;
  1472. uv_fs_readdir(loop, &req, dir->dir, NULL);
  1473. if (req.result < 0) {
  1474. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1475. } else {
  1476. uv_dir_t *dir = req.ptr;
  1477. for (size_t i = 0, n = req.result; i < n; i++) {
  1478. uv_dirent_t *dirent = &dir->dirents[i];
  1479. js_value_t *entry;
  1480. err = js_create_object(env, &entry);
  1481. assert(err == 0);
  1482. err = js_set_element(env, argv[2], i, entry);
  1483. assert(err == 0);
  1484. size_t name_len = strlen(dirent->name);
  1485. js_value_t *name;
  1486. void *data;
  1487. err = js_create_arraybuffer(env, name_len, &data, &name);
  1488. assert(err == 0);
  1489. memcpy(data, dirent->name, name_len);
  1490. err = js_set_named_property(env, entry, "name", name);
  1491. assert(err == 0);
  1492. js_value_t *type;
  1493. err = js_create_uint32(env, dirent->type, &type);
  1494. assert(err == 0);
  1495. err = js_set_named_property(env, entry, "type", type);
  1496. assert(err == 0);
  1497. }
  1498. }
  1499. uv_fs_req_cleanup(&req);
  1500. return NULL;
  1501. }
  1502. static js_value_t *
  1503. bare_fs_closedir (js_env_t *env, js_callback_info_t *info) {
  1504. int err;
  1505. size_t argc = 2;
  1506. js_value_t *argv[2];
  1507. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1508. assert(err == 0);
  1509. assert(argc == 2);
  1510. bare_fs_req_t *req;
  1511. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
  1512. assert(err == 0);
  1513. bare_fs_dir_t *dir;
  1514. err = js_get_typedarray_info(env, argv[1], NULL, (void **) &dir, NULL, NULL, NULL);
  1515. assert(err == 0);
  1516. uv_loop_t *loop;
  1517. js_get_env_loop(env, &loop);
  1518. uv_fs_closedir(loop, (uv_fs_t *) req, dir->dir, on_fs_response);
  1519. return NULL;
  1520. }
  1521. static js_value_t *
  1522. bare_fs_closedir_sync (js_env_t *env, js_callback_info_t *info) {
  1523. int err;
  1524. size_t argc = 1;
  1525. js_value_t *argv[1];
  1526. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1527. assert(err == 0);
  1528. assert(argc == 1);
  1529. bare_fs_dir_t *dir;
  1530. err = js_get_typedarray_info(env, argv[0], NULL, (void **) &dir, NULL, NULL, NULL);
  1531. assert(err == 0);
  1532. uv_loop_t *loop;
  1533. js_get_env_loop(env, &loop);
  1534. uv_fs_t req;
  1535. uv_fs_closedir(loop, &req, dir->dir, NULL);
  1536. if (req.result < 0) {
  1537. js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
  1538. }
  1539. uv_fs_req_cleanup(&req);
  1540. return NULL;
  1541. }
  1542. static js_value_t *
  1543. bare_fs_watcher_init (js_env_t *env, js_callback_info_t *info) {
  1544. int err;
  1545. size_t argc = 5;
  1546. js_value_t *argv[5];
  1547. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1548. assert(err == 0);
  1549. assert(argc == 5);
  1550. bare_fs_path_t path;
  1551. err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
  1552. assert(err == 0);
  1553. bool recursive;
  1554. err = js_get_value_bool(env, argv[1], &recursive);
  1555. assert(err == 0);
  1556. js_value_t *result;
  1557. bare_fs_watcher_t *watcher;
  1558. err = js_create_arraybuffer(env, sizeof(bare_fs_watcher_t), (void **) &watcher, &result);
  1559. assert(err == 0);
  1560. uv_loop_t *loop;
  1561. js_get_env_loop(env, &loop);
  1562. err = uv_fs_event_init(loop, &watcher->handle);
  1563. if (err < 0) {
  1564. js_throw_error(env, uv_err_name(err), uv_strerror(err));
  1565. return NULL;
  1566. }
  1567. err = uv_fs_event_start(&watcher->handle, on_fs_watcher_event, (char *) path, recursive ? UV_FS_EVENT_RECURSIVE : 0);
  1568. if (err < 0) {
  1569. js_throw_error(env, uv_err_name(err), uv_strerror(err));
  1570. return NULL;
  1571. }
  1572. watcher->env = env;
  1573. err = js_create_reference(env, argv[2], 1, &watcher->ctx);
  1574. assert(err == 0);
  1575. err = js_create_reference(env, argv[3], 1, &watcher->on_event);
  1576. assert(err == 0);
  1577. err = js_create_reference(env, argv[4], 1, &watcher->on_close);
  1578. assert(err == 0);
  1579. return result;
  1580. }
  1581. static js_value_t *
  1582. bare_fs_watcher_close (js_env_t *env, js_callback_info_t *info) {
  1583. int err;
  1584. size_t argc = 1;
  1585. js_value_t *argv[1];
  1586. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1587. assert(err == 0);
  1588. assert(argc == 1);
  1589. bare_fs_watcher_t *watcher;
  1590. err = js_get_arraybuffer_info(env, argv[0], (void **) &watcher, NULL);
  1591. assert(err == 0);
  1592. err = uv_fs_event_stop(&watcher->handle);
  1593. assert(err == 0);
  1594. uv_close((uv_handle_t *) &watcher->handle, on_fs_watcher_close);
  1595. return NULL;
  1596. }
  1597. static js_value_t *
  1598. bare_fs_watcher_ref (js_env_t *env, js_callback_info_t *info) {
  1599. int err;
  1600. size_t argc = 1;
  1601. js_value_t *argv[1];
  1602. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1603. assert(err == 0);
  1604. assert(argc == 1);
  1605. bare_fs_watcher_t *watcher;
  1606. err = js_get_arraybuffer_info(env, argv[0], (void **) &watcher, NULL);
  1607. assert(err == 0);
  1608. uv_ref((uv_handle_t *) &watcher->handle);
  1609. return NULL;
  1610. }
  1611. static js_value_t *
  1612. bare_fs_watcher_unref (js_env_t *env, js_callback_info_t *info) {
  1613. int err;
  1614. size_t argc = 1;
  1615. js_value_t *argv[1];
  1616. err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
  1617. assert(err == 0);
  1618. assert(argc == 1);
  1619. bare_fs_watcher_t *watcher;
  1620. err = js_get_arraybuffer_info(env, argv[0], (void **) &watcher, NULL);
  1621. assert(err == 0);
  1622. uv_unref((uv_handle_t *) &watcher->handle);
  1623. return NULL;
  1624. }
  1625. static js_value_t *
  1626. init (js_env_t *env, js_value_t *exports) {
  1627. int err;
  1628. {
  1629. js_value_t *val;
  1630. js_create_uint32(env, sizeof(bare_fs_t), &val);
  1631. js_set_named_property(env, exports, "sizeofFS", val);
  1632. }
  1633. {
  1634. js_value_t *val;
  1635. js_create_uint32(env, sizeof(bare_fs_req_t), &val);
  1636. js_set_named_property(env, exports, "sizeofFSReq", val);
  1637. }
  1638. {
  1639. js_value_t *val;
  1640. js_create_uint32(env, offsetof(bare_fs_req_t, id), &val);
  1641. js_set_named_property(env, exports, "offsetofFSReqID", val);
  1642. }
  1643. {
  1644. js_value_t *val;
  1645. js_create_uint32(env, sizeof(bare_fs_dir_t), &val);
  1646. js_set_named_property(env, exports, "sizeofFSDir", val);
  1647. }
  1648. {
  1649. js_value_t *val;
  1650. js_create_uint32(env, sizeof(bare_fs_dirent_t), &val);
  1651. js_set_named_property(env, exports, "sizeofFSDirent", val);
  1652. }
  1653. {
  1654. js_value_t *val;
  1655. js_create_uint32(env, sizeof(bare_fs_path_t), &val);
  1656. js_set_named_property(env, exports, "sizeofFSPath", val);
  1657. }
  1658. #define V(name, fn) \
  1659. { \
  1660. js_value_t *val; \
  1661. err = js_create_function(env, name, -1, fn, NULL, &val); \
  1662. assert(err == 0); \
  1663. js_set_named_property(env, exports, name, val); \
  1664. assert(err == 0); \
  1665. }
  1666. V("init", bare_fs_init)
  1667. V("destroy", bare_fs_destroy)
  1668. V("initReq", bare_fs_req_init)
  1669. V("open", bare_fs_open)
  1670. V("openSync", bare_fs_open_sync)
  1671. V("close", bare_fs_close)
  1672. V("closeSync", bare_fs_close_sync)
  1673. V("access", bare_fs_access)
  1674. V("accessSync", bare_fs_access_sync)
  1675. V("read", bare_fs_read)
  1676. V("readSync", bare_fs_read_sync)
  1677. V("readv", bare_fs_readv)
  1678. V("write", bare_fs_write)
  1679. V("writeSync", bare_fs_write_sync)
  1680. V("writev", bare_fs_writev)
  1681. V("ftruncate", bare_fs_ftruncate)
  1682. V("chmod", bare_fs_chmod)
  1683. V("chmodSync", bare_fs_chmod_sync)
  1684. V("fchmod", bare_fs_fchmod)
  1685. V("fchmodSync", bare_fs_fchmod_sync)
  1686. V("rename", bare_fs_rename)
  1687. V("renameSync", bare_fs_rename_sync)
  1688. V("mkdir", bare_fs_mkdir)
  1689. V("mkdirSync", bare_fs_mkdir_sync)
  1690. V("rmdir", bare_fs_rmdir)
  1691. V("rmdirSync", bare_fs_rmdir_sync)
  1692. V("stat", bare_fs_stat)
  1693. V("statSync", bare_fs_stat_sync)
  1694. V("lstat", bare_fs_lstat)
  1695. V("lstatSync", bare_fs_lstat_sync)
  1696. V("fstat", bare_fs_fstat)
  1697. V("fstatSync", bare_fs_fstat_sync)
  1698. V("unlink", bare_fs_unlink)
  1699. V("unlinkSync", bare_fs_unlink_sync)
  1700. V("realpath", bare_fs_realpath)
  1701. V("realpathSync", bare_fs_realpath_sync)
  1702. V("readlink", bare_fs_readlink)
  1703. V("readlinkSync", bare_fs_readlink_sync)
  1704. V("symlink", bare_fs_symlink)
  1705. V("symlinkSync", bare_fs_symlink_sync)
  1706. V("opendir", bare_fs_opendir)
  1707. V("opendirSync", bare_fs_opendir_sync)
  1708. V("readdir", bare_fs_readdir)
  1709. V("readdirSync", bare_fs_readdir_sync)
  1710. V("closedir", bare_fs_closedir)
  1711. V("closedirSync", bare_fs_closedir_sync)
  1712. V("watcherInit", bare_fs_watcher_init)
  1713. V("watcherClose", bare_fs_watcher_close)
  1714. V("watcherRef", bare_fs_watcher_ref)
  1715. V("watcherUnref", bare_fs_watcher_unref)
  1716. #undef V
  1717. #define V(name) \
  1718. { \
  1719. js_value_t *val; \
  1720. err = js_create_uint32(env, name, &val); \
  1721. assert(err == 0); \
  1722. err = js_set_named_property(env, exports, #name, val); \
  1723. assert(err == 0); \
  1724. }
  1725. V(O_RDWR)
  1726. V(O_RDONLY)
  1727. V(O_WRONLY)
  1728. V(O_CREAT)
  1729. V(O_TRUNC)
  1730. V(O_APPEND)
  1731. #ifdef F_OK
  1732. V(F_OK)
  1733. #endif
  1734. #ifdef R_OK
  1735. V(R_OK)
  1736. #endif
  1737. #ifdef W_OK
  1738. V(W_OK)
  1739. #endif
  1740. #ifdef X_OK
  1741. V(X_OK)
  1742. #endif
  1743. V(S_IFMT)
  1744. V(S_IFREG)
  1745. V(S_IFDIR)
  1746. V(S_IFCHR)
  1747. V(S_IFLNK)
  1748. #ifdef S_IFBLK
  1749. V(S_IFBLK)
  1750. #endif
  1751. #ifdef S_IFIFO
  1752. V(S_IFIFO)
  1753. #endif
  1754. #ifdef S_IFSOCK
  1755. V(S_IFSOCK)
  1756. #endif
  1757. #ifdef S_IRUSR
  1758. V(S_IRUSR)
  1759. #endif
  1760. #ifdef S_IWUSR
  1761. V(S_IWUSR)
  1762. #endif
  1763. #ifdef S_IXUSR
  1764. V(S_IXUSR)
  1765. #endif
  1766. #ifdef S_IRGRP
  1767. V(S_IRGRP)
  1768. #endif
  1769. #ifdef S_IWGRP
  1770. V(S_IWGRP)
  1771. #endif
  1772. #ifdef S_IXGRP
  1773. V(S_IXGRP)
  1774. #endif
  1775. #ifdef S_IROTH
  1776. V(S_IROTH)
  1777. #endif
  1778. #ifdef S_IWOTH
  1779. V(S_IWOTH)
  1780. #endif
  1781. #ifdef S_IXOTH
  1782. V(S_IXOTH)
  1783. #endif
  1784. V(UV_DIRENT_UNKNOWN)
  1785. V(UV_DIRENT_FILE)
  1786. V(UV_DIRENT_DIR)
  1787. V(UV_DIRENT_LINK)
  1788. V(UV_DIRENT_FIFO)
  1789. V(UV_DIRENT_SOCKET)
  1790. V(UV_DIRENT_CHAR)
  1791. V(UV_DIRENT_BLOCK)
  1792. V(UV_FS_SYMLINK_DIR)
  1793. V(UV_FS_SYMLINK_JUNCTION)
  1794. V(UV_RENAME)
  1795. V(UV_CHANGE)
  1796. #undef V
  1797. return exports;
  1798. }
  1799. BARE_MODULE(bare_fs, init)