use std::collections::HashMap; use std::ffi::OsString; use std::fs; use std::io::{self, Read}; use std::os::unix::ffi::OsStringExt; use std::path::{Path, PathBuf}; use std::str;
/// Returns all XDG user directories obtained from $(XDG_CONFIG_HOME)/user-dirs.dirs. pubfn all(home_dir_path: &Path, user_dir_file_path: &Path) -> HashMap<String, PathBuf> { let bytes = read_all(user_dir_file_path).unwrap_or(Vec::new());
parse_user_dirs(home_dir_path, None, &bytes)
}
/// Returns a single XDG user directory obtained from $(XDG_CONFIG_HOME)/user-dirs.dirs. pubfn single(home_dir_path: &Path, user_dir_file_path: &Path, user_dir_name: &str) -> HashMap<String, PathBuf> { let bytes = read_all(user_dir_file_path).unwrap_or(Vec::new());
parse_user_dirs(home_dir_path, Some(user_dir_name), &bytes)
}
for line in bytes.split(|b| *b == b'\n') { letmut single_dir_found = false; let (key, value) = match split_once(line, b'=') {
Some(kv) => kv,
None => continue,
};
let key = trim_blank(key); let key = if key.starts_with(b"XDG_") && key.ends_with(b"_DIR") { match str::from_utf8(&key[4..key.len()-4]) {
Ok(key) => if user_dir.is_some() && option_contains(user_dir, key) {
single_dir_found = true;
key
} elseif user_dir.is_none() {
key
} else { continue
},
Err(_) => continue,
}
} else { continue
};
// xdg-user-dirs-update uses double quotes and we don't support anything else. let value = trim_blank(value); letmut value = if value.starts_with(b"\"") && value.ends_with(b"\"") {
&value[1..value.len()-1]
} else { continue
};
// Path should be either relative to the home directory or absolute. let is_relative = if value == b"$HOME/" { // "Note: To disable a directory, point it to the homedir." // Source: https://www.freedesktop.org/wiki/Software/xdg-user-dirs/ // Additionally directory is reassigned to homedir when removed. continue
} elseif value.starts_with(b"$HOME/") {
value = &value[b"$HOME/".len()..]; true
} elseif value.starts_with(b"/") { false
} else { continue
};
let value = OsString::from_vec(shell_unescape(value));
let path = if is_relative { letmut path = PathBuf::from(&home_dir);
path.push(value);
path
} else {
PathBuf::from(value)
};
user_dirs.insert(key.to_owned(), path); if single_dir_found { break;
}
}
user_dirs
}
/// Reads the entire contents of a file into a byte vector. fn read_all(path: &Path) -> io::Result<Vec<u8>> { letmut file = fs::File::open(path)?; letmut bytes = Vec::with_capacity(1024);
file.read_to_end(&mut bytes)?;
Ok(bytes)
}
/// Returns bytes before and after first occurrence of separator. fn split_once(bytes: &[u8], separator: u8) -> Option<(&[u8], &[u8])> {
bytes.iter().position(|b| *b == separator).map(|i| {
(&bytes[..i], &bytes[i+1..])
})
}
/// Returns a slice with leading and trailing <blank> characters removed. fn trim_blank(bytes: &[u8]) -> &[u8] { // Trim leading <blank> characters. let i = bytes.iter().cloned().take_while(|b| *b == b' ' || *b == b'\t').count(); let bytes = &bytes[i..];
// Trim trailing <blank> characters. let i = bytes.iter().cloned().rev().take_while(|b| *b == b' ' || *b == b'\t').count();
&bytes[..bytes.len()-i]
}
/// Unescape bytes escaped with POSIX shell double-quotes rules (as used by xdg-user-dirs-update). fn shell_unescape(escaped: &[u8]) -> Vec<u8> { // We assume that byte string was created by xdg-user-dirs-update which // escapes all characters that might potentially have special meaning, // so there is no need to check if backslash is actually followed by // $ ` " \ or a <newline>.
letmut unescaped: Vec<u8> = Vec::with_capacity(escaped.len()); letmut i = escaped.iter().cloned();
#[cfg(test)] mod tests { use std::collections::HashMap; use std::path::{Path, PathBuf}; usesuper::{trim_blank, shell_unescape, split_once, parse_user_dirs};
let bytes = br#" # This file is written by xdg-user-dirs-update # If you want to change or add directories, just edit the line you're # interested in. All local changes will be retained on the next run. # Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped # homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an # absolute path. No other format is supported.
XDG_DESKTOP_DIR="$HOME/Desktop"
XDG_DOWNLOAD_DIR="$HOME/Downloads"
XDG_TEMPLATES_DIR=""
XDG_PUBLICSHARE_DIR="$HOME"
XDG_DOCUMENTS_DIR="$HOME/"
XDG_PICTURES_DIR="/home/eve/pics"
XDG_VIDEOS_DIR="$HOxyzME/Videos" "#;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.