File tree Expand file tree Collapse file tree 5 files changed +94
-0
lines changed Expand file tree Collapse file tree 5 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -131,6 +131,31 @@ impl File {
131
131
. map ( |f| Self :: from_std ( f. into ( ) ) )
132
132
}
133
133
134
+ /// Constructs a new instance of `Self` in write-only mode by opening,
135
+ /// creating or truncating, the given path as a file using the host
136
+ /// process' ambient authority.
137
+ ///
138
+ /// # Ambient Authority
139
+ ///
140
+ /// This function is not sandboxed and may access any path that the host
141
+ /// process has access to.
142
+ #[ inline]
143
+ pub async fn create_ambient < P : AsRef < Path > > (
144
+ path : P ,
145
+ ambient_authority : AmbientAuthority ,
146
+ ) -> io:: Result < Self > {
147
+ let path = path. as_ref ( ) . to_path_buf ( ) ;
148
+ spawn_blocking ( move || {
149
+ open_ambient (
150
+ path. as_ref ( ) ,
151
+ OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) ,
152
+ ambient_authority,
153
+ )
154
+ } )
155
+ . await
156
+ . map ( |f| Self :: from_std ( f. into ( ) ) )
157
+ }
158
+
134
159
/// Constructs a new instance of `Self` with the options specified by
135
160
/// `options` by opening the given path as a file using the host process'
136
161
/// ambient authority.
Original file line number Diff line number Diff line change @@ -125,6 +125,25 @@ impl File {
125
125
. map ( Self :: from_cap_std)
126
126
}
127
127
128
+ /// Constructs a new instance of `Self` in write-only mode by opening,
129
+ /// creating or truncating, the given path as a file using the host
130
+ /// process' ambient authority.
131
+ ///
132
+ /// # Ambient Authority
133
+ ///
134
+ /// This function is not sandboxed and may access any path that the host
135
+ /// process has access to.
136
+ #[ inline]
137
+ pub async fn create_ambient < P : AsRef < Path > > (
138
+ path : P ,
139
+ ambient_authority : AmbientAuthority ,
140
+ ) -> io:: Result < Self > {
141
+ let path = from_utf8 ( path) ?;
142
+ crate :: fs:: File :: create_ambient ( path, ambient_authority)
143
+ . await
144
+ . map ( Self :: from_cap_std)
145
+ }
146
+
128
147
/// Constructs a new instance of `Self` with the options specified by
129
148
/// `options` by opening the given path as a file using the host process'
130
149
/// ambient authority.
Original file line number Diff line number Diff line change @@ -123,6 +123,27 @@ impl File {
123
123
Ok ( Self :: from_std ( std) )
124
124
}
125
125
126
+ /// Constructs a new instance of `Self` in write-only mode by opening,
127
+ /// creating or truncating, the given path as a file using the host
128
+ /// process' ambient authority.
129
+ ///
130
+ /// # Ambient Authority
131
+ ///
132
+ /// This function is not sandboxed and may access any path that the host
133
+ /// process has access to.
134
+ #[ inline]
135
+ pub fn create_ambient < P : AsRef < Path > > (
136
+ path : P ,
137
+ ambient_authority : AmbientAuthority ,
138
+ ) -> io:: Result < Self > {
139
+ let std = open_ambient (
140
+ path. as_ref ( ) ,
141
+ OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) ,
142
+ ambient_authority,
143
+ ) ?;
144
+ Ok ( Self :: from_std ( std) )
145
+ }
146
+
126
147
/// Constructs a new instance of `Self` with the options specified by
127
148
/// `options` by opening the given path as a file using the host process'
128
149
/// ambient authority.
Original file line number Diff line number Diff line change @@ -128,6 +128,26 @@ impl File {
128
128
) ?) )
129
129
}
130
130
131
+ /// Constructs a new instance of `Self` in write-only mode by opening,
132
+ /// creating or truncating, the given path as a file using the host
133
+ /// process' ambient authority.
134
+ ///
135
+ /// # Ambient Authority
136
+ ///
137
+ /// This function is not sandboxed and may access any path that the host
138
+ /// process has access to.
139
+ #[ inline]
140
+ pub fn create_ambient < P : AsRef < Utf8Path > > (
141
+ path : P ,
142
+ ambient_authority : AmbientAuthority ,
143
+ ) -> io:: Result < Self > {
144
+ let path = from_utf8 ( path. as_ref ( ) ) ?;
145
+ Ok ( Self :: from_cap_std ( crate :: fs:: File :: create_ambient (
146
+ path,
147
+ ambient_authority,
148
+ ) ?) )
149
+ }
150
+
131
151
/// Constructs a new instance of `Self` with the options specified by
132
152
/// `options` by opening the given path as a file using the host process'
133
153
/// ambient authority.
Original file line number Diff line number Diff line change @@ -9,6 +9,15 @@ fn test_open_ambient() {
9
9
let _ = File :: open_ambient ( "Cargo.toml" , ambient_authority ( ) ) . unwrap ( ) ;
10
10
}
11
11
12
+ #[ test]
13
+ fn test_create_ambient ( ) {
14
+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
15
+ let foo_path = dir. path ( ) . join ( "foo" ) ;
16
+ let _ = File :: create_ambient ( & foo_path, ambient_authority ( ) ) . unwrap ( ) ;
17
+ let _ = File :: open_ambient ( & foo_path, ambient_authority ( ) ) . unwrap ( ) ;
18
+ let _ = File :: create_ambient ( & foo_path, ambient_authority ( ) ) . unwrap ( ) ;
19
+ }
20
+
12
21
#[ test]
13
22
fn test_create_dir_ambient ( ) {
14
23
let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments