mirror of
https://git.proxmox.com/git/proxmox
synced 2025-05-29 17:15:57 +00:00
sys: formatting
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
c280f73793
commit
b213dbb7c8
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||||
|
|
||||||
@ -107,4 +106,3 @@ impl AsRawFd for FdRef {
|
|||||||
unsafe { *(self as *const Self as *const RawFd) }
|
unsafe { *(self as *const Self as *const RawFd) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,8 +97,11 @@ impl Iterator for ReadDir {
|
|||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.iter.next().map(|res| {
|
self.iter.next().map(|res| {
|
||||||
res.map(|entry| ReadDirEntry { entry, parent_fd: self.dir_fd })
|
res.map(|entry| ReadDirEntry {
|
||||||
.map_err(Error::from)
|
entry,
|
||||||
|
parent_fd: self.dir_fd,
|
||||||
|
})
|
||||||
|
.map_err(Error::from)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -157,7 +160,6 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Helper trait to provide a combinators for directory entry iterators.
|
/// Helper trait to provide a combinators for directory entry iterators.
|
||||||
pub trait FileIterOps<T, E>
|
pub trait FileIterOps<T, E>
|
||||||
where
|
where
|
||||||
@ -255,7 +257,7 @@ where
|
|||||||
}
|
}
|
||||||
// file did not match regex or isn't valid utf-8
|
// file did not match regex or isn't valid utf-8
|
||||||
continue;
|
continue;
|
||||||
},
|
}
|
||||||
Err(_) => return Some(item),
|
Err(_) => return Some(item),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -288,24 +290,34 @@ fn do_lock_dir_noblock(
|
|||||||
would_block_msg: &str,
|
would_block_msg: &str,
|
||||||
exclusive: bool,
|
exclusive: bool,
|
||||||
) -> Result<DirLockGuard, Error> {
|
) -> Result<DirLockGuard, Error> {
|
||||||
let mut handle = Dir::open(path, OFlag::O_RDONLY, Mode::empty())
|
let mut handle = Dir::open(path, OFlag::O_RDONLY, Mode::empty()).map_err(|err| {
|
||||||
.map_err(|err| {
|
format_err!(
|
||||||
format_err!("unable to open {} directory {:?} for locking - {}", what, path, err)
|
"unable to open {} directory {:?} for locking - {}",
|
||||||
})?;
|
what,
|
||||||
|
path,
|
||||||
|
err
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
// acquire in non-blocking mode, no point in waiting here since other
|
// acquire in non-blocking mode, no point in waiting here since other
|
||||||
// backups could still take a very long time
|
// backups could still take a very long time
|
||||||
crate::fs::lock_file(&mut handle, exclusive, Some(std::time::Duration::from_nanos(0)))
|
crate::fs::lock_file(
|
||||||
.map_err(|err| {
|
&mut handle,
|
||||||
format_err!(
|
exclusive,
|
||||||
"unable to acquire lock on {} directory {:?} - {}", what, path,
|
Some(std::time::Duration::from_nanos(0)),
|
||||||
if err.would_block() {
|
)
|
||||||
String::from(would_block_msg)
|
.map_err(|err| {
|
||||||
} else {
|
format_err!(
|
||||||
err.to_string()
|
"unable to acquire lock on {} directory {:?} - {}",
|
||||||
}
|
what,
|
||||||
)
|
path,
|
||||||
})?;
|
if err.would_block() {
|
||||||
|
String::from(would_block_msg)
|
||||||
|
} else {
|
||||||
|
err.to_string()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(handle)
|
Ok(handle)
|
||||||
}
|
}
|
||||||
|
@ -81,23 +81,21 @@ pub fn flistxattr(fd: RawFd) -> Result<ListXAttr, nix::errno::Errno> {
|
|||||||
// it gets dynamically increased until big enough.
|
// it gets dynamically increased until big enough.
|
||||||
let mut size = 256;
|
let mut size = 256;
|
||||||
let mut buffer = vec::undefined(size);
|
let mut buffer = vec::undefined(size);
|
||||||
let mut bytes = unsafe {
|
let mut bytes =
|
||||||
libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len())
|
unsafe { libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len()) };
|
||||||
};
|
|
||||||
while bytes < 0 {
|
while bytes < 0 {
|
||||||
let err = Errno::last();
|
let err = Errno::last();
|
||||||
match err {
|
match err {
|
||||||
Errno::ERANGE => {
|
Errno::ERANGE => {
|
||||||
// Buffer was not big enough to fit the list, retry with double the size
|
// Buffer was not big enough to fit the list, retry with double the size
|
||||||
size = size.checked_mul(2).ok_or(Errno::ENOMEM)?;
|
size = size.checked_mul(2).ok_or(Errno::ENOMEM)?;
|
||||||
},
|
}
|
||||||
_ => return Err(err),
|
_ => return Err(err),
|
||||||
}
|
}
|
||||||
// Retry to read the list with new buffer
|
// Retry to read the list with new buffer
|
||||||
buffer.resize(size, 0);
|
buffer.resize(size, 0);
|
||||||
bytes = unsafe {
|
bytes =
|
||||||
libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len())
|
unsafe { libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len()) };
|
||||||
};
|
|
||||||
}
|
}
|
||||||
buffer.truncate(bytes as usize);
|
buffer.truncate(bytes as usize);
|
||||||
|
|
||||||
@ -112,7 +110,12 @@ pub fn fgetxattr(fd: RawFd, name: &CStr) -> Result<Vec<u8>, nix::errno::Errno> {
|
|||||||
let mut size = 256;
|
let mut size = 256;
|
||||||
let mut buffer = vec::undefined(size);
|
let mut buffer = vec::undefined(size);
|
||||||
let mut bytes = unsafe {
|
let mut bytes = unsafe {
|
||||||
libc::fgetxattr(fd, name.as_ptr(), buffer.as_mut_ptr() as *mut core::ffi::c_void, buffer.len())
|
libc::fgetxattr(
|
||||||
|
fd,
|
||||||
|
name.as_ptr(),
|
||||||
|
buffer.as_mut_ptr() as *mut core::ffi::c_void,
|
||||||
|
buffer.len(),
|
||||||
|
)
|
||||||
};
|
};
|
||||||
while bytes < 0 {
|
while bytes < 0 {
|
||||||
let err = Errno::last();
|
let err = Errno::last();
|
||||||
@ -120,12 +123,17 @@ pub fn fgetxattr(fd: RawFd, name: &CStr) -> Result<Vec<u8>, nix::errno::Errno> {
|
|||||||
Errno::ERANGE => {
|
Errno::ERANGE => {
|
||||||
// Buffer was not big enough to fit the value, retry with double the size
|
// Buffer was not big enough to fit the value, retry with double the size
|
||||||
size = size.checked_mul(2).ok_or(Errno::ENOMEM)?;
|
size = size.checked_mul(2).ok_or(Errno::ENOMEM)?;
|
||||||
},
|
}
|
||||||
_ => return Err(err),
|
_ => return Err(err),
|
||||||
}
|
}
|
||||||
buffer.resize(size, 0);
|
buffer.resize(size, 0);
|
||||||
bytes = unsafe {
|
bytes = unsafe {
|
||||||
libc::fgetxattr(fd, name.as_ptr() as *const libc::c_char, buffer.as_mut_ptr() as *mut core::ffi::c_void, buffer.len())
|
libc::fgetxattr(
|
||||||
|
fd,
|
||||||
|
name.as_ptr() as *const libc::c_char,
|
||||||
|
buffer.as_mut_ptr() as *mut core::ffi::c_void,
|
||||||
|
buffer.len(),
|
||||||
|
)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
buffer.resize(bytes as usize, 0);
|
buffer.resize(bytes as usize, 0);
|
||||||
@ -136,7 +144,13 @@ pub fn fgetxattr(fd: RawFd, name: &CStr) -> Result<Vec<u8>, nix::errno::Errno> {
|
|||||||
/// Set an extended attribute on a file descriptor.
|
/// Set an extended attribute on a file descriptor.
|
||||||
pub fn fsetxattr(fd: RawFd, name: &CStr, data: &[u8]) -> Result<(), nix::errno::Errno> {
|
pub fn fsetxattr(fd: RawFd, name: &CStr, data: &[u8]) -> Result<(), nix::errno::Errno> {
|
||||||
let result = unsafe {
|
let result = unsafe {
|
||||||
libc::fsetxattr(fd, name.as_ptr(), data.as_ptr() as *const libc::c_void, data.len(), 0)
|
libc::fsetxattr(
|
||||||
|
fd,
|
||||||
|
name.as_ptr(),
|
||||||
|
data.as_ptr() as *const libc::c_void,
|
||||||
|
data.len(),
|
||||||
|
0,
|
||||||
|
)
|
||||||
};
|
};
|
||||||
if result < 0 {
|
if result < 0 {
|
||||||
return Err(Errno::last());
|
return Err(Errno::last());
|
||||||
@ -156,7 +170,7 @@ pub fn is_security_capability(name: &CStr) -> bool {
|
|||||||
|
|
||||||
pub fn is_acl(name: &CStr) -> bool {
|
pub fn is_acl(name: &CStr) -> bool {
|
||||||
name.to_bytes() == xattr_acl_access().to_bytes()
|
name.to_bytes() == xattr_acl_access().to_bytes()
|
||||||
|| name.to_bytes() == xattr_acl_default().to_bytes()
|
|| name.to_bytes() == xattr_acl_default().to_bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the passed name buffer starts with a valid xattr namespace prefix
|
/// Check if the passed name buffer starts with a valid xattr namespace prefix
|
||||||
@ -203,7 +217,10 @@ mod tests {
|
|||||||
assert!(fsetxattr(fd, c_str!("user.empty"), b"").is_ok());
|
assert!(fsetxattr(fd, c_str!("user.empty"), b"").is_ok());
|
||||||
|
|
||||||
if nix::unistd::Uid::current() != nix::unistd::ROOT {
|
if nix::unistd::Uid::current() != nix::unistd::ROOT {
|
||||||
assert_eq!(fsetxattr(fd, c_str!("trusted.attribute0"), b"value0"), Err(Errno::EPERM));
|
assert_eq!(
|
||||||
|
fsetxattr(fd, c_str!("trusted.attribute0"), b"value0"),
|
||||||
|
Err(Errno::EPERM)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let v0 = fgetxattr(fd, c_str!("user.attribute0")).unwrap();
|
let v0 = fgetxattr(fd, c_str!("user.attribute0")).unwrap();
|
||||||
@ -211,7 +228,10 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(v0, b"value0".as_ref());
|
assert_eq!(v0, b"value0".as_ref());
|
||||||
assert_eq!(v1, b"".as_ref());
|
assert_eq!(v1, b"".as_ref());
|
||||||
assert_eq!(fgetxattr(fd, c_str!("user.attribute1")), Err(Errno::ENODATA));
|
assert_eq!(
|
||||||
|
fgetxattr(fd, c_str!("user.attribute1")),
|
||||||
|
Err(Errno::ENODATA)
|
||||||
|
);
|
||||||
|
|
||||||
std::fs::remove_file(&path).unwrap();
|
std::fs::remove_file(&path).unwrap();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user