-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFS.cpp
More file actions
1104 lines (918 loc) · 29.8 KB
/
FS.cpp
File metadata and controls
1104 lines (918 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "FS.h"
#include <algorithm>
#include <array>
#include <cctype>
#include <cerrno>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <string>
#include <system_error>
#include <vector>
#include "js_native_api.h"
#include "native_api_util.h"
namespace nativescript {
namespace {
namespace fs = std::filesystem;
std::string ToLower(std::string value) {
std::transform(
value.begin(), value.end(), value.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return value;
}
bool CoerceToString(napi_env env, napi_value value, std::string& out) {
napi_value coerced;
if (napi_coerce_to_string(env, value, &coerced) != napi_ok) {
return false;
}
size_t length = 0;
if (napi_get_value_string_utf8(env, coerced, nullptr, 0, &length) !=
napi_ok) {
return false;
}
std::vector<char> buffer(length + 1);
if (napi_get_value_string_utf8(env, coerced, buffer.data(), buffer.size(),
nullptr) != napi_ok) {
return false;
}
out.assign(buffer.data(), length);
return true;
}
bool IsNullOrUndefined(napi_env env, napi_value value) {
if (value == nullptr) {
return true;
}
napi_valuetype type;
if (napi_typeof(env, value, &type) != napi_ok) {
return false;
}
return type == napi_null || type == napi_undefined;
}
std::string ReadEncodingOption(napi_env env, napi_value options) {
if (IsNullOrUndefined(env, options)) {
return "";
}
napi_valuetype type;
napi_typeof(env, options, &type);
if (type == napi_string) {
std::string value;
if (CoerceToString(env, options, value)) {
return ToLower(value);
}
return "";
}
if (type != napi_object) {
return "";
}
bool hasEncoding = false;
if (napi_has_named_property(env, options, "encoding", &hasEncoding) !=
napi_ok ||
!hasEncoding) {
return "";
}
napi_value encodingValue;
if (napi_get_named_property(env, options, "encoding", &encodingValue) !=
napi_ok) {
return "";
}
if (IsNullOrUndefined(env, encodingValue)) {
return "";
}
std::string encoding;
if (!CoerceToString(env, encodingValue, encoding)) {
return "";
}
return ToLower(encoding);
}
bool ReadBooleanOption(napi_env env, napi_value options, const char* name,
bool defaultValue) {
if (IsNullOrUndefined(env, options)) {
return defaultValue;
}
napi_valuetype type;
napi_typeof(env, options, &type);
if (type != napi_object) {
return defaultValue;
}
bool hasProp = false;
if (napi_has_named_property(env, options, name, &hasProp) != napi_ok ||
!hasProp) {
return defaultValue;
}
napi_value propValue;
if (napi_get_named_property(env, options, name, &propValue) != napi_ok) {
return defaultValue;
}
napi_value boolValue;
if (napi_coerce_to_bool(env, propValue, &boolValue) != napi_ok) {
return defaultValue;
}
bool result = defaultValue;
napi_get_value_bool(env, boolValue, &result);
return result;
}
std::string GetErrnoName(int err) {
switch (err) {
case EACCES:
return "EACCES";
case EEXIST:
return "EEXIST";
case EINVAL:
return "EINVAL";
case EIO:
return "EIO";
case EISDIR:
return "EISDIR";
case ENOENT:
return "ENOENT";
case ENOTDIR:
return "ENOTDIR";
case ENOTEMPTY:
return "ENOTEMPTY";
case EPERM:
return "EPERM";
default:
return "ERR_FS";
}
}
void ThrowFsError(napi_env env, const std::string& syscall,
const std::string& path, const std::string& code,
const std::string& details) {
std::string message = code + ": " + syscall + " '" + path + "'";
if (!details.empty()) {
message += ", " + details;
}
napi_value errorMessage = napi_util::to_js_string(env, message);
napi_value errorObj;
napi_create_error(env, nullptr, errorMessage, &errorObj);
napi_set_named_property(env, errorObj, "code",
napi_util::to_js_string(env, code));
napi_set_named_property(env, errorObj, "path",
napi_util::to_js_string(env, path));
napi_set_named_property(env, errorObj, "syscall",
napi_util::to_js_string(env, syscall));
napi_throw(env, errorObj);
}
void ThrowFsError(napi_env env, const std::string& syscall,
const std::string& path, const std::error_code& ec) {
std::string code = GetErrnoName(ec.value());
ThrowFsError(env, syscall, path, code, ec.message());
}
void ThrowInvalidEncoding(napi_env env, const std::string& encoding) {
std::string message = "Unsupported encoding: " + encoding;
napi_throw_type_error(env, nullptr, message.c_str());
}
bool GetPathArg(napi_env env, napi_value value, std::string& pathOut) {
if (!CoerceToString(env, value, pathOut)) {
napi_throw_type_error(env, nullptr, "Path must be coercible to string");
return false;
}
if (pathOut.empty()) {
napi_throw_type_error(env, nullptr, "Path cannot be empty");
return false;
}
return true;
}
std::string BytesToHex(const std::vector<uint8_t>& bytes) {
static constexpr char kHexDigits[] = "0123456789abcdef";
std::string result;
result.resize(bytes.size() * 2);
size_t out = 0;
for (auto b : bytes) {
result[out++] = kHexDigits[(b >> 4) & 0x0F];
result[out++] = kHexDigits[b & 0x0F];
}
return result;
}
std::string BytesToBase64(const std::vector<uint8_t>& bytes) {
static constexpr char kBase64Chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string result;
result.reserve(((bytes.size() + 2) / 3) * 4);
size_t i = 0;
while (i + 2 < bytes.size()) {
uint32_t n = (static_cast<uint32_t>(bytes[i]) << 16) |
(static_cast<uint32_t>(bytes[i + 1]) << 8) |
static_cast<uint32_t>(bytes[i + 2]);
result.push_back(kBase64Chars[(n >> 18) & 63]);
result.push_back(kBase64Chars[(n >> 12) & 63]);
result.push_back(kBase64Chars[(n >> 6) & 63]);
result.push_back(kBase64Chars[n & 63]);
i += 3;
}
if (i < bytes.size()) {
uint32_t n = static_cast<uint32_t>(bytes[i]) << 16;
result.push_back(kBase64Chars[(n >> 18) & 63]);
if (i + 1 < bytes.size()) {
n |= static_cast<uint32_t>(bytes[i + 1]) << 8;
result.push_back(kBase64Chars[(n >> 12) & 63]);
result.push_back(kBase64Chars[(n >> 6) & 63]);
result.push_back('=');
} else {
result.push_back(kBase64Chars[(n >> 12) & 63]);
result.push_back('=');
result.push_back('=');
}
}
return result;
}
bool HexToBytes(const std::string& input, std::vector<uint8_t>& bytesOut) {
auto hexToNibble = [](char c) -> int {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
return -1;
};
if (input.size() % 2 != 0) {
return false;
}
bytesOut.clear();
bytesOut.reserve(input.size() / 2);
for (size_t i = 0; i < input.size(); i += 2) {
int hi = hexToNibble(input[i]);
int lo = hexToNibble(input[i + 1]);
if (hi < 0 || lo < 0) {
return false;
}
bytesOut.push_back(static_cast<uint8_t>((hi << 4) | lo));
}
return true;
}
bool Base64ToBytes(const std::string& input, std::vector<uint8_t>& bytesOut) {
static std::array<int8_t, 256> lut = [] {
std::array<int8_t, 256> map{};
map.fill(-1);
for (int i = 0; i < 26; i++) {
map[static_cast<uint8_t>('A' + i)] = i;
map[static_cast<uint8_t>('a' + i)] = i + 26;
}
for (int i = 0; i < 10; i++) {
map[static_cast<uint8_t>('0' + i)] = i + 52;
}
map[static_cast<uint8_t>('+')] = 62;
map[static_cast<uint8_t>('/')] = 63;
return map;
}();
std::string normalized;
normalized.reserve(input.size());
for (char c : input) {
if (!std::isspace(static_cast<unsigned char>(c))) {
normalized.push_back(c);
}
}
if (normalized.size() % 4 != 0) {
return false;
}
bytesOut.clear();
bytesOut.reserve((normalized.size() / 4) * 3);
for (size_t i = 0; i < normalized.size(); i += 4) {
char c0 = normalized[i];
char c1 = normalized[i + 1];
char c2 = normalized[i + 2];
char c3 = normalized[i + 3];
if (c0 == '=' || c1 == '=') {
return false;
}
int8_t v0 = lut[static_cast<uint8_t>(c0)];
int8_t v1 = lut[static_cast<uint8_t>(c1)];
if (v0 < 0 || v1 < 0) {
return false;
}
uint32_t n =
(static_cast<uint32_t>(v0) << 18) | (static_cast<uint32_t>(v1) << 12);
if (c2 == '=') {
bytesOut.push_back(static_cast<uint8_t>((n >> 16) & 0xFF));
if (c3 != '=') {
return false;
}
continue;
}
int8_t v2 = lut[static_cast<uint8_t>(c2)];
if (v2 < 0) {
return false;
}
n |= static_cast<uint32_t>(v2) << 6;
if (c3 == '=') {
bytesOut.push_back(static_cast<uint8_t>((n >> 16) & 0xFF));
bytesOut.push_back(static_cast<uint8_t>((n >> 8) & 0xFF));
continue;
}
int8_t v3 = lut[static_cast<uint8_t>(c3)];
if (v3 < 0) {
return false;
}
n |= static_cast<uint32_t>(v3);
bytesOut.push_back(static_cast<uint8_t>((n >> 16) & 0xFF));
bytesOut.push_back(static_cast<uint8_t>((n >> 8) & 0xFF));
bytesOut.push_back(static_cast<uint8_t>(n & 0xFF));
}
return true;
}
napi_value BytesToJsValue(napi_env env, const std::vector<uint8_t>& bytes,
const std::string& encoding) {
std::string normalized = ToLower(encoding);
if (normalized.empty()) {
void* data = nullptr;
napi_value arrayBuffer;
if (bytes.empty()) {
napi_create_arraybuffer(env, 0, &data, &arrayBuffer);
return arrayBuffer;
}
uint8_t* storage = static_cast<uint8_t*>(malloc(bytes.size()));
memcpy(storage, bytes.data(), bytes.size());
napi_create_external_arraybuffer(
env, storage, bytes.size(),
[](napi_env, void* ptr, void*) { free(ptr); }, nullptr, &arrayBuffer);
return arrayBuffer;
}
if (normalized == "utf8" || normalized == "utf-8") {
return napi_util::to_js_string(env,
std::string(bytes.begin(), bytes.end()));
}
if (normalized == "ascii" || normalized == "latin1" ||
normalized == "binary") {
std::string utf8;
utf8.reserve(bytes.size() * 2);
for (auto b : bytes) {
if (b < 0x80) {
utf8.push_back(static_cast<char>(b));
} else {
utf8.push_back(static_cast<char>(0xC0 | (b >> 6)));
utf8.push_back(static_cast<char>(0x80 | (b & 0x3F)));
}
}
return napi_util::to_js_string(env, utf8);
}
if (normalized == "hex") {
return napi_util::to_js_string(env, BytesToHex(bytes));
}
if (normalized == "base64") {
return napi_util::to_js_string(env, BytesToBase64(bytes));
}
ThrowInvalidEncoding(env, encoding);
return nullptr;
}
bool JsValueToBytes(napi_env env, napi_value value, const std::string& encoding,
std::vector<uint8_t>& bytesOut) {
napi_valuetype type;
if (napi_typeof(env, value, &type) != napi_ok) {
return false;
}
bool isArrayBuffer = false;
napi_is_arraybuffer(env, value, &isArrayBuffer);
if (isArrayBuffer) {
void* data = nullptr;
size_t length = 0;
napi_get_arraybuffer_info(env, value, &data, &length);
bytesOut.assign(static_cast<uint8_t*>(data),
static_cast<uint8_t*>(data) + length);
return true;
}
bool isTypedArray = false;
napi_is_typedarray(env, value, &isTypedArray);
if (isTypedArray) {
napi_typedarray_type typedArrayType;
size_t length = 0;
void* data = nullptr;
napi_value buffer;
size_t byteOffset = 0;
napi_get_typedarray_info(env, value, &typedArrayType, &length, &data,
&buffer, &byteOffset);
size_t elementSize = 1;
switch (typedArrayType) {
case napi_int16_array:
case napi_uint16_array:
elementSize = 2;
break;
case napi_int32_array:
case napi_uint32_array:
case napi_float32_array:
elementSize = 4;
break;
case napi_float64_array:
case napi_bigint64_array:
case napi_biguint64_array:
elementSize = 8;
break;
default:
elementSize = 1;
break;
}
const uint8_t* ptr = static_cast<uint8_t*>(data);
bytesOut.assign(ptr, ptr + (length * elementSize));
return true;
}
std::string text;
if (!CoerceToString(env, value, text)) {
return false;
}
std::string normalized = ToLower(encoding);
if (normalized.empty() || normalized == "utf8" || normalized == "utf-8" ||
normalized == "ascii" || normalized == "latin1" ||
normalized == "binary") {
bytesOut.assign(text.begin(), text.end());
return true;
}
if (normalized == "hex") {
if (!HexToBytes(text, bytesOut)) {
napi_throw_type_error(env, nullptr, "Invalid hex string");
return false;
}
return true;
}
if (normalized == "base64") {
if (!Base64ToBytes(text, bytesOut)) {
napi_throw_type_error(env, nullptr, "Invalid base64 string");
return false;
}
return true;
}
ThrowInvalidEncoding(env, encoding);
return false;
}
bool ReadFileToBytes(const std::string& path, std::vector<uint8_t>& bytesOut,
std::error_code& ec) {
ec.clear();
std::ifstream input(path, std::ios::binary);
if (!input.is_open()) {
ec = std::error_code(errno, std::generic_category());
return false;
}
input.seekg(0, std::ios::end);
std::streamsize size = input.tellg();
input.seekg(0, std::ios::beg);
if (size < 0) {
ec = std::make_error_code(std::errc::io_error);
return false;
}
bytesOut.resize(static_cast<size_t>(size));
if (size > 0 &&
!input.read(reinterpret_cast<char*>(bytesOut.data()), size).good()) {
ec = std::error_code(errno, std::generic_category());
return false;
}
return true;
}
bool WriteBytesToFile(const std::string& path,
const std::vector<uint8_t>& bytes, std::error_code& ec) {
ec.clear();
std::ofstream output(path, std::ios::binary | std::ios::trunc);
if (!output.is_open()) {
ec = std::error_code(errno, std::generic_category());
return false;
}
if (!bytes.empty()) {
output.write(reinterpret_cast<const char*>(bytes.data()),
static_cast<std::streamsize>(bytes.size()));
}
if (!output.good()) {
ec = std::error_code(errno, std::generic_category());
return false;
}
return true;
}
napi_value BuildStatObject(napi_env env, const fs::file_status& status,
const fs::path& path) {
napi_value statObj;
napi_create_object(env, &statObj);
std::error_code ec;
uintmax_t size = fs::is_regular_file(status) ? fs::file_size(path, ec) : 0;
if (ec) {
size = 0;
}
long long mtimeMs = 0;
auto writeTime = fs::last_write_time(path, ec);
if (!ec) {
auto systemTime =
std::chrono::time_point_cast<std::chrono::system_clock::duration>(
writeTime - fs::file_time_type::clock::now() +
std::chrono::system_clock::now());
mtimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(
systemTime.time_since_epoch())
.count();
}
napi_set_named_property(
env, statObj, "size",
napi_util::to_js_number(env, static_cast<double>(size)));
napi_set_named_property(
env, statObj, "mtimeMs",
napi_util::to_js_number(env, static_cast<double>(mtimeMs)));
napi_set_named_property(
env, statObj, "ctimeMs",
napi_util::to_js_number(env, static_cast<double>(mtimeMs)));
napi_set_named_property(
env, statObj, "atimeMs",
napi_util::to_js_number(env, static_cast<double>(mtimeMs)));
napi_set_named_property(
env, statObj, "birthtimeMs",
napi_util::to_js_number(env, static_cast<double>(mtimeMs)));
napi_set_named_property(env, statObj, "_isFile",
fs::is_regular_file(status)
? napi_util::get_true(env)
: napi_util::get_false(env));
napi_set_named_property(env, statObj, "_isDirectory",
fs::is_directory(status) ? napi_util::get_true(env)
: napi_util::get_false(env));
napi_set_named_property(env, statObj, "_isSymbolicLink",
fs::is_symlink(status) ? napi_util::get_true(env)
: napi_util::get_false(env));
auto boolAccessor = [](napi_env env, napi_callback_info info) -> napi_value {
NAPI_CALLBACK_BEGIN(0)
const char* prop = static_cast<const char*>(data);
bool hasProp = false;
napi_has_named_property(env, jsThis, prop, &hasProp);
if (!hasProp) {
return napi_util::get_false(env);
}
napi_value value;
napi_get_named_property(env, jsThis, prop, &value);
napi_value result;
napi_coerce_to_bool(env, value, &result);
return result;
};
napi_util::napi_set_function(env, statObj, "isFile", boolAccessor,
(void*)"_isFile");
napi_util::napi_set_function(env, statObj, "isDirectory", boolAccessor,
(void*)"_isDirectory");
napi_util::napi_set_function(env, statObj, "isSymbolicLink", boolAccessor,
(void*)"_isSymbolicLink");
return statObj;
}
napi_value BuildDirentObject(napi_env env, const fs::directory_entry& entry) {
napi_value direntObj;
napi_create_object(env, &direntObj);
auto name = entry.path().filename().string();
napi_set_named_property(env, direntObj, "name",
napi_util::to_js_string(env, name));
std::error_code ec;
auto status = entry.symlink_status(ec);
bool isFile = !ec && fs::is_regular_file(status);
bool isDir = !ec && fs::is_directory(status);
bool isSymlink = !ec && fs::is_symlink(status);
napi_set_named_property(
env, direntObj, "_isFile",
isFile ? napi_util::get_true(env) : napi_util::get_false(env));
napi_set_named_property(
env, direntObj, "_isDirectory",
isDir ? napi_util::get_true(env) : napi_util::get_false(env));
napi_set_named_property(
env, direntObj, "_isSymbolicLink",
isSymlink ? napi_util::get_true(env) : napi_util::get_false(env));
auto boolAccessor = [](napi_env env, napi_callback_info info) -> napi_value {
NAPI_CALLBACK_BEGIN(0)
const char* prop = static_cast<const char*>(data);
bool hasProp = false;
napi_has_named_property(env, jsThis, prop, &hasProp);
if (!hasProp) {
return napi_util::get_false(env);
}
napi_value value;
napi_get_named_property(env, jsThis, prop, &value);
napi_value result;
napi_coerce_to_bool(env, value, &result);
return result;
};
napi_util::napi_set_function(env, direntObj, "isFile", boolAccessor,
(void*)"_isFile");
napi_util::napi_set_function(env, direntObj, "isDirectory", boolAccessor,
(void*)"_isDirectory");
napi_util::napi_set_function(env, direntObj, "isSymbolicLink", boolAccessor,
(void*)"_isSymbolicLink");
return direntObj;
}
napi_value ReadFileSyncImpl(napi_env env, size_t argc, napi_value* argv) {
if (argc < 1) {
napi_throw_type_error(env, nullptr, "fs.readFileSync(path[, options])");
return nullptr;
}
std::string path;
if (!GetPathArg(env, argv[0], path)) {
return nullptr;
}
std::string encoding = argc > 1 ? ReadEncodingOption(env, argv[1]) : "";
std::error_code ec;
std::vector<uint8_t> bytes;
if (!ReadFileToBytes(path, bytes, ec)) {
ThrowFsError(env, "readFileSync", path, ec);
return nullptr;
}
return BytesToJsValue(env, bytes, encoding);
}
napi_value WriteFileSyncImpl(napi_env env, size_t argc, napi_value* argv) {
if (argc < 2) {
napi_throw_type_error(env, nullptr,
"fs.writeFileSync(path, data[, options])");
return nullptr;
}
std::string path;
if (!GetPathArg(env, argv[0], path)) {
return nullptr;
}
std::string encoding = argc > 2 ? ReadEncodingOption(env, argv[2]) : "";
std::vector<uint8_t> bytes;
if (!JsValueToBytes(env, argv[1], encoding, bytes)) {
return nullptr;
}
std::error_code ec;
if (!WriteBytesToFile(path, bytes, ec)) {
ThrowFsError(env, "writeFileSync", path, ec);
return nullptr;
}
return napi_util::undefined(env);
}
void InstallFsWrapper(napi_env env, napi_value exports) {
const char* wrapperScript = R"JS(
(function (exports) {
const readFileSync = exports.readFileSync;
const writeFileSync = exports.writeFileSync;
const mkdirSync = exports.mkdirSync;
const readdirSync = exports.readdirSync;
const statSync = exports.statSync;
const lstatSync = exports.lstatSync;
const unlinkSync = exports.unlinkSync;
const rmSync = exports.rmSync;
function toAsync(fn) {
return function (...args) {
return Promise.resolve().then(() => fn(...args));
};
}
exports.readFile = function readFile(path, options, callback) {
if (typeof options === "function") {
callback = options;
options = undefined;
}
if (typeof callback === "function") {
setTimeout(() => {
try {
callback(null, readFileSync(path, options));
} catch (err) {
callback(err);
}
}, 0);
return;
}
return toAsync(readFileSync)(path, options);
};
exports.writeFile = function writeFile(path, data, options, callback) {
if (typeof options === "function") {
callback = options;
options = undefined;
}
if (typeof callback === "function") {
setTimeout(() => {
try {
callback(null, writeFileSync(path, data, options));
} catch (err) {
callback(err);
}
}, 0);
return;
}
return toAsync(writeFileSync)(path, data, options);
};
exports.promises = {
readFile: toAsync(readFileSync),
writeFile: toAsync(writeFileSync),
mkdir: toAsync(mkdirSync),
readdir: toAsync(readdirSync),
stat: toAsync(statSync),
lstat: toAsync(lstatSync),
unlink: toAsync(unlinkSync),
rm: toAsync(rmSync),
};
})
)JS";
napi_value script;
napi_create_string_utf8(env, wrapperScript, NAPI_AUTO_LENGTH, &script);
napi_value fn;
napi_run_script(env, script, &fn);
napi_value global;
napi_get_global(env, &global);
napi_value argv[1] = {exports};
napi_call_function(env, global, fn, 1, argv, nullptr);
}
} // namespace
napi_value FS::CreateModule(napi_env env) {
napi_value moduleObj;
napi_value exports;
napi_create_object(env, &moduleObj);
napi_create_object(env, &exports);
napi_util::napi_set_function(env, exports, "readFileSync", ReadFileSync);
napi_util::napi_set_function(env, exports, "writeFileSync", WriteFileSync);
napi_util::napi_set_function(env, exports, "existsSync", ExistsSync);
napi_util::napi_set_function(env, exports, "mkdirSync", MkdirSync);
napi_util::napi_set_function(env, exports, "readdirSync", ReaddirSync);
napi_util::napi_set_function(env, exports, "statSync", StatSync);
napi_util::napi_set_function(env, exports, "lstatSync", LstatSync);
napi_util::napi_set_function(env, exports, "unlinkSync", UnlinkSync);
napi_util::napi_set_function(env, exports, "rmSync", RmSync);
napi_value constants;
napi_create_object(env, &constants);
napi_set_named_property(env, constants, "F_OK",
napi_util::to_js_number(env, 0));
napi_set_named_property(env, constants, "R_OK",
napi_util::to_js_number(env, 4));
napi_set_named_property(env, constants, "W_OK",
napi_util::to_js_number(env, 2));
napi_set_named_property(env, constants, "X_OK",
napi_util::to_js_number(env, 1));
napi_set_named_property(env, exports, "constants", constants);
InstallFsWrapper(env, exports);
napi_set_named_property(env, moduleObj, "exports", exports);
return moduleObj;
}
napi_value FS::ReadFileSync(napi_env env, napi_callback_info info) {
NAPI_CALLBACK_BEGIN_VARGS()
return ReadFileSyncImpl(env, argc, argv.data());
}
napi_value FS::WriteFileSync(napi_env env, napi_callback_info info) {
NAPI_CALLBACK_BEGIN_VARGS()
return WriteFileSyncImpl(env, argc, argv.data());
}
napi_value FS::ExistsSync(napi_env env, napi_callback_info info) {
NAPI_CALLBACK_BEGIN(1)
std::string path;
if (!GetPathArg(env, argv[0], path)) {
return nullptr;
}
std::error_code ec;
bool exists = fs::exists(path, ec);
if (ec) {
exists = false;
}
napi_value result;
napi_get_boolean(env, exists, &result);
return result;
}
napi_value FS::MkdirSync(napi_env env, napi_callback_info info) {
NAPI_CALLBACK_BEGIN_VARGS()
if (argc < 1) {
napi_throw_type_error(env, nullptr, "fs.mkdirSync(path[, options])");
return nullptr;
}
std::string path;
if (!GetPathArg(env, argv[0], path)) {
return nullptr;
}
bool recursive =
argc > 1 ? ReadBooleanOption(env, argv[1], "recursive", false) : false;
std::error_code ec;
bool created = recursive ? fs::create_directories(path, ec)
: fs::create_directory(path, ec);
if (ec) {
ThrowFsError(env, "mkdirSync", path, ec);
return nullptr;
}
if (!recursive && !created && fs::exists(path)) {
ThrowFsError(env, "mkdirSync", path, "EEXIST", "file already exists");
return nullptr;
}
return napi_util::undefined(env);
}
napi_value FS::ReaddirSync(napi_env env, napi_callback_info info) {
NAPI_CALLBACK_BEGIN_VARGS()
if (argc < 1) {
napi_throw_type_error(env, nullptr, "fs.readdirSync(path[, options])");
return nullptr;
}
std::string path;
if (!GetPathArg(env, argv[0], path)) {
return nullptr;
}
std::string encoding = argc > 1 ? ReadEncodingOption(env, argv[1]) : "utf8";
if (encoding.empty()) {
encoding = "utf8";
}
bool withFileTypes =
argc > 1 ? ReadBooleanOption(env, argv[1], "withFileTypes", false)
: false;
std::error_code ec;
if (!fs::exists(path, ec)) {
ThrowFsError(
env, "readdirSync", path,
ec ? ec : std::make_error_code(std::errc::no_such_file_or_directory));
return nullptr;
}
if (!fs::is_directory(path, ec)) {
ThrowFsError(env, "readdirSync", path,
ec ? ec : std::make_error_code(std::errc::not_a_directory));
return nullptr;
}
if (ec) {
ThrowFsError(env, "readdirSync", path, ec);
return nullptr;
}
std::vector<napi_value> entries;
for (const auto& entry : fs::directory_iterator(path, ec)) {
if (ec) {
ThrowFsError(env, "readdirSync", path, ec);
return nullptr;
}
if (withFileTypes) {
entries.push_back(BuildDirentObject(env, entry));
continue;
}
auto name = entry.path().filename().string();
std::vector<uint8_t> nameBytes(name.begin(), name.end());
entries.push_back(BytesToJsValue(env, nameBytes, encoding));
}
napi_value result;
napi_create_array_with_length(env, entries.size(), &result);
for (size_t i = 0; i < entries.size(); i++) {
napi_set_element(env, result, i, entries[i]);
}
return result;
}
napi_value FS::StatSync(napi_env env, napi_callback_info info) {
NAPI_CALLBACK_BEGIN(1)
std::string path;
if (!GetPathArg(env, argv[0], path)) {
return nullptr;
}
std::error_code ec;
auto fileStatus = fs::status(path, ec);