This commit is contained in:
pjht 2024-08-06 19:43:26 -05:00
parent 6386665f34
commit a5639f879e
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E

View File

@ -1,7 +1,12 @@
use std::{fs::File, io::{Read, Seek}, os::mikros::{ipc, syscalls}, path::PathBuf, sync::Arc, usize}; use std::{
fs::File,
io::{Read, Seek},
os::mikros::{ipc, syscalls},
path::PathBuf,
sync::Arc,
};
use parking_lot::RwLock; use parking_lot::RwLock;
use syslog_rpc::{BinaryMessage, Message};
use tar::Archive; use tar::Archive;
#[derive(Clone)] #[derive(Clone)]
@ -14,12 +19,16 @@ impl fs_rpc::Server for Serv {
fn mount(&self, dev: &std::path::Path) -> Result<u64, ()> { fn mount(&self, dev: &std::path::Path) -> Result<u64, ()> {
let archive = File::open(dev).map_err(|_| ())?; let archive = File::open(dev).map_err(|_| ())?;
let mut archive = Archive::new(archive); let mut archive = Archive::new(archive);
let entries = archive.entries_with_seek().unwrap().map(|entry| { let entries = archive
let entry = entry.unwrap(); .entries_with_seek()
let path = entry.path().unwrap().into_owned(); .unwrap()
let file_offset = entry.raw_file_position(); .map(|entry| {
(path, file_offset, entry.size() as usize) let entry = entry.unwrap();
}).collect(); let path = entry.path().unwrap().into_owned();
let file_offset = entry.raw_file_position();
(path, file_offset, entry.size() as usize)
})
.collect();
self.mounts.write().push((archive.into_inner(), entries)); self.mounts.write().push((archive.into_inner(), entries));
Ok((self.mounts.read().len() - 1) as u64) Ok((self.mounts.read().len() - 1) as u64)
} }
@ -27,8 +36,15 @@ impl fs_rpc::Server for Serv {
fn open(&self, path: &std::path::Path, mount_id: u64) -> Result<(Option<u64>, u64), ()> { fn open(&self, path: &std::path::Path, mount_id: u64) -> Result<(Option<u64>, u64), ()> {
let mounts = self.mounts.read(); let mounts = self.mounts.read();
let mount = &mounts[mount_id as usize]; let mount = &mounts[mount_id as usize];
let (&file_offset, &file_size) = mount.1.iter().find(|(entry_path, _offset, _size)| entry_path == path).map(|(_x, y, z)| (y, z)).ok_or(())?; let (&file_offset, &file_size) = mount
self.files.write().push((mount_id as usize, file_offset, file_size)); .1
.iter()
.find(|(entry_path, _offset, _size)| entry_path == path)
.map(|(_x, y, z)| (y, z))
.ok_or(())?;
self.files
.write()
.push((mount_id as usize, file_offset, file_size));
Ok((None, (self.files.read().len() - 1) as u64)) Ok((None, (self.files.read().len() - 1) as u64))
} }
} }
@ -41,10 +57,12 @@ impl file_rpc::Server for Serv {
return Ok(Vec::new()); return Ok(Vec::new());
}; };
let mounts = self.mounts.read(); let mounts = self.mounts.read();
let mount = &mounts[mount_id as usize]; let mount = &mounts[mount_id];
(&mount.0).seek(std::io::SeekFrom::Start(file_offset + pos)).unwrap(); (&mount.0)
.seek(std::io::SeekFrom::Start(file_offset + pos))
.unwrap();
let mut buf = vec![0; read_len]; let mut buf = vec![0; read_len];
let read_bytes = (&mount.0).read(&mut buf).map_err(|_| ()) ?; let read_bytes = (&mount.0).read(&mut buf).map_err(|_| ())?;
buf.truncate(read_bytes); buf.truncate(read_bytes);
Ok(buf) Ok(buf)
} }
@ -83,14 +101,14 @@ fn main() {
break; break;
} }
} }
syslog_rpc::Client::new(syslog_pid).send_message(Message { syslog_rpc::Client::new(syslog_pid)
from: "tarfs".to_string(), .send_text_binary_message(
text: Some("Tar archive fs initialized".to_string()), "tarfs".to_string(),
binary: Some(BinaryMessage { "Tar archive fs initialized".to_string(),
kind: 0, 0,
data: vec![], [],
}), )
}).unwrap(); .unwrap();
loop { loop {
ipc::process_messages() ipc::process_messages()
} }