-
Notifications
You must be signed in to change notification settings - Fork 5
refactor(file): replace fdisk with gptman+mbrman #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HarryWaschkeit
merged 5 commits into
omnect:main
from
HarryWaschkeit:feature/gptman-mbrman-partition-detection
Mar 20, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b75240
refactor(file): replace fdisk with gptman+mbrman
HarryWaschkeit 1db31b5
fix: safety measure for corrupt GPT partitions
HarryWaschkeit 8199207
fix(file): address PR review findings
HarryWaschkeit d951022
chore: bump version to 0.27.2
HarryWaschkeit c9ec9b1
fix(file): use try_from for MBR partition index comparison
HarryWaschkeit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use anyhow::{Context, Result}; | ||
| use std::fs::File; | ||
| use std::io::SeekFrom; | ||
| use std::io::Seek; | ||
| use std::path::Path; | ||
|
|
||
| pub struct PartitionData { | ||
| pub num: u32, | ||
| pub start: u64, | ||
| pub count: u64, | ||
| } | ||
|
|
||
| pub fn get_partition_data<P: AsRef<Path>>(path: P, partition_num: u32) -> Result<PartitionData> { | ||
| let path = path.as_ref(); | ||
| let mut file = File::open(path) | ||
| .with_context(|| format!("failed to open image: {}", path.display()))?; | ||
|
|
||
| // Try GPT first (validates CRC32 — more robust than signature check) | ||
| if let Ok(gpt) = gptman::GPT::find_from(&mut file) { | ||
| let entry = &gpt[partition_num]; | ||
|
HarryWaschkeit marked this conversation as resolved.
Outdated
HarryWaschkeit marked this conversation as resolved.
Outdated
|
||
| anyhow::ensure!(entry.is_used(), "GPT partition {partition_num} is not used"); | ||
| anyhow::ensure!( | ||
| entry.ending_lba >= entry.starting_lba, | ||
| "GPT partition {partition_num} has invalid LBA range (ending_lba < starting_lba)" | ||
| ); | ||
| let count = entry | ||
| .ending_lba | ||
| .checked_sub(entry.starting_lba) | ||
| .and_then(|v| v.checked_add(1)) | ||
| .context("GPT partition {partition_num} has an LBA range that overflows u64")?; | ||
|
HarryWaschkeit marked this conversation as resolved.
Outdated
|
||
| return Ok(PartitionData { | ||
| num: partition_num, | ||
| start: entry.starting_lba, | ||
| count, | ||
| }); | ||
| } | ||
|
|
||
| // Try MBR — iter() includes logical partitions (5, 6, ...) | ||
| file.seek(SeekFrom::Start(0)) | ||
| .context("failed to seek to start of image")?; | ||
| let mbr = mbrman::MBR::read_from(&mut file, 512) | ||
| .context("image is neither valid GPT nor MBR")?; | ||
| for (i, p) in mbr.iter() { | ||
| if i == partition_num as usize && p.is_used() { | ||
| return Ok(PartitionData { | ||
| num: partition_num, | ||
| start: p.starting_lba as u64, | ||
|
JanZachmann marked this conversation as resolved.
|
||
| count: p.sectors as u64, | ||
| }); | ||
|
HarryWaschkeit marked this conversation as resolved.
|
||
| } | ||
| } | ||
| anyhow::bail!("partition {partition_num} not found in image") | ||
| } | ||
|
HarryWaschkeit marked this conversation as resolved.
JanZachmann marked this conversation as resolved.
|
||
|
|
||
| pub fn is_gpt<P: AsRef<Path>>(path: P) -> Result<bool> { | ||
| let mut file = File::open(path.as_ref()) | ||
| .context("is_gpt: failed to open image")?; | ||
| Ok(gptman::GPT::find_from(&mut file).is_ok()) | ||
|
HarryWaschkeit marked this conversation as resolved.
JanZachmann marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.