Skip to content

Commit 87e4836

Browse files
committed
Refactor some code out of open_manually.rs into separate files.
1 parent 91fa87f commit 87e4836

File tree

4 files changed

+64
-57
lines changed

4 files changed

+64
-57
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::{borrow::Cow, ffi::OsStr, path::Component};
2+
3+
/// Like `std::path::Component` except we combine `Prefix` and `RootDir` since
4+
/// we don't support absolute paths, and `Normal` has a `Cow` instead of a plain
5+
/// `OsStr` reference, so it can optionally own its own string.
6+
#[derive(Debug)]
7+
pub(super) enum CowComponent<'borrow> {
8+
PrefixOrRootDir,
9+
CurDir,
10+
ParentDir,
11+
Normal(Cow<'borrow, OsStr>),
12+
}
13+
14+
/// Convert a `Component` into a `CowComponent` which borrows strings.
15+
pub(super) fn to_borrowed_component(component: Component) -> CowComponent {
16+
match component {
17+
Component::Prefix(_) | Component::RootDir => CowComponent::PrefixOrRootDir,
18+
Component::CurDir => CowComponent::CurDir,
19+
Component::ParentDir => CowComponent::ParentDir,
20+
Component::Normal(os_str) => CowComponent::Normal(os_str.into()),
21+
}
22+
}
23+
24+
/// Convert a `Component` into a `CowComponent` which owns strings.
25+
pub(super) fn to_owned_component<'borrow>(component: Component) -> CowComponent<'borrow> {
26+
match component {
27+
Component::Prefix(_) | Component::RootDir => CowComponent::PrefixOrRootDir,
28+
Component::CurDir => CowComponent::CurDir,
29+
Component::ParentDir => CowComponent::ParentDir,
30+
Component::Normal(os_str) => CowComponent::Normal(os_str.to_os_string().into()),
31+
}
32+
}

cap-primitives/src/fs/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod canonicalize;
44
mod canonicalize_manually;
55
mod copy;
6+
mod cow_component;
67
mod dir_builder;
78
mod dir_entry;
89
mod dir_options;
@@ -24,6 +25,7 @@ mod open_entry_manually;
2425
mod open_manually;
2526
mod open_options;
2627
mod open_parent;
28+
mod open_unchecked_error;
2729
mod permissions;
2830
mod read_dir;
2931
mod readlink;
@@ -48,6 +50,8 @@ mod unlink_via_parent;
4850

4951
pub(crate) mod errors;
5052

53+
use cow_component::*;
54+
5155
pub(crate) use canonicalize_manually::*;
5256
#[cfg(not(feature = "no_racy_asserts"))]
5357
pub(crate) use get_path::*;
@@ -58,6 +62,7 @@ pub(crate) use mkdir_via_parent::*;
5862
pub(crate) use open_entry_manually::*;
5963
pub(crate) use open_manually::*;
6064
pub(crate) use open_parent::*;
65+
pub(crate) use open_unchecked_error::*;
6166
pub(crate) use readlink_one::*;
6267
#[cfg(not(windows))] // doesn't work on windows; use a windows-specific impl
6368
pub(crate) use readlink_via_parent::*;

cap-primitives/src/fs/open_manually.rs

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,16 @@
44
#[cfg(not(feature = "no_racy_asserts"))]
55
use crate::fs::is_same_file;
66
use crate::fs::{
7-
dir_options, errors, open_unchecked, path_requires_dir, readlink_one, FollowSymlinks,
8-
MaybeOwnedFile, OpenOptions,
7+
dir_options, errors, open_unchecked, path_requires_dir, readlink_one, to_borrowed_component,
8+
to_owned_component, CowComponent, FollowSymlinks, MaybeOwnedFile, OpenOptions,
9+
OpenUncheckedError,
910
};
1011
use std::{
11-
borrow::Cow,
1212
ffi::OsStr,
1313
fs, io,
1414
path::{Component, Path, PathBuf},
1515
};
1616

17-
/// Like `std::path::Component` except we combine `Prefix` and `RootDir` since
18-
/// we don't support absolute paths, and `Normal` has a `Cow` instead of a plain
19-
/// `OsStr` reference, so it can optionally own its own string.
20-
#[derive(Debug)]
21-
enum CowComponent<'borrow> {
22-
PrefixOrRootDir,
23-
CurDir,
24-
ParentDir,
25-
Normal(Cow<'borrow, OsStr>),
26-
}
27-
28-
/// Convert a `Component` into a `CowComponent` which borrows strings.
29-
fn to_borrowed_component(component: Component) -> CowComponent {
30-
match component {
31-
Component::Prefix(_) | Component::RootDir => CowComponent::PrefixOrRootDir,
32-
Component::CurDir => CowComponent::CurDir,
33-
Component::ParentDir => CowComponent::ParentDir,
34-
Component::Normal(os_str) => CowComponent::Normal(os_str.into()),
35-
}
36-
}
37-
38-
/// Convert a `Component` into a `CowComponent` which owns strings.
39-
fn to_owned_component<'borrow>(component: Component) -> CowComponent<'borrow> {
40-
match component {
41-
Component::Prefix(_) | Component::RootDir => CowComponent::PrefixOrRootDir,
42-
Component::CurDir => CowComponent::CurDir,
43-
Component::ParentDir => CowComponent::ParentDir,
44-
Component::Normal(os_str) => CowComponent::Normal(os_str.to_os_string().into()),
45-
}
46-
}
47-
4817
/// Utility for collecting the canonical path components.
4918
struct CanonicalPath<'path_buf> {
5019
/// If the user requested a canonical path, a reference to the `PathBuf` to
@@ -122,8 +91,8 @@ pub(crate) fn open_manually_wrapper(
12291
) -> io::Result<fs::File> {
12392
let mut symlink_count = 0;
12493
let start = MaybeOwnedFile::borrowed(start);
125-
open_manually(start, path, options, &mut symlink_count, None)
126-
.and_then(|maybe_owned| maybe_owned.into_file(options))
94+
let maybe_owned = open_manually(start, path, options, &mut symlink_count, None)?;
95+
maybe_owned.into_file(options)
12796
}
12897

12998
/// Implement `open` by breaking up the path into components, resolving each
@@ -171,7 +140,7 @@ pub(crate) fn open_manually<'start>(
171140
match c {
172141
CowComponent::PrefixOrRootDir => return Err(errors::escape_attempt()),
173142
CowComponent::CurDir => {
174-
// If the path ends in `.` and we want write access, fail.
143+
// If the path ends in `.` and we can't open a directory, fail.
175144
if components.is_empty() {
176145
if dir_precluded {
177146
return Err(errors::is_directory());
@@ -204,7 +173,7 @@ pub(crate) fn open_manually<'start>(
204173
assert!(canonical_path.pop());
205174
}
206175
CowComponent::Normal(one) => {
207-
// If the path requires a directory and we'd open it for writing, fail.
176+
// If the path requires a directory and we can't open a directory, fail.
208177
if components.is_empty() && dir_required && dir_precluded {
209178
return Err(errors::is_directory());
210179
}
@@ -275,7 +244,7 @@ pub(crate) fn open_manually<'start>(
275244
}
276245

277246
#[cfg(not(feature = "no_racy_asserts"))]
278-
check_open(&start_clone, path, options, &canonical_path, &base);
247+
check_open_manually(&start_clone, path, options, &canonical_path, &base);
279248

280249
canonical_path.complete();
281250
Ok(base)
@@ -290,7 +259,7 @@ fn should_emulate_o_path(use_options: &OpenOptions) -> bool {
290259
}
291260

292261
#[cfg(not(feature = "no_racy_asserts"))]
293-
fn check_open(
262+
fn check_open_manually(
294263
start: &fs::File,
295264
path: &Path,
296265
options: &OpenOptions,
@@ -332,20 +301,3 @@ fn check_open(
332301
}
333302
}
334303
}
335-
336-
#[derive(Debug)]
337-
pub(crate) enum OpenUncheckedError {
338-
Other(io::Error),
339-
Symlink(io::Error),
340-
NotFound(io::Error),
341-
}
342-
343-
impl From<OpenUncheckedError> for io::Error {
344-
fn from(error: OpenUncheckedError) -> Self {
345-
match error {
346-
OpenUncheckedError::Other(err)
347-
| OpenUncheckedError::Symlink(err)
348-
| OpenUncheckedError::NotFound(err) => err,
349-
}
350-
}
351-
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::io;
2+
3+
#[derive(Debug)]
4+
pub(crate) enum OpenUncheckedError {
5+
Other(io::Error),
6+
Symlink(io::Error),
7+
NotFound(io::Error),
8+
}
9+
10+
impl From<OpenUncheckedError> for io::Error {
11+
fn from(error: OpenUncheckedError) -> Self {
12+
match error {
13+
OpenUncheckedError::Other(err)
14+
| OpenUncheckedError::Symlink(err)
15+
| OpenUncheckedError::NotFound(err) => err,
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)