Skip to content

Commit

Permalink
Merge pull request #117 from lu-zero/non-linux
Browse files Browse the repository at this point in the history
feat: Initial support for non-linux
  • Loading branch information
reubeno authored Jul 15, 2024
2 parents dec0f7f + 18df610 commit d18bc0a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ permissions:
contents: read

jobs:
build_x86_64:
name: "Build (x86_64/Linux)"
runs-on: ubuntu-latest
build:
strategy:
matrix:
include:
- host: "ubuntu-latest"
os: "linux"
arch: "x86_64"
- host: "macos-latest"
os: "macos"
arch: "aarch64"

name: "Build (${{ matrix.arch }}/${{ matrix.os }})"
runs-on: ${{ matrix.host }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -47,7 +57,7 @@ jobs:
- name: "Upload binaries"
uses: actions/upload-artifact@v4
with:
name: binaries-x86_64-linux
name: binaries-${{ matrix.arch }}-${{ matrix.os }}
path: target/release/brush

build_cross:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion brush-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async-recursion = "1.1.0"
async-trait = "0.1.80"
brush-parser = { version = "^0.2.3", path = "../brush-parser" }
cached = "0.51.3"
cfg-if = "1.0.0"
# N.B. Pin to 4.4.18 for now to keep to 1.72.0 as MSRV; 4.5.x requires a later version.
clap = { version = "=4.4.18", features = ["derive", "wrap_help"] }
fancy-regex = "0.13.0"
Expand Down Expand Up @@ -57,9 +58,11 @@ whoami = "1.5.1"
[target.'cfg(unix)'.dependencies]
command-fds = "0.3.0"
nix = { version = "0.29.0", features = ["fs", "process", "signal", "term", "user"] }
procfs = "0.16.0"
uzers = "0.12.0"

[target.'cfg(target_os = "linux")'.dependencies]
procfs = "0.16.0"

[dev-dependencies]
anyhow = "1.0.86"
criterion = { version = "0.5.1", features = ["async_tokio", "html_reports"] }
Expand Down
23 changes: 18 additions & 5 deletions brush-core/src/builtins/umask.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{builtin, commands, error};
use cfg_if::cfg_if;
use clap::Parser;
use nix::sys::stat::Mode;
use std::io::Write;

/// Manage the process umask.
Expand Down Expand Up @@ -53,14 +55,25 @@ impl builtin::Command for UmaskCommand {
}
}

fn get_umask() -> Result<u32, error::Error> {
let me = procfs::process::Process::myself()?;
let status = me.status()?;
status.umask.ok_or_else(|| error::Error::InvalidUmask)
cfg_if! {
if #[cfg(target_os = "linux")] {
fn get_umask() -> Result<u32, error::Error> {
let me = procfs::process::Process::myself()?;
let status = me.status()?;
status.umask.ok_or_else(|| error::Error::InvalidUmask)
}
} else {
fn get_umask() -> Result<u32, error::Error> {
let u = nix::sys::stat::umask(Mode::empty());
nix::sys::stat::umask(u);
Ok(u.bits() as u32)
}
}
}

fn set_umask(value: u32) -> Result<(), error::Error> {
let mode = nix::sys::stat::Mode::from_bits(value).ok_or_else(|| error::Error::InvalidUmask)?;
let mode =
nix::sys::stat::Mode::from_bits(value as _).ok_or_else(|| error::Error::InvalidUmask)?;
nix::sys::stat::umask(mode);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion brush-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub enum Error {
InvalidUmask,

/// An error occurred reading from procfs.
#[cfg(unix)]
#[cfg(target_os = "linux")]
#[error("procfs error: {0}")]
ProcfsError(#[from] procfs::ProcError),

Expand Down
4 changes: 2 additions & 2 deletions brush-core/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use brush_parser::ast::{self, CommandPrefixOrSuffixItem};
use itertools::Itertools;
use std::collections::VecDeque;
use std::io::Write;
#[cfg(unix)]
#[cfg(target_os = "linux")]
use std::os::fd::{AsFd, AsRawFd};
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
Expand Down Expand Up @@ -1469,7 +1469,7 @@ fn setup_open_file_with_contents(contents: &str) -> Result<OpenFile, error::Erro
let bytes = contents.as_bytes();
let len = i32::try_from(bytes.len())?;

#[cfg(unix)]
#[cfg(target_os = "linux")]
nix::fcntl::fcntl(
reader.as_fd().as_raw_fd(),
nix::fcntl::FcntlArg::F_SETPIPE_SZ(len),
Expand Down

0 comments on commit d18bc0a

Please sign in to comment.