-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathuvwasi.c
2654 lines (2209 loc) · 73.7 KB
/
uvwasi.c
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 <stdlib.h>
#include <string.h>
#ifndef _WIN32
# include <sched.h>
# include <sys/types.h>
# include <unistd.h>
# include <dirent.h>
# include <time.h>
#else
# include <io.h>
#endif /* _WIN32 */
#define UVWASI__READDIR_NUM_ENTRIES 1
#if !defined(_WIN32) && !defined(__ANDROID__)
# define UVWASI_FD_READDIR_SUPPORTED 1
#endif
#include "uvwasi.h"
#include "uvwasi_alloc.h"
#include "uv.h"
#include "uv_mapping.h"
#include "fd_table.h"
#include "clocks.h"
#include "path_resolver.h"
#include "poll_oneoff.h"
#include "wasi_rights.h"
#include "wasi_serdes.h"
#include "debug.h"
/* IBMi PASE does not support posix_fadvise() */
#ifdef __PASE__
# undef POSIX_FADV_NORMAL
#endif
#define VALIDATE_FSTFLAGS_OR_RETURN(flags) \
do { \
if ((flags) & ~(UVWASI_FILESTAT_SET_ATIM | \
UVWASI_FILESTAT_SET_ATIM_NOW | \
UVWASI_FILESTAT_SET_MTIM | \
UVWASI_FILESTAT_SET_MTIM_NOW)) { \
return UVWASI_EINVAL; \
} \
} while (0)
static uvwasi_errno_t uvwasi__get_filestat_set_times(
uvwasi_timestamp_t* st_atim,
uvwasi_timestamp_t* st_mtim,
uvwasi_fstflags_t fst_flags,
uv_file* fd,
char* path
) {
uvwasi_filestat_t stat;
uvwasi_timestamp_t now;
uvwasi_errno_t err;
uv_fs_t req;
int r;
/* Check if either value requires the current time. */
if ((fst_flags &
(UVWASI_FILESTAT_SET_ATIM_NOW | UVWASI_FILESTAT_SET_MTIM_NOW)) != 0) {
err = uvwasi__clock_gettime_realtime(&now);
if (err != UVWASI_ESUCCESS)
return err;
}
/* Check if either value is omitted. libuv doesn't have an 'omitted' option,
so get the current stats for the file. This approach isn't perfect, but it
will do until libuv can get better support here. */
if ((fst_flags &
(UVWASI_FILESTAT_SET_ATIM | UVWASI_FILESTAT_SET_ATIM_NOW)) == 0 ||
(fst_flags &
(UVWASI_FILESTAT_SET_MTIM | UVWASI_FILESTAT_SET_MTIM_NOW)) == 0) {
if (fd != NULL)
r = uv_fs_fstat(NULL, &req, *fd, NULL);
else
r = uv_fs_lstat(NULL, &req, path, NULL);
if (r != 0) {
uv_fs_req_cleanup(&req);
return uvwasi__translate_uv_error(r);
}
uvwasi__stat_to_filestat(&req.statbuf, &stat);
uv_fs_req_cleanup(&req);
}
/* Choose the provided time or 'now' and convert WASI timestamps from
nanoseconds to seconds due to libuv. */
if ((fst_flags & UVWASI_FILESTAT_SET_ATIM_NOW) != 0)
*st_atim = now / NANOS_PER_SEC;
else if ((fst_flags & UVWASI_FILESTAT_SET_ATIM) != 0)
*st_atim = *st_atim / NANOS_PER_SEC;
else
*st_atim = stat.st_atim / NANOS_PER_SEC;
if ((fst_flags & UVWASI_FILESTAT_SET_MTIM_NOW) != 0)
*st_mtim = now / NANOS_PER_SEC;
else if ((fst_flags & UVWASI_FILESTAT_SET_MTIM) != 0)
*st_mtim = *st_mtim / NANOS_PER_SEC;
else
*st_mtim = stat.st_mtim / NANOS_PER_SEC;
return UVWASI_ESUCCESS;
}
static void* default_malloc(size_t size, void* mem_user_data) {
return malloc(size);
}
static void default_free(void* ptr, void* mem_user_data) {
free(ptr);
}
static void* default_calloc(size_t nmemb, size_t size, void* mem_user_data) {
return calloc(nmemb, size);
}
static void* default_realloc(void* ptr, size_t size, void* mem_user_data) {
return realloc(ptr, size);
}
void* uvwasi__malloc(const uvwasi_t* uvwasi, size_t size) {
return uvwasi->allocator->malloc(size, uvwasi->allocator->mem_user_data);
}
void uvwasi__free(const uvwasi_t* uvwasi, void* ptr) {
if (ptr == NULL)
return;
uvwasi->allocator->free(ptr, uvwasi->allocator->mem_user_data);
}
void* uvwasi__calloc(const uvwasi_t* uvwasi, size_t nmemb, size_t size) {
return uvwasi->allocator->calloc(nmemb,
size,
uvwasi->allocator->mem_user_data);
}
void* uvwasi__realloc(const uvwasi_t* uvwasi, void* ptr, size_t size) {
return uvwasi->allocator->realloc(ptr,
size,
uvwasi->allocator->mem_user_data);
}
static const uvwasi_mem_t default_allocator = {
NULL,
default_malloc,
default_free,
default_calloc,
default_realloc,
};
static uvwasi_errno_t uvwasi__lseek(uv_file fd,
uvwasi_filedelta_t offset,
uvwasi_whence_t whence,
uvwasi_filesize_t* newoffset) {
int real_whence;
if (whence == UVWASI_WHENCE_CUR)
real_whence = SEEK_CUR;
else if (whence == UVWASI_WHENCE_END)
real_whence = SEEK_END;
else if (whence == UVWASI_WHENCE_SET)
real_whence = SEEK_SET;
else
return UVWASI_EINVAL;
#ifdef _WIN32
int64_t r;
r = _lseeki64(fd, offset, real_whence);
if (-1L == r)
return uvwasi__translate_uv_error(uv_translate_sys_error(errno));
#else
off_t r;
r = lseek(fd, offset, real_whence);
if ((off_t) -1 == r)
return uvwasi__translate_uv_error(uv_translate_sys_error(errno));
#endif /* _WIN32 */
*newoffset = r;
return UVWASI_ESUCCESS;
}
static uvwasi_errno_t uvwasi__setup_iovs(const uvwasi_t* uvwasi,
uv_buf_t** buffers,
const uvwasi_iovec_t* iovs,
uvwasi_size_t iovs_len) {
uv_buf_t* bufs;
uvwasi_size_t i;
if ((iovs_len * sizeof(*bufs)) / (sizeof(*bufs)) != iovs_len)
return UVWASI_ENOMEM;
bufs = uvwasi__malloc(uvwasi, iovs_len * sizeof(*bufs));
if (bufs == NULL)
return UVWASI_ENOMEM;
for (i = 0; i < iovs_len; ++i)
bufs[i] = uv_buf_init(iovs[i].buf, iovs[i].buf_len);
*buffers = bufs;
return UVWASI_ESUCCESS;
}
static uvwasi_errno_t uvwasi__setup_ciovs(const uvwasi_t* uvwasi,
uv_buf_t** buffers,
const uvwasi_ciovec_t* iovs,
uvwasi_size_t iovs_len) {
uv_buf_t* bufs;
uvwasi_size_t i;
if ((iovs_len * sizeof(*bufs)) / (sizeof(*bufs)) != iovs_len)
return UVWASI_ENOMEM;
bufs = uvwasi__malloc(uvwasi, iovs_len * sizeof(*bufs));
if (bufs == NULL)
return UVWASI_ENOMEM;
for (i = 0; i < iovs_len; ++i)
bufs[i] = uv_buf_init((char*)iovs[i].buf, iovs[i].buf_len);
*buffers = bufs;
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_init(uvwasi_t* uvwasi, const uvwasi_options_t* options) {
uv_fs_t realpath_req;
uv_fs_t open_req;
uvwasi_errno_t err;
uvwasi_size_t args_size;
uvwasi_size_t size;
uvwasi_size_t offset;
uvwasi_size_t env_count;
uvwasi_size_t env_buf_size;
uvwasi_size_t i;
int r;
if (uvwasi == NULL || options == NULL || options->fd_table_size == 0)
return UVWASI_EINVAL;
uvwasi->allocator = options->allocator;
if (uvwasi->allocator == NULL)
uvwasi->allocator = &default_allocator;
uvwasi->argv_buf = NULL;
uvwasi->argv = NULL;
uvwasi->env_buf = NULL;
uvwasi->env = NULL;
uvwasi->fds = NULL;
args_size = 0;
for (i = 0; i < options->argc; ++i)
args_size += strlen(options->argv[i]) + 1;
uvwasi->argc = options->argc;
uvwasi->argv_buf_size = args_size;
if (args_size > 0) {
uvwasi->argv_buf = uvwasi__malloc(uvwasi, args_size);
if (uvwasi->argv_buf == NULL) {
err = UVWASI_ENOMEM;
goto exit;
}
uvwasi->argv = uvwasi__calloc(uvwasi, options->argc, sizeof(char*));
if (uvwasi->argv == NULL) {
err = UVWASI_ENOMEM;
goto exit;
}
offset = 0;
for (i = 0; i < options->argc; ++i) {
size = strlen(options->argv[i]) + 1;
memcpy(uvwasi->argv_buf + offset, options->argv[i], size);
uvwasi->argv[i] = uvwasi->argv_buf + offset;
offset += size;
}
}
env_count = 0;
env_buf_size = 0;
if (options->envp != NULL) {
while (options->envp[env_count] != NULL) {
env_buf_size += strlen(options->envp[env_count]) + 1;
env_count++;
}
}
uvwasi->envc = env_count;
uvwasi->env_buf_size = env_buf_size;
if (env_buf_size > 0) {
uvwasi->env_buf = uvwasi__malloc(uvwasi, env_buf_size);
if (uvwasi->env_buf == NULL) {
err = UVWASI_ENOMEM;
goto exit;
}
uvwasi->env = uvwasi__calloc(uvwasi, env_count, sizeof(char*));
if (uvwasi->env == NULL) {
err = UVWASI_ENOMEM;
goto exit;
}
offset = 0;
for (i = 0; i < env_count; ++i) {
size = strlen(options->envp[i]) + 1;
memcpy(uvwasi->env_buf + offset, options->envp[i], size);
uvwasi->env[i] = uvwasi->env_buf + offset;
offset += size;
}
}
for (i = 0; i < options->preopenc; ++i) {
if (options->preopens[i].real_path == NULL ||
options->preopens[i].mapped_path == NULL) {
err = UVWASI_EINVAL;
goto exit;
}
}
err = uvwasi_fd_table_init(uvwasi, options);
if (err != UVWASI_ESUCCESS)
goto exit;
for (i = 0; i < options->preopenc; ++i) {
r = uv_fs_realpath(NULL,
&realpath_req,
options->preopens[i].real_path,
NULL);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
uv_fs_req_cleanup(&realpath_req);
goto exit;
}
r = uv_fs_open(NULL, &open_req, realpath_req.ptr, 0, 0666, NULL);
if (r < 0) {
err = uvwasi__translate_uv_error(r);
uv_fs_req_cleanup(&realpath_req);
uv_fs_req_cleanup(&open_req);
goto exit;
}
err = uvwasi_fd_table_insert_preopen(uvwasi,
uvwasi->fds,
open_req.result,
options->preopens[i].mapped_path,
realpath_req.ptr);
uv_fs_req_cleanup(&realpath_req);
uv_fs_req_cleanup(&open_req);
if (err != UVWASI_ESUCCESS)
goto exit;
}
return UVWASI_ESUCCESS;
exit:
uvwasi_destroy(uvwasi);
return err;
}
void uvwasi_destroy(uvwasi_t* uvwasi) {
if (uvwasi == NULL)
return;
uvwasi_fd_table_free(uvwasi, uvwasi->fds);
uvwasi__free(uvwasi, uvwasi->argv_buf);
uvwasi__free(uvwasi, uvwasi->argv);
uvwasi__free(uvwasi, uvwasi->env_buf);
uvwasi__free(uvwasi, uvwasi->env);
uvwasi->fds = NULL;
uvwasi->argv_buf = NULL;
uvwasi->argv = NULL;
uvwasi->env_buf = NULL;
uvwasi->env = NULL;
}
void uvwasi_options_init(uvwasi_options_t* options) {
if (options == NULL)
return;
options->in = 0;
options->out = 1;
options->err = 2;
options->fd_table_size = 3;
options->argc = 0;
options->argv = NULL;
options->envp = NULL;
options->preopenc = 0;
options->preopens = NULL;
options->allocator = NULL;
}
uvwasi_errno_t uvwasi_embedder_remap_fd(uvwasi_t* uvwasi,
const uvwasi_fd_t fd,
uv_file new_host_fd) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
if (uvwasi == NULL)
return UVWASI_EINVAL;
err = uvwasi_fd_table_get(uvwasi->fds, fd, &wrap, 0, 0);
if (err != UVWASI_ESUCCESS)
return err;
wrap->fd = new_host_fd;
uv_mutex_unlock(&wrap->mutex);
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_args_get(uvwasi_t* uvwasi, char** argv, char* argv_buf) {
uvwasi_size_t i;
UVWASI_DEBUG("uvwasi_args_get(uvwasi=%p, argv=%p, argv_buf=%p)\n",
uvwasi,
argv,
argv_buf);
if (uvwasi == NULL || argv == NULL || argv_buf == NULL)
return UVWASI_EINVAL;
for (i = 0; i < uvwasi->argc; ++i) {
argv[i] = argv_buf + (uvwasi->argv[i] - uvwasi->argv_buf);
}
memcpy(argv_buf, uvwasi->argv_buf, uvwasi->argv_buf_size);
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_args_sizes_get(uvwasi_t* uvwasi,
uvwasi_size_t* argc,
uvwasi_size_t* argv_buf_size) {
UVWASI_DEBUG("uvwasi_args_sizes_get(uvwasi=%p, argc=%p, argv_buf_size=%p)\n",
uvwasi,
argc,
argv_buf_size);
if (uvwasi == NULL || argc == NULL || argv_buf_size == NULL)
return UVWASI_EINVAL;
*argc = uvwasi->argc;
*argv_buf_size = uvwasi->argv_buf_size;
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_clock_res_get(uvwasi_t* uvwasi,
uvwasi_clockid_t clock_id,
uvwasi_timestamp_t* resolution) {
UVWASI_DEBUG("uvwasi_clock_res_get(uvwasi=%p, clock_id=%d, resolution=%p)\n",
uvwasi,
clock_id,
resolution);
if (uvwasi == NULL || resolution == NULL)
return UVWASI_EINVAL;
switch (clock_id) {
case UVWASI_CLOCK_MONOTONIC:
case UVWASI_CLOCK_REALTIME:
*resolution = 1; /* Nanosecond precision. */
return UVWASI_ESUCCESS;
case UVWASI_CLOCK_PROCESS_CPUTIME_ID:
return uvwasi__clock_getres_process_cputime(resolution);
case UVWASI_CLOCK_THREAD_CPUTIME_ID:
return uvwasi__clock_getres_thread_cputime(resolution);
default:
return UVWASI_EINVAL;
}
}
uvwasi_errno_t uvwasi_clock_time_get(uvwasi_t* uvwasi,
uvwasi_clockid_t clock_id,
uvwasi_timestamp_t precision,
uvwasi_timestamp_t* time) {
UVWASI_DEBUG("uvwasi_clock_time_get(uvwasi=%p, clock_id=%d, "
"precision=%"PRIu64", time=%p)\n",
uvwasi,
clock_id,
precision,
time);
if (uvwasi == NULL || time == NULL)
return UVWASI_EINVAL;
switch (clock_id) {
case UVWASI_CLOCK_MONOTONIC:
*time = uv_hrtime();
return UVWASI_ESUCCESS;
case UVWASI_CLOCK_REALTIME:
return uvwasi__clock_gettime_realtime(time);
case UVWASI_CLOCK_PROCESS_CPUTIME_ID:
return uvwasi__clock_gettime_process_cputime(time);
case UVWASI_CLOCK_THREAD_CPUTIME_ID:
return uvwasi__clock_gettime_thread_cputime(time);
default:
return UVWASI_EINVAL;
}
}
uvwasi_errno_t uvwasi_environ_get(uvwasi_t* uvwasi,
char** environment,
char* environ_buf) {
uvwasi_size_t i;
UVWASI_DEBUG("uvwasi_environ_get(uvwasi=%p, environment=%p, "
"environ_buf=%p)\n",
uvwasi,
environment,
environ_buf);
if (uvwasi == NULL || environment == NULL || environ_buf == NULL)
return UVWASI_EINVAL;
for (i = 0; i < uvwasi->envc; ++i) {
environment[i] = environ_buf + (uvwasi->env[i] - uvwasi->env_buf);
}
memcpy(environ_buf, uvwasi->env_buf, uvwasi->env_buf_size);
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_environ_sizes_get(uvwasi_t* uvwasi,
uvwasi_size_t* environ_count,
uvwasi_size_t* environ_buf_size) {
UVWASI_DEBUG("uvwasi_environ_sizes_get(uvwasi=%p, environ_count=%p, "
"environ_buf_size=%p)\n",
uvwasi,
environ_count,
environ_buf_size);
if (uvwasi == NULL || environ_count == NULL || environ_buf_size == NULL)
return UVWASI_EINVAL;
*environ_count = uvwasi->envc;
*environ_buf_size = uvwasi->env_buf_size;
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_fd_advise(uvwasi_t* uvwasi,
uvwasi_fd_t fd,
uvwasi_filesize_t offset,
uvwasi_filesize_t len,
uvwasi_advice_t advice) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
#ifdef POSIX_FADV_NORMAL
int mapped_advice;
int r;
#endif /* POSIX_FADV_NORMAL */
UVWASI_DEBUG("uvwasi_fd_advise(uvwasi=%p, fd=%d, offset=%"PRIu64", "
"len=%"PRIu64", advice=%d)\n",
uvwasi,
fd,
offset,
len,
advice);
if (uvwasi == NULL)
return UVWASI_EINVAL;
switch (advice) {
case UVWASI_ADVICE_DONTNEED:
#ifdef POSIX_FADV_NORMAL
mapped_advice = POSIX_FADV_DONTNEED;
#endif /* POSIX_FADV_NORMAL */
break;
case UVWASI_ADVICE_NOREUSE:
#ifdef POSIX_FADV_NORMAL
mapped_advice = POSIX_FADV_NOREUSE;
#endif /* POSIX_FADV_NORMAL */
break;
case UVWASI_ADVICE_NORMAL:
#ifdef POSIX_FADV_NORMAL
mapped_advice = POSIX_FADV_NORMAL;
#endif /* POSIX_FADV_NORMAL */
break;
case UVWASI_ADVICE_RANDOM:
#ifdef POSIX_FADV_NORMAL
mapped_advice = POSIX_FADV_RANDOM;
#endif /* POSIX_FADV_NORMAL */
break;
case UVWASI_ADVICE_SEQUENTIAL:
#ifdef POSIX_FADV_NORMAL
mapped_advice = POSIX_FADV_SEQUENTIAL;
#endif /* POSIX_FADV_NORMAL */
break;
case UVWASI_ADVICE_WILLNEED:
#ifdef POSIX_FADV_NORMAL
mapped_advice = POSIX_FADV_WILLNEED;
#endif /* POSIX_FADV_NORMAL */
break;
default:
return UVWASI_EINVAL;
}
err = uvwasi_fd_table_get(uvwasi->fds, fd, &wrap, UVWASI_RIGHT_FD_ADVISE, 0);
if (err != UVWASI_ESUCCESS)
return err;
err = UVWASI_ESUCCESS;
#ifdef POSIX_FADV_NORMAL
r = posix_fadvise(wrap->fd, offset, len, mapped_advice);
if (r != 0)
err = uvwasi__translate_uv_error(uv_translate_sys_error(r));
#endif /* POSIX_FADV_NORMAL */
uv_mutex_unlock(&wrap->mutex);
return err;
}
uvwasi_errno_t uvwasi_fd_allocate(uvwasi_t* uvwasi,
uvwasi_fd_t fd,
uvwasi_filesize_t offset,
uvwasi_filesize_t len) {
#if !defined(__POSIX__)
uv_fs_t req;
uint64_t st_size;
#endif /* !__POSIX__ */
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
int r;
UVWASI_DEBUG("uvwasi_fd_allocate(uvwasi=%p, fd=%d, offset=%"PRIu64", "
"len=%"PRIu64")\n",
uvwasi,
fd,
offset,
len);
if (uvwasi == NULL)
return UVWASI_EINVAL;
err = uvwasi_fd_table_get(uvwasi->fds,
fd,
&wrap,
UVWASI_RIGHT_FD_ALLOCATE,
0);
if (err != UVWASI_ESUCCESS)
return err;
/* Try to use posix_fallocate(). If that's not an option, fall back to the
race condition prone combination of fstat() + ftruncate(). */
#if defined(__POSIX__)
r = posix_fallocate(wrap->fd, offset, len);
if (r != 0) {
err = uvwasi__translate_uv_error(uv_translate_sys_error(r));
goto exit;
}
#else
r = uv_fs_fstat(NULL, &req, wrap->fd, NULL);
st_size = req.statbuf.st_size;
uv_fs_req_cleanup(&req);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
goto exit;
}
if (st_size < offset + len) {
r = uv_fs_ftruncate(NULL, &req, wrap->fd, offset + len, NULL);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
goto exit;
}
}
#endif /* __POSIX__ */
err = UVWASI_ESUCCESS;
exit:
uv_mutex_unlock(&wrap->mutex);
return err;
}
uvwasi_errno_t uvwasi_fd_close(uvwasi_t* uvwasi, uvwasi_fd_t fd) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
uv_fs_t req;
int r;
UVWASI_DEBUG("uvwasi_fd_close(uvwasi=%p, fd=%d)\n", uvwasi, fd);
if (uvwasi == NULL)
return UVWASI_EINVAL;
uvwasi_fd_table_lock(uvwasi->fds);
err = uvwasi_fd_table_get_nolock(uvwasi->fds, fd, &wrap, 0, 0);
if (err != UVWASI_ESUCCESS)
goto exit;
r = uv_fs_close(NULL, &req, wrap->fd, NULL);
uv_mutex_unlock(&wrap->mutex);
uv_fs_req_cleanup(&req);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
goto exit;
}
err = uvwasi_fd_table_remove_nolock(uvwasi, uvwasi->fds, fd);
exit:
uvwasi_fd_table_unlock(uvwasi->fds);
return err;
}
uvwasi_errno_t uvwasi_fd_datasync(uvwasi_t* uvwasi, uvwasi_fd_t fd) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
uv_fs_t req;
int r;
UVWASI_DEBUG("uvwasi_fd_datasync(uvwasi=%p, fd=%d)\n", uvwasi, fd);
if (uvwasi == NULL)
return UVWASI_EINVAL;
err = uvwasi_fd_table_get(uvwasi->fds,
fd,
&wrap,
UVWASI_RIGHT_FD_DATASYNC,
0);
if (err != UVWASI_ESUCCESS)
return err;
r = uv_fs_fdatasync(NULL, &req, wrap->fd, NULL);
uv_mutex_unlock(&wrap->mutex);
uv_fs_req_cleanup(&req);
if (r != 0)
return uvwasi__translate_uv_error(r);
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_fd_fdstat_get(uvwasi_t* uvwasi,
uvwasi_fd_t fd,
uvwasi_fdstat_t* buf) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
#ifndef _WIN32
int r;
#endif
UVWASI_DEBUG("uvwasi_fd_fdstat_get(uvwasi=%p, fd=%d, buf=%p)\n",
uvwasi,
fd,
buf);
if (uvwasi == NULL || buf == NULL)
return UVWASI_EINVAL;
err = uvwasi_fd_table_get(uvwasi->fds, fd, &wrap, 0, 0);
if (err != UVWASI_ESUCCESS)
return err;
buf->fs_filetype = wrap->type;
buf->fs_rights_base = wrap->rights_base;
buf->fs_rights_inheriting = wrap->rights_inheriting;
#ifdef _WIN32
buf->fs_flags = 0; /* TODO(cjihrig): Missing Windows support. */
#else
r = fcntl(wrap->fd, F_GETFL);
if (r < 0) {
err = uvwasi__translate_uv_error(uv_translate_sys_error(errno));
uv_mutex_unlock(&wrap->mutex);
return err;
}
buf->fs_flags = r;
#endif /* _WIN32 */
uv_mutex_unlock(&wrap->mutex);
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_fd_fdstat_set_flags(uvwasi_t* uvwasi,
uvwasi_fd_t fd,
uvwasi_fdflags_t flags) {
#ifdef _WIN32
UVWASI_DEBUG("uvwasi_fd_fdstat_set_flags(uvwasi=%p, fd=%d, flags=%d)\n",
uvwasi,
fd,
flags);
/* TODO(cjihrig): Windows is not supported. */
return UVWASI_ENOSYS;
#else
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
int mapped_flags;
int r;
UVWASI_DEBUG("uvwasi_fd_fdstat_set_flags(uvwasi=%p, fd=%d, flags=%d)\n",
uvwasi,
fd,
flags);
if (uvwasi == NULL)
return UVWASI_EINVAL;
err = uvwasi_fd_table_get(uvwasi->fds,
fd,
&wrap,
UVWASI_RIGHT_FD_FDSTAT_SET_FLAGS,
0);
if (err != UVWASI_ESUCCESS)
return err;
mapped_flags = 0;
if ((flags & UVWASI_FDFLAG_APPEND) == UVWASI_FDFLAG_APPEND)
mapped_flags |= O_APPEND;
if ((flags & UVWASI_FDFLAG_DSYNC) == UVWASI_FDFLAG_DSYNC)
#ifdef O_DSYNC
mapped_flags |= O_DSYNC;
#else
mapped_flags |= O_SYNC;
#endif /* O_DSYNC */
if ((flags & UVWASI_FDFLAG_NONBLOCK) == UVWASI_FDFLAG_NONBLOCK)
mapped_flags |= O_NONBLOCK;
if ((flags & UVWASI_FDFLAG_RSYNC) == UVWASI_FDFLAG_RSYNC)
#ifdef O_RSYNC
mapped_flags |= O_RSYNC;
#else
mapped_flags |= O_SYNC;
#endif /* O_RSYNC */
if ((flags & UVWASI_FDFLAG_SYNC) == UVWASI_FDFLAG_SYNC)
mapped_flags |= O_SYNC;
r = fcntl(wrap->fd, F_SETFL, mapped_flags);
if (r < 0)
err = uvwasi__translate_uv_error(uv_translate_sys_error(errno));
else
err = UVWASI_ESUCCESS;
uv_mutex_unlock(&wrap->mutex);
return err;
#endif /* _WIN32 */
}
uvwasi_errno_t uvwasi_fd_fdstat_set_rights(uvwasi_t* uvwasi,
uvwasi_fd_t fd,
uvwasi_rights_t fs_rights_base,
uvwasi_rights_t fs_rights_inheriting
) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_errno_t err;
UVWASI_DEBUG("uvwasi_fd_fdstat_set_rights(uvwasi=%p, fd=%d, "
"fs_rights_base=%"PRIu64", fs_rights_inheriting=%"PRIu64")\n",
uvwasi,
fd,
fs_rights_base,
fs_rights_inheriting);
if (uvwasi == NULL)
return UVWASI_EINVAL;
err = uvwasi_fd_table_get(uvwasi->fds, fd, &wrap, 0, 0);
if (err != UVWASI_ESUCCESS)
return err;
/* Check for attempts to add new permissions. */
if ((fs_rights_base | wrap->rights_base) > wrap->rights_base) {
err = UVWASI_ENOTCAPABLE;
goto exit;
}
if ((fs_rights_inheriting | wrap->rights_inheriting) >
wrap->rights_inheriting) {
err = UVWASI_ENOTCAPABLE;
goto exit;
}
wrap->rights_base = fs_rights_base;
wrap->rights_inheriting = fs_rights_inheriting;
err = UVWASI_ESUCCESS;
exit:
uv_mutex_unlock(&wrap->mutex);
return err;
}
uvwasi_errno_t uvwasi_fd_filestat_get(uvwasi_t* uvwasi,
uvwasi_fd_t fd,
uvwasi_filestat_t* buf) {
struct uvwasi_fd_wrap_t* wrap;
uv_fs_t req;
uvwasi_errno_t err;
int r;
UVWASI_DEBUG("uvwasi_fd_filestat_get(uvwasi=%p, fd=%d, buf=%p)\n",
uvwasi,
fd,
buf);
if (uvwasi == NULL || buf == NULL)
return UVWASI_EINVAL;
err = uvwasi_fd_table_get(uvwasi->fds,
fd,
&wrap,
UVWASI_RIGHT_FD_FILESTAT_GET,
0);
if (err != UVWASI_ESUCCESS)
return err;
r = uv_fs_fstat(NULL, &req, wrap->fd, NULL);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
goto exit;
}
uvwasi__stat_to_filestat(&req.statbuf, buf);
err = UVWASI_ESUCCESS;
exit:
uv_mutex_unlock(&wrap->mutex);
uv_fs_req_cleanup(&req);
return err;
}
uvwasi_errno_t uvwasi_fd_filestat_set_size(uvwasi_t* uvwasi,
uvwasi_fd_t fd,
uvwasi_filesize_t st_size) {
/* TODO(cjihrig): uv_fs_ftruncate() takes an int64_t. st_size is uint64_t. */
struct uvwasi_fd_wrap_t* wrap;
uv_fs_t req;
uvwasi_errno_t err;
int r;
UVWASI_DEBUG("uvwasi_fd_filestat_set_size(uvwasi=%p, fd=%d, "
"st_size=%"PRIu64")\n",
uvwasi,
fd,
st_size);
if (uvwasi == NULL)
return UVWASI_EINVAL;
err = uvwasi_fd_table_get(uvwasi->fds,
fd,
&wrap,
UVWASI_RIGHT_FD_FILESTAT_SET_SIZE,
0);
if (err != UVWASI_ESUCCESS)
return err;
r = uv_fs_ftruncate(NULL, &req, wrap->fd, st_size, NULL);
uv_mutex_unlock(&wrap->mutex);
uv_fs_req_cleanup(&req);
if (r != 0)
return uvwasi__translate_uv_error(r);
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_fd_filestat_set_times(uvwasi_t* uvwasi,
uvwasi_fd_t fd,
uvwasi_timestamp_t st_atim,
uvwasi_timestamp_t st_mtim,
uvwasi_fstflags_t fst_flags) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_timestamp_t atim;
uvwasi_timestamp_t mtim;
uv_fs_t req;