xdg Rust Library
repository·main·Indexed 41 days ago
https://github.com/whitequark/rust-xdgA Rust library for storing and retrieving files according to the XDG Base Directory specification. It provides the BaseDirectories struct to manage paths for configuration, data, cache, state, and runtime files, ensuring applications follow Linux desktop standards for file placement. Version 3.0.0.
What's inside xdg
- rust-xdg is a library designed to help developers follow the X Desktop Group specifications. As of the current version, it specifically implements the XDG Base Directory specification, allowing applications to correctly locate standard directories for configuration, data, and cache files.
Install the xdg crate
mainTo use
rust-xdgin your Rust project, add thexdgcrate to yourCargo.tomldependencies. This library provides implementations for the XDG Base Directory specification.[dependencies] xdg = "^2.6"How application prefixes and profiles work
mainWhen initializing
BaseDirectories, you can provide aprefixand an optionalprofile. These are used to isolate your application's files from other applications and to support multiple user profiles.- Prefix: If a prefix (e.g.,
myapp) is provided,get_*methods will automatically look into a subdirectory named after the prefix (e.g.,~/.config/myapp/). - Profile: If a profile (e.g.,
default_profile) is provided, it creates an additional layer of nesting (e.g.,~/.config/myapp/default_profile/).
Search Behavior:
get_*methods use the prefix and profile to construct a direct path.find_*methods search through the hierarchy. When searching for configuration files, the search includes the application's specific directory (including the profile) and system-wide directories defined inXDG_CONFIG_DIRS. However, system-wide directories typically do not contain the application'sprofilesubdirectory.
- Prefix: If a prefix (e.g.,
List configuration and data files
mainYou can iterate over files within the XDG directories using
list_*methods.list_config_files(prefix: &str) -> Vec<PathBuf>: Lists all configuration files found in all configuration directories (including system-wideXDG_CONFIG_DIRS).list_config_files_once(prefix: &str) -> Vec<PathBuf>: Lists configuration files but only from the primaryXDG_CONFIG_HOME(ignoring system-wide directories).list_data_files(prefix: &str) -> Vec<PathBuf>: Lists data files across all data directories.
// Example: listing all config files let files = xd.list_config_files(".");Find existing configuration or data files
mainTo locate files that already exist on the system, use the
find_*methods. These methods search through user-specific directories and system-wide directories (defined byXDG_CONFIG_DIRSorXDG_DATA_DIRS) in order of priority.find_config_file(path): SearchesXDG_CONFIG_HOMEandXDG_CONFIG_DIRS.find_data_file(path): SearchesXDG_DATA_HOMEandXDG_DATA_DIRS.find_cache_file(path): SearchesXDG_CACHE_HOME.find_state_file(path): SearchesXDG_STATE_HOME.find_runtime_file(path): SearchesXDG_RUNTIME_DIR.
// Example: Finding a data file like a logo let logo_path = xdg_dirs .find_data_file("logo.png") .expect("application data not present");Access the XDG Runtime Directory
mainThe runtime directory (set by
XDG_RUNTIME_DIR) is used for non-persistent files like sockets or named pipes.get_runtime_directory(): Returns the path ifXDG_RUNTIME_DIRis set and is secure (permissions must be0700). Returns anErrorif the directory is missing or insecure.has_runtime_directory(): A boolean check to see if a valid runtime directory is available.place_runtime_file(path): Places a file in the runtime directory.find_runtime_file(path): Finds an existing file in the runtime directory.
Get specific configuration, data, or cache paths
mainUse the
get_*methods to retrieve the path for a specific file within the application's dedicated directory. Unlikefind_*, these methods target the specific directory assigned to your application (using theprefixprovided duringBaseDirectoriesinitialization) and do not search system-wide directories.Available methods:
get_config_file(name: &str) -> Result<PathBuf, Error>get_data_file(name: &str) -> Result<PathBuf, Error>get_cache_file(name: &str) -> Result<PathBuf, Error>
// Example: getting the path for a specific app config file let file = xd.get_config_file("myapp/user_config.file").unwrap();Create application directories
mainUse the
create_*_directorymethods to ensure a specific subdirectory exists within the appropriate XDG home. These methods create the directory and all necessary parent components.create_config_directory(path)create_data_directory(path)create_cache_directory(path)create_state_directory(path)create_runtime_directory(path)
Initialize BaseDirectories
mainUse
BaseDirectoriesto look up paths for configuration, data, cache, state, and runtime files according to the XDG Base Directory specification.There are three main ways to initialize it:
new(): Uses standard environment variables (e.g.,HOME,XDG_CONFIG_HOME).with_prefix(prefix): Automatically prepends a prefix (usually your application name) to all looked-up paths.with_profile(prefix, profile): Prepends both a prefix and a profile name to user-specific directories, allowing multiple user profiles for the same application.
// Standard initialization let xdg_dirs = xdg::BaseDirectories::new(); // Initialization with an application prefix (e.g., "myapp") let xdg_dirs = xdg::BaseDirectories::with_prefix("myapp"); // Initialization with prefix and profile (e.g., "myapp" and "user1") let dirs = xdg::BaseDirectories::with_profile("myapp", "user1");List files in XDG directories
mainYou can list files within specific XDG paths using
list_*_files.list_config_files(path)/list_data_files(path): Returns all files in directories matching the path, searching both user and system directories.list_config_files_once(path)/list_data_files_once(path): Similar to the above, but returns only the first occurrence of any distinct filename (useful if the same config exists in both/etc/xdgand~/.config).list_cache_files(path)/list_state_files(path): Lists files in the user-specific cache or state directories.
Use BaseDirectories to access XDG base directories
mainThe
BaseDirectoriesstruct provides access to standard XDG base directories (likeCONFIG,DATA,CACHE,STATE,XDG_CONFIG_HOME, etc.) on Unix-like systems and Redox. It allows you to locate and interact with standard filesystem locations used by desktop environments.Key components:
BaseDirectories: The main entry point for accessing directory paths.FileFindIterator: An iterator used for finding files within these directories.Error: A specialized error type for base directory operations.
use xdg::BaseDirectories; // Example usage (conceptual based on exported types): // let base_dirs = BaseDirectories::new(...); // let config_dir = base_dirs.config_dir();Manage runtime directory files
mainThe runtime directory (
XDG_RUNTIME_DIR) is used for non-persistent files.BaseDirectoriesprovides methods to manage files within this directory.has_runtime_directory() -> bool: Checks if a valid runtime directory is available.create_runtime_directory(name: &str) -> Result<(), Error>: Creates a subdirectory within the runtime directory.place_runtime_file(path: &str) -> Result<PathBuf, Error>: Returns aPathBuffor a file within the runtime directory, creating parent directories as needed. Note that this does not create the file itself; you must create it (e.g., usingFile::create).find_runtime_file(path: &str) -> Option<PathBuf>: Searches for a file in the runtime directory.list_runtime_files(prefix: &str) -> Vec<PathBuf>: Lists files/directories within the runtime directory matching the prefix.
// Example: placing a file in the runtime directory let w = xd.place_runtime_file("bar/baz").unwrap(); std::fs::File::create(&w).unwrap();