1111use std:: io:: { Read , Seek , SeekFrom } ;
1212use std:: ptr;
1313
14+ use windows:: core:: PCWSTR ;
1415use windows:: Win32 :: Foundation :: { CloseHandle , GENERIC_READ , HANDLE } ;
1516use windows:: Win32 :: Storage :: FileSystem :: {
1617 CreateFileW , FILE_FLAGS_AND_ATTRIBUTES , FILE_SHARE_READ , FILE_SHARE_WRITE , OPEN_EXISTING ,
1718} ;
18- use windows:: core:: PCWSTR ;
1919
2020use super :: { FsError , FsResult , PartitionInfo } ;
2121
@@ -79,8 +79,8 @@ impl RawDisk {
7979 }
8080
8181 fn get_disk_geometry ( handle : HANDLE ) -> FsResult < ( u64 , u64 ) > {
82- use windows:: Win32 :: System :: IO :: DeviceIoControl ;
8382 use windows:: Win32 :: System :: Ioctl :: IOCTL_DISK_GET_DRIVE_GEOMETRY_EX ;
83+ use windows:: Win32 :: System :: IO :: DeviceIoControl ;
8484
8585 #[ repr( C ) ]
8686 #[ derive( Default ) ]
@@ -150,7 +150,7 @@ impl RawDisk {
150150
151151 unsafe {
152152 ReadFile ( self . handle , Some ( buf) , Some ( & mut bytes_read) , None )
153- . map_err ( |e| std:: io:: Error :: new ( std :: io :: ErrorKind :: Other , e. to_string ( ) ) ) ?;
153+ . map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) ) ?;
154154 }
155155
156156 self . current_position += bytes_read as u64 ;
@@ -159,7 +159,9 @@ impl RawDisk {
159159
160160 /// Perform a raw seek (sets the file pointer)
161161 fn raw_seek ( & mut self , pos : SeekFrom ) -> std:: io:: Result < u64 > {
162- use windows:: Win32 :: Storage :: FileSystem :: { SetFilePointerEx , FILE_BEGIN , FILE_CURRENT , FILE_END } ;
162+ use windows:: Win32 :: Storage :: FileSystem :: {
163+ SetFilePointerEx , FILE_BEGIN , FILE_CURRENT , FILE_END ,
164+ } ;
163165
164166 let ( method, distance) = match pos {
165167 SeekFrom :: Start ( n) => ( FILE_BEGIN , n as i64 ) ,
@@ -171,7 +173,7 @@ impl RawDisk {
171173
172174 unsafe {
173175 SetFilePointerEx ( self . handle , distance, Some ( & mut new_position) , method)
174- . map_err ( |e| std:: io:: Error :: new ( std :: io :: ErrorKind :: Other , e. to_string ( ) ) ) ?;
176+ . map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) ) ?;
175177 }
176178
177179 self . current_position = new_position as u64 ;
@@ -190,7 +192,7 @@ impl RawDisk {
190192 let start_sector = offset / self . sector_size ;
191193 let offset_in_start_sector = ( offset % self . sector_size ) as usize ;
192194 let end_offset = offset + buffer. len ( ) as u64 ;
193- let end_sector = ( end_offset + self . sector_size - 1 ) / self . sector_size ;
195+ let end_sector = end_offset. div_ceil ( self . sector_size ) ;
194196 let num_sectors = ( end_sector - start_sector) as usize ;
195197
196198 // Calculate aligned read parameters
@@ -251,7 +253,7 @@ impl Read for RawDisk {
251253 let start_sector = offset / self . sector_size ;
252254 let offset_in_start_sector = ( offset % self . sector_size ) as usize ;
253255 let end_offset = offset + buf. len ( ) as u64 ;
254- let end_sector = ( end_offset + self . sector_size - 1 ) / self . sector_size ;
256+ let end_sector = end_offset. div_ceil ( self . sector_size ) ;
255257 let num_sectors = ( end_sector - start_sector) as usize ;
256258
257259 // Calculate aligned read parameters
@@ -439,21 +441,22 @@ pub fn detect_gpt_partitions(drive_number: u32) -> FsResult<Vec<PartitionInfo>>
439441
440442 // Check GPT signature
441443 if & header_buf[ 0 ..8 ] != b"EFI PART" {
442- return Err ( FsError :: FilesystemError ( "Invalid GPT signature" . to_string ( ) ) ) ;
444+ return Err ( FsError :: FilesystemError (
445+ "Invalid GPT signature" . to_string ( ) ,
446+ ) ) ;
443447 }
444448
445449 // Parse GPT header
446- let header: GptHeader = unsafe {
447- std:: ptr:: read_unaligned ( header_buf. as_ptr ( ) as * const GptHeader )
448- } ;
450+ let header: GptHeader =
451+ unsafe { std:: ptr:: read_unaligned ( header_buf. as_ptr ( ) as * const GptHeader ) } ;
449452
450453 let num_entries = header. num_partition_entries ;
451454 let entry_size = header. partition_entry_size ;
452455 let entries_start_lba = header. partition_entry_lba ;
453456
454457 // Read partition entries
455458 let entries_size = ( num_entries as u64 ) * ( entry_size as u64 ) ;
456- let entries_sectors = ( entries_size + sector_size - 1 ) / sector_size ;
459+ let entries_sectors = entries_size. div_ceil ( sector_size) ;
457460 let mut entries_buf = vec ! [ 0u8 ; ( entries_sectors * sector_size) as usize ] ;
458461 disk. read_at ( entries_start_lba * sector_size, & mut entries_buf) ?;
459462
@@ -492,7 +495,8 @@ pub fn detect_gpt_partitions(drive_number: u32) -> FsResult<Vec<PartitionInfo>>
492495 for j in 0 ..36 {
493496 let char_offset = name_offset + j * 2 ;
494497 if char_offset + 2 <= entries_buf. len ( ) {
495- let ch = u16:: from_le_bytes ( [ entries_buf[ char_offset] , entries_buf[ char_offset + 1 ] ] ) ;
498+ let ch =
499+ u16:: from_le_bytes ( [ entries_buf[ char_offset] , entries_buf[ char_offset + 1 ] ] ) ;
496500 if ch == 0 {
497501 break ;
498502 }
@@ -538,7 +542,9 @@ pub fn detect_mbr_partitions(drive_number: u32) -> FsResult<Vec<PartitionInfo>>
538542
539543 // Check MBR signature (0x55AA at offset 510)
540544 if mbr. len ( ) < 512 || mbr[ 510 ] != 0x55 || mbr[ 511 ] != 0xAA {
541- return Err ( FsError :: FilesystemError ( "Invalid MBR signature" . to_string ( ) ) ) ;
545+ return Err ( FsError :: FilesystemError (
546+ "Invalid MBR signature" . to_string ( ) ,
547+ ) ) ;
542548 }
543549
544550 let mut partitions = Vec :: new ( ) ;
@@ -561,7 +567,12 @@ pub fn detect_mbr_partitions(drive_number: u32) -> FsResult<Vec<PartitionInfo>>
561567 // Check if this is an extended partition
562568 if entry. partition_type == 0x05 || entry. partition_type == 0x0F {
563569 // Parse extended partitions recursively
564- if let Ok ( extended) = parse_extended_partitions ( & mut disk, start_offset, sector_size, partitions. len ( ) as u32 + 1 ) {
570+ if let Ok ( extended) = parse_extended_partitions (
571+ & mut disk,
572+ start_offset,
573+ sector_size,
574+ partitions. len ( ) as u32 + 1 ,
575+ ) {
565576 partitions. extend ( extended) ;
566577 }
567578 } else {
@@ -620,7 +631,7 @@ fn parse_extended_partitions(
620631 let size = entry1. size_sectors as u64 * sector_size;
621632
622633 partitions. push ( PartitionInfo {
623- device_path : format ! ( "Extended partition" ) ,
634+ device_path : "Extended partition" . to_string ( ) ,
624635 label : None ,
625636 fs_type,
626637 size,
@@ -653,17 +664,16 @@ pub fn get_partition_geometry(drive_number: u32, partition_number: u32) -> FsRes
653664 disk. read_at ( sector_size, & mut header_buf) ?;
654665
655666 if & header_buf[ 0 ..8 ] == b"EFI PART" {
656- let header: GptHeader = unsafe {
657- std:: ptr:: read_unaligned ( header_buf. as_ptr ( ) as * const GptHeader )
658- } ;
667+ let header: GptHeader =
668+ unsafe { std:: ptr:: read_unaligned ( header_buf. as_ptr ( ) as * const GptHeader ) } ;
659669
660670 let num_entries = header. num_partition_entries ;
661671 let entry_size = header. partition_entry_size ;
662672 let entries_start_lba = header. partition_entry_lba ;
663673
664674 // Read partition entries
665675 let entries_size = ( num_entries as u64 ) * ( entry_size as u64 ) ;
666- let entries_sectors = ( entries_size + sector_size - 1 ) / sector_size ;
676+ let entries_sectors = entries_size. div_ceil ( sector_size) ;
667677 let mut entries_buf = vec ! [ 0u8 ; ( entries_sectors * sector_size) as usize ] ;
668678 disk. read_at ( entries_start_lba * sector_size, & mut entries_buf) ?;
669679
@@ -676,7 +686,9 @@ pub fn get_partition_geometry(drive_number: u32, partition_number: u32) -> FsRes
676686 }
677687
678688 let entry: GptPartitionEntry = unsafe {
679- std:: ptr:: read_unaligned ( entries_buf[ offset..] . as_ptr ( ) as * const GptPartitionEntry )
689+ std:: ptr:: read_unaligned (
690+ entries_buf[ offset..] . as_ptr ( ) as * const GptPartitionEntry
691+ )
680692 } ;
681693
682694 // Check if entry is used
@@ -695,7 +707,10 @@ pub fn get_partition_geometry(drive_number: u32, partition_number: u32) -> FsRes
695707 }
696708 }
697709
698- return Err ( FsError :: NotFound ( format ! ( "Partition {} not found in GPT" , partition_number) ) ) ;
710+ return Err ( FsError :: NotFound ( format ! (
711+ "Partition {} not found in GPT" ,
712+ partition_number
713+ ) ) ) ;
699714 }
700715
701716 // Fall back to MBR
@@ -706,7 +721,9 @@ pub fn get_partition_geometry(drive_number: u32, partition_number: u32) -> FsRes
706721 disk. read_at ( 0 , & mut mbr) ?;
707722
708723 if mbr. len ( ) < 512 || mbr[ 510 ] != 0x55 || mbr[ 511 ] != 0xAA {
709- return Err ( FsError :: FilesystemError ( "Invalid MBR signature" . to_string ( ) ) ) ;
724+ return Err ( FsError :: FilesystemError (
725+ "Invalid MBR signature" . to_string ( ) ,
726+ ) ) ;
710727 }
711728
712729 let mut current_partition = 1u32 ;
@@ -736,7 +753,8 @@ pub fn get_partition_geometry(drive_number: u32, partition_number: u32) -> FsRes
736753 if entry1. partition_type != 0 && entry1. size_sectors > 0 {
737754 current_partition += 1 ;
738755 if current_partition == partition_number {
739- let start_offset = current_ebr_offset + entry1. start_lba as u64 * sector_size;
756+ let start_offset =
757+ current_ebr_offset + entry1. start_lba as u64 * sector_size;
740758 let size = entry1. size_sectors as u64 * sector_size;
741759 return Ok ( ( start_offset, size) ) ;
742760 }
@@ -759,7 +777,10 @@ pub fn get_partition_geometry(drive_number: u32, partition_number: u32) -> FsRes
759777 }
760778 }
761779
762- Err ( FsError :: NotFound ( format ! ( "Partition {} not found" , partition_number) ) )
780+ Err ( FsError :: NotFound ( format ! (
781+ "Partition {} not found" ,
782+ partition_number
783+ ) ) )
763784}
764785
765786/// Identify partition type from GPT GUID
0 commit comments