Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace users crate #14

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/rust
{
"name": "Rust",
"image": "mcr.microsoft.com/devcontainers/rust:1-1-bookworm"
}
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[package]
name = "standard_paths"
description = "A port of QStandardPaths class which provides methods for accessing standard paths on the local filesystem (config, cache, user directories and etc.)."
description = """\
A port of QStandardPaths class which provides methods for accessing \
standard paths on the local filesystem (config, cache, user directories \
and etc.).\
"""
version = "2.0.1"
edition = "2021"
authors = ["Petr Tsymbarovich <petr@tsymbarovich.ru>"]
Expand All @@ -15,10 +19,10 @@ readme = "README.md"
home = "0.5"

[target.'cfg(target_os = "linux")'.dependencies]
users = "0.11"
nix = { version = "^0.26", default-features = false, features = ["ioctl", "user"] }

[target.'cfg(windows)'.dependencies]
winapi = { version = "^0.3", features = ["shlobj", "combaseapi", "winbase"] }

[dev_dependencies]
[dev-dependencies]
argparse = "0.2"
24 changes: 16 additions & 8 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
os::{linux::fs::MetadataExt, unix::fs::PermissionsExt},
path::PathBuf,
};
use users::{get_current_uid, get_user_by_uid};

use crate::{LocationType, StandardPaths};

Expand Down Expand Up @@ -101,7 +100,8 @@ impl StandardPaths {

LocationType::RuntimeLocation => {
// http://standards.freedesktop.org/basedir-spec/latest/
let user = get_user_by_uid(get_current_uid()).unwrap();
let uid = nix::unistd::geteuid();
let user_id = uid.as_raw();
let (path, md) = match env::var("XDG_RUNTIME_DIR") {
Ok(path) => {
let md = fs::metadata(&path)?;
Expand All @@ -116,8 +116,17 @@ impl StandardPaths {
(PathBuf::from(path), md)
}
_ => {
let user = match nix::unistd::User::from_uid(uid) {
Ok(opt) => opt.unwrap(),
Err(err) => {
return Err(Error::new(
ErrorKind::NotFound,
format!("Failed to detect current user: {err}"),
))
}
};
let mut runtime_dir = String::from("runtime-");
runtime_dir.push_str(&user.name().to_string_lossy());
runtime_dir.push_str(&user.name);
let mut path = env::temp_dir();
path.push(runtime_dir);
let md = fs::metadata(&path)?;
Expand All @@ -129,14 +138,13 @@ impl StandardPaths {
};

// The directory MUST be owned by the user
if md.st_uid() != user.uid() {
let ts_uid = md.st_uid();
if ts_uid != user_id {
return Err(Error::new(
ErrorKind::PermissionDenied,
format!(
"Wrong ownership on runtime directory '{}' - {} instead of {}",
path.to_str().unwrap(),
md.st_uid(),
user.uid()
"Wrong ownership on runtime directory '{}' - {ts_uid} instead of {user_id}",
path.to_string_lossy(),
),
));
}
Expand Down
Loading