forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasmfs.cpp
More file actions
2132 lines (1880 loc) · 74.9 KB
/
asmfs.cpp
File metadata and controls
2132 lines (1880 loc) · 74.9 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
// Copyright 2016 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#define __NEED_struct_iovec
#include "syscall_arch.h"
#include <ctype.h>
#include <emscripten/emscripten.h>
#include <emscripten/fetch.h>
#include <emscripten/threading.h>
#include <libc/fcntl.h>
#include <math.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <wasi/api.h>
// Uncomment the following and clear the cache with emcc --clear-cache to rebuild this file to
// enable internal debugging. #define ASMFS_DEBUG
extern "C" {
// http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
#define MAX_PATHNAME_LENGTH 2000
#define INODE_TYPE uint32_t
#define INODE_FILE 1
#define INODE_DIR 2
struct inode {
char name[NAME_MAX + 1]; // NAME_MAX actual bytes + one byte for null termination.
inode* parent; // ID of the parent node
inode* sibling; // ID of a sibling node (these form a singular linked list that specifies the
// content under a directory)
inode* child; // ID of the first child node in a chain of children (the root of a linked list of
// inodes)
uint32_t uid; // User ID of the owner
uint32_t gid; // Group ID of the owning group
uint32_t mode; // r/w/x modes
time_t ctime; // Time when the inode was last modified
time_t mtime; // Time when the content was last modified
time_t atime; // Time when the content was last accessed
size_t size; // Size of the file in bytes
size_t capacity; // Amount of bytes allocated to pointer data
uint8_t* data; // The actual file contents.
INODE_TYPE type;
emscripten_fetch_t* fetch;
// Specifies a remote server address where this inode can be located.
char* remoteurl;
};
#define EM_FILEDESCRIPTOR_MAGIC 0x64666d65U // 'emfd'
struct FileDescriptor {
uint32_t magic;
ssize_t file_pos;
uint32_t mode;
uint32_t flags;
inode* node;
};
static inode* create_inode(INODE_TYPE type, int mode) {
inode* i = (inode*)malloc(sizeof(inode));
memset(i, 0, sizeof(inode));
i->ctime = i->mtime = i->atime = time(0);
i->type = type;
i->mode = mode;
return i;
}
// The current working directory of the application process.
static inode* cwd_inode = 0;
static inode* filesystem_root() {
static inode* root_node = create_inode(INODE_DIR, 0777);
return root_node;
}
static inode* get_cwd() {
if (!cwd_inode)
cwd_inode = filesystem_root();
return cwd_inode;
}
static void set_cwd(inode* node) { cwd_inode = node; }
static void inode_abspath(inode* node, char* dst, int dstLen) {
if (!node) {
assert(dstLen >= (int)strlen("(null)") + 1);
strcpy(dst, "(null)");
return;
}
if (node == filesystem_root()) {
assert(dstLen >= (int)strlen("/") + 1);
strcpy(dst, "/");
return;
}
#define MAX_DIRECTORY_DEPTH 512
inode* stack[MAX_DIRECTORY_DEPTH];
int depth = 0;
while (node->parent && depth < MAX_DIRECTORY_DEPTH) {
stack[depth++] = node;
node = node->parent;
}
char* dstEnd = dst + dstLen;
*dstEnd-- = '\0';
while (depth > 0 && dst < dstEnd) {
if (dst < dstEnd)
*dst++ = '/';
--depth;
int len = strlen(stack[depth]->name);
if (len > dstEnd - dst)
len = dstEnd - dst;
strncpy(dst, stack[depth]->name, len);
dst += len;
}
}
// Deletes the given inode. Ignores (orphans) any children there might be
static void delete_inode(inode* node) {
if (!node)
return;
if (node == filesystem_root())
return; // As special case, do not allow deleting the filesystem root directory
#ifdef ASMFS_DEBUG
EM_ASM(err('delete_inode: ' + UTF8ToString($0)), node->name);
#endif
if (node->fetch)
emscripten_fetch_close(node->fetch);
free(node->remoteurl);
free(node);
}
// Deletes the given inode and its subtree
static void delete_inode_tree(inode* node) {
if (!node)
return;
#ifdef ASMFS_DEBUG
EM_ASM(err('delete_inode_tree: ' + UTF8ToString($0)), node->name);
#endif
inode* child = node->child;
while (child) {
inode* sibling = child->sibling;
delete_inode_tree(child->child);
delete_inode(child);
child = sibling;
}
if (node !=
filesystem_root()) // As special case, do not allow deleting the filesystem root directory
{
delete_inode(node);
} else {
// For filesystem root, just make sure all children are gone.
node->child = 0;
}
}
// Makes node the child of parent.
static void link_inode(inode* node, inode* parent) {
char parentName[PATH_MAX];
inode_abspath(parent, parentName, PATH_MAX);
#ifdef ASMFS_DEBUG
EM_ASM(err('link_inode: node "' + UTF8ToString($0) + '" to parent "' + UTF8ToString($1) + '".'),
node->name, parentName);
#endif
// When linking a node, it can't be part of the filesystem tree (but it can have children of its
// own)
assert(!node->parent);
assert(!node->sibling);
// The inode pointed by 'node' is not yet part of the filesystem, so it's not shared memory and
// only this thread is accessing it. Therefore setting the node's parent here is not yet racy, do
// that operation first.
node->parent = parent;
// This node is to become the first child of the parent, and the old first child of the parent
// should become the sibling of this node, i.e.
// 1) node->sibling = parent->child;
// 2) parent->child = node;
// However these two operations need to occur atomically in order to be coherent. To ensure that,
// run the two operations in a CAS loop, which is possible because the first operation is not racy
// until the node is 'published' to the filesystem tree by the compare_exchange operation.
do {
__atomic_load(
&parent->child, &node->sibling, __ATOMIC_SEQ_CST); // node->sibling <- parent->child
} while (
!__atomic_compare_exchange(&parent->child, &node->sibling, &node, false, __ATOMIC_SEQ_CST,
__ATOMIC_SEQ_CST)); // parent->child <- node if it had not raced to change value in between
}
// Traverse back in sibling linked list, or 0 if no such node exist.
static inode* find_predecessor_sibling(inode* node, inode* parent) {
inode* child = parent->child;
if (child == node)
return 0;
while (child && child->sibling != node)
child = child->sibling;
if (!child->sibling)
return 0;
return child;
}
static void unlink_inode(inode* node) {
#ifdef ASMFS_DEBUG
EM_ASM(
err('unlink_inode: node ' + UTF8ToString($0) + ' from its parent ' + UTF8ToString($1) + '.'),
node->name, node->parent->name);
#endif
inode* parent = node->parent;
if (!parent)
return;
node->parent = 0;
if (parent->child == node) {
parent->child = node->sibling;
} else {
inode* predecessor = find_predecessor_sibling(node, parent);
if (predecessor)
predecessor->sibling = node->sibling;
}
node->parent = node->sibling = 0;
}
// Compares two strings for equality until a '\0' or a '/' is hit. Returns 0 if the strings differ,
// or a pointer to the beginning of the next directory component name of s1 if the strings are
// equal.
static const char* path_cmp(const char* s1, const char* s2, bool* is_directory) {
*is_directory = true;
while (*s1 == *s2) {
if (*s1 == '/')
return s1 + 1;
if (*s1 == '\0') {
*is_directory = false;
return s1;
}
++s1;
++s2;
}
if (*s1 == '/' && *s2 == '\0')
return s1 + 1;
if (*s1 == '\0' && *s2 == '/')
return s1;
return 0;
}
#define NIBBLE_TO_CHAR(x) ("0123456789abcdef"[(x)])
static void uriEncode(char* dst, int dstLengthBytes, const char* src) {
char* end =
dst + dstLengthBytes - 4; // Use last 4 bytes of dst as a guard area to avoid overflow below.
while (*src && dst < end) {
if (isalnum(*src) || *src == '-' || *src == '_' || *src == '.' || *src == '~')
*dst++ = *src;
else if (*src == '/')
*dst++ = *src; // NB. forward slashes should generally be uriencoded, but for file path
// purposes, we want to keep them intact.
else
*dst++ = '%', *dst++ = NIBBLE_TO_CHAR(*src >> 4),
*dst++ = NIBBLE_TO_CHAR(*src & 15); // This charater needs uriencoding.
++src;
}
*dst = '\0';
}
// Copies string 'path' to 'dst', but stops on the first forward slash '/' character.
// Returns number of bytes written, excluding null terminator
static int strcpy_inodename(char* dst, const char* path) {
char* d = dst;
while (*path && *path != '/')
*dst++ = *path++;
*dst = '\0';
return dst - d;
}
// Copies src to dst, writes at most maxBytesToWrite out. Always null terminates dst. Returns the
// number of characters written, excluding null terminator.
static int strcpy_safe(char* dst, const char* src, int maxBytesToWrite) {
char* dst_start = dst;
char* dst_end = dst + maxBytesToWrite - 1;
while (dst < dst_end && *src)
*dst++ = *src++;
*dst = '\0';
return dst - dst_start;
}
// Returns a pointer to the basename part of the string, i.e. the string after the last occurrence
// of a forward slash character
static const char* basename_part(const char* path) {
const char* s = path;
while (*path) {
if (*path == '/')
s = path + 1;
++path;
}
return s;
}
static inode* create_directory_hierarchy_for_file(
inode* root, const char* path_to_file, unsigned int mode) {
assert(root);
if (!root)
return 0;
// Traverse . and ..
while (path_to_file[0] == '.') {
if (path_to_file[1] == '/')
path_to_file += 2; // Skip over redundant "./././././" blocks
else if (path_to_file[1] == '\0')
path_to_file += 1;
else if (path_to_file[1] == '.' &&
(path_to_file[2] == '/' ||
path_to_file[2] == '\0')) // Go up to parent directories with ".."
{
root = root->parent;
if (!root)
return 0;
assert(
root->type == INODE_DIR); // Anything that is a parent should automatically be a directory.
path_to_file += (path_to_file[2] == '/') ? 3 : 2;
} else
break;
}
if (path_to_file[0] == '\0')
return 0;
inode* node = root->child;
while (node) {
bool is_directory = false;
const char* child_path = path_cmp(path_to_file, node->name, &is_directory);
#ifdef ASMFS_DEBUG
EM_ASM_INT({err('path_cmp ' + UTF8ToString($0) + ', ' + UTF8ToString($1) + ', ' +
UTF8ToString($2) + ' .')},
path_to_file, node->name, child_path);
#endif
if (child_path) {
if (is_directory && node->type != INODE_DIR)
return 0; // "A component used as a directory in pathname is not, in fact, a directory"
// The directory name matches.
path_to_file = child_path;
// Traverse . and ..
while (path_to_file[0] == '.') {
if (path_to_file[1] == '/')
path_to_file += 2; // Skip over redundant "./././././" blocks
else if (path_to_file[1] == '\0')
path_to_file += 1;
else if (path_to_file[1] == '.' &&
(path_to_file[2] == '/' ||
path_to_file[2] == '\0')) // Go up to parent directories with ".."
{
node = node->parent;
if (!node)
return 0;
assert(node->type ==
INODE_DIR); // Anything that is a parent should automatically be a directory.
path_to_file += (path_to_file[2] == '/') ? 3 : 2;
} else
break;
}
if (path_to_file[0] == '\0')
return node;
if (path_to_file[0] == '/' && path_to_file[1] == '\0' /* && node is a directory*/)
return node;
root = node;
node = node->child;
} else {
node = node->sibling;
}
}
const char* basename_pos = basename_part(path_to_file);
#ifdef ASMFS_DEBUG
EM_ASM(err('path_to_file ' + UTF8ToString($0) + ' .'), path_to_file);
EM_ASM(err('basename_pos ' + UTF8ToString($0) + ' .'), basename_pos);
#endif
while (*path_to_file && path_to_file < basename_pos) {
node = create_inode(INODE_DIR, mode);
path_to_file += strcpy_inodename(node->name, path_to_file) + 1;
link_inode(node, root);
#ifdef ASMFS_DEBUG
EM_ASM(out('create_directory_hierarchy_for_file: created directory ' + UTF8ToString($0) +
' under parent ' + UTF8ToString($1) + '.'),
node->name, node->parent->name);
#endif
root = node;
}
return root;
}
#define RETURN_NODE_AND_ERRNO(node, errno) \
do { \
*out_errno = (errno); \
return (node); \
} while (0)
// Given a pathname to a file/directory, finds the inode of the directory that would contain the
// file/directory, or 0 if the intermediate path doesn't exist. Note that the file/directory pointed
// to by path does not need to exist, only its parent does.
static inode* find_parent_inode(inode* root, const char* path, int* out_errno) {
char rootName[PATH_MAX];
inode_abspath(root, rootName, PATH_MAX);
#ifdef ASMFS_DEBUG
EM_ASM(err('find_parent_inode(root="' + UTF8ToString($0) + '", path="' + UTF8ToString($1) + '")'),
rootName, path);
#endif
assert(out_errno); // Passing in error is mandatory.
if (!root)
RETURN_NODE_AND_ERRNO(0, ENOENT);
if (!path)
RETURN_NODE_AND_ERRNO(0, ENOENT);
// Traverse . and ..
while (path[0] == '.') {
if (path[1] == '/')
path += 2; // Skip over redundant "./././././" blocks
else if (path[1] == '\0')
path += 1;
else if (path[1] == '.' &&
(path[2] == '/' || path[2] == '\0')) // Go up to parent directories with ".."
{
root = root->parent;
if (!root)
RETURN_NODE_AND_ERRNO(0, ENOENT);
assert(
root->type == INODE_DIR); // Anything that is a parent should automatically be a directory.
path += (path[2] == '/') ? 3 : 2;
} else
break;
}
if (path[0] == '\0')
RETURN_NODE_AND_ERRNO(0, ENOENT);
if (path[0] == '/' && path[1] == '\0')
RETURN_NODE_AND_ERRNO(0, ENOENT);
if (root->type != INODE_DIR)
RETURN_NODE_AND_ERRNO(
0, ENOTDIR); // "A component used as a directory in pathname is not, in fact, a directory"
// TODO: RETURN_ERRNO(ELOOP, "Too many symbolic links were encountered in translating pathname");
// TODO: RETURN_ERRNO(EACCES, "one of the directories in the path prefix of pathname did not allow
// search permission");
const char* basename = basename_part(path);
if (path == basename)
RETURN_NODE_AND_ERRNO(root, 0);
inode* node = root->child;
while (node) {
bool is_directory = false;
const char* child_path = path_cmp(path, node->name, &is_directory);
if (child_path) {
if (is_directory && node->type != INODE_DIR)
RETURN_NODE_AND_ERRNO(
0, ENOTDIR); // "A component used as a directory in pathname is not, in fact, a directory"
// The directory name matches.
path = child_path;
// Traverse . and ..
while (path[0] == '.') {
if (path[1] == '/')
path += 2; // Skip over redundant "./././././" blocks
else if (path[1] == '\0')
path += 1;
else if (path[1] == '.' &&
(path[2] == '/' || path[2] == '\0')) // Go up to parent directories with ".."
{
node = node->parent;
if (!node)
RETURN_NODE_AND_ERRNO(0, ENOENT);
assert(node->type ==
INODE_DIR); // Anything that is a parent should automatically be a directory.
path += (path[2] == '/') ? 3 : 2;
} else
break;
}
if (path >= basename)
RETURN_NODE_AND_ERRNO(node, 0);
if (!*path)
RETURN_NODE_AND_ERRNO(0, ENOENT);
node = node->child;
if (node->type != INODE_DIR)
RETURN_NODE_AND_ERRNO(
0, ENOTDIR); // "A component used as a directory in pathname is not, in fact, a directory"
} else {
node = node->sibling;
}
}
RETURN_NODE_AND_ERRNO(
0, ENOTDIR); // "A component used as a directory in pathname is not, in fact, a directory"
}
// Given a root inode of the filesystem and a path relative to it, e.g.
// "some/directory/dir_or_file", returns the inode that corresponds to "dir_or_file", or 0 if it
// doesn't exist. If the parameter out_closest_parent is specified, the closest (grand)parent node
// will be returned.
static inode* find_inode(inode* root, const char* path, int* out_errno) {
char rootName[PATH_MAX];
inode_abspath(root, rootName, PATH_MAX);
#ifdef ASMFS_DEBUG
EM_ASM(err('find_inode(root="' + UTF8ToString($0) + '", path="' + UTF8ToString($1) + '")'),
rootName, path);
#endif
assert(out_errno); // Passing in error is mandatory.
if (!root)
RETURN_NODE_AND_ERRNO(0, ENOENT);
// TODO: RETURN_ERRNO(ELOOP, "Too many symbolic links were encountered in translating pathname");
// TODO: RETURN_ERRNO(EACCES, "one of the directories in the path prefix of pathname did not allow
// search permission");
// special-case finding empty string path "", "." or "/" returns the root searched in.
if (root->type != INODE_DIR)
RETURN_NODE_AND_ERRNO(
0, ENOTDIR); // "A component used as a directory in pathname is not, in fact, a directory"
if (!path)
RETURN_NODE_AND_ERRNO(root, 0);
// Traverse . and ..
while (path[0] == '.') {
if (path[1] == '/')
path += 2; // Skip over redundant "./././././" blocks
else if (path[1] == '\0')
path += 1;
else if (path[1] == '.' &&
(path[2] == '/' || path[2] == '\0')) // Go up to parent directories with ".."
{
root = root->parent;
if (!root)
RETURN_NODE_AND_ERRNO(0, ENOENT);
assert(
root->type == INODE_DIR); // Anything that is a parent should automatically be a directory.
path += (path[2] == '/') ? 3 : 2;
} else
break;
}
if (path[0] == '\0')
RETURN_NODE_AND_ERRNO(root, 0);
inode* node = root->child;
while (node) {
bool is_directory = false;
const char* child_path = path_cmp(path, node->name, &is_directory);
if (child_path) {
if (is_directory && node->type != INODE_DIR)
RETURN_NODE_AND_ERRNO(
0, ENOTDIR); // "A component used as a directory in pathname is not, in fact, a directory"
// The directory name matches.
path = child_path;
// Traverse . and ..
while (path[0] == '.') {
if (path[1] == '/')
path += 2; // Skip over redundant "./././././" blocks
else if (path[1] == '\0')
path += 1;
else if (path[1] == '.' &&
(path[2] == '/' || path[2] == '\0')) // Go up to parent directories with ".."
{
node = node->parent;
if (!node)
RETURN_NODE_AND_ERRNO(0, ENOENT);
assert(node->type ==
INODE_DIR); // Anything that is a parent should automatically be a directory.
path += (path[2] == '/') ? 3 : 2;
} else
break;
}
// If we arrived to the end of the search, this is the node we were looking for.
if (path[0] == '\0')
RETURN_NODE_AND_ERRNO(node, 0);
if (path[0] == '/' && node->type != INODE_DIR)
RETURN_NODE_AND_ERRNO(
0, ENOTDIR); // "A component used as a directory in pathname is not, in fact, a directory"
if (path[0] == '/' && path[1] == '\0')
RETURN_NODE_AND_ERRNO(node, 0);
node = node->child;
} else {
node = node->sibling;
}
}
RETURN_NODE_AND_ERRNO(0, ENOENT);
}
// Same as above, but the root node is deduced from 'path'. (either absolute if path starts with
// "/", or relative)
static inode* find_inode(const char* path, int* out_errno) {
inode* root;
if (path[0] == '/')
root = filesystem_root(), ++path;
else
root = get_cwd();
return find_inode(root, path, out_errno);
}
void emscripten_asmfs_set_remote_url(const char* filename, const char* remoteUrl) {
int err;
inode* node = find_inode(filename, &err);
if (!node)
return;
free(node->remoteurl);
node->remoteurl = strdup(remoteUrl);
}
void emscripten_asmfs_set_file_data(const char* filename, char* data, size_t size) {
int err;
inode* node = find_inode(filename, &err);
if (!node) {
free(data);
return;
}
free(node->data);
node->data = (uint8_t*)data;
node->size = node->capacity = size;
}
char* find_last_occurrence(char* str, char ch) {
char* o = 0;
while (*str) {
if (*str == ch)
o = str;
++str;
}
return o;
}
// Given a filename outputs the remote URL address that file can be located in.
void emscripten_asmfs_remote_url(const char* filename, char* outRemoteUrl, int maxBytesToWrite) {
if (maxBytesToWrite <= 0 || !outRemoteUrl)
return;
*outRemoteUrl = '\0';
if (maxBytesToWrite == 1)
return;
char trailing_path[PATH_MAX + 1] = {};
char full_path[PATH_MAX + 1] = {};
char full_path_temp[PATH_MAX + 1] = {};
strcpy(full_path, filename);
int err;
inode* node = find_inode(full_path, &err);
while (!node) {
char* s = find_last_occurrence(full_path, '/');
if (!s) {
node = filesystem_root();
strcpy(full_path_temp, trailing_path);
strcpy(trailing_path, full_path);
if (full_path_temp[0] != '\0') {
strcat(trailing_path, "/");
strcat(trailing_path, full_path_temp);
}
break;
}
*s = '\0';
node = find_inode(full_path, &err);
strcpy(full_path_temp, trailing_path);
strcpy(trailing_path, filename + (s - full_path));
if (full_path_temp[0] != '\0') {
strcat(trailing_path, "/");
strcat(trailing_path, full_path_temp);
}
}
char uriEncodedPathName[3 * PATH_MAX + 4];
full_path[0] = full_path[PATH_MAX] = full_path_temp[0] = full_path_temp[PATH_MAX] = '\0';
while (node) {
if (node->remoteurl && node->remoteurl[0] != '\0') {
int nWritten = strcpy_safe(outRemoteUrl, node->remoteurl, maxBytesToWrite);
if (maxBytesToWrite - nWritten > 1 && outRemoteUrl[nWritten - 1] != '/' &&
full_path[0] != '/') {
outRemoteUrl[nWritten++] = '/';
outRemoteUrl[nWritten] = '\0';
}
strcat(full_path + strlen(full_path), trailing_path);
uriEncode(uriEncodedPathName, 3 * PATH_MAX + 4, full_path);
strcpy_safe(outRemoteUrl + nWritten,
(outRemoteUrl[nWritten - 1] == '/' && uriEncodedPathName[0] == '/')
? (uriEncodedPathName + 1)
: uriEncodedPathName,
maxBytesToWrite - nWritten);
return;
}
strcpy_safe(full_path_temp, full_path, PATH_MAX);
int nWritten = strcpy_safe(full_path, node->name, PATH_MAX);
if (full_path_temp[0] != '\0') {
full_path[nWritten++] = '/';
full_path[nWritten] = '\0';
strcpy_safe(full_path + nWritten, full_path_temp, PATH_MAX - nWritten);
}
node = node->parent;
}
strcat(full_path + strlen(full_path), trailing_path);
uriEncode(uriEncodedPathName, 3 * PATH_MAX + 4, full_path);
strcpy_safe(outRemoteUrl, uriEncodedPathName, maxBytesToWrite);
}
// Debug function that dumps out the filesystem tree to console.
void emscripten_dump_fs_tree(inode* root, char* path) {
char str[256];
sprintf(str, "%s:", path);
EM_ASM(out(UTF8ToString($0)), str);
// Print out:
// file mode | number of links | owner name | group name | file size in bytes | file last modified
// time | path name which aligns with "ls -AFTRl" on console
inode* child = root->child;
uint64_t totalSize = 0;
while (child) {
sprintf(str, "%c%c%c%c%c%c%c%c%c%c %d user%u group%u %lu Jan 1 1970 %s%c",
child->type == INODE_DIR ? 'd' : '-', (child->mode & S_IRUSR) ? 'r' : '-',
(child->mode & S_IWUSR) ? 'w' : '-', (child->mode & S_IXUSR) ? 'x' : '-',
(child->mode & S_IRGRP) ? 'r' : '-', (child->mode & S_IWGRP) ? 'w' : '-',
(child->mode & S_IXGRP) ? 'x' : '-', (child->mode & S_IROTH) ? 'r' : '-',
(child->mode & S_IWOTH) ? 'w' : '-', (child->mode & S_IXOTH) ? 'x' : '-',
1, // number of links to this file
child->uid, child->gid,
child->size ? child->size : (child->fetch ? (int)child->fetch->numBytes : 0), child->name,
child->type == INODE_DIR ? '/' : ' ');
EM_ASM(out(UTF8ToString($0)), str);
totalSize += child->size;
child = child->sibling;
}
sprintf(str, "total %llu bytes\n", totalSize);
EM_ASM(out(UTF8ToString($0)), str);
child = root->child;
char* path_end = path + strlen(path);
while (child) {
if (child->type == INODE_DIR) {
strcpy(path_end, child->name);
strcat(path_end, "/");
emscripten_dump_fs_tree(child, path);
}
child = child->sibling;
}
}
void emscripten_asmfs_dump() {
EM_ASM({err('emscripten_asmfs_dump()')});
char path[PATH_MAX] = "/";
emscripten_dump_fs_tree(filesystem_root(), path);
}
void emscripten_asmfs_discard_tree(const char* path) {
#ifdef ASMFS_DEBUG
emscripten_asmfs_dump();
EM_ASM(err('emscripten_asmfs_discard_tree: ' + UTF8ToString($0)), path);
#endif
int err;
inode* node = find_inode(path, &err);
if (node && !err) {
unlink_inode(node);
delete_inode_tree(node);
}
#ifdef ASMFS_DEBUG
else
EM_ASM(err('emscripten_asmfs_discard_tree failed, error ' + $0), err);
emscripten_asmfs_dump();
#endif
}
#ifdef ASMFS_DEBUG
#define RETURN_ERRNO(errno, error_reason) \
do { \
EM_ASM(err(UTF8ToString($0) + '() returned errno ' + #errno + '(' + $1 + '): ' + \
error_reason + '!'), \
__FUNCTION__, errno); \
return -errno; \
} while (0)
#else
#define RETURN_ERRNO(errno, error_reason) \
do { \
return -(errno); \
} while (0)
#endif
static char stdout_buffer[4096] = {};
static int stdout_buffer_end = 0;
static char stderr_buffer[4096] = {};
static int stderr_buffer_end = 0;
static void print_stream(void* bytes, int numBytes, bool stdout) {
char* buffer = stdout ? stdout_buffer : stderr_buffer;
int& buffer_end = stdout ? stdout_buffer_end : stderr_buffer_end;
memcpy(buffer + buffer_end, bytes, numBytes);
buffer_end += numBytes;
int new_buffer_start = 0;
for (int i = 0; i < buffer_end; ++i) {
if (buffer[i] == '\n') {
buffer[i] = 0;
EM_ASM_INT({out(UTF8ToString($0))}, buffer + new_buffer_start);
new_buffer_start = i + 1;
}
}
size_t new_buffer_size = buffer_end - new_buffer_start;
memmove(buffer, buffer + new_buffer_start, new_buffer_size);
buffer_end = new_buffer_size;
}
// TODO: Make thread-local storage.
static emscripten_asmfs_open_t __emscripten_asmfs_file_open_behavior_mode =
EMSCRIPTEN_ASMFS_OPEN_REMOTE_DISCOVER;
void emscripten_asmfs_set_file_open_behavior(emscripten_asmfs_open_t behavior) {
__emscripten_asmfs_file_open_behavior_mode = behavior;
}
emscripten_asmfs_open_t emscripten_asmfs_get_file_open_behavior() {
return __emscripten_asmfs_file_open_behavior_mode;
}
// Returns true if the given file can be synchronously read by the main browser thread.
static bool emscripten_asmfs_file_is_synchronously_accessible(inode* node) {
return node->data // If file was created from memory without XHR, e.g. via fopen("foo.txt", "w"),
// it will have node->data ptr backing.
||
(node->fetch && node->fetch->data); // If the file was downloaded, it will be backed here.
}
static long open(const char* pathname, int flags, int mode) {
#ifdef ASMFS_DEBUG
EM_ASM(err('open(pathname="' + UTF8ToString($0) + '", flags=0x' + ($1).toString(16) + ', mode=0' +
($2).toString(8) + ')'),
pathname, flags, mode);
#endif
int accessMode = (flags & O_ACCMODE);
if ((flags & O_ASYNC))
RETURN_ERRNO(ENOTSUP, "TODO: Opening files with O_ASYNC flag is not supported in ASMFS");
if ((flags & O_DIRECT))
RETURN_ERRNO(ENOTSUP, "TODO: O_DIRECT flag is not supported in ASMFS");
if ((flags & O_DSYNC))
RETURN_ERRNO(ENOTSUP, "TODO: O_DSYNC flag is not supported in ASMFS");
// Spec says that the result of O_EXCL without O_CREAT is undefined.
// We could enforce it as an error condition, as follows:
// if ((flags & O_EXCL) && !(flags & O_CREAT)) RETURN_ERRNO(EINVAL, "open() with O_EXCL flag
//needs to always be paired with O_CREAT");
// However existing earlier unit tests in Emscripten expect that O_EXCL is simply ignored when
// O_CREAT was not passed. So do that for now.
if ((flags & O_EXCL) && !(flags & O_CREAT)) {
#ifdef ASMFS_DEBUG
EM_ASM(err('warning: open(pathname="' + UTF8ToString($0) + '", flags=0x' + ($1).toString(16) +
', mode=0' + ($2).toString(8) +
': flag O_EXCL should always be paired with O_CREAT. Ignoring O_EXCL)'),
pathname, flags, mode);
#endif
flags &= ~O_EXCL;
}
if ((flags & (O_NONBLOCK | O_NDELAY)))
RETURN_ERRNO(
ENOTSUP, "TODO: Opening files with O_NONBLOCK or O_NDELAY flags is not supported in ASMFS");
if ((flags & O_PATH))
RETURN_ERRNO(ENOTSUP, "TODO: Opening files with O_PATH flag is not supported in ASMFS");
if ((flags & O_SYNC))
RETURN_ERRNO(ENOTSUP, "TODO: Opening files with O_SYNC flag is not supported in ASMFS");
// The flags:O_CLOEXEC flag is ignored, doesn't have meaning for Emscripten
// TODO: the flags:O_DIRECT flag seems like a great way to let applications explicitly control
// XHR/IndexedDB read/write buffering behavior?
// The flags:O_LARGEFILE flag is ignored, we should always be largefile-compatible
// TODO: The flags:O_NOATIME is ignored, file access times have not been implemented yet
// The flags O_NOCTTY, O_NOFOLLOW
if ((flags & O_TMPFILE)) {
if (accessMode != O_WRONLY && accessMode != O_RDWR)
RETURN_ERRNO(
EINVAL, "O_TMPFILE was specified in flags, but neither O_WRONLY nor O_RDWR was specified");
else
RETURN_ERRNO(
EOPNOTSUPP, "TODO: The filesystem containing pathname does not support O_TMPFILE");
}
// TODO: if (too_many_files_open) RETURN_ERRNO(EMFILE, "The per-process limit on the number of
// open file descriptors has been reached, see getrlimit(RLIMIT_NOFILE)");
int len = strlen(pathname);
if (len > MAX_PATHNAME_LENGTH)
RETURN_ERRNO(ENAMETOOLONG, "pathname was too long");
if (len == 0)
RETURN_ERRNO(ENOENT, "pathname is empty");
// Find if this file exists already in the filesystem?
inode* root = (pathname[0] == '/') ? filesystem_root() : get_cwd();
const char* relpath = (pathname[0] == '/') ? pathname + 1 : pathname;
int err;
inode* node = find_inode(root, relpath, &err);
if (err == ENOTDIR)
RETURN_ERRNO(
ENOTDIR, "A component used as a directory in pathname is not, in fact, a directory");
if (err == ELOOP)
RETURN_ERRNO(ELOOP, "Too many symbolic links were encountered in resolving pathname");
if (err == EACCES)
RETURN_ERRNO(EACCES,
"Search permission is denied for one of the directories in the path prefix of pathname");
if (err && err != ENOENT)
RETURN_ERRNO(err, "find_inode() error");
if (node) {
if ((flags & O_DIRECTORY) && node->type != INODE_DIR)
RETURN_ERRNO(ENOTDIR, "O_DIRECTORY was specified and pathname was not a directory");
if (!(node->mode & 0444))
RETURN_ERRNO(EACCES, "The requested access to the file is not allowed");
if ((flags & O_CREAT) && (flags & O_EXCL))
RETURN_ERRNO(EEXIST, "pathname already exists and O_CREAT and O_EXCL were used");
if (node->type == INODE_DIR && accessMode != O_RDONLY)
RETURN_ERRNO(EISDIR, "pathname refers to a directory and the access requested involved writing (that is, O_WRONLY or O_RDWR is set)");
if (node->type == INODE_DIR && (flags & O_TRUNC))
RETURN_ERRNO(EISDIR,
"pathname refers to a directory and the access flags specified invalid flag O_TRUNC");
// A current download exists to the file? Then wait for it to complete.
if (node->fetch) {
// On the main thread, the fetch must have already completed before we come here. If not, we
// cannot stop to wait for it to finish, and must return a failure (file not found)
if (emscripten_is_main_browser_thread()) {
if (emscripten_fetch_wait(node->fetch, 0) != EMSCRIPTEN_RESULT_SUCCESS) {
RETURN_ERRNO(ENOENT, "Attempted to open a file that is still downloading on the main browser thread. Could not block to wait! (try preloading the file to the filesystem before application start)");
}
} else {
// On worker threads, we can pause to wait for the fetch.
emscripten_fetch_wait(node->fetch, INFINITY);
}
}
}
if ((flags & O_CREAT) && ((flags & O_TRUNC) || (flags & O_EXCL))) {
// Create a new empty file or truncate existing one.
if (node) {
if (node->fetch)
emscripten_fetch_close(node->fetch);
node->fetch = 0;
node->size = 0;
} else if ((flags & O_CREAT)) {
inode* directory = create_directory_hierarchy_for_file(root, relpath, mode);
node = create_inode((flags & O_DIRECTORY) ? INODE_DIR : INODE_FILE, mode);
strcpy(node->name, basename_part(pathname));
link_inode(node, directory);
}
} else if (!node || (node->type == INODE_FILE && !node->fetch && !node->data)) {
emscripten_fetch_t* fetch = 0;
if (!(flags & O_DIRECTORY) && accessMode != O_WRONLY) // Opening a file for reading?
{
// If there's no inode entry, check if we're not even interested in downloading the file?
if (!node &&
__emscripten_asmfs_file_open_behavior_mode != EMSCRIPTEN_ASMFS_OPEN_REMOTE_DISCOVER) {
RETURN_ERRNO(
ENOENT, "O_CREAT is not set, the named file does not exist in local filesystem and EMSCRIPTEN_ASMFS_OPEN_REMOTE_DISCOVER is not specified");
}
// Report an error if there is an inode entry, but file data is not synchronously available
// and it should have been.
if (node && !node->data &&
__emscripten_asmfs_file_open_behavior_mode == EMSCRIPTEN_ASMFS_OPEN_MEMORY) {
RETURN_ERRNO(
ENOENT, "O_CREAT is not set, the named file exists, but file data is not synchronously available in memory (EMSCRIPTEN_ASMFS_OPEN_MEMORY specified)");
}
if (emscripten_is_main_browser_thread() &&
(!node || !emscripten_asmfs_file_is_synchronously_accessible(node))) {
RETURN_ERRNO(ENOENT,
"O_CREAT is not set, the named file exists, but file data is not synchronously available in memory, and file open is attempted on the main thread which cannot synchronously open files! (try preloading the file to the filesystem before application start)");
}
// Kick off the file download, either from IndexedDB or via an XHR.
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_APPEND | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY |
EMSCRIPTEN_FETCH_WAITABLE | EMSCRIPTEN_FETCH_PERSIST_FILE;
// If asked to only do a read from IndexedDB, don't perform an XHR.
if (__emscripten_asmfs_file_open_behavior_mode == EMSCRIPTEN_ASMFS_OPEN_INDEXEDDB) {
attr.attributes |= EMSCRIPTEN_FETCH_NO_DOWNLOAD;
}