CLI is working!

This commit is contained in:
Arthur Beck 2025-03-10 19:31:06 -05:00
parent 454958e95d
commit 2aa855f5cf
No known key found for this signature in database
GPG key ID: ACE3D14F5CEF14BF
4 changed files with 227 additions and 37 deletions

View file

@ -12,6 +12,7 @@ use std::{
ffi::OsString,
io::{Read, Write},
path::PathBuf,
os::unix::fs::PermissionsExt
};
use base64::Engine;
@ -197,7 +198,7 @@ pub fn get_current_user() -> OsString {
///
/// This directory is not guaranteed to exist! If you need it to, use [base_dir]!
pub fn get_base_dir() -> PathBuf {
PathBuf::from("/home/arthur/pacwoman/debug")
PathBuf::from("/pacwoman")
}
/// Same as [get_base_dir]; however, this function will create the directory if it doesn't
@ -897,6 +898,8 @@ pub fn receive_package(
package: String,
system: bool,
) -> std::io::Result<PathBuf> {
print!("Attempting to recieve package {}...", package);
let path = index_directory()?
.join(repo.format())
.join(&package)
@ -1005,6 +1008,8 @@ pub fn receive_package(
packages.write_fmt(format_args!("{}-{}\n", desc_hash, package))?;
drop(packages);
println!(" Success.");
Ok(dir)
}
@ -1016,6 +1021,7 @@ pub fn recieve_package_and_dependencies(
system: bool,
) -> std::io::Result<Vec<PathBuf>> {
let mut out: Vec<PathBuf> = vec![];
out.push(receive_package(
mirror.clone(),
repo.clone(),
@ -1039,7 +1045,6 @@ pub fn recieve_package_and_dependencies(
let desc = read_desc(package.clone(), repo.clone())?;
for package in desc.depends {
println!("{}", package);
out.push(receive_package(
mirror.clone(),
repo.clone(),
@ -1047,7 +1052,6 @@ pub fn recieve_package_and_dependencies(
system,
)?);
}
println!("recieved all dependencies");
Ok(out)
}
@ -1061,9 +1065,6 @@ pub fn install_package(
) -> std::io::Result<()> {
let out = bin_dir(system)?;
for path in recieve_package_and_dependencies(mirror, repo, package, system)? {
println!("{}", path.to_string_lossy());
println!("{}", out.to_string_lossy());
if std::fs::exists(index_directory()?.join("INSTALLED"))? {
let mut packages = std::fs::OpenOptions::new()
.read(true)
@ -1105,8 +1106,6 @@ pub fn install_package(
.unwrap_or(&entry.path().to_string_lossy()),
);
println!("{}", entry_path.clone().to_string_lossy());
if entry.file_name() == *".BUILDINFO"
|| entry.file_name() == *".MTREE"
|| entry.file_name() == *".PKGINFO"
@ -1143,18 +1142,18 @@ pub fn install_package(
}
if std::fs::exists(out.join(".INSTALL"))? {
if users::get_effective_uid() == 0 {
std::os::unix::fs::chroot(&out)?;
} else {
println!("[WARN] .INSTALL script for package {} is being run without a chroot.", path.file_name().unwrap().to_string_lossy());
if users::get_effective_uid() != 0 {
println!(
"[WARN] .INSTALL script for package {} is being run without root.",
path.file_name().unwrap().to_string_lossy()
);
println!("[WARN] The script may give permission errors or ask for authentication.");
println!(" [TIP] To run in a chroot, run the program as root.");
}
if !std::process::Command::new("/usr/bin/bash")
.arg("-c")
.arg("source ./.INSTALL && post_install && post_upgrade")
.current_dir(&out)
.current_dir("/")
.spawn()?
.wait()?
.success()
@ -1165,10 +1164,6 @@ pub fn install_package(
);
}
if users::get_effective_uid() == 0 {
std::os::unix::fs::chroot(".")?;
}
std::fs::remove_file(out.join(".INSTALL"))?;
}
@ -1188,17 +1183,47 @@ pub fn install_package(
Ok(())
}
/// Creates all of the necessary directories, for the system if root or the pacwoman user, and
/// Remove a package from the store. This WILL lead to dangling symlinks because I don't
/// feel like fixing that!
pub fn remove_package(package: String, system: bool, repo: RepoDescriptor) -> std::io::Result<()> {
let path = index_directory()?
.join(repo.format())
.join(&package)
.join("desc");
let mut desc = std::fs::OpenOptions::new().read(true).open(path)?;
let mut desc_data = String::new();
desc.read_to_string(&mut desc_data)?;
drop(desc);
let desc_hash = sha256::digest(desc_data);
std::fs::remove_dir_all(store_directory(system)?.join(format!("{}-{}", desc_hash, package)))?;
Ok(())
}
/// Creates all of the necessary directories, for the system if running as root, and
/// for the current user if not. If [std::fs::create_dir_all] returns an error, it will be
/// propagated.
pub fn create_directories() -> Result<(), std::io::Error> {
if users::get_effective_uid() == 0 || get_current_user() == "pacwoman" {
pub fn create_directories(system: bool) -> Result<(), std::io::Error> {
if system {
store_directory(true)?;
config_directory(true)?;
index_directory()?;
gpg_dir()?;
bin_dir(true)?;
let user_dir = base_dir()?.join("user");
std::fs::create_dir_all(&user_dir)?;
std::fs::set_permissions(user_dir, std::fs::Permissions::from_mode(0o777))?;
} else {
let user_dir = base_dir()?.join("user").join(get_current_user());
std::fs::create_dir_all(&user_dir)?;
std::fs::set_permissions(user_dir, std::fs::Permissions::from_mode(0o700))?;
store_directory(false)?;
config_directory(false)?;
bin_dir(false)?;