Skip to content

Commit 4de172a

Browse files
committed
Rename cap-primitives' rmdir to remove_dir.
1 parent ea2cde6 commit 4de172a

File tree

16 files changed

+72
-66
lines changed

16 files changed

+72
-66
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_all, remove_open_dir, remove_open_dir_all, rename, rmdir, set_permissions,
11-
stat, unlink, DirOptions, FollowSymlinks, Permissions,
10+
readlink, remove_dir, remove_dir_all, remove_open_dir, remove_open_dir_all, rename,
11+
set_permissions, stat, unlink, DirOptions, FollowSymlinks, Permissions,
1212
};
1313
use std::{
1414
fmt,
@@ -318,7 +318,7 @@ impl Dir {
318318
#[inline]
319319
pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
320320
let file = unsafe { as_sync(&self.std_file) };
321-
rmdir(&file, path.as_ref())
321+
remove_dir(&file, path.as_ref())
322322
}
323323

324324
/// Removes a directory at this path, after removing all its contents. Use carefully!

cap-primitives/src/fs/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ mod open_unchecked_error;
2626
mod permissions;
2727
mod read_dir;
2828
mod readlink;
29+
mod remove_dir;
2930
mod remove_dir_all;
3031
mod remove_open_dir;
3132
mod rename;
32-
mod rmdir;
3333
mod set_permissions;
3434
mod set_times;
3535
mod stat;
@@ -70,10 +70,10 @@ pub use open_options::*;
7070
pub use permissions::*;
7171
pub use read_dir::*;
7272
pub use readlink::*;
73+
pub use remove_dir::*;
7374
pub use remove_dir_all::*;
7475
pub use remove_open_dir::*;
7576
pub use rename::*;
76-
pub use rmdir::*;
7777
pub use set_permissions::*;
7878
pub use set_times::*;
7979
pub use stat::*;

cap-primitives/src/fs/rmdir.rs renamed to cap-primitives/src/fs/remove_dir.rs

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

810
/// Perform a `rmdirat`-like operation, ensuring that the resolution of the path
911
/// never escapes the directory tree rooted at `start`.
1012
#[cfg_attr(not(racy_asserts), allow(clippy::let_and_return))]
1113
#[inline]
12-
pub fn rmdir(start: &fs::File, path: &Path) -> io::Result<()> {
14+
pub fn remove_dir(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 = rmdir_impl(start, path);
19+
let result = remove_dir_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_rmdir(start, path, &stat_before, &result, &stat_after);
25+
check_remove_dir(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_rmdir(
32+
fn check_remove_dir(
3133
start: &fs::File,
3234
path: &Path,
3335
stat_before: &io::Result<Metadata>,
@@ -51,7 +53,7 @@ fn check_rmdir(
5153
}
5254

5355
(_, Err((InvalidInput, _)), _) => {
54-
// `rmdir(".")` apparently returns `EINVAL`
56+
// `remove_dir(".")` apparently returns `EINVAL`
5557
}
5658

5759
(_, Err((kind, message)), _) => {
@@ -60,13 +62,13 @@ fn check_rmdir(
6062
path,
6163
FollowSymlinks::No,
6264
)) {
63-
Ok(canon) => match map_result(&rmdir_unchecked(start, &canon)) {
65+
Ok(canon) => match map_result(&remove_dir_unchecked(start, &canon)) {
6466
Err((_unchecked_kind, _unchecked_message)) => {
6567
/* TODO: Check error messages.
6668
assert_eq!(
6769
kind,
6870
unchecked_kind,
69-
"unexpected error kind from rmdir start='{:?}', \
71+
"unexpected error kind from remove_dir start='{:?}', \
7072
path='{}':\nstat_before={:#?}\nresult={:#?}\nstat_after={:#?}",
7173
start,
7274
path.display(),
@@ -78,7 +80,7 @@ fn check_rmdir(
7880
*/
7981
}
8082
_ => panic!(
81-
"unsandboxed rmdir success on start={:?} path={:?}; expected {:?}: {}",
83+
"unsandboxed remove_dir success on start={:?} path={:?}; expected {:?}: {}",
8284
start, path, kind, message
8385
),
8486
},
@@ -92,7 +94,7 @@ fn check_rmdir(
9294
}
9395

9496
other => panic!(
95-
"inconsistent rmdir checks: start='{:?}' path='{}':\n{:#?}",
97+
"inconsistent remove_dir checks: start='{:?}' path='{}':\n{:#?}",
9698
start,
9799
path.display(),
98100
other,
@@ -102,16 +104,16 @@ fn check_rmdir(
102104
match stat_after {
103105
Ok(_unchecked_metadata) => match &result {
104106
Ok(()) => panic!(
105-
"file still exists after rmdir start='{:?}', path='{}'",
107+
"file still exists after remove_dir start='{:?}', path='{}'",
106108
start,
107109
path.display()
108110
),
109111
Err(e) => match e.kind() {
110112
io::ErrorKind::PermissionDenied
111-
| io::ErrorKind::InvalidInput // `rmdir(".")` apparently returns `EINVAL`
113+
| io::ErrorKind::InvalidInput // `remove_dir(".")` apparently returns `EINVAL`
112114
| io::ErrorKind::Other => (), // directory not empty, among other things
113115
_ => panic!(
114-
"unexpected error rmdiring start='{:?}', path='{}': {:?}",
116+
"unexpected error remove_dir'ing start='{:?}', path='{}': {:?}",
115117
start,
116118
path.display(),
117119
e

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ mod hard_link;
77
mod open_parent;
88
#[cfg(not(windows))] // doesn't work on windows; use a windows-specific impl
99
mod readlink;
10+
mod remove_dir;
1011
mod rename;
11-
mod rmdir;
1212
#[cfg(windows)]
1313
mod set_permissions;
1414
#[cfg(not(windows))]
@@ -22,8 +22,8 @@ pub(crate) use create_dir::create_dir;
2222
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;
25+
pub(crate) use remove_dir::remove_dir;
2526
pub(crate) use rename::rename;
26-
pub(crate) use rmdir::rmdir;
2727
#[cfg(windows)]
2828
pub(crate) use set_permissions::set_permissions;
2929
#[cfg(not(windows))]
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_dir_unchecked, MaybeOwnedFile};
3+
use std::{fs, io, path::Path};
4+
5+
/// Implement `remove_dir` by `open`ing up the parent component of the path and
6+
/// then calling `remove_dir_unchecked` on the last component.
7+
pub(crate) fn remove_dir(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_dir_unchecked(&dir, basename.as_ref())
13+
}

cap-primitives/src/fs/via_parent/rmdir.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
@@ -18,9 +18,9 @@ mod permissions_ext;
1818
mod read_dir_inner;
1919
mod readlink_unchecked;
2020
mod remove_dir_all_impl;
21+
mod remove_dir_unchecked;
2122
mod remove_open_dir_by_searching;
2223
mod rename_unchecked;
23-
mod rmdir_unchecked;
2424
#[cfg(not(target_os = "linux"))]
2525
mod set_permissions_impl;
2626
#[cfg(not(target_os = "linux"))]
@@ -66,7 +66,7 @@ pub(crate) use crate::fs::{
6666
via_parent::create_dir as create_dir_impl,
6767
via_parent::readlink as readlink_impl,
6868
via_parent::rename as rename_impl,
69-
via_parent::rmdir as rmdir_impl,
69+
via_parent::remove_dir as remove_dir_impl,
7070
via_parent::symlink as symlink_impl,
7171
via_parent::unlink as unlink_impl,
7272
remove_open_dir_by_searching as remove_open_dir_impl,
@@ -92,9 +92,9 @@ pub(crate) use permissions_ext::*;
9292
pub(crate) use read_dir_inner::*;
9393
pub(crate) use readlink_unchecked::*;
9494
pub(crate) use remove_dir_all_impl::*;
95+
pub(crate) use remove_dir_unchecked::*;
9596
pub(crate) use remove_open_dir_by_searching::*;
9697
pub(crate) use rename_unchecked::*;
97-
pub(crate) use rmdir_unchecked::*;
9898
pub(crate) use stat_unchecked::*;
9999
pub(crate) use symlink_unchecked::*;
100100
#[allow(unused_imports)]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::fs::{
2-
open_dir, open_dir_unchecked, open_entry_impl, read_dir_unchecked, rmdir_unchecked,
2+
open_dir, open_dir_unchecked, open_entry_impl, read_dir_unchecked, remove_dir_unchecked,
33
stat_unchecked, unlink_unchecked, DirEntryInner, FollowSymlinks, Metadata, OpenOptions,
44
ReadDir,
55
};
@@ -53,7 +53,7 @@ impl ReadDirInner {
5353
}
5454

5555
pub(super) fn remove_dir(&self, file_name: &OsStr) -> io::Result<()> {
56-
unsafe { rmdir_unchecked(&self.to_std_file(), file_name.as_ref()) }
56+
unsafe { remove_dir_unchecked(&self.to_std_file(), file_name.as_ref()) }
5757
}
5858

5959
pub(super) fn self_metadata(&self) -> io::Result<Metadata> {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::fs::{
2-
read_dir, read_dir_unchecked, remove_open_dir, rmdir, stat, unlink, FollowSymlinks, ReadDir,
2+
read_dir, read_dir_unchecked, remove_dir, remove_open_dir, stat, unlink, FollowSymlinks,
3+
ReadDir,
34
};
45
use std::{
56
fs, io,
@@ -15,7 +16,7 @@ pub(crate) fn remove_dir_all_impl(start: &fs::File, path: &Path) -> io::Result<(
1516
unlink(start, path)
1617
} else {
1718
remove_dir_all_recursive(read_dir(start, path)?)?;
18-
rmdir(start, path)
19+
remove_dir(start, path)
1920
}
2021
}
2122

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_dir`, but which does not perform
5+
/// sandboxing.
6+
pub(crate) fn remove_dir_unchecked(start: &fs::File, path: &Path) -> io::Result<()> {
7+
unlinkat(start, path, AtFlags::REMOVEDIR)
8+
}

cap-primitives/src/posish/fs/rmdir_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
@@ -19,9 +19,9 @@ mod read_dir_inner;
1919
mod readlink_impl;
2020
mod readlink_unchecked;
2121
mod remove_dir_all_impl;
22+
mod remove_dir_unchecked;
2223
mod remove_open_dir_impl;
2324
mod rename_unchecked;
24-
mod rmdir_unchecked;
2525
mod set_permissions_unchecked;
2626
mod set_times_impl;
2727
mod stat_unchecked;
@@ -36,7 +36,7 @@ pub(crate) use crate::fs::{
3636
via_parent::hard_link as hard_link_impl,
3737
via_parent::create_dir as create_dir_impl,
3838
via_parent::rename as rename_impl,
39-
via_parent::rmdir as rmdir_impl,
39+
via_parent::remove_dir as remove_dir_impl,
4040
via_parent::set_permissions as set_permissions_impl,
4141
manually::stat as stat_impl,
4242
via_parent::symlink_dir as symlink_dir_impl,
@@ -63,9 +63,9 @@ pub(crate) use read_dir_inner::*;
6363
pub(crate) use readlink_impl::*;
6464
pub(crate) use readlink_unchecked::*;
6565
pub(crate) use remove_dir_all_impl::*;
66+
pub(crate) use remove_dir_unchecked::*;
6667
pub(crate) use remove_open_dir_impl::*;
6768
pub(crate) use rename_unchecked::*;
68-
pub(crate) use rmdir_unchecked::*;
6969
pub(crate) use set_permissions_unchecked::*;
7070
pub(crate) use set_times_impl::*;
7171
pub(crate) use stat_unchecked::*;

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::fs::{read_dir_unchecked, remove_open_dir, rmdir, stat, unlink, FollowSymlinks};
1+
use crate::fs::{read_dir_unchecked, remove_dir, remove_open_dir, stat, unlink, FollowSymlinks};
22
#[cfg(feature = "windows_file_type_ext")]
33
use std::os::windows::fs::FileTypeExt;
44
use std::{
@@ -12,11 +12,12 @@ pub(crate) fn remove_dir_all_impl(start: &fs::File, path: &Path) -> io::Result<(
1212
let filetype = stat(start, path, FollowSymlinks::No)?.file_type();
1313
if filetype.is_symlink() {
1414
// On Windows symlinks to files and directories are removed differently.
15-
// rmdir only deletes dir symlinks and junctions, not file symlinks.
16-
rmdir(start, path)
15+
// `remove_dir` only deletes dir symlinks and junctions, not file
16+
// symlinks.
17+
remove_dir(start, path)
1718
} else {
1819
remove_dir_all_recursive(start, path)?;
19-
rmdir(start, path)
20+
remove_dir(start, path)
2021
}
2122
}
2223

@@ -36,9 +37,9 @@ fn remove_dir_all_recursive(start: &fs::File, path: &Path) -> io::Result<()> {
3637
if child_type.is_dir() {
3738
let path = path.join(child.file_name());
3839
remove_dir_all_recursive(start, &path)?;
39-
rmdir(start, &path)?;
40+
remove_dir(start, &path)?;
4041
} else if child_type.is_symlink_dir() {
41-
rmdir(start, &path.join(child.file_name()))?;
42+
remove_dir(start, &path.join(child.file_name()))?;
4243
} else {
4344
unlink(start, &path.join(child.file_name()))?;
4445
}
@@ -54,9 +55,9 @@ fn remove_dir_all_recursive(start: &fs::File, path: &Path) -> io::Result<()> {
5455
if child_type.is_dir() {
5556
let path = path.join(child.file_name());
5657
remove_dir_all_recursive(start, &path)?;
57-
rmdir(start, &path)?;
58+
remove_dir(start, &path)?;
5859
} else {
59-
match rmdir(start, &path.join(child.file_name())) {
60+
match remove_dir(start, &path.join(child.file_name())) {
6061
Ok(()) => (),
6162
Err(e) => {
6263
if e.raw_os_error() == Some(winapi::shared::winerror::ERROR_DIRECTORY as i32) {
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 `rmdir`, but which does not perform sandboxing.
5-
pub(crate) fn rmdir_unchecked(start: &fs::File, path: &Path) -> io::Result<()> {
4+
/// *Unsandboxed* function similar to `remove_dir`, but which does not perform
5+
/// sandboxing.
6+
pub(crate) fn remove_dir_unchecked(start: &fs::File, path: &Path) -> io::Result<()> {
67
let full_path = concatenate_or_return_absolute(start, path)?;
78
fs::remove_dir(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_all, remove_open_dir, remove_open_dir_all, rename, rmdir, set_permissions,
5-
stat, unlink, DirOptions, FollowSymlinks, Permissions,
4+
readlink, remove_dir, remove_dir_all, remove_open_dir, remove_open_dir_all, rename,
5+
set_permissions, stat, unlink, DirOptions, FollowSymlinks, Permissions,
66
};
77
#[cfg(target_os = "wasi")]
88
use std::os::wasi::{
@@ -307,7 +307,7 @@ impl Dir {
307307
/// [`std::fs::remove_dir`]: https://doc.rust-lang.org/std/fs/fn.remove_dir.html
308308
#[inline]
309309
pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
310-
rmdir(&self.std_file, path.as_ref())
310+
remove_dir(&self.std_file, path.as_ref())
311311
}
312312

313313
/// Removes a directory at this path, after removing all its contents. Use carefully!

fuzz/fuzz_targets/cap-primitives.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl Plan {
130130
.ok();
131131
}
132132
Operation::Rmdir(dirno, path) => {
133-
cap_primitives::fs::rmdir(
133+
cap_primitives::fs::remove_dir(
134134
&files[*dirno % files.len()],
135135
&paths[*path % paths.len()],
136136
)

0 commit comments

Comments
 (0)