do not read/write/set_len to padding files

This commit is contained in:
Igor Katson 2024-10-14 15:51:25 +01:00
parent 32d658e2d9
commit af7a0ddb4f
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 36 additions and 17 deletions

View file

@ -21,6 +21,7 @@ use crate::{
pub fn update_hash_from_file<Sha1: ISha1>( pub fn update_hash_from_file<Sha1: ISha1>(
file_id: usize, file_id: usize,
file_info: &FileInfo,
mut pos: u64, mut pos: u64,
files: &dyn TorrentStorage, files: &dyn TorrentStorage,
hash: &mut Sha1, hash: &mut Sha1,
@ -30,9 +31,15 @@ pub fn update_hash_from_file<Sha1: ISha1>(
let mut read = 0; let mut read = 0;
while bytes_to_read > 0 { while bytes_to_read > 0 {
let chunk = std::cmp::min(buf.len(), bytes_to_read); let chunk = std::cmp::min(buf.len(), bytes_to_read);
files if file_info.attrs.padding {
.pread_exact(file_id, pos, &mut buf[..chunk]) buf[..chunk].fill(0);
.with_context(|| format!("failed reading chunk of size {chunk}, read so far {read}"))?; } else {
files
.pread_exact(file_id, pos, &mut buf[..chunk])
.with_context(|| {
format!("failed reading chunk of size {chunk}, read so far {read}")
})?;
}
bytes_to_read -= chunk; bytes_to_read -= chunk;
read += chunk; read += chunk;
pos += chunk as u64; pos += chunk as u64;
@ -138,6 +145,7 @@ impl<'a> FileOps<'a> {
if let Err(err) = update_hash_from_file( if let Err(err) = update_hash_from_file(
current_file.index, current_file.index,
current_file.fi,
pos, pos,
self.files, self.files,
&mut computed_hash, &mut computed_hash,
@ -199,6 +207,7 @@ impl<'a> FileOps<'a> {
); );
update_hash_from_file( update_hash_from_file(
file_idx, file_idx,
fi,
absolute_offset, absolute_offset,
self.files, self.files,
&mut h, &mut h,
@ -250,7 +259,8 @@ impl<'a> FileOps<'a> {
let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info); let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info);
let mut buf = result_buf; let mut buf = result_buf;
for (file_idx, file_len) in self.file_infos.iter().map(|d| d.len).enumerate() { for (file_idx, file_info) in self.file_infos.iter().enumerate() {
let file_len = file_info.len;
if absolute_offset > file_len { if absolute_offset > file_len {
absolute_offset -= file_len; absolute_offset -= file_len;
continue; continue;
@ -266,11 +276,15 @@ impl<'a> FileOps<'a> {
absolute_offset, absolute_offset,
&chunk_info &chunk_info
); );
self.files if file_info.attrs.padding {
.pread_exact(file_idx, absolute_offset, &mut buf[..to_read_in_file]) buf[..to_read_in_file].fill(0);
.with_context(|| { } else {
format!("error reading {file_idx} bytes, file_id: {to_read_in_file}") self.files
})?; .pread_exact(file_idx, absolute_offset, &mut buf[..to_read_in_file])
.with_context(|| {
format!("error reading {file_idx} bytes, file_id: {to_read_in_file}")
})?;
}
buf = &mut buf[to_read_in_file..]; buf = &mut buf[to_read_in_file..];
@ -316,14 +330,16 @@ impl<'a> FileOps<'a> {
to_write, to_write,
absolute_offset absolute_offset
); );
self.files if !file_info.attrs.padding {
.pwrite_all(file_idx, absolute_offset, &buf[..to_write]) self.files
.with_context(|| { .pwrite_all(file_idx, absolute_offset, &buf[..to_write])
format!( .with_context(|| {
"error writing to file {file_idx} (\"{:?}\")", format!(
file_info.relative_filename "error writing to file {file_idx} (\"{:?}\")",
) file_info.relative_filename
})?; )
})?;
}
buf = &buf[to_write..]; buf = &buf[to_write..];
if buf.is_empty() { if buf.is_empty() {
break; break;

View file

@ -245,6 +245,9 @@ impl TorrentStateInitializing {
.unwrap_or(true) .unwrap_or(true)
{ {
let now = Instant::now(); let now = Instant::now();
if fi.attrs.padding {
continue;
}
if let Err(err) = self.files.ensure_file_length(idx, fi.len) { if let Err(err) = self.files.ensure_file_length(idx, fi.len) {
warn!( warn!(
"Error setting length for file {:?} to {}: {:#?}", "Error setting length for file {:?} to {}: {:#?}",