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 macOS #39

Merged
merged 1 commit into from
Nov 12, 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
16 changes: 8 additions & 8 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ jobs:
os: ubuntu-latest
features: ""
target: "x86_64-unknown-linux-gnu"
# - rust: "stable"
# os: macos-latest
# features: ""
# target: "x86_64-apple-darwin"
# - rust: "stable"
# os: macos-14
# features: ""
# target: "aarch64-apple-darwin"
- rust: "stable"
os: macos-latest
features: ""
target: "x86_64-apple-darwin"
- rust: "stable"
os: macos-14
features: ""
target: "aarch64-apple-darwin"
# - rust: stable-x86_64-gnu
# os: windows-2022
# features: ""
Expand Down
15 changes: 10 additions & 5 deletions bzip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ use libbzip2_rs_sys::{
use libc::{
_exit, close, exit, fclose, fdopen, ferror, fflush, fgetc, fileno, fopen, fread, fwrite, open,
perror, remove, rewind, signal, stat, strcat, strcmp, strlen, strncpy, ungetc, utimbuf, write,
FILE, SIGBUS, SIGHUP, SIGINT, SIGSEGV, SIGTERM,
FILE, O_CREAT, O_EXCL, O_WRONLY, SIGBUS, SIGHUP, SIGINT, SIGSEGV, SIGTERM, S_IRUSR, S_IWUSR,
};

// FIXME remove this
extern "C" {
#[cfg_attr(target_os = "macos", link_name = "__stdinp")]
static mut stdin: *mut FILE;
#[cfg_attr(target_os = "macos", link_name = "__stdoutp")]
static mut stdout: *mut FILE;
}

type Bool = libc::c_uchar;

type IntNative = libc::c_int;
Expand Down Expand Up @@ -903,8 +908,8 @@ unsafe fn fileExists(name: *mut c_char) -> Bool {
unsafe fn fopen_output_safely(name: *mut c_char, mode: *const libc::c_char) -> *mut FILE {
let fh = open(
name,
0o1 as libc::c_int | 0o100 as libc::c_int | 0o200 as libc::c_int,
0o200 as libc::c_int | 0o400 as libc::c_int,
O_WRONLY | O_CREAT | O_EXCL,
S_IWUSR as libc::c_uint | S_IRUSR as libc::c_uint,
);
if fh == -1 as libc::c_int {
return std::ptr::null_mut::<FILE>();
Expand Down Expand Up @@ -967,13 +972,13 @@ unsafe fn countHardLinks(name: *mut c_char) -> i32 {

#[cfg(unix)]
fn count_hardlinks(path: &Path) -> u64 {
use std::os::linux::fs::MetadataExt;
use std::os::unix::fs::MetadataExt;

let Ok(metadata) = path.metadata() else {
return 0;
};

metadata.st_nlink().saturating_sub(1)
metadata.nlink().saturating_sub(1)
}

#[cfg(not(unix))]
Expand Down
3 changes: 2 additions & 1 deletion bzip2recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ fn main_help(program_name: &Path, in_filename: &Path) -> Result<(), Error> {
options.write(true).create(true);

#[cfg(unix)]
options.mode(libc::S_IWUSR | libc::S_IRUSR);
#[allow(clippy::unnecessary_cast)]
options.mode(libc::S_IWUSR as u32 | libc::S_IRUSR as u32);

#[cfg(unix)]
options.custom_flags(libc::O_EXCL);
Expand Down
7 changes: 5 additions & 2 deletions bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ use crate::decompress::{self, decompress};
use crate::libbzip2_rs_sys_version;
use crate::BZ_MAX_UNUSED;

// FIXME remove this
extern "C" {
static stdin: *mut FILE;
static stdout: *mut FILE;
#[cfg_attr(target_os = "macos", link_name = "__stdinp")]
static mut stdin: *mut FILE;
#[cfg_attr(target_os = "macos", link_name = "__stdoutp")]
static mut stdout: *mut FILE;
}
Comment on lines +13 to 19
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah these are also a problem on windows, leading me to suspect that this binary was not actually cross-platform . Invalid windows paths are handled, but this would not even build on windows...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C stdin and stdout are macros which expand to a libc specific expression. Unfortunately the libc crate doesn't provide the definitions of these macros, so we have to manually write their expansion instead.


pub(crate) const BZ_MAX_ALPHA_SIZE: usize = 258;
Expand Down
16 changes: 10 additions & 6 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,29 +1594,33 @@ mod compress_command {
#[test]
#[cfg(unix)]
fn io_error() {
use std::os::fd::FromRawFd;

let mut cmd = command();

let tmpdir = tempfile::tempdir().unwrap();

let sample1_ref = tmpdir.path().join("sample1.ref");
std::fs::copy("tests/input/quick/sample1.ref", &sample1_ref).unwrap();

let (master, tty) = setup_custom_tty();

// dropping here triggers an IO error down the line
drop(master);
let mut pair = [0; 2];
unsafe {
assert_eq!(libc::pipe(&mut pair as *mut [i32; 2] as *mut i32), 0);
// closing the read side here triggers an IO error down the line
assert_eq!(libc::close(pair[0]), 0);
}

expect_failure!(
cmd.arg("-z")
.arg("-c")
.arg("-k")
.arg(&sample1_ref)
.stdout(tty),
.stdout(unsafe { Stdio::from_raw_fd(pair[1]) }),
format!(
concat!(
"\n",
"bzip2: I/O or other error, bailing out. Possible reason follows.\n",
"bzip2: Input/output error\n",
"bzip2: Broken pipe\n",
"\tInput file = {in_file}, output file = (stdout)\n",
""
),
Expand Down
Loading