-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
84 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,8 +1,40 @@ | ||
use std::fs::FileType; | ||
use libc::{O_RDONLY, c_ulong, close, ioctl, open}; | ||
use std::os::unix::fs::FileTypeExt; | ||
use std::os::unix::ffi::OsStrExt; | ||
use std::fs::{FileType, Metadata}; | ||
use std::ffi::CString; | ||
use std::path::Path; | ||
|
||
|
||
/// Return `device size / 512` (`long *` arg) | ||
/// | ||
/// Extracted from my armv6l machine | ||
/// | ||
/// Would be probably a good idea to get this at build time from a build script | ||
const BLKGETSIZE: c_ulong = (0x12 << 8) | 96; | ||
|
||
|
||
/// OS-specific check for fileness | ||
pub fn is_device(tp: &FileType) -> bool { | ||
tp.is_block_device() || tp.is_char_device() || tp.is_fifo() || tp.is_socket() | ||
} | ||
|
||
/// Check file length responsibly | ||
pub fn file_length<P: AsRef<Path>>(meta: &Metadata, path: &P) -> u64 { | ||
if is_device(&meta.file_type()) { | ||
let mut block_count: c_ulong = 0; | ||
|
||
let path_c = CString::new(path.as_ref().as_os_str().as_bytes()).unwrap(); | ||
let dev_file = unsafe { open(path_c.as_ptr(), O_RDONLY) }; | ||
if dev_file >= 0 { | ||
let ok = unsafe { ioctl(dev_file, BLKGETSIZE, &mut block_count as *mut c_ulong) } == 0; | ||
unsafe { close(dev_file) }; | ||
|
||
if ok { | ||
return block_count as u64 * 512; | ||
} | ||
} | ||
} | ||
|
||
meta.len() | ||
} |
This file contains 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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
use std::fs::FileType; | ||
use std::fs::{FileType, Metadata}; | ||
use std::path::Path; | ||
|
||
|
||
/// OS-specific check for fileness | ||
pub fn is_device(_: &FileType) -> bool { | ||
false | ||
} | ||
|
||
/// Check file length responsibly | ||
pub fn file_length<P: AsRef<Path>>(meta: &Metadata, _: &P) -> u64 { | ||
meta.len() | ||
} |