ableos_userland/libraries/tar/src/lib.rs
2022-12-05 00:47:52 -06:00

120 lines
3.2 KiB
Rust

#![no_std]
use versioning::Version;
pub const VERSION: Version = Version::new(0, 1, 1);
pub struct Header {
pub filename: [char; 100],
pub filemode: [char; 8],
pub user_id: [char; 8],
pub group_id: [char; 8],
pub size: [char; 12],
pub modified_time: [char; 12],
pub checksum: [char; 8],
pub typeflag: char,
}
impl From<[char; 512]> for Header {
fn from(item: [char; 512]) -> Self {
let mut filename: [char; 100] = ['\0'; 100];
filename[..100].clone_from_slice(&item[0..100]);
let mut filemode: [char; 8] = ['\0'; 8];
filemode[..8].clone_from_slice(&item[100..108]);
let mut user_id: [char; 8] = ['\0'; 8];
user_id[..8].clone_from_slice(&item[108..116]);
let mut group_id: [char; 8] = ['\0'; 8];
group_id[..8].clone_from_slice(&item[116..124]);
let mut size: [char; 12] = ['\0'; 12];
size[..12].clone_from_slice(&item[124..136]);
let mut modified_time: [char; 12] = ['\0'; 12];
modified_time[..12].clone_from_slice(&item[136..148]);
let mut checksum: [char; 8] = ['\0'; 8];
checksum[..8].clone_from_slice(&item[148..154]);
let typeflag: char = item[155];
Self {
filename,
filemode,
user_id,
group_id,
size,
modified_time,
checksum,
typeflag,
}
}
}
impl Into<[char; 512]> for Header {
fn into(self) -> [char; 512] {
let mut header: [char; 512] = ['\0'; 512];
header[..100].clone_from_slice(&self.filename);
header[100..108].clone_from_slice(&self.filemode);
header[108..116].clone_from_slice(&self.user_id);
header[116..124].clone_from_slice(&self.group_id);
header[124..136].clone_from_slice(&self.size);
header[136..148].clone_from_slice(&self.modified_time);
header[148..156].clone_from_slice(&self.checksum);
header[155] = self.typeflag;
header
}
}
// TODO Write tests for this library
#[test]
fn from2tar() {
let mut filename = ['\0'; 100];
for (i, c) in "file.txt".chars().enumerate() {
filename[i] = c;
}
let header = Header {
filename,
filemode: ['6', '4', '4', ' ', ' ', ' ', ' ', '\0'],
user_id: ['0', '0', '0', '0', '0', '0', '0', '0'],
group_id: ['0', '0', '0', '0', '0', '0', '0', '0'],
size: ['8', '1', '9', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'],
modified_time: ['1', '6', '0', '7', '1', '1', '4', '9', '8', '0', ' ', '\0'],
checksum: ['0', '0', '0', '0', '0', '0', ' ', '\0'],
typeflag: '0',
};
let _array: [char; 512] = header.try_into().unwrap();
// assert_eq!(array)
}
pub enum TarError {
InvalidChecksum,
InvalidFileData,
InvalidTarFormat,
InvalidTarHeader,
UnsupportedCompression,
UnsupportedEncryption,
UnsupportedTarFormat,
}
#[derive(Debug)]
pub enum FileType {
Directory,
Regular,
Other(char),
}
impl From<char> for FileType {
fn from(c: char) -> Self {
match c {
'0' | '\0' => FileType::Regular,
'5' => FileType::Directory,
_ => FileType::Other(c),
}
}
}