tree-wide: use the new vec/io tools modules

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-05-22 15:02:16 +02:00
parent 6f0069ae1e
commit 8ea3b1d188
5 changed files with 21 additions and 17 deletions

View File

@ -14,6 +14,9 @@ use std::os::unix::io::AsRawFd;
use uuid::Uuid; use uuid::Uuid;
//use chrono::{Local, TimeZone}; //use chrono::{Local, TimeZone};
use crate::tools::io::ops::*;
use crate::tools::vec;
/// Header format definition for dynamic index files (`.dixd`) /// Header format definition for dynamic index files (`.dixd`)
#[repr(C)] #[repr(C)]
pub struct DynamicIndexHeader { pub struct DynamicIndexHeader {
@ -67,10 +70,9 @@ impl DynamicIndexReader {
// todo: use static assertion when available in rust // todo: use static assertion when available in rust
if header_size != 4096 { bail!("got unexpected header size for {:?}", path); } if header_size != 4096 { bail!("got unexpected header size for {:?}", path); }
let mut buffer = vec![0u8; header_size]; let buffer = file.read_exact_allocated(header_size)?;
file.read_exact(&mut buffer)?;
let header = unsafe { &mut * (buffer.as_ptr() as *mut DynamicIndexHeader) }; let header = unsafe { &* (buffer.as_ptr() as *const DynamicIndexHeader) };
if header.magic != *b"PROXMOX-DIDX" { if header.magic != *b"PROXMOX-DIDX" {
bail!("got unknown magic number for {:?}", path); bail!("got unknown magic number for {:?}", path);
@ -416,7 +418,7 @@ impl DynamicIndexWriter {
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let mut buffer = vec![0u8; header_size]; let mut buffer = vec::undefined(header_size);
let header = crate::tools::map_struct_mut::<DynamicIndexHeader>(&mut buffer)?; let header = crate::tools::map_struct_mut::<DynamicIndexHeader>(&mut buffer)?;
header.magic = *b"PROXMOX-DIDX"; header.magic = *b"PROXMOX-DIDX";

View File

@ -24,6 +24,8 @@ use nix::sys::stat::Mode;
use nix::errno::Errno; use nix::errno::Errno;
use nix::sys::stat::FileStat; use nix::sys::stat::FileStat;
use crate::tools::vec;
/// The format requires to build sorted directory lookup tables in /// The format requires to build sorted directory lookup tables in
/// memory, so we restrict the number of allowed entries to limit /// memory, so we restrict the number of allowed entries to limit
/// maximum memory usage. /// maximum memory usage.
@ -480,7 +482,7 @@ impl <'a, W: Write> Encoder<'a, W> {
} }
} else if ifmt == libc::S_IFLNK { } else if ifmt == libc::S_IFLNK {
let mut buffer = [0u8; libc::PATH_MAX as usize]; let mut buffer = vec::undefined(libc::PATH_MAX as usize);
let res = filename.with_nix_path(|cstr| { let res = filename.with_nix_path(|cstr| {
unsafe { libc::readlinkat(rawfd, cstr.as_ptr(), buffer.as_mut_ptr() as *mut libc::c_char, buffer.len()-1) } unsafe { libc::readlinkat(rawfd, cstr.as_ptr(), buffer.as_mut_ptr() as *mut libc::c_char, buffer.len()-1) }

View File

@ -20,6 +20,9 @@ use nix::fcntl::OFlag;
use nix::sys::stat::Mode; use nix::sys::stat::Mode;
use nix::errno::Errno; use nix::errno::Errno;
use nix::NixPath; use nix::NixPath;
use crate::tools::io::ops::*;
use crate::tools::vec;
use crate::tools::xattr; use crate::tools::xattr;
// This one need Read, but works without Seek // This one need Read, but works without Seek
@ -34,7 +37,7 @@ const HEADER_SIZE: u64 = std::mem::size_of::<CaFormatHeader>() as u64;
impl <'a, R: Read> SequentialDecoder<'a, R> { impl <'a, R: Read> SequentialDecoder<'a, R> {
pub fn new(reader: &'a mut R, no_xattrs: bool, no_fcaps: bool) -> Self { pub fn new(reader: &'a mut R, no_xattrs: bool, no_fcaps: bool) -> Self {
let skip_buffer = vec![0u8; 64*1024]; let skip_buffer = vec::undefined(64*1024);
let mut feature_flags = CA_FORMAT_DEFAULT; let mut feature_flags = CA_FORMAT_DEFAULT;
if no_xattrs { if no_xattrs {
feature_flags ^= CA_FORMAT_WITH_XATTRS; feature_flags ^= CA_FORMAT_WITH_XATTRS;
@ -78,8 +81,7 @@ impl <'a, R: Read> SequentialDecoder<'a, R> {
bail!("link target too long ({}).", target_len); bail!("link target too long ({}).", target_len);
} }
let mut buffer = vec![0u8; target_len as usize]; let mut buffer = self.reader.read_exact_allocated(target_len as usize)?;
self.reader.read_exact(&mut buffer)?;
let last_byte = buffer.pop().unwrap(); let last_byte = buffer.pop().unwrap();
if last_byte != 0u8 { if last_byte != 0u8 {
@ -118,8 +120,7 @@ impl <'a, R: Read> SequentialDecoder<'a, R> {
bail!("filename too long ({}).", name_len); bail!("filename too long ({}).", name_len);
} }
let mut buffer = vec![0u8; name_len as usize]; let mut buffer = self.reader.read_exact_allocated(name_len as usize)?;
self.reader.read_exact(&mut buffer)?;
let last_byte = buffer.pop().unwrap(); let last_byte = buffer.pop().unwrap();
if last_byte != 0u8 { if last_byte != 0u8 {
@ -147,8 +148,7 @@ impl <'a, R: Read> SequentialDecoder<'a, R> {
} }
fn read_xattr(&mut self, size: usize) -> Result<CaFormatXAttr, Error> { fn read_xattr(&mut self, size: usize) -> Result<CaFormatXAttr, Error> {
let mut buffer = vec![0u8; size]; let buffer = self.reader.read_exact_allocated(size)?;
self.reader.read_exact(&mut buffer)?;
match buffer.iter().position(|c| *c == '\0' as u8) { match buffer.iter().position(|c| *c == '\0' as u8) {
Some(pos) => { Some(pos) => {
@ -172,8 +172,7 @@ impl <'a, R: Read> SequentialDecoder<'a, R> {
} }
fn read_fcaps(&mut self, size: usize) -> Result<CaFormatFCaps, Error> { fn read_fcaps(&mut self, size: usize) -> Result<CaFormatFCaps, Error> {
let mut buffer = vec![0u8; size]; let buffer = self.reader.read_exact_allocated(size)?;
self.reader.read_exact(&mut buffer)?;
Ok(CaFormatFCaps { data: buffer }) Ok(CaFormatFCaps { data: buffer })
} }

View File

@ -302,7 +302,7 @@ pub fn file_chunker<C, R>(
if chunk_size > READ_BUFFER_SIZE { bail!("chunk size too large!"); } if chunk_size > READ_BUFFER_SIZE { bail!("chunk size too large!"); }
let mut buf = vec![0u8; READ_BUFFER_SIZE]; let mut buf = vec::undefined(READ_BUFFER_SIZE);
let mut pos = 0; let mut pos = 0;
let mut file_pos = 0; let mut file_pos = 0;

View File

@ -5,12 +5,13 @@ extern crate libc;
use std::os::unix::io::RawFd; use std::os::unix::io::RawFd;
use nix::errno::Errno; use nix::errno::Errno;
use crate::pxar::{CaFormatXAttr, CaFormatFCaps}; use crate::pxar::{CaFormatXAttr, CaFormatFCaps};
use crate::tools::vec;
pub fn flistxattr(fd: RawFd) -> Result<Vec<u8>, nix::errno::Errno> { pub fn flistxattr(fd: RawFd) -> Result<Vec<u8>, nix::errno::Errno> {
// Initial buffer size for the attribute list, if content does not fit // Initial buffer size for the attribute list, if content does not fit
// 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![0u8; size]; let mut buffer = vec::undefined(size);
let mut bytes = unsafe { let mut bytes = unsafe {
libc::flistxattr(fd, buffer.as_mut_ptr() as *mut i8, buffer.len()) libc::flistxattr(fd, buffer.as_mut_ptr() as *mut i8, buffer.len())
}; };
@ -37,7 +38,7 @@ pub fn flistxattr(fd: RawFd) -> Result<Vec<u8>, nix::errno::Errno> {
pub fn fgetxattr(fd: RawFd, name: &[u8]) -> Result<Vec<u8>, nix::errno::Errno> { pub fn fgetxattr(fd: RawFd, name: &[u8]) -> Result<Vec<u8>, nix::errno::Errno> {
let mut size = 256; let mut size = 256;
let mut buffer = vec![0u8; size]; let mut buffer = vec::undefined(size);
let mut bytes = unsafe { let mut bytes = unsafe {
libc::fgetxattr(fd, name.as_ptr() as *const i8, buffer.as_mut_ptr() as *mut core::ffi::c_void, buffer.len()) libc::fgetxattr(fd, name.as_ptr() as *const i8, buffer.as_mut_ptr() as *mut core::ffi::c_void, buffer.len())
}; };