Missing cfg (#600)

* Check whether NPM root is owned by the current user (fix #599)

* Missing cfg

* Another missing config
This commit is contained in:
Roey Darwish Dror
2021-01-14 09:49:18 +02:00
committed by GitHub
parent 258acce481
commit 4fdafa0ba1

View File

@@ -5,9 +5,12 @@ use crate::terminal::print_separator;
use crate::utils::{require, PathExt};
use crate::{error::SkipStep, execution_context::ExecutionContext};
use anyhow::Result;
use log::debug;
use directories::BaseDirs;
use log::debug;
#[cfg(unix)]
use nix::unistd::Uid;
#[cfg(unix)]
use std::os::unix::prelude::MetadataExt;
use std::path::PathBuf;
use std::process::Command;
@@ -20,7 +23,7 @@ impl NPM {
Self { command }
}
#[cfg(not(target_os = "macos"))]
#[cfg(target_os = "linux")]
fn root(&self) -> Result<PathBuf> {
Command::new(&self.command)
.args(&["root", "-g"])
@@ -38,13 +41,17 @@ impl NPM {
pub fn run_npm_upgrade(_base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
let npm = require("npm").map(NPM::new)?;
#[cfg(not(target_os = "macos"))]
#[cfg(target_os = "linux")]
{
let npm_root = npm.root()?;
if !npm_root.is_descendant_of(_base_dirs.home_dir()) {
let metadata = std::fs::metadata(&npm_root)?;
let uid = Uid::effective();
if metadata.uid() != uid.as_raw() {
return Err(SkipStep(format!(
"NPM root at {} isn't a decandent of the user's home directory",
npm_root.display()
"NPM root at {} is owned by {} which is not the current user",
npm_root.display(),
uid
))
.into());
}