-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc598b8
commit 88a8344
Showing
5 changed files
with
94 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
/out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::{ | ||
fs::{create_dir_all, File, OpenOptions}, | ||
os::unix::fs::FileExt, | ||
}; | ||
|
||
use crate::metainfo::Info; | ||
|
||
#[derive(Debug)] | ||
pub struct FileManager { | ||
piece_length: u64, | ||
files: Vec<(File, u64)>, | ||
} | ||
|
||
impl FileManager { | ||
pub fn new(output_dir: String, info_dict: &Info) -> Self { | ||
create_dir_all(&output_dir).unwrap(); | ||
match info_dict { | ||
Info::SingleFile(info) => { | ||
let file_path = format!("{}/{}", output_dir, info.name); | ||
let file = OpenOptions::new() | ||
.read(true) | ||
.write(true) | ||
.create(true) | ||
.open(file_path) | ||
.unwrap(); | ||
FileManager { | ||
piece_length: info.base_info.piece_length as u64, | ||
files: vec![(file, info.length)], | ||
} | ||
} | ||
Info::MultiFile(info) => { | ||
let mut files = Vec::new(); | ||
for file_info in &info.files { | ||
let file_path = format!("{}/{}", output_dir, file_info.path.join("/")); | ||
let file = OpenOptions::new() | ||
.read(true) | ||
.write(true) | ||
.create(true) | ||
.open(file_path) | ||
.unwrap(); | ||
files.push((file, file_info.length)); | ||
} | ||
FileManager { | ||
piece_length: info.base_info.piece_length as u64, | ||
files, | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn save_block(&mut self, piece_index: usize, begin: u32, data: Vec<u8>) { | ||
let byte_offset = self.piece_length * piece_index as u64 + begin as u64; | ||
let mut accumulated_size = 0; | ||
for (file, file_size) in &mut self.files { | ||
if byte_offset < accumulated_size + *file_size { | ||
file.write_at(&data, byte_offset - accumulated_size) | ||
.unwrap(); | ||
break; | ||
} | ||
accumulated_size += *file_size; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters