xdg-home-1.3.0/.cargo_vcs_info.json0000644000000001360000000000100125450ustar { "git": { "sha1": "b0e810f0ee1970804df4ad97e2af422ec980702e" }, "path_in_vcs": "" }xdg-home-1.3.0/.github/renovate.json000064400000000000000000000006541046102023000154200ustar 00000000000000{ "packageRules": [ { "matchManagers": ["github-actions"], "commitMessagePrefix": "⬆️ " }, { "matchManagers": ["cargo"], "commitMessagePrefix": "⬆️ ", "commitMessageTopic": "{{depName}}", "lockFileMaintenance": { "enabled": true } }, { "matchUpdateTypes": ["patch", "pin", "digest"], "automerge": true, "rebaseWhen": "conflicted" } ] } xdg-home-1.3.0/.github/workflows/release.yml000064400000000000000000000004341046102023000170760ustar 00000000000000name: Release on: push: tags: - "*.*.*" jobs: release: runs-on: ubuntu-latest permissions: contents: write steps: - uses: actions/checkout@v4 with: ref: ${{ github.ref }} - uses: spenserblack/actions-tag-to-release@main xdg-home-1.3.0/.github/workflows/rust.yml000064400000000000000000000020341046102023000164510ustar 00000000000000name: Rust on: push: branches: ["main"] pull_request: branches: ["main"] env: CARGO_TERM_COLOR: always jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] steps: - uses: actions/checkout@v4 - uses: actions-rs/toolchain@v1 with: toolchain: stable - uses: actions-rs/cargo@v1 with: command: test fmt: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions-rs/toolchain@v1 with: toolchain: nightly override: true components: rustfmt - uses: actions-rs/cargo@v1 with: command: fmt args: --all -- --check clippy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: toolchain: nightly override: true components: clippy - uses: actions-rs/cargo@v1 with: command: clippy args: -- -D warnings xdg-home-1.3.0/.gitignore000064400000000000000000000000071046102023000133220ustar 00000000000000target xdg-home-1.3.0/Cargo.toml0000644000000023040000000000100105420ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.60" name = "xdg-home" version = "1.3.0" authors = ["Zeeshan Ali Khan "] build = false autobins = false autoexamples = false autotests = false autobenches = false description = "The user's home directory as per XDG Specification" readme = "README.md" keywords = [ "xdg", "home", ] categories = [ "filesystem", "os::unix-apis", "os::windows-apis", ] license = "MIT" repository = "https://github.com/zeenix/xdg-home" [lib] name = "xdg_home" path = "src/lib.rs" [target."cfg(unix)".dependencies.libc] version = "0.2" [target."cfg(windows)".dependencies.windows-sys] version = "0.59" features = [ "Win32_Foundation", "Win32_UI_Shell", "Win32_System_Com", ] xdg-home-1.3.0/Cargo.toml.orig000064400000000000000000000012541046102023000142260ustar 00000000000000[package] name = "xdg-home" version = "1.3.0" edition = "2021" authors = ["Zeeshan Ali Khan "] rust-version = "1.60" description = "The user's home directory as per XDG Specification" repository = "https://github.com/zeenix/xdg-home" license = "MIT" keywords = ["xdg", "home"] categories = ["filesystem", "os::unix-apis", "os::windows-apis"] readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [target.'cfg(unix)'.dependencies] libc = "0.2" [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.59", features = [ "Win32_Foundation", "Win32_UI_Shell", "Win32_System_Com", ] } xdg-home-1.3.0/LICENSE-MIT000064400000000000000000000017771046102023000130050ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. xdg-home-1.3.0/README.md000064400000000000000000000014321046102023000126140ustar 00000000000000# xdg-home Gets the user's home directory as per [XDG Base Directory Specification][xdg]. This is almost the same as [`home`] (and [`dirs`]) crate, except it honors `HOME` environment variable on the Windows platform as well, which is conformant to the XDG Base Directory Specification. Use it where the XDG Base Directory Specification is applicable, such as in [D-Bus] code. ## Example ```rust use xdg_home::home_dir; let home = home_dir().unwrap(); assert!(home.is_absolute()); assert!(home.exists()); println!("Home directory: {}", home.display()); ``` [xdg]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html [`home`]: https://crates.io/crates/home [`dirs`]: https://crates.io/crates/dirs [D-Bus]: https://dbus.freedesktop.org/doc/dbus-specification.html xdg-home-1.3.0/src/lib.rs000064400000000000000000000065201046102023000132430ustar 00000000000000#![doc = include_str!("../README.md")] #![doc(test(attr( warn(unused), deny(warnings), // W/o this, we seem to get some bogus warning about `extern crate ..`. allow(unused_extern_crates), )))] use std::path::PathBuf; /// Get the path of the current user's home directory. /// /// See the library documentation for more information. pub fn home_dir() -> Option { match std::env::var("HOME") { Ok(home) => Some(home.into()), Err(_) => { #[cfg(unix)] { unix::home_dir() } #[cfg(windows)] { win32::home_dir() } } } } #[cfg(unix)] mod unix { use std::ffi::{CStr, OsStr}; use std::os::unix::ffi::OsStrExt; use std::path::PathBuf; pub(super) fn home_dir() -> Option { let uid = unsafe { libc::geteuid() }; // SAFETY: Not initalizing references here so it's safe. let mut passwd: libc::passwd = unsafe { std::mem::zeroed() }; // This has to be enough for everyone. let mut passwd_buf = [0_u8; 1024]; let mut result = std::ptr::null_mut(); let ret = unsafe { libc::getpwuid_r( uid, &mut passwd, passwd_buf.as_mut_ptr() as *mut _, passwd_buf.len(), &mut result, ) }; if ret != 0 || result.is_null() || passwd.pw_dir.is_null() { return None; } // SAFETY: `getpwuid()->pw_dir` is a valid pointer to a c-string. let home_dir = unsafe { CStr::from_ptr(passwd.pw_dir) }; Some(PathBuf::from(OsStr::from_bytes(home_dir.to_bytes()))) } } #[cfg(windows)] mod win32 { use std::{path::PathBuf, ptr::null_mut}; use windows_sys::Win32::Foundation::S_OK; use windows_sys::Win32::System::Com::CoTaskMemFree; use windows_sys::Win32::UI::Shell::FOLDERID_Profile; use windows_sys::Win32::UI::Shell::SHGetKnownFolderPath; pub(super) fn home_dir() -> Option { let rfid = FOLDERID_Profile; let mut psz_path = null_mut(); let res = unsafe { SHGetKnownFolderPath(&rfid, 0, null_mut(), &mut psz_path as *mut _) }; if res != S_OK { return None; } // Determine the length of the UTF-16 string. let mut len = 0; // SAFETY: `psz_path` guaranteed to be a valid pointer to a null-terminated UTF-16 string. while unsafe { *(psz_path as *const u16).offset(len) } != 0 { len += 1; } let slice = unsafe { std::slice::from_raw_parts(psz_path, len as usize) }; let path = String::from_utf16(slice).ok()?; unsafe { CoTaskMemFree(psz_path as *mut _); } Some(PathBuf::from(path)) } } #[cfg(test)] mod tests { use super::*; #[test] fn home() { let home = home_dir().unwrap(); assert!(home.is_dir()); if let Ok(env_home) = std::env::var("HOME") { // If `HOME` is set, `home_dir` took the value from it. let env_home = PathBuf::from(env_home); assert_eq!(home, env_home); // With `HOME` unset, `home_dir` should still return the same value. std::env::remove_var("HOME"); assert_eq!(home_dir().unwrap(), env_home); } } }