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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
use crate::clocks::Datetime;
use crate::runtime::{AbortOnDropJoinHandle, spawn_blocking};
use cap_fs_ext::{FileTypeExt as _, MetadataExt as _};
use fs_set_times::SystemTimeSpec;
use std::collections::hash_map;
use std::sync::Arc;
use tracing::debug;
use wasmtime::component::{HasData, Resource, ResourceTable};
use wasmtime::error::Context as _;
/// A helper struct which implements [`HasData`] for the `wasi:filesystem` APIs.
///
/// This can be useful when directly calling `add_to_linker` functions directly,
/// such as [`wasmtime_wasi::p2::bindings::filesystem::types::add_to_linker`] as
/// the `D` type parameter. See [`HasData`] for more information about the type
/// parameter's purpose.
///
/// When using this type you can skip the [`WasiFilesystemView`] trait, for
/// example.
///
/// [`wasmtime_wasi::p2::bindings::filesystem::types::add_to_linker`]: crate::p2::bindings::filesystem::types::add_to_linker
///
/// # Examples
///
/// ```
/// use wasmtime::component::{Linker, ResourceTable};
/// use wasmtime::{Engine, Result};
/// use wasmtime_wasi::filesystem::*;
///
/// struct MyStoreState {
/// table: ResourceTable,
/// filesystem: WasiFilesystemCtx,
/// }
///
/// fn main() -> Result<()> {
/// let engine = Engine::default();
/// let mut linker = Linker::new(&engine);
///
/// wasmtime_wasi::p2::bindings::filesystem::types::add_to_linker::<MyStoreState, WasiFilesystem>(
/// &mut linker,
/// |state| WasiFilesystemCtxView {
/// table: &mut state.table,
/// ctx: &mut state.filesystem,
/// },
/// )?;
/// Ok(())
/// }
/// ```
pub struct WasiFilesystem;
impl HasData for WasiFilesystem {
type Data<'a> = WasiFilesystemCtxView<'a>;
}
#[derive(Clone, Default)]
pub struct WasiFilesystemCtx {
pub(crate) allow_blocking_current_thread: bool,
pub(crate) preopens: Vec<(Dir, String)>,
}
pub struct WasiFilesystemCtxView<'a> {
pub ctx: &'a mut WasiFilesystemCtx,
pub table: &'a mut ResourceTable,
}
pub trait WasiFilesystemView: Send {
fn filesystem(&mut self) -> WasiFilesystemCtxView<'_>;
}
bitflags::bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct FilePerms: usize {
const READ = 0b1;
const WRITE = 0b10;
}
}
bitflags::bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct OpenMode: usize {
const READ = 0b1;
const WRITE = 0b10;
}
}
bitflags::bitflags! {
/// Permission bits for operating on a directory.
///
/// Directories can be limited to being readonly. This will restrict what
/// can be done with them, for example preventing creation of new files.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct DirPerms: usize {
/// This directory can be read, for example its entries can be iterated
/// over and files can be opened.
const READ = 0b1;
/// This directory can be mutated, for example by creating new files
/// within it.
const MUTATE = 0b10;
}
}
bitflags::bitflags! {
/// Flags determining the method of how paths are resolved.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct PathFlags: usize {
/// This directory can be read, for example its entries can be iterated
/// over and files can be opened.
const SYMLINK_FOLLOW = 0b1;
}
}
bitflags::bitflags! {
/// Open flags used by `open-at`.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct OpenFlags: usize {
/// Create file if it does not exist, similar to `O_CREAT` in POSIX.
const CREATE = 0b1;
/// Fail if not a directory, similar to `O_DIRECTORY` in POSIX.
const DIRECTORY = 0b10;
/// Fail if file already exists, similar to `O_EXCL` in POSIX.
const EXCLUSIVE = 0b100;
/// Truncate file to size 0, similar to `O_TRUNC` in POSIX.
const TRUNCATE = 0b1000;
}
}
bitflags::bitflags! {
/// Descriptor flags.
///
/// Note: This was called `fdflags` in earlier versions of WASI.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) struct DescriptorFlags: usize {
/// Read mode: Data can be read.
const READ = 0b1;
/// Write mode: Data can be written to.
const WRITE = 0b10;
/// Request that writes be performed according to synchronized I/O file
/// integrity completion. The data stored in the file and the file's
/// metadata are synchronized. This is similar to `O_SYNC` in POSIX.
///
/// The precise semantics of this operation have not yet been defined for
/// WASI. At this time, it should be interpreted as a request, and not a
/// requirement.
const FILE_INTEGRITY_SYNC = 0b100;
/// Request that writes be performed according to synchronized I/O data
/// integrity completion. Only the data stored in the file is
/// synchronized. This is similar to `O_DSYNC` in POSIX.
///
/// The precise semantics of this operation have not yet been defined for
/// WASI. At this time, it should be interpreted as a request, and not a
/// requirement.
const DATA_INTEGRITY_SYNC = 0b1000;
/// Requests that reads be performed at the same level of integrity
/// requested for writes. This is similar to `O_RSYNC` in POSIX.
///
/// The precise semantics of this operation have not yet been defined for
/// WASI. At this time, it should be interpreted as a request, and not a
/// requirement.
const REQUESTED_WRITE_SYNC = 0b10000;
/// Mutating directories mode: Directory contents may be mutated.
///
/// When this flag is unset on a descriptor, operations using the
/// descriptor which would create, rename, delete, modify the data or
/// metadata of filesystem objects, or obtain another handle which
/// would permit any of those, shall fail with `error-code::read-only` if
/// they would otherwise succeed.
///
/// This may only be set on directories.
const MUTATE_DIRECTORY = 0b100000;
}
}
/// Error codes returned by functions, similar to `errno` in POSIX.
/// Not all of these error codes are returned by the functions provided by this
/// API; some are used in higher-level library layers, and others are provided
/// merely for alignment with POSIX.
#[cfg_attr(
windows,
expect(dead_code, reason = "on Windows, some of these are not used")
)]
pub(crate) enum ErrorCode {
/// Permission denied, similar to `EACCES` in POSIX.
Access,
/// Connection already in progress, similar to `EALREADY` in POSIX.
Already,
/// Bad descriptor, similar to `EBADF` in POSIX.
BadDescriptor,
/// Device or resource busy, similar to `EBUSY` in POSIX.
Busy,
/// File exists, similar to `EEXIST` in POSIX.
Exist,
/// File too large, similar to `EFBIG` in POSIX.
FileTooLarge,
/// Illegal byte sequence, similar to `EILSEQ` in POSIX.
IllegalByteSequence,
/// Operation in progress, similar to `EINPROGRESS` in POSIX.
InProgress,
/// Interrupted function, similar to `EINTR` in POSIX.
Interrupted,
/// Invalid argument, similar to `EINVAL` in POSIX.
Invalid,
/// I/O error, similar to `EIO` in POSIX.
Io,
/// Is a directory, similar to `EISDIR` in POSIX.
IsDirectory,
/// Too many levels of symbolic links, similar to `ELOOP` in POSIX.
Loop,
/// Too many links, similar to `EMLINK` in POSIX.
TooManyLinks,
/// Filename too long, similar to `ENAMETOOLONG` in POSIX.
NameTooLong,
/// No such file or directory, similar to `ENOENT` in POSIX.
NoEntry,
/// Not enough space, similar to `ENOMEM` in POSIX.
InsufficientMemory,
/// No space left on device, similar to `ENOSPC` in POSIX.
InsufficientSpace,
/// Not a directory or a symbolic link to a directory, similar to `ENOTDIR` in POSIX.
NotDirectory,
/// Directory not empty, similar to `ENOTEMPTY` in POSIX.
NotEmpty,
/// Not supported, similar to `ENOTSUP` and `ENOSYS` in POSIX.
Unsupported,
/// Value too large to be stored in data type, similar to `EOVERFLOW` in POSIX.
Overflow,
/// Operation not permitted, similar to `EPERM` in POSIX.
NotPermitted,
/// Broken pipe, similar to `EPIPE` in POSIX.
Pipe,
/// Invalid seek, similar to `ESPIPE` in POSIX.
InvalidSeek,
}
fn datetime_from(t: std::time::SystemTime) -> Datetime {
// FIXME make this infallible or handle errors properly
Datetime::try_from(cap_std::time::SystemTime::from_std(t)).unwrap()
}
/// The type of a filesystem object referenced by a descriptor.
///
/// Note: This was called `filetype` in earlier versions of WASI.
pub(crate) enum DescriptorType {
/// The type of the descriptor or file is unknown or is different from
/// any of the other types specified.
Unknown,
/// The descriptor refers to a block device inode.
BlockDevice,
/// The descriptor refers to a character device inode.
CharacterDevice,
/// The descriptor refers to a directory inode.
Directory,
/// The file refers to a symbolic link inode.
SymbolicLink,
/// The descriptor refers to a regular file inode.
RegularFile,
}
impl From<cap_std::fs::FileType> for DescriptorType {
fn from(ft: cap_std::fs::FileType) -> Self {
if ft.is_dir() {
DescriptorType::Directory
} else if ft.is_symlink() {
DescriptorType::SymbolicLink
} else if ft.is_block_device() {
DescriptorType::BlockDevice
} else if ft.is_char_device() {
DescriptorType::CharacterDevice
} else if ft.is_file() {
DescriptorType::RegularFile
} else {
DescriptorType::Unknown
}
}
}
/// File attributes.
///
/// Note: This was called `filestat` in earlier versions of WASI.
pub(crate) struct DescriptorStat {
/// File type.
pub type_: DescriptorType,
/// Number of hard links to the file.
pub link_count: u64,
/// For regular files, the file size in bytes. For symbolic links, the
/// length in bytes of the pathname contained in the symbolic link.
pub size: u64,
/// Last data access timestamp.
///
/// If the `option` is none, the platform doesn't maintain an access
/// timestamp for this file.
pub data_access_timestamp: Option<Datetime>,
/// Last data modification timestamp.
///
/// If the `option` is none, the platform doesn't maintain a
/// modification timestamp for this file.
pub data_modification_timestamp: Option<Datetime>,
/// Last file status-change timestamp.
///
/// If the `option` is none, the platform doesn't maintain a
/// status-change timestamp for this file.
pub status_change_timestamp: Option<Datetime>,
}
impl From<cap_std::fs::Metadata> for DescriptorStat {
fn from(meta: cap_std::fs::Metadata) -> Self {
Self {
type_: meta.file_type().into(),
link_count: meta.nlink(),
size: meta.len(),
data_access_timestamp: meta.accessed().map(|t| datetime_from(t.into_std())).ok(),
data_modification_timestamp: meta.modified().map(|t| datetime_from(t.into_std())).ok(),
status_change_timestamp: meta.created().map(|t| datetime_from(t.into_std())).ok(),
}
}
}
/// A 128-bit hash value, split into parts because wasm doesn't have a
/// 128-bit integer type.
pub(crate) struct MetadataHashValue {
/// 64 bits of a 128-bit hash value.
pub lower: u64,
/// Another 64 bits of a 128-bit hash value.
pub upper: u64,
}
impl From<&cap_std::fs::Metadata> for MetadataHashValue {
fn from(meta: &cap_std::fs::Metadata) -> Self {
use cap_fs_ext::MetadataExt;
// Without incurring any deps, std provides us with a 64 bit hash
// function:
use std::hash::Hasher;
// Note that this means that the metadata hash (which becomes a preview1 ino) may
// change when a different rustc release is used to build this host implementation:
let mut hasher = hash_map::DefaultHasher::new();
hasher.write_u64(meta.dev());
hasher.write_u64(meta.ino());
let lower = hasher.finish();
// MetadataHashValue has a pair of 64-bit members for representing a
// single 128-bit number. However, we only have 64 bits of entropy. To
// synthesize the upper 64 bits, lets xor the lower half with an arbitrary
// constant, in this case the 64 bit integer corresponding to the IEEE
// double representation of (a number as close as possible to) pi.
// This seems better than just repeating the same bits in the upper and
// lower parts outright, which could make folks wonder if the struct was
// mangled in the ABI, or worse yet, lead to consumers of this interface
// expecting them to be equal.
let upper = lower ^ 4614256656552045848u64;
Self { lower, upper }
}
}
#[cfg(unix)]
fn from_raw_os_error(err: Option<i32>) -> Option<ErrorCode> {
use rustix::io::Errno as RustixErrno;
if err.is_none() {
return None;
}
Some(match RustixErrno::from_raw_os_error(err.unwrap()) {
RustixErrno::PIPE => ErrorCode::Pipe,
RustixErrno::PERM => ErrorCode::NotPermitted,
RustixErrno::NOENT => ErrorCode::NoEntry,
RustixErrno::NOMEM => ErrorCode::InsufficientMemory,
RustixErrno::IO => ErrorCode::Io,
RustixErrno::BADF => ErrorCode::BadDescriptor,
RustixErrno::BUSY => ErrorCode::Busy,
RustixErrno::ACCESS => ErrorCode::Access,
RustixErrno::NOTDIR => ErrorCode::NotDirectory,
RustixErrno::ISDIR => ErrorCode::IsDirectory,
RustixErrno::INVAL => ErrorCode::Invalid,
RustixErrno::EXIST => ErrorCode::Exist,
RustixErrno::FBIG => ErrorCode::FileTooLarge,
RustixErrno::NOSPC => ErrorCode::InsufficientSpace,
RustixErrno::SPIPE => ErrorCode::InvalidSeek,
RustixErrno::MLINK => ErrorCode::TooManyLinks,
RustixErrno::NAMETOOLONG => ErrorCode::NameTooLong,
RustixErrno::NOTEMPTY => ErrorCode::NotEmpty,
RustixErrno::LOOP => ErrorCode::Loop,
RustixErrno::OVERFLOW => ErrorCode::Overflow,
RustixErrno::ILSEQ => ErrorCode::IllegalByteSequence,
RustixErrno::NOTSUP => ErrorCode::Unsupported,
RustixErrno::ALREADY => ErrorCode::Already,
RustixErrno::INPROGRESS => ErrorCode::InProgress,
RustixErrno::INTR => ErrorCode::Interrupted,
// On some platforms, these have the same value as other errno values.
#[allow(unreachable_patterns, reason = "see comment")]
RustixErrno::OPNOTSUPP => ErrorCode::Unsupported,
_ => return None,
})
}
#[cfg(windows)]
fn from_raw_os_error(raw_os_error: Option<i32>) -> Option<ErrorCode> {
use windows_sys::Win32::Foundation;
Some(match raw_os_error.map(|code| code as u32) {
Some(Foundation::ERROR_FILE_NOT_FOUND) => ErrorCode::NoEntry,
Some(Foundation::ERROR_PATH_NOT_FOUND) => ErrorCode::NoEntry,
Some(Foundation::ERROR_ACCESS_DENIED) => ErrorCode::Access,
Some(Foundation::ERROR_SHARING_VIOLATION) => ErrorCode::Access,
Some(Foundation::ERROR_PRIVILEGE_NOT_HELD) => ErrorCode::NotPermitted,
Some(Foundation::ERROR_INVALID_HANDLE) => ErrorCode::BadDescriptor,
Some(Foundation::ERROR_INVALID_NAME) => ErrorCode::NoEntry,
Some(Foundation::ERROR_NOT_ENOUGH_MEMORY) => ErrorCode::InsufficientMemory,
Some(Foundation::ERROR_OUTOFMEMORY) => ErrorCode::InsufficientMemory,
Some(Foundation::ERROR_DIR_NOT_EMPTY) => ErrorCode::NotEmpty,
Some(Foundation::ERROR_NOT_READY) => ErrorCode::Busy,
Some(Foundation::ERROR_BUSY) => ErrorCode::Busy,
Some(Foundation::ERROR_NOT_SUPPORTED) => ErrorCode::Unsupported,
Some(Foundation::ERROR_FILE_EXISTS) => ErrorCode::Exist,
Some(Foundation::ERROR_BROKEN_PIPE) => ErrorCode::Pipe,
Some(Foundation::ERROR_BUFFER_OVERFLOW) => ErrorCode::NameTooLong,
Some(Foundation::ERROR_NOT_A_REPARSE_POINT) => ErrorCode::Invalid,
Some(Foundation::ERROR_NEGATIVE_SEEK) => ErrorCode::Invalid,
Some(Foundation::ERROR_DIRECTORY) => ErrorCode::NotDirectory,
Some(Foundation::ERROR_ALREADY_EXISTS) => ErrorCode::Exist,
Some(Foundation::ERROR_STOPPED_ON_SYMLINK) => ErrorCode::Loop,
Some(Foundation::ERROR_DIRECTORY_NOT_SUPPORTED) => ErrorCode::IsDirectory,
_ => return None,
})
}
impl<'a> From<&'a std::io::Error> for ErrorCode {
fn from(err: &'a std::io::Error) -> ErrorCode {
match from_raw_os_error(err.raw_os_error()) {
Some(errno) => errno,
None => {
debug!("unknown raw os error: {err}");
match err.kind() {
std::io::ErrorKind::NotFound => ErrorCode::NoEntry,
std::io::ErrorKind::PermissionDenied => ErrorCode::NotPermitted,
std::io::ErrorKind::AlreadyExists => ErrorCode::Exist,
std::io::ErrorKind::InvalidInput => ErrorCode::Invalid,
_ => ErrorCode::Io,
}
}
}
}
}
impl From<std::io::Error> for ErrorCode {
fn from(err: std::io::Error) -> ErrorCode {
ErrorCode::from(&err)
}
}
#[derive(Clone)]
pub enum Descriptor {
File(File),
Dir(Dir),
}
impl Descriptor {
pub(crate) fn file(&self) -> Result<&File, ErrorCode> {
match self {
Descriptor::File(f) => Ok(f),
Descriptor::Dir(_) => Err(ErrorCode::BadDescriptor),
}
}
pub(crate) fn dir(&self) -> Result<&Dir, ErrorCode> {
match self {
Descriptor::Dir(d) => Ok(d),
Descriptor::File(_) => Err(ErrorCode::NotDirectory),
}
}
async fn get_metadata(&self) -> std::io::Result<cap_std::fs::Metadata> {
match self {
Self::File(f) => {
// No permissions check on metadata: if opened, allowed to stat it
f.run_blocking(|f| f.metadata()).await
}
Self::Dir(d) => {
// No permissions check on metadata: if opened, allowed to stat it
d.run_blocking(|d| d.dir_metadata()).await
}
}
}
pub(crate) async fn sync_data(&self) -> Result<(), ErrorCode> {
match self {
Self::File(f) => {
match f.run_blocking(|f| f.sync_data()).await {
Ok(()) => Ok(()),
// On windows, `sync_data` uses `FileFlushBuffers` which fails with
// `ERROR_ACCESS_DENIED` if the file is not upen for writing. Ignore
// this error, for POSIX compatibility.
#[cfg(windows)]
Err(err)
if err.raw_os_error()
== Some(windows_sys::Win32::Foundation::ERROR_ACCESS_DENIED as _) =>
{
Ok(())
}
Err(err) => Err(err.into()),
}
}
Self::Dir(d) => {
d.run_blocking(|d| {
let d = d.open(std::path::Component::CurDir)?;
d.sync_data()?;
Ok(())
})
.await
}
}
}
pub(crate) async fn get_flags(&self) -> Result<DescriptorFlags, ErrorCode> {
use system_interface::fs::{FdFlags, GetSetFdFlags};
fn get_from_fdflags(flags: FdFlags) -> DescriptorFlags {
let mut out = DescriptorFlags::empty();
if flags.contains(FdFlags::DSYNC) {
out |= DescriptorFlags::REQUESTED_WRITE_SYNC;
}
if flags.contains(FdFlags::RSYNC) {
out |= DescriptorFlags::DATA_INTEGRITY_SYNC;
}
if flags.contains(FdFlags::SYNC) {
out |= DescriptorFlags::FILE_INTEGRITY_SYNC;
}
out
}
match self {
Self::File(f) => {
let flags = f.run_blocking(|f| f.get_fd_flags()).await?;
let mut flags = get_from_fdflags(flags);
if f.open_mode.contains(OpenMode::READ) {
flags |= DescriptorFlags::READ;
}
if f.open_mode.contains(OpenMode::WRITE) {
flags |= DescriptorFlags::WRITE;
}
Ok(flags)
}
Self::Dir(d) => {
let flags = d.run_blocking(|d| d.get_fd_flags()).await?;
let mut flags = get_from_fdflags(flags);
if d.open_mode.contains(OpenMode::READ) {
flags |= DescriptorFlags::READ;
}
if d.open_mode.contains(OpenMode::WRITE) {
flags |= DescriptorFlags::MUTATE_DIRECTORY;
}
Ok(flags)
}
}
}
pub(crate) async fn get_type(&self) -> Result<DescriptorType, ErrorCode> {
match self {
Self::File(f) => {
let meta = f.run_blocking(|f| f.metadata()).await?;
Ok(meta.file_type().into())
}
Self::Dir(_) => Ok(DescriptorType::Directory),
}
}
pub(crate) async fn set_times(
&self,
atim: Option<SystemTimeSpec>,
mtim: Option<SystemTimeSpec>,
) -> Result<(), ErrorCode> {
use fs_set_times::SetTimes as _;
match self {
Self::File(f) => {
if !f.perms.contains(FilePerms::WRITE) {
return Err(ErrorCode::NotPermitted);
}
f.run_blocking(|f| f.set_times(atim, mtim)).await?;
Ok(())
}
Self::Dir(d) => {
if !d.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
d.run_blocking(|d| d.set_times(atim, mtim)).await?;
Ok(())
}
}
}
pub(crate) async fn sync(&self) -> Result<(), ErrorCode> {
match self {
Self::File(f) => {
match f.run_blocking(|f| f.sync_all()).await {
Ok(()) => Ok(()),
// On windows, `sync_data` uses `FileFlushBuffers` which fails with
// `ERROR_ACCESS_DENIED` if the file is not upen for writing. Ignore
// this error, for POSIX compatibility.
#[cfg(windows)]
Err(err)
if err.raw_os_error()
== Some(windows_sys::Win32::Foundation::ERROR_ACCESS_DENIED as _) =>
{
Ok(())
}
Err(err) => Err(err.into()),
}
}
Self::Dir(d) => {
d.run_blocking(|d| {
let d = d.open(std::path::Component::CurDir)?;
d.sync_all()?;
Ok(())
})
.await
}
}
}
pub(crate) async fn stat(&self) -> Result<DescriptorStat, ErrorCode> {
match self {
Self::File(f) => {
// No permissions check on stat: if opened, allowed to stat it
let meta = f.run_blocking(|f| f.metadata()).await?;
Ok(meta.into())
}
Self::Dir(d) => {
// No permissions check on stat: if opened, allowed to stat it
let meta = d.run_blocking(|d| d.dir_metadata()).await?;
Ok(meta.into())
}
}
}
pub(crate) async fn is_same_object(&self, other: &Self) -> wasmtime::Result<bool> {
use cap_fs_ext::MetadataExt;
let meta_a = self.get_metadata().await?;
let meta_b = other.get_metadata().await?;
if meta_a.dev() == meta_b.dev() && meta_a.ino() == meta_b.ino() {
// MetadataHashValue does not derive eq, so use a pair of
// comparisons to check equality:
debug_assert_eq!(
MetadataHashValue::from(&meta_a).upper,
MetadataHashValue::from(&meta_b).upper,
);
debug_assert_eq!(
MetadataHashValue::from(&meta_a).lower,
MetadataHashValue::from(&meta_b).lower,
);
Ok(true)
} else {
// Hash collisions are possible, so don't assert the negative here
Ok(false)
}
}
pub(crate) async fn metadata_hash(&self) -> Result<MetadataHashValue, ErrorCode> {
let meta = self.get_metadata().await?;
Ok(MetadataHashValue::from(&meta))
}
}
#[derive(Clone)]
pub struct File {
/// The operating system File this struct is mediating access to.
///
/// Wrapped in an Arc because the same underlying file is used for
/// implementing the stream types. A copy is also needed for
/// `spawn_blocking`.
pub file: Arc<cap_std::fs::File>,
/// Permissions to enforce on access to the file. These permissions are
/// specified by a user of the `crate::WasiCtxBuilder`, and are
/// enforced prior to any enforced by the underlying operating system.
pub perms: FilePerms,
/// The mode the file was opened under: bits for reading, and writing.
/// Required to correctly report the DescriptorFlags, because cap-std
/// doesn't presently provide a cross-platform equivalent of reading the
/// oflags back out using fcntl.
pub open_mode: OpenMode,
allow_blocking_current_thread: bool,
}
impl File {
pub fn new(
file: cap_std::fs::File,
perms: FilePerms,
open_mode: OpenMode,
allow_blocking_current_thread: bool,
) -> Self {
Self {
file: Arc::new(file),
perms,
open_mode,
allow_blocking_current_thread,
}
}
/// Execute the blocking `body` function.
///
/// Depending on how the WasiCtx was configured, the body may either be:
/// - Executed directly on the current thread. In this case the `async`
/// signature of this method is effectively a lie and the returned
/// Future will always be immediately Ready. Or:
/// - Spawned on a background thread using [`tokio::task::spawn_blocking`]
/// and immediately awaited.
///
/// Intentionally blocking the executor thread might seem unorthodox, but is
/// not actually a problem for specific workloads. See:
/// - [`crate::WasiCtxBuilder::allow_blocking_current_thread`]
/// - [Poor performance of wasmtime file I/O maybe because tokio](https://github.com/bytecodealliance/wasmtime/issues/7973)
/// - [Implement opt-in for enabling WASI to block the current thread](https://github.com/bytecodealliance/wasmtime/pull/8190)
pub(crate) async fn run_blocking<F, R>(&self, body: F) -> R
where
F: FnOnce(&cap_std::fs::File) -> R + Send + 'static,
R: Send + 'static,
{
match self.as_blocking_file() {
Some(file) => body(file),
None => self.spawn_blocking(body).await,
}
}
pub(crate) fn spawn_blocking<F, R>(&self, body: F) -> AbortOnDropJoinHandle<R>
where
F: FnOnce(&cap_std::fs::File) -> R + Send + 'static,
R: Send + 'static,
{
let f = self.file.clone();
spawn_blocking(move || body(&f))
}
/// Returns `Some` when the current thread is allowed to block in filesystem
/// operations, and otherwise returns `None` to indicate that
/// `spawn_blocking` must be used.
pub(crate) fn as_blocking_file(&self) -> Option<&cap_std::fs::File> {
if self.allow_blocking_current_thread {
Some(&self.file)
} else {
None
}
}
/// Returns reference to the underlying [`cap_std::fs::File`]
#[cfg(feature = "p3")]
pub(crate) fn as_file(&self) -> &Arc<cap_std::fs::File> {
&self.file
}
pub(crate) async fn advise(
&self,
offset: u64,
len: u64,
advice: system_interface::fs::Advice,
) -> Result<(), ErrorCode> {
use system_interface::fs::FileIoExt as _;
self.run_blocking(move |f| f.advise(offset, len, advice))
.await?;
Ok(())
}
pub(crate) async fn set_size(&self, size: u64) -> Result<(), ErrorCode> {
if !self.perms.contains(FilePerms::WRITE) {
return Err(ErrorCode::NotPermitted);
}
self.run_blocking(move |f| f.set_len(size)).await?;
Ok(())
}
}
#[derive(Clone)]
pub struct Dir {
/// The operating system file descriptor this struct is mediating access
/// to.
///
/// Wrapped in an Arc because a copy is needed for `run_blocking`.
pub dir: Arc<cap_std::fs::Dir>,
/// Permissions to enforce on access to this directory. These permissions
/// are specified by a user of the `crate::WasiCtxBuilder`, and
/// are enforced prior to any enforced by the underlying operating system.
///
/// These permissions are also enforced on any directories opened under
/// this directory.
pub perms: DirPerms,
/// Permissions to enforce on any files opened under this directory.
pub file_perms: FilePerms,
/// The mode the directory was opened under: bits for reading, and writing.
/// Required to correctly report the DescriptorFlags, because cap-std
/// doesn't presently provide a cross-platform equivalent of reading the
/// oflags back out using fcntl.
pub open_mode: OpenMode,
pub(crate) allow_blocking_current_thread: bool,
}
impl Dir {
pub fn new(
dir: cap_std::fs::Dir,
perms: DirPerms,
file_perms: FilePerms,
open_mode: OpenMode,
allow_blocking_current_thread: bool,
) -> Self {
Dir {
dir: Arc::new(dir),
perms,
file_perms,
open_mode,
allow_blocking_current_thread,
}
}
/// Execute the blocking `body` function.
///
/// Depending on how the WasiCtx was configured, the body may either be:
/// - Executed directly on the current thread. In this case the `async`
/// signature of this method is effectively a lie and the returned
/// Future will always be immediately Ready. Or:
/// - Spawned on a background thread using [`tokio::task::spawn_blocking`]
/// and immediately awaited.
///
/// Intentionally blocking the executor thread might seem unorthodox, but is
/// not actually a problem for specific workloads. See:
/// - [`crate::WasiCtxBuilder::allow_blocking_current_thread`]
/// - [Poor performance of wasmtime file I/O maybe because tokio](https://github.com/bytecodealliance/wasmtime/issues/7973)
/// - [Implement opt-in for enabling WASI to block the current thread](https://github.com/bytecodealliance/wasmtime/pull/8190)
pub(crate) async fn run_blocking<F, R>(&self, body: F) -> R
where
F: FnOnce(&cap_std::fs::Dir) -> R + Send + 'static,
R: Send + 'static,
{
if self.allow_blocking_current_thread {
body(&self.dir)
} else {
let d = self.dir.clone();
spawn_blocking(move || body(&d)).await
}
}
/// Returns reference to the underlying [`cap_std::fs::Dir`]
#[cfg(feature = "p3")]
pub(crate) fn as_dir(&self) -> &Arc<cap_std::fs::Dir> {
&self.dir
}
pub(crate) async fn create_directory_at(&self, path: String) -> Result<(), ErrorCode> {
if !self.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
self.run_blocking(move |d| d.create_dir(&path)).await?;
Ok(())
}
pub(crate) async fn stat_at(
&self,
path_flags: PathFlags,
path: String,
) -> Result<DescriptorStat, ErrorCode> {
if !self.perms.contains(DirPerms::READ) {
return Err(ErrorCode::NotPermitted);
}
let meta = if path_flags.contains(PathFlags::SYMLINK_FOLLOW) {
self.run_blocking(move |d| d.metadata(&path)).await?
} else {
self.run_blocking(move |d| d.symlink_metadata(&path))
.await?
};
Ok(meta.into())
}
pub(crate) async fn set_times_at(
&self,
path_flags: PathFlags,
path: String,
atim: Option<SystemTimeSpec>,
mtim: Option<SystemTimeSpec>,
) -> Result<(), ErrorCode> {
use cap_fs_ext::DirExt as _;
if !self.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
if path_flags.contains(PathFlags::SYMLINK_FOLLOW) {
self.run_blocking(move |d| {
d.set_times(
&path,
atim.map(cap_fs_ext::SystemTimeSpec::from_std),
mtim.map(cap_fs_ext::SystemTimeSpec::from_std),
)
})
.await?;
} else {
self.run_blocking(move |d| {
d.set_symlink_times(
&path,
atim.map(cap_fs_ext::SystemTimeSpec::from_std),
mtim.map(cap_fs_ext::SystemTimeSpec::from_std),
)
})
.await?;
}
Ok(())
}
pub(crate) async fn link_at(
&self,
old_path_flags: PathFlags,
old_path: String,
new_dir: &Self,
new_path: String,
) -> Result<(), ErrorCode> {
if !self.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
if !new_dir.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
if old_path_flags.contains(PathFlags::SYMLINK_FOLLOW) {
return Err(ErrorCode::Invalid);
}
let new_dir_handle = Arc::clone(&new_dir.dir);
self.run_blocking(move |d| d.hard_link(&old_path, &new_dir_handle, &new_path))
.await?;
Ok(())
}
pub(crate) async fn open_at(
&self,
path_flags: PathFlags,
path: String,
oflags: OpenFlags,
flags: DescriptorFlags,
allow_blocking_current_thread: bool,
) -> Result<Descriptor, ErrorCode> {
use cap_fs_ext::{FollowSymlinks, OpenOptionsFollowExt, OpenOptionsMaybeDirExt};
use system_interface::fs::{FdFlags, GetSetFdFlags};
if !self.perms.contains(DirPerms::READ) {
return Err(ErrorCode::NotPermitted);
}
if !self.perms.contains(DirPerms::MUTATE) {
if oflags.contains(OpenFlags::CREATE) || oflags.contains(OpenFlags::TRUNCATE) {
return Err(ErrorCode::NotPermitted);
}
if flags.contains(DescriptorFlags::WRITE) {
return Err(ErrorCode::NotPermitted);
}
}
// Track whether we are creating file, for permission check:
let mut create = false;
// Track open mode, for permission check and recording in created descriptor:
let mut open_mode = OpenMode::empty();
// Construct the OpenOptions to give the OS:
let mut opts = cap_std::fs::OpenOptions::new();
opts.maybe_dir(true);
if oflags.contains(OpenFlags::CREATE) {
if oflags.contains(OpenFlags::EXCLUSIVE) {
opts.create_new(true);
} else {
opts.create(true);
}
create = true;
opts.write(true);
open_mode |= OpenMode::WRITE;
}
if oflags.contains(OpenFlags::TRUNCATE) {
opts.truncate(true).write(true);
}
if flags.contains(DescriptorFlags::READ) {
opts.read(true);
open_mode |= OpenMode::READ;
}
if flags.contains(DescriptorFlags::WRITE) {
opts.write(true);
open_mode |= OpenMode::WRITE;
} else {
// If not opened write, open read. This way the OS lets us open
// the file, but we can use perms to reject use of the file later.
opts.read(true);
open_mode |= OpenMode::READ;
}
if path_flags.contains(PathFlags::SYMLINK_FOLLOW) {
opts.follow(FollowSymlinks::Yes);
} else {
opts.follow(FollowSymlinks::No);
}
// These flags are not yet supported in cap-std:
if flags.contains(DescriptorFlags::FILE_INTEGRITY_SYNC)
|| flags.contains(DescriptorFlags::DATA_INTEGRITY_SYNC)
|| flags.contains(DescriptorFlags::REQUESTED_WRITE_SYNC)
{
return Err(ErrorCode::Unsupported);
}
if oflags.contains(OpenFlags::DIRECTORY) {
if oflags.contains(OpenFlags::CREATE)
|| oflags.contains(OpenFlags::EXCLUSIVE)
|| oflags.contains(OpenFlags::TRUNCATE)
{
return Err(ErrorCode::Invalid);
}
}
// Now enforce this WasiCtx's permissions before letting the OS have
// its shot:
if !self.perms.contains(DirPerms::MUTATE) && create {
return Err(ErrorCode::NotPermitted);
}
if !self.file_perms.contains(FilePerms::WRITE) && open_mode.contains(OpenMode::WRITE) {
return Err(ErrorCode::NotPermitted);
}
// Represents each possible outcome from the spawn_blocking operation.
// This makes sure we don't have to give spawn_blocking any way to
// manipulate the table.
enum OpenResult {
Dir(cap_std::fs::Dir),
File(cap_std::fs::File),
NotDir,
}
let opened = self
.run_blocking::<_, std::io::Result<OpenResult>>(move |d| {
let mut opened = d.open_with(&path, &opts)?;
if opened.metadata()?.is_dir() {
Ok(OpenResult::Dir(cap_std::fs::Dir::from_std_file(
opened.into_std(),
)))
} else if oflags.contains(OpenFlags::DIRECTORY) {
Ok(OpenResult::NotDir)
} else {
// FIXME cap-std needs a nonblocking open option so that files reads and writes
// are nonblocking. Instead we set it after opening here:
let set_fd_flags = opened.new_set_fd_flags(FdFlags::NONBLOCK)?;
opened.set_fd_flags(set_fd_flags)?;
Ok(OpenResult::File(opened))
}
})
.await?;
match opened {
// Paper over a divergence between Windows and POSIX, where
// POSIX returns EISDIR if you open a directory with the
// WRITE flag: https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html#:~:text=EISDIR
#[cfg(windows)]
OpenResult::Dir(_) if flags.contains(DescriptorFlags::WRITE) => {
Err(ErrorCode::IsDirectory)
}
OpenResult::Dir(dir) => Ok(Descriptor::Dir(Dir::new(
dir,
self.perms,
self.file_perms,
open_mode,
allow_blocking_current_thread,
))),
OpenResult::File(file) => Ok(Descriptor::File(File::new(
file,
self.file_perms,
open_mode,
allow_blocking_current_thread,
))),
OpenResult::NotDir => Err(ErrorCode::NotDirectory),
}
}
pub(crate) async fn readlink_at(&self, path: String) -> Result<String, ErrorCode> {
if !self.perms.contains(DirPerms::READ) {
return Err(ErrorCode::NotPermitted);
}
let link = self.run_blocking(move |d| d.read_link(&path)).await?;
link.into_os_string()
.into_string()
.or(Err(ErrorCode::IllegalByteSequence))
}
pub(crate) async fn remove_directory_at(&self, path: String) -> Result<(), ErrorCode> {
if !self.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
self.run_blocking(move |d| d.remove_dir(&path)).await?;
Ok(())
}
pub(crate) async fn rename_at(
&self,
old_path: String,
new_dir: &Self,
new_path: String,
) -> Result<(), ErrorCode> {
if !self.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
if !new_dir.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
let new_dir_handle = Arc::clone(&new_dir.dir);
self.run_blocking(move |d| d.rename(&old_path, &new_dir_handle, &new_path))
.await?;
Ok(())
}
pub(crate) async fn symlink_at(
&self,
src_path: String,
dest_path: String,
) -> Result<(), ErrorCode> {
// On windows, Dir.symlink is provided by DirExt
#[cfg(windows)]
use cap_fs_ext::DirExt;
if !self.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
self.run_blocking(move |d| d.symlink(&src_path, &dest_path))
.await?;
Ok(())
}
pub(crate) async fn unlink_file_at(&self, path: String) -> Result<(), ErrorCode> {
use cap_fs_ext::DirExt;
if !self.perms.contains(DirPerms::MUTATE) {
return Err(ErrorCode::NotPermitted);
}
self.run_blocking(move |d| d.remove_file_or_symlink(&path))
.await?;
Ok(())
}
pub(crate) async fn metadata_hash_at(
&self,
path_flags: PathFlags,
path: String,
) -> Result<MetadataHashValue, ErrorCode> {
// No permissions check on metadata: if dir opened, allowed to stat it
let meta = self
.run_blocking(move |d| {
if path_flags.contains(PathFlags::SYMLINK_FOLLOW) {
d.metadata(path)
} else {
d.symlink_metadata(path)
}
})
.await?;
Ok(MetadataHashValue::from(&meta))
}
}
impl WasiFilesystemCtxView<'_> {
pub(crate) fn get_directories(
&mut self,
) -> wasmtime::Result<Vec<(Resource<Descriptor>, String)>> {
let preopens = self.ctx.preopens.clone();
let mut results = Vec::with_capacity(preopens.len());
for (dir, name) in preopens {
let fd = self
.table
.push(Descriptor::Dir(dir))
.with_context(|| format!("failed to push preopen {name}"))?;
results.push((fd, name));
}
Ok(results)
}
}