Skip to content

Commit 4395a30

Browse files
committed
Rename cap-primitives' unlink to remove_file.
1 parent 4de172a commit 4395a30

File tree

16 files changed

+67
-61
lines changed

16 files changed

+67
-61
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use async_std::os::wasi::{
77
use async_std::{fs, io};
88
use cap_primitives::fs::{
99
canonicalize, copy, create_dir, hard_link, open, open_ambient_dir, open_dir, read_dir,
10-
readlink, remove_dir, remove_dir_all, remove_open_dir, remove_open_dir_all, rename,
11-
set_permissions, stat, unlink, DirOptions, FollowSymlinks, Permissions,
10+
readlink, remove_dir, remove_dir_all, remove_file, remove_open_dir, remove_open_dir_all,
11+
rename, set_permissions, stat, DirOptions, FollowSymlinks, Permissions,
1212
};
1313
use std::{
1414
fmt,
@@ -365,7 +365,7 @@ impl Dir {
365365
#[inline]
366366
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
367367
let file = unsafe { as_sync(&self.std_file) };
368-
unlink(&file, path.as_ref())
368+
remove_file(&file, path.as_ref())
369369
}
370370

371371
/// Rename a file or directory to a new name, replacing the original file if to already exists.

cap-primitives/src/fs/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ mod read_dir;
2828
mod readlink;
2929
mod remove_dir;
3030
mod remove_dir_all;
31+
mod remove_file;
3132
mod remove_open_dir;
3233
mod rename;
3334
mod set_permissions;
3435
mod set_times;
3536
mod stat;
3637
mod symlink;
3738
mod system_time_spec;
38-
mod unlink;
3939

4040
pub(crate) mod errors;
4141
pub(crate) mod manually;
@@ -72,14 +72,14 @@ pub use read_dir::*;
7272
pub use readlink::*;
7373
pub use remove_dir::*;
7474
pub use remove_dir_all::*;
75+
pub use remove_file::*;
7576
pub use remove_open_dir::*;
7677
pub use rename::*;
7778
pub use set_permissions::*;
7879
pub use set_times::*;
7980
pub use stat::*;
8081
pub use symlink::*;
8182
pub use system_time_spec::*;
82-
pub use unlink::*;
8383

8484
#[cfg(racy_asserts)]
8585
fn map_result<T: Clone>(result: &std::io::Result<T>) -> Result<T, (std::io::ErrorKind, String)> {

cap-primitives/src/fs/unlink.rs renamed to cap-primitives/src/fs/remove_file.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
//! This defines `unlink`, the primary entrypoint to sandboxed file removal.
1+
//! This defines `remove_file`, the primary entrypoint to sandboxed file removal.
22
3-
use crate::fs::unlink_impl;
3+
use crate::fs::remove_file_impl;
44
#[cfg(racy_asserts)]
5-
use crate::fs::{manually, map_result, stat_unchecked, unlink_unchecked, FollowSymlinks, Metadata};
5+
use crate::fs::{
6+
manually, map_result, remove_file_unchecked, stat_unchecked, FollowSymlinks, Metadata,
7+
};
68
use std::{fs, io, path::Path};
79

8-
/// Perform a `unlinkat`-like operation, ensuring that the resolution of the path
9-
/// never escapes the directory tree rooted at `start`.
10+
/// Perform a `remove_fileat`-like operation, ensuring that the resolution of
11+
/// the path never escapes the directory tree rooted at `start`.
1012
#[cfg_attr(not(racy_asserts), allow(clippy::let_and_return))]
1113
#[inline]
12-
pub fn unlink(start: &fs::File, path: &Path) -> io::Result<()> {
14+
pub fn remove_file(start: &fs::File, path: &Path) -> io::Result<()> {
1315
#[cfg(racy_asserts)]
1416
let stat_before = stat_unchecked(start, path, FollowSymlinks::No);
1517

1618
// Call the underlying implementation.
17-
let result = unlink_impl(start, path);
19+
let result = remove_file_impl(start, path);
1820

1921
#[cfg(racy_asserts)]
2022
let stat_after = stat_unchecked(start, path, FollowSymlinks::No);
2123

2224
#[cfg(racy_asserts)]
23-
check_unlink(start, path, &stat_before, &result, &stat_after);
25+
check_remove_file(start, path, &stat_before, &result, &stat_after);
2426

2527
result
2628
}
2729

2830
#[cfg(racy_asserts)]
2931
#[allow(clippy::enum_glob_use)]
30-
fn check_unlink(
32+
fn check_remove_file(
3133
start: &fs::File,
3234
path: &Path,
3335
stat_before: &io::Result<Metadata>,
@@ -56,13 +58,13 @@ fn check_unlink(
5658
path,
5759
FollowSymlinks::No,
5860
)) {
59-
Ok(canon) => match map_result(&unlink_unchecked(start, &canon)) {
61+
Ok(canon) => match map_result(&remove_file_unchecked(start, &canon)) {
6062
Err((_unchecked_kind, _unchecked_message)) => {
6163
/* TODO: Check error messages.
6264
assert_eq!(
6365
kind,
6466
unchecked_kind,
65-
"unexpected error kind from unlink start='{:?}', \
67+
"unexpected error kind from remove_file start='{:?}', \
6668
path='{}':\nstat_before={:#?}\nresult={:#?}\nstat_after={:#?}",
6769
start,
6870
path.display(),
@@ -73,7 +75,7 @@ fn check_unlink(
7375
assert_eq!(message, unchecked_message);
7476
*/
7577
}
76-
_ => panic!("unsandboxed unlink success"),
78+
_ => panic!("unsandboxed remove_file success"),
7779
},
7880
Err((_canon_kind, _canon_message)) => {
7981
/* TODO: Check error messages.
@@ -85,7 +87,7 @@ fn check_unlink(
8587
}
8688

8789
other => panic!(
88-
"inconsistent unlink checks: start='{:?}' path='{}':\n{:#?}",
90+
"inconsistent remove_file checks: start='{:?}' path='{}':\n{:#?}",
8991
start,
9092
path.display(),
9193
other,
@@ -94,15 +96,15 @@ fn check_unlink(
9496

9597
match (result, stat_after) {
9698
(Ok(()), Ok(_unchecked_metadata)) => panic!(
97-
"file still exists after unlink start='{:?}', path='{}'",
99+
"file still exists after remove_file start='{:?}', path='{}'",
98100
start,
99101
path.display()
100102
),
101103
(Err(e), Ok(unchecked_metadata)) => match e.kind() {
102104
io::ErrorKind::PermissionDenied => (),
103105
io::ErrorKind::Other if unchecked_metadata.is_dir() => (),
104106
_ => panic!(
105-
"unexpected error unlinking start='{:?}', path='{}': {:?}",
107+
"unexpected error removing file start='{:?}', path='{}': {:?}",
106108
start,
107109
path.display(),
108110
e

cap-primitives/src/fs/via_parent/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ mod open_parent;
88
#[cfg(not(windows))] // doesn't work on windows; use a windows-specific impl
99
mod readlink;
1010
mod remove_dir;
11+
mod remove_file;
1112
mod rename;
1213
#[cfg(windows)]
1314
mod set_permissions;
1415
#[cfg(not(windows))]
1516
mod set_times_nofollow;
1617
mod symlink;
17-
mod unlink;
1818

1919
use open_parent::open_parent;
2020

@@ -23,6 +23,7 @@ pub(crate) use hard_link::hard_link;
2323
#[cfg(not(windows))] // doesn't work on windows; use a windows-specific impl
2424
pub(crate) use readlink::readlink;
2525
pub(crate) use remove_dir::remove_dir;
26+
pub(crate) use remove_file::remove_file;
2627
pub(crate) use rename::rename;
2728
#[cfg(windows)]
2829
pub(crate) use set_permissions::set_permissions;
@@ -32,4 +33,3 @@ pub(crate) use set_times_nofollow::set_times_nofollow;
3233
pub(crate) use symlink::symlink;
3334
#[cfg(windows)]
3435
pub(crate) use symlink::{symlink_dir, symlink_file};
35-
pub(crate) use unlink::unlink;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use super::open_parent;
2+
use crate::fs::{remove_file_unchecked, MaybeOwnedFile};
3+
use std::{fs, io, path::Path};
4+
5+
/// Implement `remove_file` by `open`ing up the parent component of the path and
6+
/// then calling `remove_file_unchecked` on the last component.
7+
pub(crate) fn remove_file(start: &fs::File, path: &Path) -> io::Result<()> {
8+
let start = MaybeOwnedFile::borrowed(start);
9+
10+
let (dir, basename) = open_parent(start, path)?;
11+
12+
remove_file_unchecked(&dir, basename.as_ref())
13+
}

cap-primitives/src/fs/via_parent/unlink.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

cap-primitives/src/posish/fs/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod read_dir_inner;
1919
mod readlink_unchecked;
2020
mod remove_dir_all_impl;
2121
mod remove_dir_unchecked;
22+
mod remove_file_unchecked;
2223
mod remove_open_dir_by_searching;
2324
mod rename_unchecked;
2425
#[cfg(not(target_os = "linux"))]
@@ -28,7 +29,6 @@ mod set_times_impl;
2829
mod stat_unchecked;
2930
mod symlink_unchecked;
3031
mod times;
31-
mod unlink_unchecked;
3232

3333
pub(crate) mod errors;
3434

@@ -68,7 +68,7 @@ pub(crate) use crate::fs::{
6868
via_parent::rename as rename_impl,
6969
via_parent::remove_dir as remove_dir_impl,
7070
via_parent::symlink as symlink_impl,
71-
via_parent::unlink as unlink_impl,
71+
via_parent::remove_file as remove_file_impl,
7272
remove_open_dir_by_searching as remove_open_dir_impl,
7373
};
7474

@@ -93,13 +93,13 @@ pub(crate) use read_dir_inner::*;
9393
pub(crate) use readlink_unchecked::*;
9494
pub(crate) use remove_dir_all_impl::*;
9595
pub(crate) use remove_dir_unchecked::*;
96+
pub(crate) use remove_file_unchecked::*;
9697
pub(crate) use remove_open_dir_by_searching::*;
9798
pub(crate) use rename_unchecked::*;
9899
pub(crate) use stat_unchecked::*;
99100
pub(crate) use symlink_unchecked::*;
100101
#[allow(unused_imports)]
101102
pub(crate) use times::{set_times_follow_unchecked, set_times_nofollow_unchecked};
102-
pub(crate) use unlink_unchecked::*;
103103

104104
// On Linux, there is a limit of 40 symlink expansions.
105105
// Source: https://man7.org/linux/man-pages/man7/path_resolution.7.html

cap-primitives/src/posish/fs/read_dir_inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::fs::{
22
open_dir, open_dir_unchecked, open_entry_impl, read_dir_unchecked, remove_dir_unchecked,
3-
stat_unchecked, unlink_unchecked, DirEntryInner, FollowSymlinks, Metadata, OpenOptions,
3+
remove_file_unchecked, stat_unchecked, DirEntryInner, FollowSymlinks, Metadata, OpenOptions,
44
ReadDir,
55
};
66
use posish::fs::Dir;
@@ -49,7 +49,7 @@ impl ReadDirInner {
4949
}
5050

5151
pub(super) fn remove_file(&self, file_name: &OsStr) -> io::Result<()> {
52-
unsafe { unlink_unchecked(&self.to_std_file(), file_name.as_ref()) }
52+
unsafe { remove_file_unchecked(&self.to_std_file(), file_name.as_ref()) }
5353
}
5454

5555
pub(super) fn remove_dir(&self, file_name: &OsStr) -> io::Result<()> {

cap-primitives/src/posish/fs/remove_dir_all_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::fs::{
2-
read_dir, read_dir_unchecked, remove_dir, remove_open_dir, stat, unlink, FollowSymlinks,
2+
read_dir, read_dir_unchecked, remove_dir, remove_file, remove_open_dir, stat, FollowSymlinks,
33
ReadDir,
44
};
55
use std::{
@@ -13,7 +13,7 @@ pub(crate) fn remove_dir_all_impl(start: &fs::File, path: &Path) -> io::Result<(
1313
// 108e90ca78f052c0c1c49c42a22c85620be19712.
1414
let filetype = stat(start, path, FollowSymlinks::No)?.file_type();
1515
if filetype.is_symlink() {
16-
unlink(start, path)
16+
remove_file(start, path)
1717
} else {
1818
remove_dir_all_recursive(read_dir(start, path)?)?;
1919
remove_dir(start, path)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use posish::fs::{unlinkat, AtFlags};
2+
use std::{fs, io, path::Path};
3+
4+
/// *Unsandboxed* function similar to `remove_file`, but which does not perform
5+
/// sandboxing.
6+
pub(crate) fn remove_file_unchecked(start: &fs::File, path: &Path) -> io::Result<()> {
7+
unlinkat(start, path, AtFlags::empty())
8+
}

cap-primitives/src/posish/fs/unlink_unchecked.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

cap-primitives/src/winx/fs/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ mod readlink_impl;
2020
mod readlink_unchecked;
2121
mod remove_dir_all_impl;
2222
mod remove_dir_unchecked;
23+
mod remove_file_unchecked;
2324
mod remove_open_dir_impl;
2425
mod rename_unchecked;
2526
mod set_permissions_unchecked;
2627
mod set_times_impl;
2728
mod stat_unchecked;
2829
mod symlink_unchecked;
29-
mod unlink_unchecked;
3030

3131
pub(crate) mod errors;
3232

@@ -41,7 +41,7 @@ pub(crate) use crate::fs::{
4141
manually::stat as stat_impl,
4242
via_parent::symlink_dir as symlink_dir_impl,
4343
via_parent::symlink_file as symlink_file_impl,
44-
via_parent::unlink as unlink_impl,
44+
via_parent::remove_file as remove_file_impl,
4545
dir_options as dir_path_options,
4646
};
4747

@@ -64,13 +64,13 @@ pub(crate) use readlink_impl::*;
6464
pub(crate) use readlink_unchecked::*;
6565
pub(crate) use remove_dir_all_impl::*;
6666
pub(crate) use remove_dir_unchecked::*;
67+
pub(crate) use remove_file_unchecked::*;
6768
pub(crate) use remove_open_dir_impl::*;
6869
pub(crate) use rename_unchecked::*;
6970
pub(crate) use set_permissions_unchecked::*;
7071
pub(crate) use set_times_impl::*;
7172
pub(crate) use stat_unchecked::*;
7273
pub(crate) use symlink_unchecked::*;
73-
pub(crate) use unlink_unchecked::*;
7474

7575
// On Windows, there is a limit of 63 reparse points on any given path.
7676
// https://docs.microsoft.com/en-us/windows/win32/fileio/reparse-points

cap-primitives/src/winx/fs/remove_dir_all_impl.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::fs::{read_dir_unchecked, remove_dir, remove_open_dir, stat, unlink, FollowSymlinks};
1+
use crate::fs::{
2+
read_dir_unchecked, remove_dir, remove_file, remove_open_dir, stat, FollowSymlinks,
3+
};
24
#[cfg(feature = "windows_file_type_ext")]
35
use std::os::windows::fs::FileTypeExt;
46
use std::{
@@ -41,7 +43,7 @@ fn remove_dir_all_recursive(start: &fs::File, path: &Path) -> io::Result<()> {
4143
} else if child_type.is_symlink_dir() {
4244
remove_dir(start, &path.join(child.file_name()))?;
4345
} else {
44-
unlink(start, &path.join(child.file_name()))?;
46+
remove_file(start, &path.join(child.file_name()))?;
4547
}
4648
}
4749
Ok(())
@@ -61,7 +63,7 @@ fn remove_dir_all_recursive(start: &fs::File, path: &Path) -> io::Result<()> {
6163
Ok(()) => (),
6264
Err(e) => {
6365
if e.raw_os_error() == Some(winapi::shared::winerror::ERROR_DIRECTORY as i32) {
64-
unlink(start, &path.join(child.file_name()))?;
66+
remove_file(start, &path.join(child.file_name()))?;
6567
} else {
6668
return Err(e);
6769
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use super::get_path::concatenate_or_return_absolute;
22
use std::{fs, io, path::Path};
33

4-
/// *Unsandboxed* function similar to `unlink`, but which does not perform sandboxing.
5-
pub(crate) fn unlink_unchecked(start: &fs::File, path: &Path) -> io::Result<()> {
4+
/// *Unsandboxed* function similar to `remove_file`, but which does not perform
5+
/// sandboxing.
6+
pub(crate) fn remove_file_unchecked(start: &fs::File, path: &Path) -> io::Result<()> {
67
let full_path = concatenate_or_return_absolute(start, path)?;
78
fs::remove_file(full_path)
89
}

cap-std/src/fs/dir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::fs::{DirBuilder, File, Metadata, OpenOptions, ReadDir};
22
use cap_primitives::fs::{
33
canonicalize, copy, create_dir, hard_link, open, open_ambient_dir, open_dir, read_dir,
4-
readlink, remove_dir, remove_dir_all, remove_open_dir, remove_open_dir_all, rename,
5-
set_permissions, stat, unlink, DirOptions, FollowSymlinks, Permissions,
4+
readlink, remove_dir, remove_dir_all, remove_file, remove_open_dir, remove_open_dir_all,
5+
rename, set_permissions, stat, DirOptions, FollowSymlinks, Permissions,
66
};
77
#[cfg(target_os = "wasi")]
88
use std::os::wasi::{
@@ -350,7 +350,7 @@ impl Dir {
350350
/// [`std::fs::remove_file`]: https://doc.rust-lang.org/std/fs/fn.remove_file.html
351351
#[inline]
352352
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
353-
unlink(&self.std_file, path.as_ref())
353+
remove_file(&self.std_file, path.as_ref())
354354
}
355355

356356
/// Rename a file or directory to a new name, replacing the original file if to already exists.

0 commit comments

Comments
 (0)