diff --git a/src/boot_sector.rs b/src/boot_sector.rs index 965f7a4..c85e61c 100644 --- a/src/boot_sector.rs +++ b/src/boot_sector.rs @@ -499,17 +499,17 @@ fn format_bpb(options: &FormatVolumeOptions) -> io::Result<(BiosParameterBlock, if let Some(volume_label_from_opts) = options.volume_label { volume_label.copy_from_slice(&volume_label_from_opts); } else { - volume_label.copy_from_slice("NO NAME ".as_bytes()); + volume_label.copy_from_slice(b"NO NAME "); } // setup fs_type_label field let mut fs_type_label = [0u8; 8]; let fs_type_label_str = match fat_type { - FatType::Fat12 => "FAT12 ", - FatType::Fat16 => "FAT16 ", - FatType::Fat32 => "FAT32 ", + FatType::Fat12 => b"FAT12 ", + FatType::Fat16 => b"FAT16 ", + FatType::Fat32 => b"FAT32 ", }; - fs_type_label.copy_from_slice(fs_type_label_str.as_bytes()); + fs_type_label.copy_from_slice(fs_type_label_str); // create Bios Parameter Block struct let bpb = BiosParameterBlock { @@ -554,7 +554,7 @@ pub(crate) fn format_boot_sector(options: &FormatVolumeOptions) -> io::Result<(B let mut boot: BootSector = Default::default(); let (bpb, fat_type) = format_bpb(options)?; boot.bpb = bpb; - boot.oem_name.copy_from_slice("MSWIN4.1".as_bytes()); + boot.oem_name.copy_from_slice(b"MSWIN4.1"); // Boot code copied from FAT32 boot sector initialized by mkfs.fat boot.bootjmp = [0xEB, 0x58, 0x90]; let boot_code: [u8; 129] = [ diff --git a/src/dir.rs b/src/dir.rs index 1f4e3a8..5e2cc73 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -84,7 +84,7 @@ impl<'a, T: ReadWriteSeek> Seek for DirRawStream<'a, T> { fn split_path<'c>(path: &'c str) -> (&'c str, Option<&'c str>) { // remove trailing slash and split into 2 components - top-most parent and rest - let mut path_split = path.trim_matches('/').splitn(2, "/"); + let mut path_split = path.trim_matches('/').splitn(2, '/'); let comp = path_split.next().unwrap(); // SAFE: splitn always returns at least one element let rest_opt = path_split.next(); (comp, rest_opt) @@ -262,7 +262,7 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> { let e = r?; let name = e.short_file_name_as_bytes(); // ignore special entries "." and ".." - if name != ".".as_bytes() && name != "..".as_bytes() { + if name != b"." && name != b".." { return Ok(false); } } @@ -324,7 +324,7 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> { let (name, rest_opt) = split_path(dst_path); if let Some(rest) = rest_opt { let e = dst_dir.find_entry(name, Some(true), None)?; - return self.rename(src_path, &mut e.to_dir(), rest); + return self.rename(src_path, &e.to_dir(), rest); } // move/rename file self.rename_internal(src_path, dst_dir, dst_path) @@ -415,7 +415,7 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> { () } - fn alloc_and_write_lfn_entries(&self, lfn_utf16: &LfnUtf16, short_name: &[u8]) -> io::Result<(DirRawStream<'a, T>, u64)> { + fn alloc_and_write_lfn_entries(&self, lfn_utf16: &LfnUtf16, short_name: &[u8; 11]) -> io::Result<(DirRawStream<'a, T>, u64)> { // get short name checksum let lfn_chsum = lfn_checksum(short_name); // create LFN entries generator @@ -445,14 +445,14 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> { let abs_pos = stream.abs_pos().map(|p| p - DIR_ENTRY_SIZE); // return new logical entry descriptor let short_name = ShortName::new(raw_entry.name()); - return Ok(DirEntry { + Ok(DirEntry { data: raw_entry, short_name, lfn_utf16, fs: self.fs, entry_pos: abs_pos.unwrap(), // SAFE: abs_pos is absent only for empty file offset_range: (start_pos, end_pos), - }); + }) } } @@ -527,7 +527,7 @@ impl<'a, T: ReadWriteSeek> DirIter<'a, T> { return Ok(Some(DirEntry { data, short_name, - lfn_utf16: lfn_buf.to_vec(), + lfn_utf16: lfn_buf.into_vec(), fs: self.fs, entry_pos: abs_pos.unwrap(), // SAFE: abs_pos is empty only for empty file offset_range: (begin_offset, offset), @@ -576,7 +576,7 @@ impl<'a, T: ReadWriteSeek> Iterator for DirIter<'a, T> { fn validate_long_name(name: &str) -> io::Result<()> { // check if length is valid - if name.len() == 0 { + if name.is_empty() { return Err(io::Error::new(ErrorKind::Other, "File name is empty")); } if name.len() > 255 { @@ -595,10 +595,10 @@ fn validate_long_name(name: &str) -> io::Result<()> { Ok(()) } -fn lfn_checksum(short_name: &[u8]) -> u8 { +fn lfn_checksum(short_name: &[u8; 11]) -> u8 { let mut chksum = num::Wrapping(0u8); - for i in 0..11 { - chksum = (chksum << 7) + (chksum >> 1) + num::Wrapping(short_name[i]); + for b in short_name { + chksum = (chksum << 7) + (chksum >> 1) + num::Wrapping(*b); } chksum.0 } @@ -625,7 +625,7 @@ impl LongNameBuilder { self.index = 0; } - fn to_vec(mut self) -> Vec { + fn into_vec(mut self) -> Vec { // Check if last processed entry had index 1 if self.index == 1 { self.truncate(); @@ -651,7 +651,7 @@ impl LongNameBuilder { fn is_empty(&self) -> bool { // Check if any LFN entry has been processed // Note: index 0 is not a valid index in LFN and can be seen only after struct initialization - return self.index == 0; + self.index == 0 } fn process(&mut self, data: &DirLfnEntryData) { @@ -688,7 +688,7 @@ impl LongNameBuilder { data.copy_name_to_slice(&mut self.buf[pos..pos + 13]); } - fn validate_chksum(&mut self, short_name: &[u8]) { + fn validate_chksum(&mut self, short_name: &[u8; 11]) { if self.is_empty() { // Nothing to validate - no LFN entries has been processed return; @@ -710,10 +710,10 @@ impl LongNameBuilder { LongNameBuilder {} } fn clear(&mut self) {} - fn to_vec(self) {} + fn into_vec(self) {} fn truncate(&mut self) {} fn process(&mut self, _data: &DirLfnEntryData) {} - fn validate_chksum(&mut self, _short_name: &[u8]) {} + fn validate_chksum(&mut self, _short_name: &[u8; 11]) {} } #[cfg(feature = "alloc")] @@ -892,7 +892,7 @@ impl ShortNameGenerator { } // check for long prefix form collision (TEXTFI~1.TXT) let prefix_len = cmp::min(self.basename_len, 6) as usize; - let num_suffix = if short_name[prefix_len] as char == '~' { + let num_suffix = if short_name[prefix_len] == b'~' { (short_name[prefix_len + 1] as char).to_digit(10) } else { None @@ -905,7 +905,7 @@ impl ShortNameGenerator { // check for short prefix + checksum form collision (TE021F~1.TXT) let prefix_len = cmp::min(self.basename_len, 2) as usize; - let num_suffix = if short_name[prefix_len + 4] as char == '~' { + let num_suffix = if short_name[prefix_len + 4] == b'~' { (short_name[prefix_len + 4 + 1] as char).to_digit(10) } else { None @@ -970,7 +970,7 @@ impl ShortNameGenerator { buf[..prefix_len].copy_from_slice(&self.short_name[..prefix_len]); prefix_len }; - buf[prefix_len] = '~' as u8; + buf[prefix_len] = b'~'; buf[prefix_len + 1] = char::from_digit(num, 10).unwrap() as u8; // SAFE buf[8..].copy_from_slice(&self.short_name[8..]); buf @@ -981,7 +981,7 @@ impl ShortNameGenerator { let c2 = char::from_digit((x as u32 >> 8) & 0xF, 16).unwrap().to_ascii_uppercase() as u8; let c3 = char::from_digit((x as u32 >> 4) & 0xF, 16).unwrap().to_ascii_uppercase() as u8; let c4 = char::from_digit((x as u32 >> 0) & 0xF, 16).unwrap().to_ascii_uppercase() as u8; - return [c1, c2, c3, c4]; + [c1, c2, c3, c4] } } @@ -998,17 +998,15 @@ mod tests { #[test] fn test_generate_short_name() { - assert_eq!(&ShortNameGenerator::new("Foo").generate().unwrap(), "FOO ".as_bytes()); - assert_eq!(&ShortNameGenerator::new("Foo.b").generate().unwrap(), "FOO B ".as_bytes()); - assert_eq!(&ShortNameGenerator::new("Foo.baR").generate().unwrap(), "FOO BAR".as_bytes()); - assert_eq!(&ShortNameGenerator::new("Foo+1.baR").generate().unwrap(), "FOO_1~1 BAR".as_bytes()); + assert_eq!(&ShortNameGenerator::new("Foo").generate().unwrap(), b"FOO "); + assert_eq!(&ShortNameGenerator::new("Foo.b").generate().unwrap(), b"FOO B "); + assert_eq!(&ShortNameGenerator::new("Foo.baR").generate().unwrap(), b"FOO BAR"); + assert_eq!(&ShortNameGenerator::new("Foo+1.baR").generate().unwrap(), b"FOO_1~1 BAR"); assert_eq!( - &ShortNameGenerator::new("ver +1.2.text").generate().unwrap(), - "VER_12~1TEX".as_bytes() + &ShortNameGenerator::new("ver +1.2.text").generate().unwrap(), b"VER_12~1TEX" ); assert_eq!( - &ShortNameGenerator::new(".bashrc.swp").generate().unwrap(), - "BASHRC~1SWP".as_bytes() + &ShortNameGenerator::new(".bashrc.swp").generate().unwrap(), b"BASHRC~1SWP" ); } @@ -1029,22 +1027,22 @@ mod tests { let mut buf: [u8; 11]; let mut gen = ShortNameGenerator::new("TextFile.Mine.txt"); buf = gen.generate().unwrap(); - assert_eq!(&buf, "TEXTFI~1TXT".as_bytes()); + assert_eq!(&buf, b"TEXTFI~1TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "TEXTFI~2TXT".as_bytes()); + assert_eq!(&buf, b"TEXTFI~2TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "TEXTFI~3TXT".as_bytes()); + assert_eq!(&buf, b"TEXTFI~3TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "TEXTFI~4TXT".as_bytes()); + assert_eq!(&buf, b"TEXTFI~4TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "TE527D~1TXT".as_bytes()); + assert_eq!(&buf, b"TE527D~1TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "TE527D~2TXT".as_bytes()); + assert_eq!(&buf, b"TE527D~2TXT"); for i in 3..10 { gen.add_existing(&buf); buf = gen.generate().unwrap(); @@ -1058,7 +1056,7 @@ mod tests { gen.add_existing(&buf); } buf = gen.generate().unwrap(); - assert_eq!(&buf, "TE527E~1TXT".as_bytes()); + assert_eq!(&buf, b"TE527E~1TXT"); } #[test] @@ -1066,24 +1064,24 @@ mod tests { let mut buf: [u8; 11]; let mut gen = ShortNameGenerator::new("x.txt"); buf = gen.generate().unwrap(); - assert_eq!(&buf, "X TXT".as_bytes()); + assert_eq!(&buf, b"X TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "X~1 TXT".as_bytes()); + assert_eq!(&buf, b"X~1 TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "X~2 TXT".as_bytes()); + assert_eq!(&buf, b"X~2 TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "X~3 TXT".as_bytes()); + assert_eq!(&buf, b"X~3 TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "X~4 TXT".as_bytes()); + assert_eq!(&buf, b"X~4 TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "X40DA~1 TXT".as_bytes()); + assert_eq!(&buf, b"X40DA~1 TXT"); gen.add_existing(&buf); buf = gen.generate().unwrap(); - assert_eq!(&buf, "X40DA~2 TXT".as_bytes()); + assert_eq!(&buf, b"X40DA~2 TXT"); } } diff --git a/src/dir_entry.rs b/src/dir_entry.rs index 33181c4..0040f69 100644 --- a/src/dir_entry.rs +++ b/src/dir_entry.rs @@ -51,7 +51,7 @@ pub(crate) struct ShortName { } impl ShortName { - const PADDING: u8 = ' ' as u8; + const PADDING: u8 = b' '; pub(crate) fn new(raw_name: &[u8; 11]) -> Self { // get name components length by looking for space character @@ -64,7 +64,7 @@ impl ShortName { let mut name = [Self::PADDING; 12]; name[..name_len].copy_from_slice(&raw_name[..name_len]); let total_len = if ext_len > 0 { - name[name_len] = '.' as u8; + name[name_len] = b'.'; name[name_len + 1..name_len + 1 + ext_len].copy_from_slice(&raw_name[8..8 + ext_len]); // Return total name length name_len + 1 + ext_len @@ -529,10 +529,10 @@ impl<'a, T: ReadWriteSeek> DirEntry<'a, T> { /// Returns long file name or if it doesn't exist fallbacks to short file name. #[cfg(feature = "alloc")] pub fn file_name(&self) -> String { - if self.lfn_utf16.len() > 0 { - String::from_utf16_lossy(&self.lfn_utf16) - } else { + if self.lfn_utf16.is_empty() { self.data.lowercase_name().to_string(self.fs.options.oem_cp_converter) + } else { + String::from_utf16_lossy(&self.lfn_utf16) } } @@ -642,9 +642,9 @@ mod tests { #[test] fn short_name_with_ext() { let mut raw_short_name = [0u8; 11]; - raw_short_name.copy_from_slice("FOO BAR".as_bytes()); + raw_short_name.copy_from_slice(b"FOO BAR"); assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "FOO.BAR"); - raw_short_name.copy_from_slice("LOOK AT M E".as_bytes()); + raw_short_name.copy_from_slice(b"LOOK AT M E"); assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "LOOK AT.M E"); raw_short_name[0] = 0x99; raw_short_name[10] = 0x99; @@ -661,16 +661,16 @@ mod tests { #[test] fn short_name_without_ext() { let mut raw_short_name = [0u8; 11]; - raw_short_name.copy_from_slice("FOO ".as_bytes()); + raw_short_name.copy_from_slice(b"FOO "); assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "FOO"); - raw_short_name.copy_from_slice("LOOK AT ".as_bytes()); + raw_short_name.copy_from_slice(b"LOOK AT "); assert_eq!(ShortName::new(&raw_short_name).to_string(&LOSSY_OEM_CP_CONVERTER), "LOOK AT"); } #[test] fn short_name_eq_ignore_case() { let mut raw_short_name = [0u8; 11]; - raw_short_name.copy_from_slice("LOOK AT M E".as_bytes()); + raw_short_name.copy_from_slice(b"LOOK AT M E"); raw_short_name[0] = 0x99; raw_short_name[10] = 0x99; assert_eq!( @@ -688,14 +688,14 @@ mod tests { let raw_short_name = [0x05; 11]; assert_eq!( ShortName::new(&raw_short_name).as_bytes(), - [0xE5, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, '.' as u8, 0x05, 0x05, 0x05] + [0xE5, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, b'.', 0x05, 0x05, 0x05] ); } #[test] fn lowercase_short_name() { let mut raw_short_name = [0u8; 11]; - raw_short_name.copy_from_slice("FOO RS ".as_bytes()); + raw_short_name.copy_from_slice(b"FOO RS "); let mut raw_entry = DirFileEntryData { name: raw_short_name, reserved_0: (1 << 3) | (1 << 4), diff --git a/src/table.rs b/src/table.rs index db8a58d..bf4ac71 100644 --- a/src/table.rs +++ b/src/table.rs @@ -456,7 +456,7 @@ impl Iterator for ClusterIterator { }, } } - self.cluster.map(|n| Ok(n)) + self.cluster.map(Ok) } }