use std::cell::Cell;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use filetime::FileTime;
use walkdir::WalkDir;
use crate::error::{Error, Result};
use crate::{lock, probe};
pub const DEFAULT_MIN_SHARED_SIZE: u64 = 1024 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Strategy {
Clone,
LinkAndCopy,
}
#[derive(Debug, Clone)]
pub struct Options {
pub src: PathBuf,
pub dest: PathBuf,
pub staging: Option<PathBuf>,
pub min_shared_size: u64,
}
impl Options {
pub fn new(src: impl Into<PathBuf>, dest: impl Into<PathBuf>) -> Self {
Self {
src: src.into(),
dest: dest.into(),
staging: None,
min_shared_size: DEFAULT_MIN_SHARED_SIZE,
}
}
}
#[derive(Debug, Clone)]
pub struct Report {
pub strategy: Strategy,
pub dest: PathBuf,
pub dirs: u64,
pub shared: u64,
pub copied: u64,
pub symlinks: u64,
pub incremental_dropped: u64,
pub shared_bytes: u64,
pub copied_bytes: u64,
pub build_locks_held: usize,
}
pub fn seed(opts: &Options) -> Result<Report> {
if !opts.src.is_dir() {
return Err(Error::SourceMissing(opts.src.clone()));
}
if opts.dest.exists() {
return Err(Error::DestinationExists(opts.dest.clone()));
}
let staging = opts
.staging
.clone()
.unwrap_or_else(|| default_staging(&opts.dest));
if staging.exists() {
return Err(Error::StagingExists(staging));
}
refuse_nesting(&opts.src, &opts.dest, &staging)?;
let dest_parent = parent_of(&opts.dest)?;
fs::create_dir_all(dest_parent).map_err(Error::io("creating", dest_parent))?;
let staging_parent = parent_of(&staging)?.to_path_buf();
fs::create_dir_all(&staging_parent).map_err(Error::io("creating", &staging_parent))?;
same_filesystem(&staging_parent, dest_parent)?;
let strategy = if probe::supports_reflink(&opts.src, &staging_parent)? {
Strategy::Clone
} else {
Strategy::LinkAndCopy
};
if strategy == Strategy::LinkAndCopy {
same_filesystem(&opts.src, &staging_parent)?;
}
let locks = lock::acquire(&opts.src)?;
let mut report = fill(&opts.src, &staging, strategy, opts.min_shared_size)?;
report.build_locks_held = locks.held().len();
drop(locks);
fs::rename(&staging, &opts.dest)
.map_err(Error::io("renaming the staged tree to", &opts.dest))?;
report.dest = opts.dest.clone();
Ok(report)
}
fn fill(src: &Path, staging: &Path, strategy: Strategy, min_shared_size: u64) -> Result<Report> {
fs::create_dir_all(staging).map_err(Error::io("creating", staging))?;
let mut report = Report {
strategy,
dest: staging.to_path_buf(),
dirs: 0,
shared: 0,
copied: 0,
symlinks: 0,
incremental_dropped: 0,
shared_bytes: 0,
copied_bytes: 0,
build_locks_held: 0,
};
let drop_incremental = strategy == Strategy::LinkAndCopy;
let dropped = Cell::new(0u64);
let walker = WalkDir::new(src)
.min_depth(1)
.into_iter()
.filter_entry(|entry| {
let prune = drop_incremental && is_profile_incremental(entry.path(), entry.file_type());
if prune {
dropped.set(dropped.get() + 1);
}
!prune
});
for entry in walker {
let entry = entry.map_err(|source| Error::Walk {
path: src.to_path_buf(),
source,
})?;
let relative = entry
.path()
.strip_prefix(src)
.expect("walkdir yields paths under its root");
let destination = staging.join(relative);
let file_type = entry.file_type();
if file_type.is_dir() {
fs::create_dir_all(&destination).map_err(Error::io("creating", &destination))?;
report.dirs += 1;
continue;
}
if file_type.is_symlink() {
let points_to = fs::read_link(entry.path())
.map_err(Error::io("reading the symlink", entry.path()))?;
symlink(entry.path(), &points_to, &destination)?;
report.symlinks += 1;
continue;
}
let metadata = entry.metadata().map_err(|source| Error::Walk {
path: entry.path().to_path_buf(),
source,
})?;
let size = metadata.len();
let share = match strategy {
Strategy::Clone => true,
Strategy::LinkAndCopy => {
under_deps(relative) && !is_dep_info(relative) && size >= min_shared_size
}
};
if share {
match strategy {
Strategy::Clone => {
reflink_copy::reflink(entry.path(), &destination)
.map_err(Error::io("cloning to", &destination))?;
carry_metadata(&metadata, &destination)?;
}
Strategy::LinkAndCopy => {
fs::hard_link(entry.path(), &destination)
.map_err(Error::io("linking to", &destination))?;
}
}
report.shared += 1;
report.shared_bytes += size;
} else {
fs::copy(entry.path(), &destination).map_err(Error::io("copying to", &destination))?;
carry_metadata(&metadata, &destination)?;
report.copied += 1;
report.copied_bytes += size;
}
}
report.incremental_dropped = dropped.get();
Ok(report)
}
fn carry_metadata(source: &fs::Metadata, destination: &Path) -> Result<()> {
fs::set_permissions(destination, source.permissions())
.map_err(Error::io("setting the mode on", destination))?;
filetime::set_file_times(
destination,
FileTime::from_last_access_time(source),
FileTime::from_last_modification_time(source),
)
.map_err(Error::io("setting the timestamps on", destination))?;
Ok(())
}
fn is_profile_incremental(path: &Path, file_type: fs::FileType) -> bool {
file_type.is_dir()
&& path.file_name() == Some(OsStr::new("incremental"))
&& path
.parent()
.is_some_and(|profile| profile.join("deps").is_dir())
}
fn is_dep_info(relative: &Path) -> bool {
relative
.extension()
.is_some_and(|extension| extension == "d")
}
fn under_deps(relative: &Path) -> bool {
relative.parent().is_some_and(|parent| {
parent
.components()
.any(|component| component.as_os_str() == "deps")
})
}
fn refuse_nesting(src: &Path, dest: &Path, staging: &Path) -> Result<()> {
let source = src.canonicalize().map_err(Error::io("resolving", src))?;
for candidate in [dest, staging] {
let resolved = resolve(candidate)?;
if resolved.starts_with(&source) {
return Err(Error::DestinationInsideSource {
src: source,
dest: resolved,
});
}
}
Ok(())
}
fn resolve(path: &Path) -> Result<PathBuf> {
let mut unresolved = Vec::new();
let mut existing = path.to_path_buf();
loop {
if let Ok(mut base) = existing.canonicalize() {
base.extend(unresolved.iter().rev());
return Ok(base);
}
let name = existing
.file_name()
.ok_or_else(|| Error::DestinationHasNoParent(path.to_path_buf()))?
.to_os_string();
unresolved.push(name);
existing = parent_of(&existing)?.to_path_buf();
}
}
fn parent_of(path: &Path) -> Result<&Path> {
match path.parent() {
Some(parent) if parent.as_os_str().is_empty() => Ok(Path::new(".")),
Some(parent) => Ok(parent),
None => Err(Error::DestinationHasNoParent(path.to_path_buf())),
}
}
fn default_staging(dest: &Path) -> PathBuf {
let name = dest.file_name().unwrap_or_else(|| OsStr::new("target"));
let mut staged = name.to_os_string();
staged.push(".partial");
dest.with_file_name(staged)
}
#[cfg(unix)]
fn same_filesystem(a: &Path, b: &Path) -> Result<()> {
use std::os::unix::fs::MetadataExt;
let left = fs::metadata(a).map_err(Error::io("reading", a))?;
let right = fs::metadata(b).map_err(Error::io("reading", b))?;
if left.dev() == right.dev() {
return Ok(());
}
Err(Error::CrossDevice {
src: a.to_path_buf(),
dest: b.to_path_buf(),
})
}
#[cfg(not(unix))]
fn same_filesystem(_a: &Path, _b: &Path) -> Result<()> {
Ok(())
}
#[cfg(unix)]
fn symlink(_link: &Path, points_to: &Path, at: &Path) -> Result<()> {
std::os::unix::fs::symlink(points_to, at).map_err(Error::io("creating the symlink", at))
}
#[cfg(not(unix))]
fn symlink(link: &Path, _points_to: &Path, at: &Path) -> Result<()> {
fs::copy(link, at)
.map(|_| ())
.map_err(Error::io("copying what the symlink pointed at, to", at))
}