5 stable releases

1.2.2 Jun 2, 2023
1.2.1 May 14, 2023
1.1.0 May 13, 2023
1.0.0 May 12, 2023

#30 in macOS and iOS APIs

Download history 90/week @ 2023-12-14 150/week @ 2023-12-21 174/week @ 2023-12-28 192/week @ 2024-01-04 89/week @ 2024-01-11 115/week @ 2024-01-18 86/week @ 2024-01-25 200/week @ 2024-02-01 88/week @ 2024-02-08 178/week @ 2024-02-15 155/week @ 2024-02-22 311/week @ 2024-02-29 104/week @ 2024-03-07 106/week @ 2024-03-14 99/week @ 2024-03-21 179/week @ 2024-03-28

503 downloads per month

Apache-2.0 OR MIT

15KB
185 lines

sysdir-rs

GitHub Actions Discord Twitter
Crate API API trunk

Enumeration of the filesystem paths for the various standard system directories where apps, resources, etc. get installed.

This crate exposes Rust bindings to the sysdir(3) library functions provided by libSystem.dylib on macOS, iOS, tvOS, and watchOS.

The sysdir API first appeared in OS X 10.12, iOS 10, watchOS 3 and tvOS 10 replacing the deprecated NSSystemDirectories(3) API.

Usage

Add this to your Cargo.toml:

[dependencies]
sysdir = "1.2.2"

Then resolve well-known directories like this:

use core::ffi::{c_char, CStr};

use sysdir::*;

let mut path = [0; PATH_MAX as usize];

let dir = sysdir_search_path_directory_t::SYSDIR_DIRECTORY_USER;
let domain_mask = SYSDIR_DOMAIN_MASK_LOCAL;

unsafe {
    let mut state = sysdir_start_search_path_enumeration(dir, domain_mask);
    loop {
        let path = path.as_mut_ptr().cast::<c_char>();
        state = sysdir_get_next_search_path_enumeration(state, path);
        if state == 0 {
            break;
        }
        let path = CStr::from_ptr(path);
        let s = path.to_str().unwrap();
        assert_eq!(s, "/Users");
    }
}

You can test this crate works on your platform by running the example:

cargo run --example enumerate_system_dirs

Implementation

sysdir-rs binds directly to libSystem with vendored bindings generated by bindgen. This crate has no dependencies other than libSystem.

Note that this crate is completely empty on non-Apple platforms.

no_std

sysdir-rs is no_std and only requires core.

Minimum Supported Rust Version

This crate requires at least Rust 1.64.0. This version can be bumped in minor releases.

License

sysdir-rs is distributed under the terms of either the MIT License or the Apache License (Version 2.0).

No runtime deps