Skip to content

Commit

Permalink
feat(-A): restore --builduser
Browse files Browse the repository at this point in the history
  • Loading branch information
fosskers committed Jul 24, 2024
1 parent 93d1735 commit a78cf17
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Governed within the `[aur]` section.
| `cache` | string | A path in which to store built package tarballs. |
| `clones` | string | A path in which to clone package metadata. |
| `hashes` | string | A path in which to store the git hash of the latest build. |
| `builduser` | string | An alternate user to build as. |
| `chroot` | string list | Packages to build with `pkgctl build` in a chroot. |
| `ignores` | string list | Packages to never update. |
| `git` | bool | Force update all VCS packages during `-Au`. |
Expand Down
32 changes: 17 additions & 15 deletions rust/aura-pm/src/command/aur/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,18 +407,24 @@ fn pkgctl_build(within: &Path, deps: &[PkgPath]) -> Result<Vec<PkgPath>, Error>
.then_some(())
.ok_or(Error::PkgctlBuild)?;

tarball_paths(false, within)
tarball_paths(None, within)
}

/// Build each package specified by the `PKGBUILD` and yield a list of the built
/// tarballs.
fn makepkg(env: &Env, within: &Path) -> Result<Vec<PkgPath>, Error> {
let mut cmd = if env.is_root {
let user = match env.aur.builduser.as_deref() {
Some(u) => Some(u),
// Assumption: The `nobody` user always exists.
nobody_permissions(within)?;
None if env.is_root => Some("nobody"),
None => None,
};

let mut cmd = if let Some(u) = user {
user_permissions(within, u)?;

let mut c = Command::new(env.sudo());
c.arg("-u").arg("nobody").arg("makepkg");
c.arg("-u").arg(u).arg("makepkg");
c
} else {
Command::new("makepkg")
Expand Down Expand Up @@ -453,17 +459,16 @@ fn makepkg(env: &Env, within: &Path) -> Result<Vec<PkgPath>, Error> {
.then_some(())
.ok_or(Error::Makepkg)?;

tarball_paths(env.is_root, within)
tarball_paths(user, within)
}

/// Grant write permissions to the given build directory for the `nobody` user.
/// This should only occur when Aura is being run by the true root user.
fn nobody_permissions(within: &Path) -> Result<(), Error> {
/// Grant write permissions to the given build directory for the given build user.
fn user_permissions(within: &Path, user: &str) -> Result<(), Error> {
debug!("Setting a+w permissions within: {}", within.display());

let status = Command::new("chown")
.arg("-R")
.arg("nobody")
.arg(user)
.arg(within)
.status()
.map_err(|_| Error::Permissions(within.to_path_buf()))?;
Expand All @@ -475,13 +480,10 @@ fn nobody_permissions(within: &Path) -> Result<(), Error> {
}
}

fn tarball_paths(is_root: bool, within: &Path) -> Result<Vec<PkgPath>, Error> {
let mut cmd = if is_root {
fn tarball_paths(build_user: Option<&str>, within: &Path) -> Result<Vec<PkgPath>, Error> {
let mut cmd = if let Some(user) = build_user {
let mut c = Command::new("sudo");
c.arg("-u")
.arg("nobody")
.arg("makepkg")
.arg("--packagelist");
c.arg("-u").arg(user).arg("makepkg").arg("--packagelist");
c
} else {
let mut c = Command::new("makepkg");
Expand Down
8 changes: 8 additions & 0 deletions rust/aura-pm/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ struct RawAur {
cache: Option<PathBuf>,
clones: Option<PathBuf>,
hashes: Option<PathBuf>,
builduser: Option<String>,
#[serde(default)]
chroot: HashSet<String>,
#[serde(default)]
Expand All @@ -299,6 +300,7 @@ pub(crate) struct Aur {
pub(crate) cache: PathBuf,
pub(crate) clones: PathBuf,
pub(crate) hashes: PathBuf,
pub(crate) builduser: Option<String>,
/// Packages to build via `pkgctl build`.
pub(crate) chroot: HashSet<String>,
/// Packages to ignore entirely.
Expand Down Expand Up @@ -330,6 +332,7 @@ impl Aur {
cache: dirs::tarballs()?,
clones: dirs::clones()?,
hashes: dirs::hashes()?,
builduser: None,
chroot: HashSet::new(),
ignores: HashSet::new(),
git: false,
Expand Down Expand Up @@ -380,6 +383,10 @@ impl Aur {
self.build = pb.to_path_buf();
}

if let Some(bu) = flags.builduser.as_deref() {
self.builduser = Some(bu.to_string());
}

// NOTE If `check` were found in `makepkg.conf`, then the flag should
// override it. If `!check` were found or there were nothing, then the
// flag agrees with it and `false` is taken.
Expand Down Expand Up @@ -411,6 +418,7 @@ impl TryFrom<RawAur> for Aur {
cache,
clones,
hashes,
builduser: raw.builduser,
chroot: raw.chroot,
ignores: raw.ignores,
git: raw.git,
Expand Down
4 changes: 4 additions & 0 deletions rust/aura-pm/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,10 @@ pub struct Aur {
#[clap(long, display_order = 4, value_name = "path")]
pub build: Option<PathBuf>,

/// The user to build as.
#[clap(long, display_order = 4, value_name = "user")]
pub builduser: Option<String>,

/// View diffs of PKGBUILDs and related build files before building.
#[clap(long, short = 'k', display_order = 4)]
pub diff: bool,
Expand Down

0 comments on commit a78cf17

Please sign in to comment.