jabedude / ofiles

Small library for associating processes and open files
BSD 3-Clause "New" or "Revised" License
9 stars 5 forks source link

Support for macOS #6

Open jabedude opened 3 years ago

jabedude commented 3 years ago

Add support for macOS arm64/x64 + FreeBSD (insofar as any code can be shared between platforms)

qforce85 commented 3 years ago

The documentation over at docs.rs misleadingly suggests that ofiles supports macOS 64-bit, but it actually doesn't work, it only compiles.

The following test program takes a file path from the command-line and prints whether the file is open or not. On Linux, this works as expected: It either prints "is_open: true" or "is_open: false". On macOS 10.15, it always prints "is_open: false".

use std::io::Result;
use std::path::{Path, PathBuf};

fn main() {
    let arg = std::env::args().nth(1).unwrap();
    let path = PathBuf::from(arg);
    match is_open(path.as_path()) {
        Ok(pred) => println!("is_open: {:?}", pred),
        Err(_) => println!("is_open failed"),
    }
}

fn is_open(path: &Path) -> Result<bool> {
    if !path.is_file() {
        return Ok(false)
    }
    use std::io::{Error, ErrorKind};
    match ofiles::opath(path) {
        Ok(pids) => Ok(!pids.is_empty()),
        Err(e) => Err(Error::new(ErrorKind::Other, e.description())),
    }
}
jabedude commented 3 years ago

Hi @qforce85, I don't see any info on docs.rs or the crates.io page that claims or suggests that files works on macOS. Could you show me a screenshot of what you're referring to?

qforce85 commented 3 years ago

Sure, see the red arrow in the attached screenshot. Screenshot-20210313-190946

jabedude commented 3 years ago

Interesting, looks like docs.rs will build for a few default targets documented here: https://docs.rs/about/metadata. Relevant snippet:

# Target to test build on, used as the default landing page (default: "x86_64-unknown-linux-gnu")
#
# Any target supported by rustup can be used.
default-target = "x86_64-unknown-linux-gnu"

# Targets to build (default: see below)
#
# Any target supported by rustup can be used.
#
# Default targets:
# - x86_64-unknown-linux-gnu
# - x86_64-apple-darwin
# - x86_64-pc-windows-msvc
# - i686-unknown-linux-gnu
# - i686-pc-windows-msvc
#
# Set this to `[]` to only build the default target.
#
# If `default-target` is unset, the first element of `targets` is treated as the default target.
# Otherwise, these `targets` are built in addition to the default target.
# If both `default-target` and `targets` are unset,
#   all tier-one targets will be built and `x86_64-unknown-linux-gnu` will be used as the default target.
targets = ["x86_64-apple-darwin", "x86_64-pc-windows-msvc"]