Skip to content
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

Add support for windows compilation #689

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/binwalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::os::unix;
use std::path;
use uuid::Uuid;

#[cfg(windows)]
use std::os::windows;

#[cfg(unix)]
use std::os::unix;

use crate::common::read_file;
use crate::extractors;
use crate::magic;
Expand Down Expand Up @@ -634,18 +639,38 @@ fn init_extraction_directory(
);

// Create a symlink from inside the extraction directory to the specified target file
match unix::fs::symlink(&target_path, &symlink_path) {
Ok(_) => {
return Ok(symlink_target_path_str);
#[cfg(unix)]
{
match unix::fs::symlink(&target_path, &symlink_path) {
Ok(_) => {
return Ok(symlink_target_path_str);
}
Err(e) => {
error!(
"Failed to create symlink {} -> {}: {}",
symlink_path.to_str().unwrap(),
target_path.to_str().unwrap(),
e
);
return Err(e);
}
}
Err(e) => {
error!(
"Failed to create symlink {} -> {}: {}",
symlink_path.to_str().unwrap(),
target_path.to_str().unwrap(),
e
);
return Err(e);
}
#[cfg(windows)]
{
match windows::fs::symlink_file(target_path, symlink_path) {
Ok(_) => {
return Ok(symlink_target_path_str);
}
Err(e) => {
error!(
"Failed to create symlink {} -> {}: {}",
symlink_path.to_str().unwrap(),
target_path.to_str().unwrap(),
e
);
return Err(e);
}
}
}
}
Expand Down
78 changes: 55 additions & 23 deletions src/extractors/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Write;
use std::os::unix;
use std::os::unix::fs::PermissionsExt;
use std::path;
use std::process;
use walkdir::WalkDir;

#[cfg(windows)]
use std::os::windows;

#[cfg(unix)]
use std::os::unix;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

/// This contstant in command line arguments will be replaced with the path to the input file
pub const SOURCE_FILE_PLACEHOLDER: &str = "%e";

Expand Down Expand Up @@ -506,20 +512,23 @@ impl Chroot {
);
}
Ok(metadata) => {
let mut permissions = metadata.permissions();
permissions.set_mode(permissions.mode() | UNIX_EXEC_FLAG);

match fs::set_permissions(safe_file_path.clone(), permissions) {
Err(e) => {
error!(
"Failed to set permissions for file {}: {}",
safe_file_path, e
);
}
Ok(_) => {
return true;
#[cfg(unix)]
{
let mode = _metadata.permissions().mode() | UNIX_EXEC_FLAG;
let new_permissions = _metadata.permissions().set_mode(mode);

match fs::set_permissions(&file_path, new_permissions) {
Err(e) => {
error!("Failed to set permissions for file {}: {}", safe_file_path, e);
false
},
Ok(_) => true
}
}
#[cfg(windows)]
{
return true
}
}
}

Expand Down Expand Up @@ -587,16 +596,39 @@ impl Chroot {
safe_target_path = path::Path::new(&safe_target);
}

match unix::fs::symlink(&safe_target_path, &safe_symlink_path) {
Ok(_) => {
return true;
#[cfg(unix)]
{
match unix::fs::symlink(&safe_target_path, &safe_symlink_path) {
Ok(_) => {
return true;
}
Err(e) => {
error!(
"Failed to create symlink from {} -> {}: {}",
symlink, target, e
);
return false;
}
}
Err(e) => {
error!(
"Failed to created symlink from {} -> {}: {}",
symlink, target, e
);
return false;
}
#[cfg(windows)]
{
// let sym = match safe_target_path.is_dir() {
// true => windows::fs::symlink_dir(safe_target_path, safe_symlink_path),
// false => windows::fs::symlink_file(safe_target_path, safe_symlink_path),
// };

match windows::fs::symlink_dir(safe_target_path, safe_symlink_path) {
Ok(_) => {
return true;
}
Err(e) => {
error!(
"Failed to create symlink from {} -> {}: {}",
symlink, target, e
);
return false;
}
}
}
}
Expand Down