Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cap-std/src/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ impl Dir {
)
}

/// Creates a new file in read-write mode; error if the file already exists.
///
/// This corresponds to [`std::fs::File::create_new`], but only accesses paths relative to
/// `self`.
#[inline]
pub fn create_new<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
self.open_with(
path,
OpenOptions::new().read(true).write(true).create_new(true),
)
}

/// Returns the canonical form of a path with all intermediate components
/// normalized and symbolic links resolved.
///
Expand Down
10 changes: 10 additions & 0 deletions cap-std/src/fs_utf8/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ impl Dir {
self.cap_std.create(path).map(File::from_cap_std)
}

/// Creates a new file in read-write mode; error if the file already exists.
///
/// This corresponds to [`std::fs::File::create_new`], but only accesses paths relative to
/// `self`.
#[inline]
pub fn create_new<P: AsRef<Utf8Path>>(&self, path: P) -> io::Result<File> {
let path = from_utf8(path.as_ref())?;
self.cap_std.create_new(path).map(File::from_cap_std)
}

/// Returns the canonical form of a path with all intermediate components
/// normalized and symbolic links resolved.
///
Expand Down
11 changes: 10 additions & 1 deletion tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ fn file_test_directoryinfo_readdir() {
}

#[test]
fn file_create_new_already_exists_error() {
fn file_create_new_via_openoptions_already_exists_error() {
let tmpdir = tmpdir();
let file = "file_create_new_error_exists";
check!(tmpdir.create(file));
Expand All @@ -529,6 +529,15 @@ fn file_create_new_already_exists_error() {
assert_eq!(e.kind(), ErrorKind::AlreadyExists);
}

#[test]
fn file_create_new_already_exists_error() {
let tmpdir = tmpdir();
let file = "file_create_new_error_exists";
check!(tmpdir.create(file));
let e = tmpdir.create_new(file).unwrap_err();
assert_eq!(e.kind(), ErrorKind::AlreadyExists);
}

#[test]
fn mkdir_path_already_exists_error() {
let tmpdir = tmpdir();
Expand Down