Skip to content

Enable copying symlinks when using remote docker. #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- #885 - handle symlinks when using remote docker.
- #868 - ignore the `CARGO` environment variable.
- #867 - fixed parsing of `build.env.passthrough` config values.

Expand Down
78 changes: 64 additions & 14 deletions src/docker/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,16 @@ fn copy_volume_files_nocache(
container: &str,
src: &Path,
dst: &Path,
copy_symlinks: bool,
msg_info: MessageInfo,
) -> Result<ExitStatus> {
// avoid any cached directories when copying
// see https://bford.info/cachedir/
// SAFETY: safe, single-threaded execution.
let tempdir = unsafe { temp::TempDir::new()? };
let temppath = tempdir.path();
copy_dir(src, temppath, 0, |e, _| is_cachedir(e))?;
let had_symlinks = copy_dir(src, temppath, copy_symlinks, 0, |e, _| is_cachedir(e))?;
warn_symlinks(had_symlinks, msg_info)?;
copy_volume_files(engine, container, temppath, dst, msg_info)
}

Expand Down Expand Up @@ -234,10 +236,18 @@ pub fn copy_volume_container_cargo(
}

// recursively copy a directory into another
fn copy_dir<Skip>(src: &Path, dst: &Path, depth: u32, skip: Skip) -> Result<()>
fn copy_dir<Skip>(
src: &Path,
dst: &Path,
copy_symlinks: bool,
depth: u32,
skip: Skip,
) -> Result<bool>
where
Skip: Copy + Fn(&fs::DirEntry, u32) -> bool,
{
let mut had_symlinks = false;

for entry in fs::read_dir(src)? {
let file = entry?;
if skip(&file, depth) {
Expand All @@ -248,13 +258,47 @@ where
let dst_path = dst.join(file.file_name());
if file.file_type()?.is_file() {
fs::copy(&src_path, &dst_path)?;
} else {
} else if file.file_type()?.is_dir() {
fs::create_dir(&dst_path).ok();
copy_dir(&src_path, &dst_path, depth + 1, skip)?;
had_symlinks = copy_dir(&src_path, &dst_path, copy_symlinks, depth + 1, skip)?;
} else if copy_symlinks {
had_symlinks = true;
let link_dst = fs::read_link(src_path)?;

#[cfg(target_family = "unix")]
{
std::os::unix::fs::symlink(link_dst, dst_path)?;
}

#[cfg(target_family = "windows")]
{
let link_dst_absolute = if link_dst.is_absolute() {
link_dst.clone()
} else {
// we cannot fail even if the linked to path does not exist.
src.join(&link_dst)
};
if link_dst_absolute.is_dir() {
std::os::windows::fs::symlink_dir(link_dst, dst_path)?;
} else {
// symlink_file handles everything that isn't a directory
std::os::windows::fs::symlink_file(link_dst, dst_path)?;
}
}
} else {
had_symlinks = true;
}
}

Ok(())
Ok(had_symlinks)
}

fn warn_symlinks(had_symlinks: bool, msg_info: MessageInfo) -> Result<()> {
if had_symlinks {
shell::warn("copied directory contained symlinks. if the volume the link points to was not mounted, the remote build may fail", msg_info)
} else {
Ok(())
}
}

// copy over files needed for all targets in the toolchain that should never change
Expand Down Expand Up @@ -284,20 +328,25 @@ fn copy_volume_container_rust_base(
let tempdir = unsafe { temp::TempDir::new()? };
let temppath = tempdir.path();
fs::create_dir_all(&temppath.join(&rustlib))?;
copy_dir(&sysroot.join("lib"), &temppath.join("lib"), 0, |e, d| {
d == 0 && e.file_name() == "rustlib"
})?;
let mut had_symlinks = copy_dir(
&sysroot.join("lib"),
&temppath.join("lib"),
true,
0,
|e, d| d == 0 && e.file_name() == "rustlib",
)?;

// next, copy the src/etc directories inside rustlib
copy_dir(
had_symlinks |= copy_dir(
&sysroot.join(&rustlib),
&temppath.join(&rustlib),
true,
0,
|e, d| d == 0 && !(e.file_name() == "src" || e.file_name() == "etc"),
)?;
copy_volume_files(engine, container, &temppath.join("lib"), &dst, msg_info)?;

Ok(())
warn_symlinks(had_symlinks, msg_info)
}

fn copy_volume_container_rust_manifest(
Expand All @@ -316,15 +365,16 @@ fn copy_volume_container_rust_manifest(
let tempdir = unsafe { temp::TempDir::new()? };
let temppath = tempdir.path();
fs::create_dir_all(&temppath.join(&rustlib))?;
copy_dir(
let had_symlinks = copy_dir(
&sysroot.join(&rustlib),
&temppath.join(&rustlib),
true,
0,
|e, d| d != 0 || e.file_type().map(|t| !t.is_file()).unwrap_or(true),
)?;
copy_volume_files(engine, container, &temppath.join("lib"), &dst, msg_info)?;

Ok(())
warn_symlinks(had_symlinks, msg_info)
}

// copy over the toolchain for a specific triple
Expand Down Expand Up @@ -570,7 +620,7 @@ fn copy_volume_container_project(
if copy_cache {
copy_volume_files(engine, container, src, dst, msg_info)
} else {
copy_volume_files_nocache(engine, container, src, dst, msg_info)
copy_volume_files_nocache(engine, container, src, dst, true, msg_info)
}
};
match volume {
Expand Down Expand Up @@ -809,7 +859,7 @@ pub(crate) fn run(
if copy_cache {
copy_volume_files(engine, &container, src, dst, msg_info)
} else {
copy_volume_files_nocache(engine, &container, src, dst, msg_info)
copy_volume_files_nocache(engine, &container, src, dst, true, msg_info)
}
};
let mount_prefix_path = mount_prefix.as_ref();
Expand Down