Git · dadoum.zone
Repositories
Help
Report an Issue
apple-crates
Code
Commits
Branches
Tags
Search
Tree:
a74924e
Branches
Tags
main
apple-crates
apple-codesign-quick
src
file_bytes.rs
Refactor and unify a lot of things for another project and implement airdrop support
Dadoum
commited
a74924e
at 2026-10-25 22:15:58
file_bytes.rs
Blame
History
Raw
use crate::error::{CodeSignError, Result}; use memmap2::{Mmap, MmapOptions}; use std::fs; use std::path::Path; pub(crate) enum FileBytes { Mapped(Mmap), Owned(Vec<u8>), } impl FileBytes { pub(crate) fn as_slice(&self) -> &[u8] { match self { FileBytes::Mapped(map) => map, FileBytes::Owned(bytes) => bytes, } } } /// Map a file read-only, or read it if it can't be mapped (empty files can't). pub(crate) fn read_file_bytes(path: &Path) -> Result<FileBytes> { let file = fs::File::open(path).map_err(CodeSignError::io(path))?; let len = file.metadata().map_err(CodeSignError::io(path))?.len(); if len == 0 { return Ok(FileBytes::Owned(Vec::new())); } // Mappings are dropped before we rewrite a file, but someone else changing the bundle while we // sign it would break us, as it would break the D version of this. let map = unsafe { MmapOptions::new().map(&file) }; match map { Ok(map) => Ok(FileBytes::Mapped(map)), Err(_) => fs::read(path) .map(FileBytes::Owned) .map_err(CodeSignError::io(path)), } }