From 26f3d26bc27b1491453fdf4afe16e825d338da54 Mon Sep 17 00:00:00 2001 From: Able Date: Mon, 5 Dec 2022 00:06:51 -0600 Subject: [PATCH] update Tar! --- libraries/tar/src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/libraries/tar/src/lib.rs b/libraries/tar/src/lib.rs index a09a55e..fecb1e5 100644 --- a/libraries/tar/src/lib.rs +++ b/libraries/tar/src/lib.rs @@ -53,3 +53,69 @@ impl From<[char; 512]> for Header { } } } + +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) +} + +enum Error { + InvalidChecksum, + InvalidFileData, + InvalidTarFormat, + InvalidTarHeader, + UnsupportedCompression, + UnsupportedEncryption, + UnsupportedTarFormat, +} + +#[derive(Debug)] +enum FileType { + Directory, + Regular, + Other(char), +} + +impl From for FileType { + fn from(c: char) -> Self { + match c { + '0' | '\0' => FileType::Regular, + '5' => FileType::Directory, + _ => FileType::Other(c), + } + } +}