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 {
131131 . map ( |f| Self :: from_std ( f. into ( ) ) )
132132 }
133133
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+
134159 /// Constructs a new instance of `Self` with the options specified by
135160 /// `options` by opening the given path as a file using the host process'
136161 /// ambient authority.
Original file line number Diff line number Diff line change @@ -125,6 +125,25 @@ impl File {
125125 . map ( Self :: from_cap_std)
126126 }
127127
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+
128147 /// Constructs a new instance of `Self` with the options specified by
129148 /// `options` by opening the given path as a file using the host process'
130149 /// ambient authority.
Original file line number Diff line number Diff line change @@ -123,6 +123,27 @@ impl File {
123123 Ok ( Self :: from_std ( std) )
124124 }
125125
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+
126147 /// Constructs a new instance of `Self` with the options specified by
127148 /// `options` by opening the given path as a file using the host process'
128149 /// ambient authority.
Original file line number Diff line number Diff line change @@ -128,6 +128,26 @@ impl File {
128128 ) ?) )
129129 }
130130
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+
131151 /// Constructs a new instance of `Self` with the options specified by
132152 /// `options` by opening the given path as a file using the host process'
133153 /// ambient authority.
Original file line number Diff line number Diff line change @@ -9,6 +9,15 @@ fn test_open_ambient() {
99 let _ = File :: open_ambient ( "Cargo.toml" , ambient_authority ( ) ) . unwrap ( ) ;
1010}
1111
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+
1221#[ test]
1322fn test_create_dir_ambient ( ) {
1423 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments