Skip to content

Commit

Permalink
Merge pull request #86 from habitat-sh/fs_root
Browse files Browse the repository at this point in the history
 prepend fs_root to environment_for_command
  • Loading branch information
mwrock committed Nov 9, 2018
2 parents 08130b8 + 5ac14df commit b83de79
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/fs.rs
Expand Up @@ -197,6 +197,22 @@ where
pkg_path
}

/// Given a linux style absolute path (prepended with '/') and a fs_root,
/// this will "re-root" the path just under the fs_root. Otherwise returns
/// the given path unchanged. Non-Windows platforms will always return the
/// unchanged path.
pub fn fs_rooted_path<T>(path: &PathBuf, fs_root: Option<T>) -> PathBuf
where
T: AsRef<Path>,
{
match fs_root {
Some(ref root) if path.starts_with("/") && cfg!(windows) => {
Path::new(root.as_ref()).join(path.strip_prefix("/").unwrap())
}
_ => path.to_path_buf(),
}
}

/// Returns the absolute path for a given command, if it exists, by searching the `PATH`
/// environment variable.
///
Expand Down
27 changes: 22 additions & 5 deletions src/package/install.rs
Expand Up @@ -237,12 +237,22 @@ impl PackageInstall {
// present for backwards compatibility with older Habitat releases.
env.remove(PATH_KEY);

let path = env::join_paths(self.runtime_paths()?)?
let mut paths = self.runtime_paths()?;

// Let's join the paths to the FS_ROOT
// In most cases, this does nothing and should only mutate
// the paths in a windows studio where FS_ROOT_PATH will
// be the studio root path (ie c:\hab\studios\...)
for path in &mut paths {
*path = fs::fs_rooted_path(&path, Some(&*self.fs_root_path));
}

let joined = env::join_paths(paths)?
.into_string()
.map_err(|s| Error::InvalidPathString(s))?;
// Only insert a PATH entry if the resulting path string is non-empty
if !path.is_empty() {
env.insert(PATH_KEY.to_string(), path);
if !joined.is_empty() {
env.insert(PATH_KEY.to_string(), joined);
}

Ok(env)
Expand Down Expand Up @@ -1420,13 +1430,20 @@ core/bar=pub:core/publish sub:core/subscribe
);

let mut expected = HashMap::new();
let fs_root_path = fs_root.into_path();
expected.insert("FOO".to_string(), "bar".to_string());
expected.insert("JAVA_HOME".to_string(), "/my/java/home".to_string());
expected.insert(
"PATH".to_string(),
env::join_paths(vec![
pkg_prefix_for(&pkg_install).join("bin"),
pkg_prefix_for(&other_pkg_install).join("sbin"),
fs::fs_rooted_path(
&pkg_prefix_for(&pkg_install).join("bin"),
Some(&*fs_root_path),
),
fs::fs_rooted_path(
&pkg_prefix_for(&other_pkg_install).join("sbin"),
Some(&*fs_root_path),
),
]).unwrap()
.to_string_lossy()
.into_owned(),
Expand Down
7 changes: 5 additions & 2 deletions src/util/win_perm.rs
Expand Up @@ -129,7 +129,7 @@ mod tests {
use std::io::Write;
use std::path::Path;

use tempdir::TempDir;
use tempfile::Builder;
use winapi::um::winnt::FILE_ALL_ACCESS;
use windows_acl::helper;

Expand All @@ -140,7 +140,10 @@ mod tests {

#[test]
fn set_permissions_ok_test() {
let tmp_dir = TempDir::new("foo").expect("create temp dir");
let tmp_dir = Builder::new()
.prefix("foo")
.tempdir()
.expect("create temp dir");
let file_path = tmp_dir.path().join("test.txt");
let mut tmp_file = File::create(&file_path).expect("create temp file");
writeln!(tmp_file, "foobar123").expect("write temp file");
Expand Down

0 comments on commit b83de79

Please sign in to comment.