#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::cargo)]
#![allow(unknown_lints)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![warn(trivial_casts, trivial_numeric_casts)]
#![warn(unused_qualifications)]
#![warn(variant_size_differences)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(
any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
),
doc = "```"
)]
#![cfg_attr(
not(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
)),
doc = "```compile_fail"
)]
#![no_std]
#![doc(html_root_url = "https://docs.rs/sysdir/1.3.3")]
#[cfg(test)]
extern crate std;
#[cfg(all(
doctest,
any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
)
))]
#[doc = include_str!("../README.md")]
mod readme {}
#[doc = include_str!("../sysdir.3")]
#[cfg(any(doc, doctest))]
pub mod man {}
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
#[allow(clippy::all)]
#[allow(clippy::pedantic)]
#[allow(clippy::restriction)]
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
))]
mod sys;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
))]
pub use self::sys::*;
#[cfg(all(
test,
any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
)
))]
mod tests {
use core::ffi::{CStr, c_char};
use std::os::unix::ffi::OsStrExt;
use std::{borrow::Cow, env};
use super::*;
fn expected_local_users_directory() -> Cow<'static, [u8]> {
match env::var_os("NEXT_ROOT") {
Some(next_root) => {
let next_root = next_root.as_os_str().as_bytes();
let next_root = next_root
.iter()
.rposition(|&byte| byte != b'/')
.map_or(&[][..], |pos| &next_root[..=pos]);
if next_root.is_empty() {
Cow::Borrowed(b"/Users")
} else {
let mut path = std::vec::Vec::with_capacity(next_root.len() + b"/Users".len());
path.extend_from_slice(next_root);
path.extend_from_slice(b"/Users");
Cow::Owned(path)
}
}
None => Cow::Borrowed(b"/Users"),
}
}
#[test]
fn example_and_linkage() {
let mut count = 0_usize;
let mut path = [0; PATH_MAX as usize];
let expected = expected_local_users_directory();
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 bytes = path.to_bytes();
assert_eq!(bytes, expected.as_ref());
count += 1;
}
}
assert_eq!(count, 1, "Should iterate once and find `/Users`");
}
#[test]
fn example_and_linkage_with_opaque_state_helpers() {
let mut count = 0_usize;
let mut path = [0; PATH_MAX as usize];
let expected = expected_local_users_directory();
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.is_finished() {
break;
}
let path = CStr::from_ptr(path);
let bytes = path.to_bytes();
assert_eq!(bytes, expected.as_ref());
count += 1;
}
}
assert_eq!(count, 1, "Should iterate once and find `/Users`");
}
}