Skip to content

Commit

Permalink
feat(log): Implement basic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Oakchris1955 committed Aug 11, 2024
1 parent 0473b24 commit c849a6b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 67 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ bincode = "1.3.3"
bitfield-struct = "0.8.0"
bitflags = { version = "2.6.0", features = ["serde"] }
displaydoc = { version = "0.2.5", default-features = false }
log = "0.4.22"
serde = { version = "1.0.204", default-features = false, features = [ "alloc", "derive" ] }
serde-big-array = "0.5.1"
time = { version = "0.3.36", default-features = false, features = [ "alloc", "parsing", "macros" ]}

[features]
default = ["std"]
std = ["displaydoc/std", "serde/std", "time/std"]

[dev-dependencies]
test-log = "0.2.16"
40 changes: 37 additions & 3 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ impl SFN {
.wrapping_add(c)
}

log::debug!("SFN checksum: {:X}", sum);

sum
}
}
Expand Down Expand Up @@ -708,6 +710,7 @@ where
let mut bytes_read = 0;
// this is the maximum amount of bytes that can be read
let read_cap = cmp::min(buf.len(), self.file_size as usize - self.offset as usize);
log::debug!("Byte read cap set to {}", read_cap);

'outer: loop {
let sector_init_offset = u32::try_from(self.offset % self.fs.cluster_size()).unwrap()
Expand All @@ -720,15 +723,27 @@ where
+ self.fs.sectors_per_cluster() as u32
- sector_init_offset
- 1;
log::debug!(
"Reading cluster {} from sectors {} to {}",
self.current_cluster,
first_sector_of_cluster,
last_sector_of_cluster
);

for sector in first_sector_of_cluster..=last_sector_of_cluster {
self.fs.read_nth_sector(sector.into())?;

let start_index = self.offset as usize % self.fs.sector_size() as usize;

let bytes_to_read = cmp::min(
read_cap - bytes_read,
self.fs.sector_size() as usize - start_index,
);
log::debug!(
"Gonna read {} bytes from sector {} starting at byte {}",
bytes_to_read,
sector,
start_index
);

buf[bytes_read..bytes_read + bytes_to_read].copy_from_slice(
&self.fs.sector_buffer[start_index..start_index + bytes_to_read],
Expand Down Expand Up @@ -788,9 +803,16 @@ where
};

if offset > self.file_size.into() {
log::debug!("Capping cursor offset to file_size");
offset = self.file_size.into();
}

log::debug!(
"Previous cursor offset is {}, new cursor offset is {}",
self.offset,
offset
);

use cmp::Ordering;
match offset.cmp(&self.offset) {
Ordering::Less => {
Expand Down Expand Up @@ -988,6 +1010,7 @@ where
.deserialize::<FSInfoFAT32>(&buffer[..bpb.bytes_per_sector as usize])?;

if !fsinfo.verify_signature() {
log::error!("FAT32 FSInfo has invalid signature(s)");
return Err(FSError::InternalFSError(InternalFSError::InvalidFSInfoSig));
}

Expand All @@ -1004,10 +1027,12 @@ where

// verify boot record signature
let fat_type = boot_record.fat_type();
log::info!("The FAT type of the filesystem is {:?}", fat_type);

match boot_record {
BootRecord::FAT(boot_record_fat) => {
if boot_record_fat.verify_signature() {
log::error!("FAT boot record has invalid signature(s)");
return Err(FSError::InternalFSError(InternalFSError::InvalidBPBSig));
}
}
Expand Down Expand Up @@ -1068,6 +1093,7 @@ where
return Err(FSError::MalformedPath);
}
if !path.is_dir() {
log::error!("Not a directory");
return Err(FSError::NotADirectory);
}

Expand All @@ -1079,6 +1105,7 @@ where
}) {
Some(entry) => entry.data_cluster,
None => {
log::error!("Directory {} not found", path);
return Err(FSError::NotFound);
}
};
Expand Down Expand Up @@ -1134,14 +1161,19 @@ where
if file.cluster_chain_is_healthy()? {
Ok(file)
} else {
log::error!("The cluster chain of a file is malformed");
Err(FSError::InternalFSError(
InternalFSError::MalformedClusterChain,
))
}
}
None => Err(FSError::NotFound),
None => {
log::error!("File {} not found", path);
Err(FSError::NotFound)
}
}
} else {
log::error!("Is a directory (not a file)");
Err(FSError::IsADirectory)
}
}
Expand Down Expand Up @@ -1294,9 +1326,10 @@ where
FATEntry::Allocated(next_cluster) => data_cluster = next_cluster,
// any other case (whether a bad, reserved or free cluster) is invalid, consider this cluster chain malformed
_ => {
log::error!("Cluster chain of directory is malformed");
return Err(FSError::InternalFSError(
InternalFSError::MalformedClusterChain,
))
));
}
}
}
Expand Down Expand Up @@ -1413,6 +1446,7 @@ where
#[cfg(all(test, feature = "std"))]
mod tests {
use super::*;
use test_log::test;
use time::macros::*;

static MINFS: &[u8] = include_bytes!("../imgs/minfs.img");
Expand Down
137 changes: 73 additions & 64 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct PathBuf {
impl PathBuf {
/// Create a new, empty [`PathBuf`] pointing to the root directory ("/")
pub fn new() -> Self {
log::debug!("New PathBuf created");
Self::default()
}

Expand Down Expand Up @@ -108,6 +109,8 @@ impl PathBuf {
_ => self.inner.push_back(p.to_string()),
}
}

log::debug!("Pushed {} into PathBuf", subpath);
}

/// If `self` is a file, returns `Ok(file_name)`, otherwise `None`
Expand Down Expand Up @@ -199,85 +202,91 @@ impl iter::Iterator for PathBuf {
}
}

#[test]
fn empty_pathbuf_tostring() {
let pathbuf = PathBuf::new();
#[cfg(all(test, feature = "std"))]
mod tests {
use super::*;
use test_log::test;

assert_eq!(pathbuf.to_string(), "/");
assert!(!pathbuf.is_malformed());
}
#[test]
fn empty_pathbuf_tostring() {
let pathbuf = PathBuf::new();

assert_eq!(pathbuf.to_string(), "/");
assert!(!pathbuf.is_malformed());
}

#[test]
fn catch_invalid_path() {
#[cfg(not(feature = "std"))]
use ::alloc::format;
#[test]
fn catch_invalid_path() {
#[cfg(not(feature = "std"))]
use ::alloc::format;

let mut pathbuf = PathBuf::new();
let mut pathbuf = PathBuf::new();

// ignore path separators
for filename in RESERVED_FILENAMES {
pathbuf.push(format!("/{filename}"));
// ignore path separators
for filename in RESERVED_FILENAMES {
pathbuf.push(format!("/{filename}"));

assert!(
pathbuf.is_malformed(),
"unable to detect invalid filename {}",
pathbuf
);
assert!(
pathbuf.is_malformed(),
"unable to detect invalid filename {}",
pathbuf
);

pathbuf.clear();
pathbuf.clear();
}
}
}

#[test]
fn catch_non_control_forbidden_chars() {
#[cfg(not(feature = "std"))]
use ::alloc::format;
#[test]
fn catch_non_control_forbidden_chars() {
#[cfg(not(feature = "std"))]
use ::alloc::format;

let mut pathbuf = PathBuf::new();
let mut pathbuf = PathBuf::new();

// ignore path separators
const PATH_SEPARATORS: &[char] = &['/', '\\'];
for c in FORBIDDEN_CHARS
.iter()
.filter(|c| !PATH_SEPARATORS.contains(c))
{
pathbuf.push(format!("/{c}"));
// ignore path separators
const PATH_SEPARATORS: &[char] = &['/', '\\'];
for c in FORBIDDEN_CHARS
.iter()
.filter(|c| !PATH_SEPARATORS.contains(c))
{
pathbuf.push(format!("/{c}"));

assert!(
pathbuf.is_malformed(),
"unable to detect character {} (hex: {:#02x}) as forbidden {:?}",
c,
(*c as usize),
pathbuf
);
assert!(
pathbuf.is_malformed(),
"unable to detect character {} (hex: {:#02x}) as forbidden {:?}",
c,
(*c as usize),
pathbuf
);

pathbuf.clear();
pathbuf.clear();
}
}
}

#[test]
fn push_to_pathbuf() {
let mut pathbuf = PathBuf::new();

pathbuf.push("foo");
pathbuf.push("bar/test");
pathbuf.push("bar2\\test2");
pathbuf.push("ignored\\../.");
pathbuf.push("fintest1");
pathbuf.push("fintest2/");
pathbuf.push("last");

assert_eq!(
pathbuf.to_string(),
"/foo/bar/test/bar2/test2/fintest1/fintest2/last"
)
}
#[test]
fn push_to_pathbuf() {
let mut pathbuf = PathBuf::new();

#[test]
fn push_absolute_path() {
let mut pathbuf = PathBuf::from("/foo/bar.txt");
pathbuf.push("foo");
pathbuf.push("bar/test");
pathbuf.push("bar2\\test2");
pathbuf.push("ignored\\../.");
pathbuf.push("fintest1");
pathbuf.push("fintest2/");
pathbuf.push("last");

assert_eq!(
pathbuf.to_string(),
"/foo/bar/test/bar2/test2/fintest1/fintest2/last"
)
}

pathbuf.push("\\test");
#[test]
fn push_absolute_path() {
let mut pathbuf = PathBuf::from("/foo/bar.txt");

assert_eq!(pathbuf.to_string(), "/test")
pathbuf.push("\\test");

assert_eq!(pathbuf.to_string(), "/test")
}
}

0 comments on commit c849a6b

Please sign in to comment.