Skip to content

Commit

Permalink
Testing (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmielchen authored Feb 1, 2024
1 parent 59fc668 commit dae4d44
Show file tree
Hide file tree
Showing 8 changed files with 568 additions and 128 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
Cargo.lock
Cargo.lock
.test/
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
edition = "2021"

name = "objstr"
version = "0.1.0"
version = "0.1.1"

description = "File stream abstraction for Rust"
repository = "https://github.com/maxmielchen/objstr-rs"
Expand Down
2 changes: 1 addition & 1 deletion src/file/file_obj_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ObjStr for FileObjStr {
match pos {
SeekFrom::Start(pos) => {
jump_stream_start(&mut self.file);
seek_backward_n(&mut self.file, pos)?;
seek_forward_n(&mut self.file, pos)?;
},
SeekFrom::End(pos) => {
jump_stream_end(&mut self.file);
Expand Down
182 changes: 153 additions & 29 deletions src/file/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,68 @@
use core::panic;
use std::{fs::File, io::{Error, ErrorKind, Read as _, Seek as _, SeekFrom, Write as _}};

pub const EMPTY: [u8; 1] = [0; 1];
pub const OP_LEN: u8 = 4;

pub fn write_empty_byte(file: &mut File) {
file.write(&EMPTY).unwrap();
let res = file.write(&EMPTY);
if let Err(e) = res {
panic!("Failed to write empty byte: {}", e);
}
if res.unwrap() != EMPTY.len() {
panic!("Failed to write empty byte: {}", "Write length mismatch.");
}
}

pub fn cut(file: &mut File) {

let pos = file.seek(SeekFrom::Current(0)).unwrap();
let pos = file.seek(SeekFrom::Current(0));

if let Err(e) = pos {
panic!("Failed to cut file: {}", e);
}

let pos = pos.unwrap();

let res = file.set_len(pos);

if let Err(e) = res {
panic!("Failed to cut file: {}", e);
}

file.set_len(pos).unwrap();
write_empty_byte(file);
}

pub fn truncate(file: &mut File) {
file.set_len(0).unwrap();
let res = file.set_len(0);

if let Err(e) = res {
panic!("Failed to truncate file: {}", e);
}

write_empty_byte(file);
jump_stream_start(file);
}

pub fn jump_stream_start(file: &mut File) {
file.seek(SeekFrom::Start(0)).unwrap();
let res = file.seek(SeekFrom::Start(0));

if let Err(e) = res {
panic!("Failed to jump stream start: {}", e);
}
}

pub fn jump_stream_end(file: &mut File) {
file.seek(SeekFrom::End(-1)).unwrap();
let res = file.seek(SeekFrom::End(-1));

if let Err(e) = res {
panic!("Failed to jump stream end: {}", e);
}
}

pub fn catch_stream_read(file: &mut File, read: usize, predict: usize) -> Result<(), Error> {
pub fn catch_stream_read(file: &mut File, read: usize, predict: usize, fix_fn: fn(file: &mut File)) -> Result<(), Error> {
if read != predict {
jump_stream_start(file);
fix_fn(file);
return Err(
Error::new(
ErrorKind::UnexpectedEof,
Expand Down Expand Up @@ -60,7 +91,11 @@ pub fn write(file: &mut File, data: Vec<u8>) -> Result<(), Error> {
obj.extend_from_slice(&data);
obj.extend_from_slice(&op);

file.write(&obj).unwrap();
let res = file.write(&obj);

if let Err(e) = res {
panic!("Failed to write data: {}", e);
}

Ok(())
}
Expand All @@ -69,28 +104,62 @@ pub fn read(file: &mut File) -> Result<Vec<u8>, Error> {

let mut len_buf_left: [u8; OP_LEN as usize] = [0; OP_LEN as usize];
let res = file.read(&mut len_buf_left);

if let Err(e) = res {
panic!("Failed to read data: {}", e);
}

let res = res.unwrap();

catch_stream_read(
file,
res?,
OP_LEN as usize
res,
OP_LEN as usize,
jump_stream_end
)?;

let len_left = u32::from_be_bytes(len_buf_left) as usize;

let mut data = vec![0; len_left];
let res = file.read(&mut data);
catch_stream_read(

if let Err(e) = res {
panic!("Failed to read data: {}", e);
}

let res = res.unwrap();

let catch = catch_stream_read(
file,
res?,
len_left
)?;
res,
len_left,
jump_stream_start
);

if let Err(e) = catch {
panic!("Failed to read data: {}", e);
}

let mut len_buf_right: [u8; OP_LEN as usize] = [0; OP_LEN as usize];
let res = file.read(&mut len_buf_right);
catch_stream_read(

if let Err(e) = res {
panic!("Failed to read data: {}", e);
}

let res = res.unwrap();

let catch = catch_stream_read(
file,
res?,
OP_LEN as usize
)?;
res,
OP_LEN as usize,
jump_stream_start
);

if let Err(e) = catch {
panic!("Failed to read data: {}", e);
}

let len_right = u32::from_be_bytes(len_buf_right) as usize;

if len_left != len_right {
Expand All @@ -104,35 +173,79 @@ pub fn seek_forward(file: &mut File) -> Result<(), Error> {
let mut len_buf: [u8; OP_LEN as usize] = [0; OP_LEN as usize];

let res = file.read(&mut len_buf);

if let Err(e) = res {
panic!("Failed to read data: {}", e);
}

catch_stream_read(
file,
res?,
OP_LEN as usize
OP_LEN as usize,
jump_stream_end
)?;

let len = u32::from_be_bytes(len_buf) as i64;

file.seek(SeekFrom::Current(len)).unwrap();
let catch = file.seek(SeekFrom::Current(len));

file.seek(SeekFrom::Current(OP_LEN as i64)).unwrap();
if let Err(e) = catch {
panic!("Failed to seek forward: {}", e);
}

let catch = file.seek(SeekFrom::Current(OP_LEN as i64));

if let Err(e) = catch {
panic!("Failed to seek forward: {}", e);
}

Ok(())
}

pub fn seek_backward(file: &mut File) -> Result<(), Error> {
let mut len_buf: [u8; OP_LEN as usize] = [0; OP_LEN as usize];

file.seek(SeekFrom::Current(-(OP_LEN as i64))).unwrap();
let res = file.seek(SeekFrom::Current(-(OP_LEN as i64)));

if let Err(e) = res {
if e.kind() == ErrorKind::InvalidInput {
return Err(
Error::new(
ErrorKind::UnexpectedEof,
"Reached stream start."
)
)
}
panic!("Failed to seek backward: {}", e);
}

let res = file.read(&mut len_buf);
catch_stream_read(
let catch = catch_stream_read(
file,
res?,
OP_LEN as usize
)?;
OP_LEN as usize,
jump_stream_start
);

if let Err(e) = catch {
panic!("Failed to seek backward: {}", e);
}

let len = u32::from_be_bytes(len_buf) as i64;

file.seek(SeekFrom::Current(-len -2*OP_LEN as i64))?;
let res = file.seek(SeekFrom::Current(-len -2*OP_LEN as i64));

if let Err(e) = res {
if e.kind() == ErrorKind::InvalidInput {
return Err(
Error::new(
ErrorKind::UnexpectedEof,
"Reached stream start."
)
)
}
panic!("Failed to seek backward: {}", e);
}

Ok(())
}
Expand All @@ -154,14 +267,25 @@ pub fn seek_backward_n(file: &mut File, n: u64) -> Result<(), Error> {
pub fn inner_len(file: &mut File) -> Result<u32, Error> {
let mut len_buf: [u8; OP_LEN as usize] = [0; OP_LEN as usize];
let res = file.read(&mut len_buf);

if let Err(e) = res {
panic!("Failed to read data: {}", e);
}

catch_stream_read(
file,
res?,
OP_LEN as usize
OP_LEN as usize,
jump_stream_end
)?;

let len = u32::from_be_bytes(len_buf) as u32;

file.seek(SeekFrom::Current(-(OP_LEN as i64)))?;
let res = file.seek(SeekFrom::Current(-(OP_LEN as i64)));

if let Err(e) = res {
panic!("Failed to seek backward: {}", e);
}

Ok(len)
}
Expand Down
Loading

0 comments on commit dae4d44

Please sign in to comment.