This commit is contained in:
Alexey
2026-09-25 19:46:31 +03:00
parent 2d63fcf376
commit 326c0ecdb9
128 changed files with 914 additions and 1248 deletions
+1 -2
View File
@@ -8,8 +8,7 @@ mod path;
mod write;
pub(crate) use path::{
AnchoredPath, chdir_nofollow_or_create, open_dir_nofollow,
open_trusted_dir_nofollow_or_create,
AnchoredPath, chdir_nofollow_or_create, open_dir_nofollow, open_trusted_dir_nofollow_or_create,
};
pub(crate) use write::{
atomic_replace, atomic_replace_async, open_append_regular, open_append_regular_at,
+3 -11
View File
@@ -93,10 +93,7 @@ pub(crate) fn open_dir_nofollow_or_create(path: &Path, mode: u32) -> io::Result<
}
/// Opens or creates a directory chain protected from untrusted entry replacement.
pub(crate) fn open_trusted_dir_nofollow_or_create(
path: &Path,
mode: u32,
) -> io::Result<OwnedFd> {
pub(crate) fn open_trusted_dir_nofollow_or_create(path: &Path, mode: u32) -> io::Result<OwnedFd> {
open_dir_components(path, Some(mode), true)
}
@@ -143,13 +140,8 @@ fn open_dir_components(
Ok(()) | Err(nix::errno::Errno::EEXIST) => {}
Err(error) => return Err(errno_to_io(error)),
}
openat(
&current,
name.as_os_str(),
DIRECTORY_FLAGS,
Mode::empty(),
)
.map_err(errno_to_io)?
openat(&current, name.as_os_str(), DIRECTORY_FLAGS, Mode::empty())
.map_err(errno_to_io)?
}
Err(error) => return Err(errno_to_io(error)),
};
+6 -23
View File
@@ -1,5 +1,5 @@
use std::io::{self, Read, Write};
use std::ffi::OsStr;
use std::io::{self, Read, Write};
use std::os::fd::{AsFd, OwnedFd};
use std::path::Path;
@@ -9,11 +9,7 @@ use nix::unistd::{UnlinkatFlags, fsync, unlinkat};
use super::path::{AnchoredPath, errno_to_io};
fn open_regular_at(
anchored: &AnchoredPath,
flags: OFlag,
mode: u32,
) -> io::Result<std::fs::File> {
fn open_regular_at(anchored: &AnchoredPath, flags: OFlag, mode: u32) -> io::Result<std::fs::File> {
let descriptor = openat(
anchored.parent(),
anchored.name(),
@@ -55,8 +51,7 @@ pub(crate) fn read_regular_limited(path: &Path, max_bytes: usize) -> io::Result<
));
}
let mut bytes = Vec::with_capacity(before.len() as usize);
Read::take(&mut file, max_bytes.saturating_add(1) as u64)
.read_to_end(&mut bytes)?;
Read::take(&mut file, max_bytes.saturating_add(1) as u64).read_to_end(&mut bytes)?;
if bytes.len() > max_bytes {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
@@ -120,11 +115,7 @@ pub(crate) fn open_append_regular_at<Fd: AsFd>(
let descriptor = openat(
parent,
name,
OFlag::O_WRONLY
| OFlag::O_APPEND
| OFlag::O_CREAT
| OFlag::O_NOFOLLOW
| OFlag::O_CLOEXEC,
OFlag::O_WRONLY | OFlag::O_APPEND | OFlag::O_CREAT | OFlag::O_NOFOLLOW | OFlag::O_CLOEXEC,
Mode::from_bits_truncate(mode),
)
.map_err(errno_to_io)?;
@@ -163,20 +154,12 @@ pub(crate) async fn atomic_replace_async(
.map_err(|error| io::Error::other(format!("secure writer task failed: {error}")))?
}
fn atomic_replace_anchored(
anchored: &AnchoredPath,
contents: &[u8],
mode: u32,
) -> io::Result<()> {
fn atomic_replace_anchored(anchored: &AnchoredPath, contents: &[u8], mode: u32) -> io::Result<()> {
let temp_name = format!(".telemt.tmp-{}", rand::random::<u64>());
let descriptor = openat(
anchored.parent(),
temp_name.as_str(),
OFlag::O_WRONLY
| OFlag::O_CREAT
| OFlag::O_EXCL
| OFlag::O_NOFOLLOW
| OFlag::O_CLOEXEC,
OFlag::O_WRONLY | OFlag::O_CREAT | OFlag::O_EXCL | OFlag::O_NOFOLLOW | OFlag::O_CLOEXEC,
Mode::from_bits_truncate(mode),
)
.map_err(errno_to_io)?;