Skip to content

Commit

Permalink
Automatically generate BLKGETSIZE for the platform at buildtime
Browse files Browse the repository at this point in the history
Ref: #66
  • Loading branch information
nabijaczleweli committed May 17, 2018
1 parent fd843a8 commit 4e94aa9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ version = "0.2"
[build-dependencies]
embed-resource = "1.1"

[target.'cfg(not(target_os = "windows"))'.build-dependencies.gcc]
version = "0.3"


[[bin]]
name = "http"
Expand Down
50 changes: 50 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,56 @@
extern crate embed_resource;
#[cfg(not(target_os = "windows"))]
extern crate gcc;

#[cfg(not(target_os = "windows"))]
use std::env;
#[cfg(not(target_os = "windows"))]
use std::io::Write;
#[cfg(not(target_os = "windows"))]
use std::path::Path;
#[cfg(not(target_os = "windows"))]
use std::fs::{self, File};


/// The last line of this, after running it through a preprocessor, will expand to the value of `BLKGETSIZE`
static IOCTL_CHECK_SOURCE: &str = r#"
#include <linux/fs.h>
BLKGETSIZE
"#;

/// Replace `{}` with the `BLKGETSIZE` expression from `IOCTL_CHECK_SOURCE`
static IOCTL_INCLUDE_SKELETON: &str = r#"
/// Return `device size / 512` (`long *` arg)
static BLKGETSIZE: c_ulong = {};
"#;


fn main() {
embed_resources();
get_ioctl_data();
}

fn embed_resources() {
embed_resource::compile("http-manifest.rc");
}

#[cfg(target_os = "windows")]
fn get_ioctl_data() {

}

#[cfg(not(target_os = "windows"))]
fn get_ioctl_data() {
let ioctl_dir = Path::new(&env::var("OUT_DIR").unwrap()).join("ioctl-data");
fs::create_dir_all(&ioctl_dir).unwrap();

let ioctl_source = ioctl_dir.join("ioctl.c");
File::create(&ioctl_source).unwrap().write_all(IOCTL_CHECK_SOURCE.as_bytes()).unwrap();

let ioctl_preprocessed = String::from_utf8(gcc::Build::new().file(ioctl_source).expand()).unwrap();
let blkgetsize_expr = ioctl_preprocessed.lines().next_back().unwrap().replace("U", " as c_ulong");

let ioctl_include = ioctl_dir.join("ioctl.rs");
File::create(&ioctl_include).unwrap().write_all(IOCTL_INCLUDE_SKELETON.replace("{}", &blkgetsize_expr).as_bytes()).unwrap();
}
4 changes: 2 additions & 2 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ impl HttpHandler {
(Header(headers::Server(USER_AGENT.to_string())),
Header(headers::LastModified(headers::HttpDate(file_time_modified(&req_p)))),
Header(headers::AcceptRanges(vec![headers::RangeUnit::Bytes]))),
req_p,
Header(headers::ContentLength(file_length(&metadata, &req_p))),
req_p.as_path(),
Header(headers::ContentLength(file_length(&req_p.metadata().expect("Failed to get requested file metadata"), &req_p))),
mt)))
}

Expand Down
7 changes: 1 addition & 6 deletions src/util/os/non_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ 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;
include!(concat!(env!("OUT_DIR"), "/ioctl-data/ioctl.rs"));


/// OS-specific check for fileness
Expand Down

0 comments on commit 4e94aa9

Please sign in to comment.