mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-07 03:23:03 +00:00
tools: create_path: return whether the dir was created
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
867a530724
commit
85742a0d57
@ -240,6 +240,9 @@ pub fn create_dir<P: AsRef<Path>>(path: P, options: CreateOptions) -> Result<(),
|
|||||||
/// Recursively create a path with separately defined metadata for intermediate directories and the
|
/// Recursively create a path with separately defined metadata for intermediate directories and the
|
||||||
/// final component in the path.
|
/// final component in the path.
|
||||||
///
|
///
|
||||||
|
/// Returns `true` if the final directory was created. Otherwise `false` is returned and no changes
|
||||||
|
/// to the directory's metadata have been performed.
|
||||||
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # use nix::sys::stat::Mode;
|
/// # use nix::sys::stat::Mode;
|
||||||
/// # use nix::unistd::{Gid, Uid};
|
/// # use nix::unistd::{Gid, Uid};
|
||||||
@ -260,7 +263,7 @@ pub fn create_path<P: AsRef<Path>>(
|
|||||||
path: P,
|
path: P,
|
||||||
intermediate_opts: Option<CreateOptions>,
|
intermediate_opts: Option<CreateOptions>,
|
||||||
final_opts: Option<CreateOptions>,
|
final_opts: Option<CreateOptions>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<bool, Error> {
|
||||||
create_path_do(path.as_ref(), intermediate_opts, final_opts)
|
create_path_do(path.as_ref(), intermediate_opts, final_opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,7 +271,7 @@ fn create_path_do(
|
|||||||
path: &Path,
|
path: &Path,
|
||||||
intermediate_opts: Option<CreateOptions>,
|
intermediate_opts: Option<CreateOptions>,
|
||||||
final_opts: Option<CreateOptions>,
|
final_opts: Option<CreateOptions>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<bool, Error> {
|
||||||
use std::path::Component;
|
use std::path::Component;
|
||||||
|
|
||||||
let mut iter = path.components().peekable();
|
let mut iter = path.components().peekable();
|
||||||
@ -309,12 +312,13 @@ fn create_path_at_do(
|
|||||||
mut iter: std::iter::Peekable<std::path::Components>,
|
mut iter: std::iter::Peekable<std::path::Components>,
|
||||||
intermediate_opts: Option<CreateOptions>,
|
intermediate_opts: Option<CreateOptions>,
|
||||||
final_opts: Option<CreateOptions>,
|
final_opts: Option<CreateOptions>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<bool, Error> {
|
||||||
|
let mut created = false;
|
||||||
loop {
|
loop {
|
||||||
use std::path::Component;
|
use std::path::Component;
|
||||||
|
|
||||||
match iter.next() {
|
match iter.next() {
|
||||||
None => return Ok(()),
|
None => return Ok(created),
|
||||||
|
|
||||||
Some(Component::ParentDir) => {
|
Some(Component::ParentDir) => {
|
||||||
at = Fd::openat(
|
at = Fd::openat(
|
||||||
@ -338,7 +342,7 @@ fn create_path_at_do(
|
|||||||
.and_then(|o| o.perm)
|
.and_then(|o| o.perm)
|
||||||
.unwrap_or(stat::Mode::from_bits_truncate(0o755));
|
.unwrap_or(stat::Mode::from_bits_truncate(0o755));
|
||||||
|
|
||||||
let created = match stat::mkdirat(at.as_raw_fd(), path, mode) {
|
created = match stat::mkdirat(at.as_raw_fd(), path, mode) {
|
||||||
Err(nix::Error::Sys(Errno::EEXIST)) => false,
|
Err(nix::Error::Sys(Errno::EEXIST)) => false,
|
||||||
Err(e) => return Err(e.into()),
|
Err(e) => return Err(e.into()),
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
|
Loading…
Reference in New Issue
Block a user