Skip to content

Commit

Permalink
continue source handler
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Jun 28, 2024
1 parent 35b24ba commit ab56f4b
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 55 deletions.
4 changes: 2 additions & 2 deletions satrs/src/cfdp/dest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ impl<
// TODO: This is the only remaining function which uses std.. the easiest way would
// probably be to use a static pre-allocated dest path buffer to store any concatenated
// paths.
if dest_path.exists() && self.vfs.is_dir(dest_path.to_str().unwrap()) {
if dest_path.exists() && self.vfs.is_dir(dest_path.to_str().unwrap())? {
// Create new destination path by concatenating the last part of the source source
// name and the destination folder. For example, for a source file of /tmp/hello.txt
// and a destination name of /home/test, the resulting file name should be
Expand All @@ -696,7 +696,7 @@ impl<
self.tparams.file_properties.dest_path_buf.push(source_name);
}
let dest_path_str = self.tparams.file_properties.dest_path_buf.to_str().unwrap();
if self.vfs.exists(dest_path_str) {
if self.vfs.exists(dest_path_str)? {
self.vfs.truncate_file(dest_path_str)?;
} else {
self.vfs.create_file(dest_path_str)?;
Expand Down
106 changes: 64 additions & 42 deletions satrs/src/cfdp/filestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@ pub trait VirtualFilestore {
path.file_name().and_then(|name| name.to_str())
}

fn is_file(&self, path: &str) -> bool;
fn is_file(&self, path: &str) -> Result<bool, FilestoreError>;

fn is_dir(&self, path: &str) -> bool {
!self.is_file(path)
fn is_dir(&self, path: &str) -> Result<bool, FilestoreError> {
Ok(!self.is_file(path)?)
}

fn exists(&self, path: &str) -> bool;
fn exists(&self, path: &str) -> Result<bool, FilestoreError>;

fn file_size(&self, path: &str) -> Result<u64, FilestoreError>;

/// This special function is the CFDP specific abstraction to verify the checksum of a file.
/// This allows to keep OS specific details like reading the whole file in the most efficient
Expand Down Expand Up @@ -160,29 +162,29 @@ pub mod std_mod {

impl VirtualFilestore for NativeFilestore {
fn create_file(&self, file_path: &str) -> Result<(), FilestoreError> {
if self.exists(file_path) {
if self.exists(file_path)? {
return Err(FilestoreError::FileAlreadyExists);
}
File::create(file_path)?;
Ok(())
}

fn remove_file(&self, file_path: &str) -> Result<(), FilestoreError> {
if !self.exists(file_path) {
if !self.exists(file_path)? {
return Err(FilestoreError::FileDoesNotExist);
}
if !self.is_file(file_path) {
if !self.is_file(file_path)? {
return Err(FilestoreError::IsNotFile);
}
fs::remove_file(file_path)?;
Ok(())
}

fn truncate_file(&self, file_path: &str) -> Result<(), FilestoreError> {
if !self.exists(file_path) {
if !self.exists(file_path)? {
return Err(FilestoreError::FileDoesNotExist);
}
if !self.is_file(file_path) {
if !self.is_file(file_path)? {
return Err(FilestoreError::IsNotFile);
}
OpenOptions::new()
Expand All @@ -201,10 +203,10 @@ pub mod std_mod {
}

fn remove_dir(&self, dir_path: &str, all: bool) -> Result<(), FilestoreError> {
if !self.exists(dir_path) {
if !self.exists(dir_path)? {
return Err(FilestoreError::DirDoesNotExist);
}
if !self.is_dir(dir_path) {
if !self.is_dir(dir_path)? {
return Err(FilestoreError::IsNotDirectory);
}
if !all {
Expand All @@ -229,10 +231,10 @@ pub mod std_mod {
}
.into());
}
if !self.exists(file_name) {
if !self.exists(file_name)? {
return Err(FilestoreError::FileDoesNotExist);
}
if !self.is_file(file_name) {
if !self.is_file(file_name)? {
return Err(FilestoreError::IsNotFile);
}
let mut file = File::open(file_name)?;
Expand All @@ -242,10 +244,10 @@ pub mod std_mod {
}

fn write_data(&self, file: &str, offset: u64, buf: &[u8]) -> Result<(), FilestoreError> {
if !self.exists(file) {
if !self.exists(file)? {
return Err(FilestoreError::FileDoesNotExist);
}
if !self.is_file(file) {
if !self.is_file(file)? {
return Err(FilestoreError::IsNotFile);
}
let mut file = OpenOptions::new().write(true).open(file)?;
Expand All @@ -254,17 +256,28 @@ pub mod std_mod {
Ok(())
}

fn is_file(&self, path: &str) -> bool {
let path = Path::new(path);
path.is_file()
fn is_file(&self, str_path: &str) -> Result<bool, FilestoreError> {
let path = Path::new(str_path);
if !self.exists(str_path)? {
return Err(FilestoreError::FileDoesNotExist);
}
Ok(path.is_file())
}

fn exists(&self, path: &str) -> bool {
fn exists(&self, path: &str) -> Result<bool, FilestoreError> {
let path = Path::new(path);
if !path.exists() {
return false;
Ok(self.exists_internal(path))
}

fn file_size(&self, str_path: &str) -> Result<u64, FilestoreError> {
let path = Path::new(str_path);
if !self.exists_internal(path) {
return Err(FilestoreError::FileDoesNotExist);
}
true
if !path.is_file() {
return Err(FilestoreError::IsNotFile);
}
Ok(path.metadata()?.len())
}

fn checksum_verify(
Expand Down Expand Up @@ -324,6 +337,13 @@ pub mod std_mod {
}
Ok(checksum)
}

fn exists_internal(&self, path: &Path) -> bool {
if !path.exists() {
return false;
}
true
}
}
}

Expand All @@ -350,32 +370,34 @@ mod tests {
assert!(result.is_ok());
let path = Path::new(&file_path);
assert!(path.exists());
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()));
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()).unwrap());
}

#[test]
fn test_basic_native_fs_file_exists() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt");
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
NATIVE_FS
.create_file(file_path.to_str().expect("getting str for file failed"))
.unwrap();
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()));
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()).unwrap());
}

#[test]
fn test_basic_native_fs_dir_exists() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let dir_path = tmpdir.path().join("testdir");
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()));
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
NATIVE_FS
.create_dir(dir_path.to_str().expect("getting str for file failed"))
.unwrap();
assert!(NATIVE_FS.exists(dir_path.to_str().unwrap()));
assert!(NATIVE_FS.is_dir(dir_path.as_path().to_str().unwrap()));
assert!(NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
assert!(NATIVE_FS
.is_dir(dir_path.as_path().to_str().unwrap())
.unwrap());
}

#[test]
Expand All @@ -385,23 +407,23 @@ mod tests {
NATIVE_FS
.create_file(file_path.to_str().expect("getting str for file failed"))
.expect("creating file failed");
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
NATIVE_FS
.remove_file(file_path.to_str().unwrap())
.expect("removing file failed");
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
}

#[test]
fn test_basic_native_fs_write() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt");
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
NATIVE_FS
.create_file(file_path.to_str().expect("getting str for file failed"))
.unwrap();
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()));
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()).unwrap());
println!("{}", file_path.to_str().unwrap());
let write_data = "hello world\n";
NATIVE_FS
Expand All @@ -415,12 +437,12 @@ mod tests {
fn test_basic_native_fs_read() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt");
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
NATIVE_FS
.create_file(file_path.to_str().expect("getting str for file failed"))
.unwrap();
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()));
assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()));
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()).unwrap());
assert!(NATIVE_FS.is_file(file_path.to_str().unwrap()).unwrap());
println!("{}", file_path.to_str().unwrap());
let write_data = "hello world\n";
NATIVE_FS
Expand Down Expand Up @@ -449,15 +471,15 @@ mod tests {
fn test_remove_dir() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let dir_path = tmpdir.path().join("testdir");
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()));
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
NATIVE_FS
.create_dir(dir_path.to_str().expect("getting str for file failed"))
.unwrap();
assert!(NATIVE_FS.exists(dir_path.to_str().unwrap()));
assert!(NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
NATIVE_FS
.remove_dir(dir_path.to_str().unwrap(), false)
.unwrap();
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()));
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
}

#[test]
Expand Down Expand Up @@ -544,7 +566,7 @@ mod tests {
.unwrap();
let result = NATIVE_FS.remove_dir(dir_path.to_str().unwrap(), true);
assert!(result.is_ok());
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()));
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()).unwrap());
}

#[test]
Expand Down
8 changes: 8 additions & 0 deletions satrs/src/cfdp/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,14 @@ pub mod alloc_mod {
Ok(())
}

pub fn has_source_file(&self) -> bool {
self.static_fields.source_file_len > 0
}

pub fn has_dest_file(&self) -> bool {
self.static_fields.dest_file_len > 0
}

pub fn source_file(&self) -> Result<&str, Utf8Error> {
core::str::from_utf8(
&self.static_fields.source_file_buf[0..self.static_fields.source_file_len],
Expand Down
Loading

0 comments on commit ab56f4b

Please sign in to comment.