use super::diagnostic::{Diagnostic, DiagnosticCategory};
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::path::{Component, Path, PathBuf};
pub(super) fn collect_root(
package: &Path,
configured: &Path,
files: &mut BTreeMap<String, PathBuf>,
) -> Result<(), Diagnostic> {
let candidate = if configured.is_absolute() {
configured.to_path_buf()
} else {
package.join(configured)
};
reject_symlink_path(package, &candidate)?;
let canonical = fs::canonicalize(&candidate).map_err(|error| {
Diagnostic::io(
DiagnosticCategory::SourceRoot,
display_configured(configured),
"cannot resolve source root",
error,
)
})?;
if !canonical.starts_with(package) {
return Err(Diagnostic::new(
DiagnosticCategory::SourceRoot,
display_configured(configured),
"source root escapes the package",
));
}
let metadata = fs::symlink_metadata(&canonical).map_err(|error| {
Diagnostic::io(
DiagnosticCategory::SourceRoot,
local_path(package, &canonical).unwrap_or_else(|_| display_configured(configured)),
"cannot inspect source root",
error,
)
})?;
if metadata.file_type().is_symlink() {
return Err(Diagnostic::new(
DiagnosticCategory::Symlink,
display_configured(configured),
"source root is a symlink",
));
}
if metadata.is_file() {
if canonical.extension() != Some(OsStr::new("rs")) {
return Err(Diagnostic::new(
DiagnosticCategory::SourceRoot,
local_path(package, &canonical)?,
"source file root must have the .rs extension",
));
}
files.insert(local_path(package, &canonical)?, canonical);
} else if metadata.is_dir() {
collect_directory(package, &canonical, files)?;
} else {
return Err(Diagnostic::new(
DiagnosticCategory::SourceRoot,
local_path(package, &canonical)?,
"source root is neither a file nor a directory",
));
}
Ok(())
}
fn collect_directory(
package: &Path,
directory: &Path,
files: &mut BTreeMap<String, PathBuf>,
) -> Result<(), Diagnostic> {
let local = local_path(package, directory)?;
let entries = fs::read_dir(directory).map_err(|error| {
Diagnostic::io(
DiagnosticCategory::Io,
&local,
"cannot read source directory",
error,
)
})?;
let mut entries = entries.collect::<Result<Vec<_>, _>>().map_err(|error| {
Diagnostic::io(
DiagnosticCategory::Io,
&local,
"cannot read source directory entry",
error,
)
})?;
entries.sort_by_key(fs::DirEntry::file_name);
for entry in entries {
let path = entry.path();
let entry_local = local_path(package, &path)?;
let metadata = fs::symlink_metadata(&path).map_err(|error| {
Diagnostic::io(
DiagnosticCategory::Io,
&entry_local,
"cannot inspect source entry",
error,
)
})?;
if metadata.file_type().is_symlink() {
return Err(Diagnostic::new(
DiagnosticCategory::Symlink,
entry_local,
"source entry is a symlink",
));
}
if metadata.is_dir() {
collect_directory(package, &path, files)?;
} else if metadata.is_file() && path.extension() == Some(OsStr::new("rs")) {
files.insert(entry_local, path);
}
}
Ok(())
}
fn reject_symlink_path(package: &Path, candidate: &Path) -> Result<(), Diagnostic> {
if let Ok(relative) = candidate.strip_prefix(package) {
let mut path = package.to_path_buf();
for component in relative.components() {
match component {
Component::CurDir => continue,
Component::ParentDir => {
path.pop();
}
Component::Normal(part) => path.push(part),
Component::RootDir | Component::Prefix(_) => continue,
}
match fs::symlink_metadata(&path) {
Ok(metadata) if metadata.file_type().is_symlink() => {
return Err(Diagnostic::new(
DiagnosticCategory::Symlink,
local_path(package, &path)?,
"source path contains a symlink",
));
}
Ok(_) => {}
Err(error) if error.kind() == io::ErrorKind::NotFound => break,
Err(error) => {
return Err(Diagnostic::io(
DiagnosticCategory::Io,
display_configured(candidate),
"cannot inspect source path",
error,
));
}
}
}
}
Ok(())
}
fn local_path(package: &Path, path: &Path) -> Result<String, Diagnostic> {
let relative = path.strip_prefix(package).map_err(|_| {
Diagnostic::new(
DiagnosticCategory::SourceRoot,
display_configured(path),
"path is outside the package",
)
})?;
let mut parts = Vec::new();
for component in relative.components() {
let Component::Normal(part) = component else {
return Err(Diagnostic::new(
DiagnosticCategory::SourceRoot,
display_configured(path),
"path cannot be normalized",
));
};
parts.push(part.to_str().ok_or_else(|| {
Diagnostic::new(
DiagnosticCategory::SourceRoot,
display_configured(path),
"source path is not UTF-8",
)
})?);
}
Ok(parts.join("/"))
}
fn display_configured(path: &Path) -> String {
path.to_string_lossy().replace('\\', "/")
}