Skip to content

Commit 8b9c817

Browse files
committed
Use the ambient-authority crate instead of unsafe.
Use the newly-created ambient-authority crate to indicate functions which have ambient authority, rather than using `unsafe`. Fixes #141.
1 parent d081bc2 commit 8b9c817

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+901
-610
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ There currently are three main ways:
9191
- Use the [`cap-directories`] crate to create `Dir`s for config, cache and
9292
other data directories.
9393
- Use the [`cap-tempfile`] crate to create `Dir`s for temporary directories.
94-
- Use the `unsafe` [`Dir::open_ambient_dir`] to open a plain path. This
95-
function is not sandboxed, and may open any file the host process has
96-
access to.
94+
- Use [`Dir::open_ambient_dir`] to open a plain path. This function is not
95+
sandboxed, and may open any file the host process has access to.
9796

9897
## Examples
9998

benches/mod.rs

+45-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use std::{fs, path::PathBuf};
1111

1212
#[bench]
1313
fn nested_directories_open(b: &mut test::Bencher) {
14-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
14+
use cap_tempfile::ambient_authority;
15+
16+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
1517

1618
let mut path = PathBuf::new();
1719
for _ in 0..256 {
@@ -41,7 +43,9 @@ fn nested_directories_open_baseline(b: &mut test::Bencher) {
4143

4244
#[bench]
4345
fn nested_directories_metadata(b: &mut test::Bencher) {
44-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
46+
use cap_tempfile::ambient_authority;
47+
48+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
4549

4650
let mut path = PathBuf::new();
4751
for _ in 0..256 {
@@ -72,7 +76,9 @@ fn nested_directories_metadata_baseline(b: &mut test::Bencher) {
7276

7377
#[bench]
7478
fn nested_directories_canonicalize(b: &mut test::Bencher) {
75-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
79+
use cap_tempfile::ambient_authority;
80+
81+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
7682

7783
let mut path = PathBuf::new();
7884
for _ in 0..256 {
@@ -103,7 +109,9 @@ fn nested_directories_canonicalize_baseline(b: &mut test::Bencher) {
103109
#[cfg(unix)]
104110
#[bench]
105111
fn nested_directories_readlink(b: &mut test::Bencher) {
106-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
112+
use cap_tempfile::ambient_authority;
113+
114+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
107115

108116
let mut path = PathBuf::new();
109117
for _ in 0..256 {
@@ -143,7 +151,9 @@ fn nested_directories_readlink_baseline(b: &mut test::Bencher) {
143151

144152
#[bench]
145153
fn curdir(b: &mut test::Bencher) {
146-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
154+
use cap_tempfile::ambient_authority;
155+
156+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
147157

148158
let mut path = PathBuf::new();
149159
for _ in 0..256 {
@@ -176,7 +186,9 @@ fn curdir_baseline(b: &mut test::Bencher) {
176186

177187
#[bench]
178188
fn parentdir(b: &mut test::Bencher) {
179-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
189+
use cap_tempfile::ambient_authority;
190+
191+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
180192

181193
let mut path = PathBuf::new();
182194
for _ in 0..256 {
@@ -219,7 +231,9 @@ fn parentdir_baseline(b: &mut test::Bencher) {
219231

220232
#[bench]
221233
fn directory_iteration(b: &mut test::Bencher) {
222-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
234+
use cap_tempfile::ambient_authority;
235+
236+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
223237

224238
for i in 0..256 {
225239
dir.create(i.to_string()).unwrap();
@@ -234,7 +248,9 @@ fn directory_iteration(b: &mut test::Bencher) {
234248

235249
#[bench]
236250
fn directory_iteration_fast(b: &mut test::Bencher) {
237-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
251+
use cap_tempfile::ambient_authority;
252+
253+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
238254

239255
for i in 0..256 {
240256
dir.create(i.to_string()).unwrap();
@@ -265,7 +281,9 @@ fn directory_iteration_baseline(b: &mut test::Bencher) {
265281
#[cfg(unix)]
266282
#[bench]
267283
fn symlink_chasing_open(b: &mut test::Bencher) {
268-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
284+
use cap_tempfile::ambient_authority;
285+
286+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
269287

270288
dir.create("0").unwrap();
271289
for i in 0..32 {
@@ -303,7 +321,9 @@ fn symlink_chasing_open_baseline(b: &mut test::Bencher) {
303321
#[cfg(unix)]
304322
#[bench]
305323
fn symlink_chasing_metadata(b: &mut test::Bencher) {
306-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
324+
use cap_tempfile::ambient_authority;
325+
326+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
307327

308328
dir.create("0").unwrap();
309329
for i in 0..32 {
@@ -341,7 +361,9 @@ fn symlink_chasing_metadata_baseline(b: &mut test::Bencher) {
341361
#[cfg(unix)]
342362
#[bench]
343363
fn symlink_chasing_canonicalize(b: &mut test::Bencher) {
344-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
364+
use cap_tempfile::ambient_authority;
365+
366+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
345367

346368
dir.create("0").unwrap();
347369
for i in 0..32 {
@@ -378,7 +400,9 @@ fn symlink_chasing_canonicalize_baseline(b: &mut test::Bencher) {
378400

379401
#[bench]
380402
fn recursive_create_delete(b: &mut test::Bencher) {
381-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
403+
use cap_tempfile::ambient_authority;
404+
405+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
382406

383407
let mut path = PathBuf::new();
384408
for depth in 0..256 {
@@ -409,7 +433,9 @@ fn recursive_create_delete_baseline(b: &mut test::Bencher) {
409433

410434
#[bench]
411435
fn copy_4b(b: &mut test::Bencher) {
412-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
436+
use cap_tempfile::ambient_authority;
437+
438+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
413439

414440
dir.write("file", &vec![1u8; 0x4]).unwrap();
415441

@@ -433,7 +459,9 @@ fn copy_4b_baseline(b: &mut test::Bencher) {
433459

434460
#[bench]
435461
fn copy_4k(b: &mut test::Bencher) {
436-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
462+
use cap_tempfile::ambient_authority;
463+
464+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
437465

438466
dir.write("file", &vec![1u8; 0x1000]).unwrap();
439467

@@ -457,7 +485,9 @@ fn copy_4k_baseline(b: &mut test::Bencher) {
457485

458486
#[bench]
459487
fn copy_4m(b: &mut test::Bencher) {
460-
let dir = unsafe { cap_tempfile::tempdir().unwrap() };
488+
use cap_tempfile::ambient_authority;
489+
490+
let dir = cap_tempfile::tempdir(ambient_authority()).unwrap();
461491

462492
dir.write("file", &vec![1u8; 0x400000]).unwrap();
463493

cap-async-std/src/fs/dir.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ use async_std::{
88
fs, io,
99
path::{Path, PathBuf},
1010
};
11-
use cap_primitives::fs::{
12-
canonicalize, copy, create_dir, hard_link, open, open_ambient_dir, open_dir, read_base_dir,
13-
read_dir, read_link, remove_dir, remove_dir_all, remove_file, remove_open_dir,
14-
remove_open_dir_all, rename, set_permissions, stat, DirOptions, FollowSymlinks, Permissions,
11+
use cap_primitives::{
12+
ambient_authority,
13+
fs::{
14+
canonicalize, copy, create_dir, hard_link, open, open_ambient_dir, open_dir, read_base_dir,
15+
read_dir, read_link, remove_dir, remove_dir_all, remove_file, remove_open_dir,
16+
remove_open_dir_all, rename, set_permissions, stat, DirOptions, FollowSymlinks,
17+
Permissions,
18+
},
19+
AmbientAuthority,
1520
};
1621
use std::fmt;
1722
use unsafe_io::{AsUnsafeFile, FromUnsafeFile, OwnsRaw};
@@ -48,12 +53,12 @@ impl Dir {
4853
/// To prevent race conditions on Windows, the file must be opened without
4954
/// `FILE_SHARE_DELETE`.
5055
///
51-
/// # Safety
56+
/// # Ambient Authority
5257
///
5358
/// `async_std::fs::File` is not sandboxed and may access any path that the host
5459
/// process has access to.
5560
#[inline]
56-
pub unsafe fn from_std_file(std_file: fs::File) -> Self {
61+
pub fn from_std_file(std_file: fs::File, _: AmbientAuthority) -> Self {
5762
Self { std_file }
5863
}
5964

@@ -87,21 +92,21 @@ impl Dir {
8792
#[cfg(not(target_os = "wasi"))]
8893
fn _open_with(file: &std::fs::File, path: &Path, options: &OpenOptions) -> io::Result<File> {
8994
let file = open(file, path.as_ref(), options)?.into();
90-
Ok(unsafe { File::from_std(file) })
95+
Ok(File::from_std(file, ambient_authority()))
9196
}
9297

9398
#[cfg(target_os = "wasi")]
9499
fn _open_with(file: &std::fs::File, path: &Path, options: &OpenOptions) -> io::Result<File> {
95100
let file = options.open_at(&self.std_file, path)?.into();
96-
Ok(unsafe { File::from_std(file) })
101+
Ok(File::from_std(file, ambient_authority()))
97102
}
98103

99104
/// Attempts to open a directory.
100105
#[inline]
101106
pub fn open_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<Self> {
102107
let file = self.std_file.as_file_view();
103108
let dir = open_dir(&file, path.as_ref().as_ref())?.into();
104-
Ok(unsafe { Self::from_std_file(dir) })
109+
Ok(Self::from_std_file(dir, ambient_authority()))
105110
}
106111

107112
/// Creates a new, empty directory at the provided path.
@@ -579,21 +584,25 @@ impl Dir {
579584
/// Constructs a new instance of `Self` by opening the given path as a
580585
/// directory using the host process' ambient authority.
581586
///
582-
/// # Safety
587+
/// # Ambient Authority
583588
///
584589
/// This function is not sandboxed and may access any path that the host
585590
/// process has access to.
586591
#[inline]
587-
pub unsafe fn open_ambient_dir<P: AsRef<Path>>(path: P) -> io::Result<Self> {
588-
open_ambient_dir(path.as_ref().as_ref()).map(|f| Self::from_std_file(f.into()))
592+
pub fn open_ambient_dir<P: AsRef<Path>>(
593+
path: P,
594+
ambient_authority: AmbientAuthority,
595+
) -> io::Result<Self> {
596+
open_ambient_dir(path.as_ref().as_ref(), ambient_authority)
597+
.map(|f| Self::from_std_file(f.into(), ambient_authority))
589598
}
590599
}
591600

592601
#[cfg(not(windows))]
593602
impl FromRawFd for Dir {
594603
#[inline]
595604
unsafe fn from_raw_fd(fd: RawFd) -> Self {
596-
Self::from_std_file(fs::File::from_raw_fd(fd))
605+
Self::from_std_file(fs::File::from_raw_fd(fd), ambient_authority())
597606
}
598607
}
599608

@@ -603,7 +612,7 @@ impl FromRawHandle for Dir {
603612
/// `FILE_SHARE_DELETE`.
604613
#[inline]
605614
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
606-
Self::from_std_file(fs::File::from_raw_handle(handle))
615+
Self::from_std_file(fs::File::from_raw_handle(handle), ambient_authority())
607616
}
608617
}
609618

cap-async-std/src/fs/dir_entry.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use async_std::io;
44
use async_std::os::unix::fs::DirEntryExt;
55
#[cfg(target_os = "wasi")]
66
use async_std::os::wasi::fs::DirEntryExt;
7+
use cap_primitives::ambient_authority;
78
use std::{ffi::OsString, fmt};
89

910
/// Entries returned by the `ReadDir` iterator.
@@ -30,21 +31,21 @@ impl DirEntry {
3031
#[inline]
3132
pub fn open(&self) -> io::Result<File> {
3233
let file = self.inner.open()?.into();
33-
Ok(unsafe { File::from_std(file) })
34+
Ok(File::from_std(file, ambient_authority()))
3435
}
3536

3637
/// Open the file with the given options.
3738
#[inline]
3839
pub fn open_with(&self, options: &OpenOptions) -> io::Result<File> {
3940
let file = self.inner.open_with(options)?.into();
40-
Ok(unsafe { File::from_std(file) })
41+
Ok(File::from_std(file, ambient_authority()))
4142
}
4243

4344
/// Open the entry as a directory.
4445
#[inline]
4546
pub fn open_dir(&self) -> io::Result<Dir> {
4647
let file = self.inner.open_dir()?.into();
47-
Ok(unsafe { Dir::from_std_file(file) })
48+
Ok(Dir::from_std_file(file, ambient_authority()))
4849
}
4950

5051
/// Removes the file from its filesystem.
@@ -97,9 +98,9 @@ impl DirEntryExt for DirEntry {
9798

9899
#[cfg(windows)]
99100
#[doc(hidden)]
100-
unsafe impl cap_primitives::fs::_WindowsDirEntryExt for DirEntry {
101+
impl cap_primitives::fs::_WindowsDirEntryExt for DirEntry {
101102
#[inline]
102-
unsafe fn full_metadata(&self) -> io::Result<Metadata> {
103+
fn full_metadata(&self) -> io::Result<Metadata> {
103104
self.inner.full_metadata()
104105
}
105106
}

cap-async-std/src/fs/file.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use async_std::{
88
io::{self, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write},
99
task::{Context, Poll},
1010
};
11-
use cap_primitives::fs::is_file_read_write;
11+
use cap_primitives::{ambient_authority, fs::is_file_read_write, AmbientAuthority};
1212
use std::{fmt, pin::Pin};
1313
use unsafe_io::{AsUnsafeFile, OwnsRaw};
1414
#[cfg(windows)]
@@ -35,12 +35,12 @@ pub struct File {
3535
impl File {
3636
/// Constructs a new instance of `Self` from the given `async_std::fs::File`.
3737
///
38-
/// # Safety
38+
/// # Ambient Authority
3939
///
4040
/// `async_std::fs::File` is not sandboxed and may access any path that the host
4141
/// process has access to.
4242
#[inline]
43-
pub unsafe fn from_std(std: fs::File) -> Self {
43+
pub fn from_std(std: fs::File, _: AmbientAuthority) -> Self {
4444
Self { std }
4545
}
4646

@@ -135,15 +135,15 @@ fn permissions_into_std(
135135
impl FromRawFd for File {
136136
#[inline]
137137
unsafe fn from_raw_fd(fd: RawFd) -> Self {
138-
Self::from_std(fs::File::from_raw_fd(fd))
138+
Self::from_std(fs::File::from_raw_fd(fd), ambient_authority())
139139
}
140140
}
141141

142142
#[cfg(windows)]
143143
impl FromRawHandle for File {
144144
#[inline]
145145
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
146-
Self::from_std(fs::File::from_raw_handle(handle))
146+
Self::from_std(fs::File::from_raw_handle(handle), ambient_authority())
147147
}
148148
}
149149

0 commit comments

Comments
 (0)