1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
//! Extensions for [`cap_std::fs::Dir`]. Key features here include:
//!
//! - "optional" variants that return `Result<Option<T>>` for nonexistent paths, when
//! it is a normal case that paths may not exist.
//! - A helper to update timestamps
//! - "atomic write" APIs that create a new file, then rename over the existing one
//! to avoid half-written updates to files.
//!
//! [`cap_std::fs::Dir`]: https://docs.rs/cap-std/latest/cap_std/fs/struct.Dir.html
use cap_std::fs::{Dir, File, Metadata};
use cap_tempfile::cap_std;
use std::ffi::OsStr;
use std::io::Result;
use std::io::{self, Write};
use std::ops::Deref;
use std::path::Path;
#[cfg(feature = "fs_utf8")]
use cap_std::fs_utf8;
#[cfg(feature = "fs_utf8")]
use fs_utf8::camino::Utf8Path;
/// Extension trait for [`cap_std::fs::Dir`].
///
/// [`cap_std::fs::Dir`]: https://docs.rs/cap-std/latest/cap_std/fs/struct.Dir.html
pub trait CapStdExtDirExt {
/// Open a file read-only, but return `Ok(None)` if it does not exist.
fn open_optional(&self, path: impl AsRef<Path>) -> Result<Option<File>>;
/// Open a directory, but return `Ok(None)` if it does not exist.
fn open_dir_optional(&self, path: impl AsRef<Path>) -> Result<Option<Dir>>;
/// Create the target directory, but do nothing if a directory already exists at that path.
/// The return value will be `true` if the directory was created. An error will be
/// returned if the path is a non-directory. Symbolic links will be followed.
fn ensure_dir_with(
&self,
p: impl AsRef<Path>,
builder: &cap_std::fs::DirBuilder,
) -> Result<bool>;
/// Gather metadata, but return `Ok(None)` if it does not exist.
fn metadata_optional(&self, path: impl AsRef<Path>) -> Result<Option<Metadata>>;
/// Gather metadata (but do not follow symlinks), but return `Ok(None)` if it does not exist.
fn symlink_metadata_optional(&self, path: impl AsRef<Path>) -> Result<Option<Metadata>>;
/// Remove (delete) a file, but return `Ok(false)` if the file does not exist.
fn remove_file_optional(&self, path: impl AsRef<Path>) -> Result<bool>;
/// Remove a file or directory but return `Ok(false)` if the file does not exist.
/// Symbolic links are not followed.
fn remove_all_optional(&self, path: impl AsRef<Path>) -> Result<bool>;
/// Set the access and modification times to the current time. Symbolic links are not followed.
#[cfg(unix)]
fn update_timestamps(&self, path: impl AsRef<Path>) -> Result<()>;
/// Atomically write a file by calling the provided closure.
///
/// This uses [`cap_tempfile::TempFile`], which is wrapped in a [`std::io::BufWriter`]
/// and passed to the closure.
///
/// # Existing files and metadata
///
/// If the target path already exists and is a regular file (not a symbolic link or directory),
/// then its access permissions (Unix mode) will be preserved. However, other metadata
/// such as extended attributes will *not* be preserved automatically. To do this will
/// require a higher level wrapper which queries the existing file and gathers such metadata
/// before replacement.
///
/// # Example, including setting permissions
///
/// The closure may also perform other file operations beyond writing, such as changing
/// file permissions:
///
/// ```rust
/// # use std::io;
/// # use std::io::Write;
/// # use cap_tempfile::cap_std;
/// # fn main() -> io::Result<()> {
/// # let somedir = cap_tempfile::tempdir(cap_std::ambient_authority())?;
/// use cap_std_ext::prelude::*;
/// let contents = b"hello world\n";
/// somedir.atomic_replace_with("somefilename", |f| -> io::Result<_> {
/// f.write_all(contents)?;
/// f.flush()?;
/// use cap_std::fs::PermissionsExt;
/// let perms = cap_std::fs::Permissions::from_mode(0o600);
/// f.get_mut().as_file_mut().set_permissions(perms)?;
/// Ok(())
/// })
/// # }
/// ```
///
/// Any existing file will be replaced.
fn atomic_replace_with<F, T, E>(
&self,
destname: impl AsRef<Path>,
f: F,
) -> std::result::Result<T, E>
where
F: FnOnce(&mut std::io::BufWriter<cap_tempfile::TempFile>) -> std::result::Result<T, E>,
E: From<std::io::Error>;
/// Atomically write the provided contents to a file.
fn atomic_write(&self, destname: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()>;
/// Atomically write the provided contents to a file, using specified permissions.
fn atomic_write_with_perms(
&self,
destname: impl AsRef<Path>,
contents: impl AsRef<[u8]>,
perms: cap_std::fs::Permissions,
) -> Result<()>;
}
#[cfg(feature = "fs_utf8")]
/// Extension trait for [`cap_std::fs_utf8::Dir`].
///
/// [`cap_std::fs_utf8::Dir`]: https://docs.rs/cap-std/latest/cap_std/fs_utf8/struct.Dir.html
pub trait CapStdExtDirExtUtf8 {
/// Open a file read-only, but return `Ok(None)` if it does not exist.
fn open_optional(&self, path: impl AsRef<Utf8Path>) -> Result<Option<fs_utf8::File>>;
/// Open a directory, but return `Ok(None)` if it does not exist.
fn open_dir_optional(&self, path: impl AsRef<Utf8Path>) -> Result<Option<fs_utf8::Dir>>;
/// Create the target directory, but do nothing if a directory already exists at that path.
/// The return value will be `true` if the directory was created. An error will be
/// returned if the path is a non-directory. Symbolic links will be followed.
fn ensure_dir_with(
&self,
p: impl AsRef<Utf8Path>,
builder: &cap_std::fs::DirBuilder,
) -> Result<bool>;
/// Gather metadata, but return `Ok(None)` if it does not exist.
fn metadata_optional(&self, path: impl AsRef<Utf8Path>) -> Result<Option<Metadata>>;
/// Gather metadata (but do not follow symlinks), but return `Ok(None)` if it does not exist.
fn symlink_metadata_optional(&self, path: impl AsRef<Utf8Path>) -> Result<Option<Metadata>>;
/// Remove (delete) a file, but return `Ok(false)` if the file does not exist.
fn remove_file_optional(&self, path: impl AsRef<Utf8Path>) -> Result<bool>;
/// Remove a file or directory but return `Ok(false)` if the file does not exist.
/// Symbolic links are not followed.
fn remove_all_optional(&self, path: impl AsRef<Utf8Path>) -> Result<bool>;
/// Set the access and modification times to the current time. Symbolic links are not followed.
#[cfg(unix)]
fn update_timestamps(&self, path: impl AsRef<Utf8Path>) -> Result<()>;
/// Atomically write a file by calling the provided closure.
///
/// This uses [`cap_tempfile::TempFile`], which is wrapped in a [`std::io::BufWriter`]
/// and passed to the closure.
///
/// # Existing files and metadata
///
/// If the target path already exists and is a regular file (not a symbolic link or directory),
/// then its access permissions (Unix mode) will be preserved. However, other metadata
/// such as extended attributes will *not* be preserved automatically. To do this will
/// require a higher level wrapper which queries the existing file and gathers such metadata
/// before replacement.
///
/// # Example, including setting permissions
///
/// The closure may also perform other file operations beyond writing, such as changing
/// file permissions:
///
/// ```rust
/// # use std::io;
/// # use std::io::Write;
/// # use cap_tempfile::cap_std;
/// # fn main() -> io::Result<()> {
/// # let somedir = cap_tempfile::tempdir(cap_std::ambient_authority())?;
/// # let somedir = cap_std::fs_utf8::Dir::from_cap_std((&*somedir).try_clone()?);
/// use cap_std_ext::prelude::*;
/// let contents = b"hello world\n";
/// somedir.atomic_replace_with("somefilename", |f| -> io::Result<_> {
/// f.write_all(contents)?;
/// f.flush()?;
/// use cap_std::fs::PermissionsExt;
/// let perms = cap_std::fs::Permissions::from_mode(0o600);
/// f.get_mut().as_file_mut().set_permissions(perms)?;
/// Ok(())
/// })
/// # }
/// ```
///
/// Any existing file will be replaced.
fn atomic_replace_with<F, T, E>(
&self,
destname: impl AsRef<Utf8Path>,
f: F,
) -> std::result::Result<T, E>
where
F: FnOnce(&mut std::io::BufWriter<cap_tempfile::TempFile>) -> std::result::Result<T, E>,
E: From<std::io::Error>;
/// Atomically write the provided contents to a file.
fn atomic_write(
&self,
destname: impl AsRef<Utf8Path>,
contents: impl AsRef<[u8]>,
) -> Result<()>;
/// Atomically write the provided contents to a file, using specified permissions.
fn atomic_write_with_perms(
&self,
destname: impl AsRef<Utf8Path>,
contents: impl AsRef<[u8]>,
perms: cap_std::fs::Permissions,
) -> Result<()>;
}
fn map_optional<R>(r: Result<R>) -> Result<Option<R>> {
match r {
Ok(v) => Ok(Some(v)),
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
Ok(None)
} else {
Err(e)
}
}
}
}
enum DirOwnedOrBorrowed<'d> {
Owned(Dir),
Borrowed(&'d Dir),
}
impl<'d> Deref for DirOwnedOrBorrowed<'d> {
type Target = Dir;
fn deref(&self) -> &Self::Target {
match self {
Self::Owned(d) => d,
Self::Borrowed(d) => d,
}
}
}
/// Given a directory reference and a path, if the path includes a subdirectory (e.g. on Unix has a `/`)
/// then open up the target directory, and return the file name.
///
/// Otherwise, reborrow the directory and return the file name.
///
/// It is an error if the target path does not name a file.
fn subdir_of<'d, 'p>(d: &'d Dir, p: &'p Path) -> io::Result<(DirOwnedOrBorrowed<'d>, &'p OsStr)> {
let name = p
.file_name()
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "Not a file name"))?;
let r = if let Some(subdir) = p
.parent()
.filter(|v| !v.as_os_str().is_empty())
.map(|p| d.open_dir(p))
{
DirOwnedOrBorrowed::Owned(subdir?)
} else {
DirOwnedOrBorrowed::Borrowed(d)
};
Ok((r, name))
}
impl CapStdExtDirExt for Dir {
fn open_optional(&self, path: impl AsRef<Path>) -> Result<Option<File>> {
map_optional(self.open(path.as_ref()))
}
fn open_dir_optional(&self, path: impl AsRef<Path>) -> Result<Option<Dir>> {
map_optional(self.open_dir(path.as_ref()))
}
fn ensure_dir_with(
&self,
p: impl AsRef<Path>,
builder: &cap_std::fs::DirBuilder,
) -> Result<bool> {
let p = p.as_ref();
match self.create_dir_with(p, builder) {
Ok(()) => Ok(true),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
if !self.symlink_metadata(p)?.is_dir() {
// TODO use https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.NotADirectory
// once it's stable.
return Err(io::Error::new(io::ErrorKind::Other, "Found non-directory"));
}
Ok(false)
}
Err(e) => Err(e),
}
}
fn metadata_optional(&self, path: impl AsRef<Path>) -> Result<Option<Metadata>> {
map_optional(self.metadata(path.as_ref()))
}
fn symlink_metadata_optional(&self, path: impl AsRef<Path>) -> Result<Option<Metadata>> {
map_optional(self.symlink_metadata(path.as_ref()))
}
fn remove_file_optional(&self, path: impl AsRef<Path>) -> Result<bool> {
match self.remove_file(path.as_ref()) {
Ok(()) => Ok(true),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e),
}
}
fn remove_all_optional(&self, path: impl AsRef<Path>) -> Result<bool> {
let path = path.as_ref();
// This is obviously racy, but correctly matching on the errors
// runs into the fact that e.g. https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.NotADirectory
// is unstable right now.
let meta = match self.symlink_metadata_optional(path)? {
Some(m) => m,
None => return Ok(false),
};
if meta.is_dir() {
self.remove_dir_all(path)?;
} else {
self.remove_file(path)?;
}
Ok(true)
}
#[cfg(unix)]
fn update_timestamps(&self, path: impl AsRef<Path>) -> Result<()> {
use rustix::fd::AsFd;
use rustix::fs::UTIME_NOW;
let path = path.as_ref();
let now = rustix::fs::Timespec {
tv_sec: 0,
tv_nsec: UTIME_NOW,
};
// https://github.com/bytecodealliance/rustix/commit/69af396b79e296717bece8148b1f6165b810885c
// means that Timespec only implements Copy on 64 bit right now.
#[allow(clippy::clone_on_copy)]
let times = rustix::fs::Timestamps {
last_access: now.clone(),
last_modification: now.clone(),
};
rustix::fs::utimensat(
self.as_fd(),
path,
×,
rustix::fs::AtFlags::SYMLINK_NOFOLLOW,
)?;
Ok(())
}
fn atomic_replace_with<F, T, E>(
&self,
destname: impl AsRef<Path>,
f: F,
) -> std::result::Result<T, E>
where
F: FnOnce(&mut std::io::BufWriter<cap_tempfile::TempFile>) -> std::result::Result<T, E>,
E: From<std::io::Error>,
{
let destname = destname.as_ref();
let (d, name) = subdir_of(self, destname)?;
let existing_metadata = d.symlink_metadata_optional(destname)?;
// If the target is already a file, then acquire its mode, which we will preserve by default.
// We don't follow symlinks here for replacement, and so we definitely don't want to pick up its mode.
let existing_perms = existing_metadata
.filter(|m| m.is_file())
.map(|m| m.permissions());
let mut t = cap_tempfile::TempFile::new(&d)?;
// Apply the permissions, if we have them
if let Some(existing_perms) = existing_perms {
t.as_file_mut().set_permissions(existing_perms)?;
}
// We always operate in terms of buffered writes
let mut bufw = std::io::BufWriter::new(t);
// Call the provided closure to generate the file content
let r = f(&mut bufw)?;
// Flush the buffer, and rename the temporary file into place
bufw.into_inner()
.map_err(From::from)
.and_then(|t| t.replace(name))?;
Ok(r)
}
fn atomic_write(&self, destname: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
self.atomic_replace_with(destname, |f| f.write_all(contents.as_ref()))
}
fn atomic_write_with_perms(
&self,
destname: impl AsRef<Path>,
contents: impl AsRef<[u8]>,
perms: cap_std::fs::Permissions,
) -> Result<()> {
self.atomic_replace_with(destname, |f| -> io::Result<_> {
// If the user is overriding the permissions, let's make the default be
// writable by us but not readable by anyone else, in case it has
// secret data.
#[cfg(unix)]
{
use cap_std::fs::PermissionsExt;
let perms = cap_std::fs::Permissions::from_mode(0o600);
f.get_mut().as_file_mut().set_permissions(perms)?;
}
f.write_all(contents.as_ref())?;
f.flush()?;
f.get_mut().as_file_mut().set_permissions(perms)?;
Ok(())
})
}
}
// Implementation for the Utf8 variant of Dir. You shouldn't need to add
// any real logic here, just delegate to the non-UTF8 version via `as_cap_std()`
// in general.
#[cfg(feature = "fs_utf8")]
impl CapStdExtDirExtUtf8 for cap_std::fs_utf8::Dir {
fn open_optional(&self, path: impl AsRef<Utf8Path>) -> Result<Option<fs_utf8::File>> {
map_optional(self.open(path.as_ref()))
}
fn open_dir_optional(&self, path: impl AsRef<Utf8Path>) -> Result<Option<fs_utf8::Dir>> {
map_optional(self.open_dir(path.as_ref()))
}
fn ensure_dir_with(
&self,
p: impl AsRef<Utf8Path>,
builder: &cap_std::fs::DirBuilder,
) -> Result<bool> {
self.as_cap_std()
.ensure_dir_with(p.as_ref().as_std_path(), builder)
}
fn metadata_optional(&self, path: impl AsRef<Utf8Path>) -> Result<Option<Metadata>> {
self.as_cap_std()
.metadata_optional(path.as_ref().as_std_path())
}
fn symlink_metadata_optional(&self, path: impl AsRef<Utf8Path>) -> Result<Option<Metadata>> {
self.as_cap_std()
.symlink_metadata_optional(path.as_ref().as_std_path())
}
fn remove_file_optional(&self, path: impl AsRef<Utf8Path>) -> Result<bool> {
self.as_cap_std()
.remove_file_optional(path.as_ref().as_std_path())
}
fn remove_all_optional(&self, path: impl AsRef<Utf8Path>) -> Result<bool> {
self.as_cap_std()
.remove_all_optional(path.as_ref().as_std_path())
}
#[cfg(unix)]
fn update_timestamps(&self, path: impl AsRef<Utf8Path>) -> Result<()> {
self.as_cap_std()
.update_timestamps(path.as_ref().as_std_path())
}
fn atomic_replace_with<F, T, E>(
&self,
destname: impl AsRef<Utf8Path>,
f: F,
) -> std::result::Result<T, E>
where
F: FnOnce(&mut std::io::BufWriter<cap_tempfile::TempFile>) -> std::result::Result<T, E>,
E: From<std::io::Error>,
{
self.as_cap_std()
.atomic_replace_with(destname.as_ref().as_std_path(), f)
}
fn atomic_write(
&self,
destname: impl AsRef<Utf8Path>,
contents: impl AsRef<[u8]>,
) -> Result<()> {
self.as_cap_std()
.atomic_write(destname.as_ref().as_std_path(), contents)
}
fn atomic_write_with_perms(
&self,
destname: impl AsRef<Utf8Path>,
contents: impl AsRef<[u8]>,
perms: cap_std::fs::Permissions,
) -> Result<()> {
self.as_cap_std()
.atomic_write_with_perms(destname.as_ref().as_std_path(), contents, perms)
}
}