Skip to content

Commit

Permalink
Enable copying symlinks when using remote docker.
Browse files Browse the repository at this point in the history
This handles the reads the link dst from the symlink when using
temporary directories, and creates a softlink to the path (keeping
relative and absolute symlinks valid) in the temporary directory, which
is then copied with the same permissions over the dst filesystem. This
should not keep the same metadata, but since we fingerprint source
files, this should not matter.
  • Loading branch information
Alexhuszagh committed Jun 30, 2022
1 parent e583132 commit 98d416a
Showing 1 changed file with 64 additions and 14 deletions.
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
} 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

0 comments on commit 98d416a

Please sign in to comment.