pub mod cli;
pub mod config;
pub mod detector;
pub mod differ;
pub mod error;
#[cfg(feature = "remote")]
pub mod fetcher;
pub mod filter;
pub mod ignorefile;
pub mod reporter;
pub mod transcoder;
pub mod walker;
pub mod writer;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Instant;
use anyhow::{Context, bail};
use rayon::prelude::*;
use crate::cli::{Cli, Commands, ConvertArgs, DiffArgs, FixArgs, TreeArgs};
use crate::detector::{DetectedEncoding, detect};
use crate::differ::DiffInput;
use crate::error::ForgeError;
use crate::filter::{FilterConfig, FilterOptions};
use crate::reporter::{
ConsoleCfg, ConvertSummary, FileRecord, FixSummary, OptionsBlock, Report, ScanSummary, Summary,
VerifySummary, print_header, print_problem, print_verbose, write_report,
};
use crate::transcoder::{TargetEncoding, TranscodeOutcome, transcode_detected};
use crate::walker::collect_files;
pub fn run(cli: Cli) -> i32 {
let outcome = match cli.command {
Commands::Scan(args) => cmd_scan(&args),
Commands::Convert(args) => cmd_convert(&args),
Commands::Verify(args) => cmd_verify(&args),
Commands::Fix(args) => cmd_fix(&args),
Commands::Diff(args) => cmd_diff(&args),
};
outcome.unwrap_or_else(|err| {
eprintln!("[utf8-forge] fatal: {err:#}");
2
})
}
struct RecBuilder<'a> {
path: &'a Path,
started: Instant,
}
impl<'a> RecBuilder<'a> {
fn new(path: &'a Path) -> RecBuilder<'a> {
RecBuilder {
path,
started: Instant::now(),
}
}
fn done(
self,
detected: Option<&'static str>,
action: &'static str,
bytes_before: Option<u64>,
bytes_after: Option<u64>,
error: Option<String>,
) -> FileRecord {
FileRecord {
path: self.path.to_string_lossy().into_owned(),
detected,
action,
bytes_before,
bytes_after,
duration_us: self.started.elapsed().as_micros() as u64,
error,
}
}
}
pub fn scan_file(path: &Path, max_file_size: u64) -> FileRecord {
let b = RecBuilder::new(path);
let size = fs::metadata(path).map(|m| m.len()).ok();
if let Some(sz) = size
&& sz > max_file_size
{
return b.done(None, "skipped_too_large", Some(sz), None, None);
}
let bytes = match fs::read(path) {
Ok(bytes) => bytes,
Err(e) => return b.done(None, "error", size, None, Some(format!("io_error: {e}"))),
};
let sz = bytes.len() as u64;
if sz > max_file_size {
return b.done(None, "skipped_too_large", Some(sz), None, None);
}
let det = detect(&bytes);
b.done(Some(det.tag()), "detected", Some(sz), None, None)
}
#[derive(Debug, Clone)]
pub struct ConvertCtx {
pub root: std::path::PathBuf,
pub target: TargetEncoding,
pub dry_run: bool,
pub backup_dir: Option<std::path::PathBuf>,
pub max_file_size: u64,
}
pub fn convert_file(path: &Path, ctx: &ConvertCtx) -> FileRecord {
let b = RecBuilder::new(path);
let size = fs::metadata(path).map(|m| m.len()).ok();
if let Some(sz) = size
&& sz > ctx.max_file_size
{
return b.done(None, "skipped_too_large", Some(sz), None, None);
}
let bytes = match fs::read(path) {
Ok(bytes) => bytes,
Err(e) => return b.done(None, "error", size, None, Some(format!("io_error: {e}"))),
};
let sz = bytes.len() as u64;
if sz > ctx.max_file_size {
return b.done(None, "skipped_too_large", Some(sz), None, None);
}
let det = detect(&bytes);
let tag = det.tag();
match transcode_detected(&bytes, det, ctx.target) {
Err(ForgeError::UnknownEncoding) => {
b.done(Some(tag), "skipped_unknown", Some(sz), None, None)
}
Err(e) => b.done(
Some(tag),
"error",
Some(sz),
None,
Some(format!("{}: {e}", e.category())),
),
Ok(TranscodeOutcome::Unchanged) => b.done(Some(tag), "unchanged", Some(sz), Some(sz), None),
Ok(TranscodeOutcome::Changed(new_bytes)) => {
if ctx.dry_run {
return b.done(
Some(tag),
"would_convert",
Some(sz),
Some(new_bytes.len() as u64),
None,
);
}
let write_protected = fs::metadata(path)
.map(|m| m.permissions().readonly())
.unwrap_or(false);
if write_protected {
return b.done(
Some(tag),
"error",
Some(sz),
None,
Some(
"io_error: file is write-protected (read-only); chmod +w to convert".into(),
),
);
}
if let Some(backup_dir) = &ctx.backup_dir
&& let Err(e) = writer::backup_original(&ctx.root, path, backup_dir, &bytes)
{
return b.done(
Some(tag),
"error",
Some(sz),
None,
Some(format!("{}: backup failed: {e}", e.category())),
);
}
match writer::atomic_write(path, &new_bytes) {
Ok(()) => b.done(
Some(tag),
"converted",
Some(sz),
Some(new_bytes.len() as u64),
None,
),
Err(e) => b.done(
Some(tag),
"error",
Some(sz),
None,
Some(format!("{}: write failed: {e}", e.category())),
),
}
}
}
}
pub fn verify_file(path: &Path, max_file_size: u64) -> FileRecord {
let b = RecBuilder::new(path);
let size = fs::metadata(path).map(|m| m.len()).ok();
if let Some(sz) = size
&& sz > max_file_size
{
return b.done(
None,
"failed",
Some(sz),
None,
Some("too_large: exceeds --max-file-size".to_string()),
);
}
let bytes = match fs::read(path) {
Ok(bytes) => bytes,
Err(e) => return b.done(None, "failed", size, None, Some(format!("io_error: {e}"))),
};
let sz = bytes.len() as u64;
let det = detect(&bytes);
let tag = det.tag();
if det == DetectedEncoding::Utf8 {
b.done(Some(tag), "passed", Some(sz), None, None)
} else {
b.done(Some(tag), "failed", Some(sz), None, None)
}
}
struct RunFacts {
root: Option<std::path::PathBuf>,
url: Option<String>,
options: OptionsBlock,
started_at: String,
verify_failed: Option<Vec<String>>,
}
fn finalize_report(
mode: &'static str,
facts: RunFacts,
mut records: Vec<FileRecord>,
elapsed: std::time::Duration,
) -> Report {
records.sort_by(|a, b| a.path.cmp(&b.path));
let total = records.len() as u64;
let summary = match mode {
"scan" => {
let mut s = ScanSummary {
total_files: total,
..ScanSummary::default()
};
for r in &records {
if r.action == "skipped_too_large" {
s.too_large += 1;
}
if r.action == "error" {
s.errors += 1;
}
match r.detected {
Some("utf-8") => s.utf8 += 1,
Some("utf-8-bom") => s.utf8_bom += 1,
Some("utf-16le") => s.utf16le += 1,
Some("utf-16be") => s.utf16be += 1,
Some("utf-32le") => s.utf32le += 1,
Some("utf-32be") => s.utf32be += 1,
Some("gb18030") => s.gb18030 += 1,
Some("unknown") => s.unknown += 1,
_ => {}
}
}
s.candidates = total - s.too_large;
Summary::Scan(s)
}
"verify" => {
let passed = records.iter().filter(|r| r.action == "passed").count() as u64;
let failed = records.iter().filter(|r| r.action == "failed").count() as u64;
Summary::Verify(VerifySummary {
total_files: total,
candidates: total,
passed,
failed,
})
}
"fix" => {
let mut s = FixSummary {
total_files: total,
..FixSummary::default()
};
for r in &records {
if r.action == "skipped_too_large" {
s.too_large += 1;
}
match r.action {
"unchanged" => s.already_utf8 += 1,
"converted" | "would_convert" => s.converted += 1,
"skipped_unknown" => s.unknown += 1,
"error" => {
let msg = r.error.as_deref().unwrap_or("");
if msg.starts_with("decode_error") {
s.decode_errors += 1;
} else {
s.io_errors += 1;
}
}
_ => {}
}
}
s.candidates = total - s.too_large;
Summary::Fix(s)
}
_ => {
let mut s = ConvertSummary {
total_files: total,
..ConvertSummary::default()
};
for r in &records {
match r.action {
"unchanged" => s.already_utf8 += 1,
"converted" | "would_convert" => s.converted += 1,
"skipped_unknown" => s.unknown += 1,
"skipped_too_large" => s.too_large += 1,
"error" => {
let msg = r.error.as_deref().unwrap_or("");
if msg.starts_with("decode_error") {
s.decode_errors += 1;
} else if msg.starts_with("fetch_error") {
s.fetch_error += 1;
} else {
s.io_errors += 1;
}
}
_ => {}
}
}
s.candidates = total - s.too_large;
Summary::Convert(s)
}
};
let unknown_files: Vec<String> = records
.iter()
.filter(|r| r.action == "skipped_unknown" || r.detected == Some("unknown"))
.map(|r| r.path.clone())
.collect();
let error_files: Vec<String> = records
.iter()
.filter(|r| r.action == "error" || r.action == "failed")
.map(|r| r.path.clone())
.collect();
Report {
tool: "utf8-forge",
version: env!("CARGO_PKG_VERSION"),
mode,
root: facts.root.map(|p| p.to_string_lossy().into_owned()),
url: facts.url,
started_at: facts.started_at,
duration_ms: elapsed.as_millis() as u64,
options: facts.options,
summary,
files: records,
unknown_files,
error_files,
verify_failed_files: facts.verify_failed.unwrap_or_default(),
}
}
fn options_block(
args: &TreeArgs,
conv: Option<&dyn ConvertOptions>,
filter: &FilterConfig,
config_file: Option<&str>,
ignore_file: Option<&str>,
) -> OptionsBlock {
let mut extensions: Vec<String> = filter.extensions.iter().cloned().collect();
extensions.sort();
let mut exclude_dirs: Vec<String> = filter.exclude_dirs.iter().cloned().collect();
exclude_dirs.sort();
let mut exclude_ext: Vec<String> = args.exclude_ext.clone();
exclude_ext.sort();
OptionsBlock {
target: conv.map(|c| c.effective_target()).unwrap_or("utf-8"),
dry_run: conv.is_some_and(|c| c.is_dry_run()),
extensions,
exclude_dirs,
exclude_ext,
exclude_globs: args.exclude_glob.clone(),
backup_dir: conv.and_then(|c| c.backup_dir().map(|p| p.to_string_lossy().into_owned())),
config_file: config_file.map(str::to_string),
ignore_file: ignore_file.map(str::to_string),
}
}
fn print_records(records: &[FileRecord], console: ConsoleCfg, target_tag: Option<&'static str>) {
for r in records {
match r.action {
"error" => print_problem(format!(
"error {} ({})",
r.path,
r.error.as_deref().unwrap_or("unspecified")
)),
"skipped_unknown" => print_problem(format!("unknown {}", r.path)),
"failed" => {
let reason = r
.error
.clone()
.unwrap_or_else(|| format!("detected={}", r.detected.unwrap_or("?")));
print_problem(format!("failed {} ({reason})", r.path));
}
_ => {}
}
match r.action {
"detected" => {
print_verbose(console, format!("{} {}", r.detected.unwrap_or("?"), r.path))
}
"unchanged" => print_verbose(
console,
format!("unchanged {} ({})", r.path, r.detected.unwrap_or("?")),
),
"converted" | "would_convert" => print_verbose(
console,
format!(
"{} {} ({} → {}, {} → {} bytes)",
r.action,
r.path,
r.detected.unwrap_or("?"),
target_tag.unwrap_or("?"),
r.bytes_before.unwrap_or(0),
r.bytes_after.unwrap_or(0)
),
),
"skipped_too_large" => print_verbose(
console,
format!(
"skipped_too_large {} ({} bytes)",
r.path,
r.bytes_before.unwrap_or(0)
),
),
"passed" => print_verbose(console, format!("passed {}", r.path)),
_ => {}
}
}
}
fn print_duration(report: Option<&Path>, elapsed: std::time::Duration, console: ConsoleCfg) {
if console.quiet {
return;
}
match report {
Some(p) => println!(
"[utf8-forge] duration={:.1}s report={}",
elapsed.as_secs_f64(),
p.display()
),
None => println!("[utf8-forge] duration={:.1}s", elapsed.as_secs_f64()),
}
}
fn print_convert_summary(
s: &ConvertSummary,
report: Option<&Path>,
elapsed: std::time::Duration,
console: ConsoleCfg,
) {
if !console.quiet {
println!(
"[utf8-forge] scanned={} candidates={}",
s.total_files, s.candidates
);
let errors = s.decode_errors + s.io_errors;
println!(
"[utf8-forge] already_utf8={} converted={} unknown={} errors={}",
s.already_utf8, s.converted, s.unknown, errors
);
if s.too_large > 0 {
println!("[utf8-forge] too_large={}", s.too_large);
}
if s.fetch_error > 0 {
println!("[utf8-forge] fetch_error={}", s.fetch_error);
}
}
print_duration(report, elapsed, console);
}
fn print_scan_summary(
s: &ScanSummary,
report: Option<&Path>,
elapsed: std::time::Duration,
console: ConsoleCfg,
) {
if !console.quiet {
println!(
"[utf8-forge] scanned={} candidates={}",
s.total_files, s.candidates
);
println!(
"[utf8-forge] utf8={} utf8_bom={} utf16le={} utf16be={} utf32le={} utf32be={} gb18030={} unknown={}",
s.utf8, s.utf8_bom, s.utf16le, s.utf16be, s.utf32le, s.utf32be, s.gb18030, s.unknown
);
if s.errors > 0 {
println!("[utf8-forge] errors={}", s.errors);
}
if s.too_large > 0 {
println!("[utf8-forge] too_large={}", s.too_large);
}
}
print_duration(report, elapsed, console);
}
fn build_filter(args: &TreeArgs) -> anyhow::Result<FilterConfig> {
let opts = FilterOptions {
ext_override: args.ext.clone(),
exclude_ext: args.exclude_ext.clone(),
exclude_dirs: args.exclude.clone(),
exclude_globs: args.exclude_glob.clone(),
include_rc: args.include_rc,
include_msbuild: args.include_msbuild,
include_hidden: args.include_hidden,
};
FilterConfig::build(&opts).map_err(anyhow::Error::msg)
}
fn merge_tree_config(common: &mut TreeArgs) -> anyhow::Result<Option<String>> {
let Some(path) = common.config.clone() else {
return Ok(None);
};
let cfg = crate::config::load(&path)?;
if common.ext.is_empty() {
common.ext = cfg.filter.extensions.clone();
}
if common.exclude.is_empty() {
common.exclude = cfg.filter.exclude_dirs.clone();
}
if common.exclude_ext.is_empty() {
common.exclude_ext = cfg.filter.exclude_ext.clone();
}
if common.exclude_glob.is_empty() {
common.exclude_glob = cfg.filter.exclude_globs.clone();
}
common.include_rc |= cfg.filter.include_rc;
common.include_msbuild |= cfg.filter.include_msbuild;
common.include_hidden |= cfg.filter.include_hidden;
if common.max_file_size.is_none() {
common.max_file_size = cfg.options.max_file_size;
}
if common.threads.is_none() {
common.threads = cfg.options.threads;
}
Ok(Some(path.to_string_lossy().into_owned()))
}
trait ConvertOptions {
fn common(&self) -> &TreeArgs;
fn common_mut(&mut self) -> &mut TreeArgs;
fn set_target(&mut self, t: Option<crate::transcoder::TargetEncoding>);
fn set_backup_dir(&mut self, p: Option<std::path::PathBuf>);
fn target(&self) -> Option<crate::transcoder::TargetEncoding>;
fn backup_dir(&self) -> Option<&std::path::PathBuf>;
fn is_dry_run(&self) -> bool;
fn effective_target(&self) -> &'static str {
self.target()
.unwrap_or(crate::transcoder::TargetEncoding::Utf8)
.tag()
}
}
impl ConvertOptions for ConvertArgs {
fn common(&self) -> &TreeArgs {
&self.common
}
fn target(&self) -> Option<crate::transcoder::TargetEncoding> {
self.target
}
fn common_mut(&mut self) -> &mut TreeArgs {
&mut self.common
}
fn set_target(&mut self, t: Option<crate::transcoder::TargetEncoding>) {
self.target = t;
}
fn set_backup_dir(&mut self, p: Option<std::path::PathBuf>) {
self.backup_dir = p;
}
fn backup_dir(&self) -> Option<&std::path::PathBuf> {
self.backup_dir.as_ref()
}
fn is_dry_run(&self) -> bool {
self.dry_run
}
}
impl ConvertOptions for FixArgs {
fn common(&self) -> &TreeArgs {
&self.common
}
fn target(&self) -> Option<crate::transcoder::TargetEncoding> {
self.target
}
fn common_mut(&mut self) -> &mut TreeArgs {
&mut self.common
}
fn set_target(&mut self, t: Option<crate::transcoder::TargetEncoding>) {
self.target = t;
}
fn set_backup_dir(&mut self, p: Option<std::path::PathBuf>) {
self.backup_dir = p;
}
fn backup_dir(&self) -> Option<&std::path::PathBuf> {
self.backup_dir.as_ref()
}
fn is_dry_run(&self) -> bool {
self.dry_run
}
}
impl FixArgs {
pub fn target_or_default(&self) -> crate::transcoder::TargetEncoding {
self.target
.unwrap_or(crate::transcoder::TargetEncoding::Utf8)
}
}
fn merge_convert_config<C: ConvertOptions + ConvertPeek>(
conv: &mut C,
) -> anyhow::Result<Option<String>> {
let recorded = merge_tree_config(conv.common_mut())?;
let Some(path) = conv.common_mut().config.clone() else {
return Ok(recorded);
};
let cfg = crate::config::load(&path)?;
if conv.target_is_none()
&& let Some(t) = cfg.target()?
{
conv.set_target(Some(t));
}
if conv.backup_is_none()
&& let Some(dir) = &cfg.convert.backup_dir
{
conv.set_backup_dir(Some(std::path::PathBuf::from(dir)));
}
Ok(recorded)
}
trait ConvertPeek {
fn target_is_none(&self) -> bool;
fn backup_is_none(&self) -> bool;
}
impl ConvertPeek for ConvertArgs {
fn target_is_none(&self) -> bool {
self.target.is_none()
}
fn backup_is_none(&self) -> bool {
self.backup_dir.is_none()
}
}
impl ConvertPeek for FixArgs {
fn target_is_none(&self) -> bool {
self.target.is_none()
}
fn backup_is_none(&self) -> bool {
self.backup_dir.is_none()
}
}
fn ensure_root(root: &Path) -> anyhow::Result<()> {
if !root.is_dir() {
bail!("root `{}` is not an existing directory", root.display());
}
Ok(())
}
fn ensure_backup_dir(args: &impl ConvertOptions) -> anyhow::Result<()> {
let Some(dir) = args.backup_dir() else {
return Ok(());
};
let root_canonical = fs::canonicalize(&args.common().root)?;
check_backup_placement(dir, &root_canonical)?;
if args.is_dry_run() {
return Ok(());
}
fs::create_dir_all(dir)
.with_context(|| format!("cannot create backup dir {}", dir.display()))?;
ensure_writable_dir(dir)?;
Ok(())
}
fn check_backup_placement(dir: &Path, root_canonical: &Path) -> anyhow::Result<()> {
let dir = if dir.is_absolute() {
dir.to_path_buf()
} else {
std::env::current_dir()
.with_context(|| format!("cannot resolve relative --backup-dir `{}`", dir.display()))?
.join(dir)
};
let mut tail: Vec<std::ffi::OsString> = Vec::new();
let mut ancestor = dir.to_path_buf();
let base = loop {
match fs::canonicalize(&ancestor) {
Ok(c) => break c,
Err(_) => {
let Some(name) = ancestor.file_name() else {
bail!("--backup-dir `{}` has no existing ancestor", dir.display());
};
let Some(parent) = ancestor.parent() else {
bail!("--backup-dir `{}` has no existing ancestor", dir.display());
};
tail.push(name.to_os_string());
ancestor = parent.to_path_buf();
}
}
};
let projected = tail.iter().rev().fold(base, |p, c| p.join(c));
if projected.starts_with(root_canonical) {
bail!(
"--backup-dir `{}` is inside `--root` `{}`; backups would be re-processed on the next run. Keep the backup directory outside the scanned tree.",
dir.display(),
root_canonical.display()
);
}
Ok(())
}
fn ensure_report_dir(report: &Path) -> anyhow::Result<()> {
if report.is_dir() {
bail!("--report `{}` is an existing directory", report.display());
}
let parent = match report.parent() {
Some(p) if !p.as_os_str().is_empty() => p.to_path_buf(),
_ => std::path::PathBuf::from("."),
};
fs::create_dir_all(&parent)
.with_context(|| format!("cannot create report directory {}", parent.display()))?;
ensure_writable_dir(&parent)?;
Ok(())
}
fn ensure_writable_dir(dir: &Path) -> anyhow::Result<()> {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let probe = dir.join(format!(
".utf8-forge-probe-{}-{}",
std::process::id(),
nanos
));
match fs::File::create(&probe) {
Ok(_) => {
let _ = fs::remove_file(&probe);
Ok(())
}
Err(e) => bail!("directory `{}` is not writable: {e}", dir.display()),
}
}
fn skip_paths(report: Option<&Path>, ignore: Option<&Path>) -> Vec<PathBuf> {
let mut v = Vec::new();
if let Some(p) = report {
v.push(p.to_path_buf());
}
if let Some(p) = ignore {
v.push(p.to_path_buf());
}
v
}
fn load_ignore(
args: &TreeArgs,
) -> anyhow::Result<(Option<ignorefile::IgnoreFile>, Option<String>, Vec<String>)> {
if args.no_ignore_file {
return Ok((None, None, Vec::new()));
}
match &args.ignore_file {
Some(path) => {
if !path.is_file() {
bail!("ignore file `{}` not found", path.display());
}
let rules = ignorefile::IgnoreFile::load(path).map_err(anyhow::Error::msg)?;
let recorded = path.to_string_lossy().into_owned();
Ok((Some(rules), Some(recorded), Vec::new()))
}
None => {
let path = args.root.join(".utf8forgeignore");
if !path.is_file() {
return Ok((None, None, Vec::new()));
}
match ignorefile::IgnoreFile::load(&path) {
Ok(rules) => {
let recorded = path.to_string_lossy().into_owned();
Ok((Some(rules), Some(recorded), Vec::new()))
}
Err(e) => Ok((
None,
None,
vec![format!(
"warning: ignoring unreadable .utf8forgeignore: {e}"
)],
)),
}
}
}
}
fn wall_now() -> String {
crate::reporter::rfc3339_now()
}
fn with_pool<R: Send>(threads: Option<usize>, f: impl FnOnce() -> R + Send) -> anyhow::Result<R> {
match threads {
Some(n) => {
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(n.max(1))
.build()
.context("failed to build rayon thread pool")?;
Ok(pool.install(f))
}
None => Ok(f()),
}
}
fn console_cfg(args: &TreeArgs) -> ConsoleCfg {
ConsoleCfg {
quiet: args.quiet,
verbose: args.verbose,
}
}
fn print_warnings(warnings: &[String], console: ConsoleCfg) {
if !console.quiet {
for w in warnings {
print_problem(w.clone());
}
}
}
fn cmd_scan(args: &TreeArgs) -> anyhow::Result<i32> {
let mut args = args.clone();
let config_file = merge_tree_config(&mut args)?;
ensure_root(&args.root)?;
let filter = build_filter(&args)?;
if let Some(path) = &args.report {
ensure_report_dir(path)?;
}
let console = console_cfg(&args);
print_header(console, format!("mode=scan root={}", args.root.display()));
let (ignore, ignore_recorded, ignore_warnings) = load_ignore(&args)?;
let mut warnings = Vec::new();
let walk = {
let skip = skip_paths(
args.report.as_deref(),
ignore_recorded
.as_deref()
.map(std::path::PathBuf::from)
.as_deref(),
);
let walk = collect_files(&args.root, &filter, &skip, ignore.as_ref());
warnings.extend(ignore_warnings);
walk
};
warnings.extend(walk.warnings);
print_warnings(&warnings, console);
let started_at = wall_now();
let started = Instant::now();
let records = with_pool(args.threads, || {
walk.files
.par_iter()
.map(|p| scan_file(p, args.effective_max_file_size()))
.collect::<Vec<_>>()
})?;
print_records(&records, console, None);
let elapsed = started.elapsed();
let report = finalize_report(
"scan",
RunFacts {
root: Some(args.root.clone()),
url: None,
options: options_block(
&args,
None,
&filter,
config_file.as_deref(),
ignore_recorded.as_deref(),
),
started_at,
verify_failed: None,
},
records,
elapsed,
);
let unknown = match &report.summary {
Summary::Scan(s) => s.unknown,
_ => 0,
};
let file_errors = report.files.iter().filter(|r| r.action == "error").count() as u64;
if let Some(path) = &args.report {
write_report(&report, path)?;
}
if let Summary::Scan(s) = &report.summary {
print_scan_summary(s, args.report.as_deref(), elapsed, console);
}
Ok(if unknown > 0 || file_errors > 0 { 1 } else { 0 })
}
fn cmd_convert(args: &ConvertArgs) -> anyhow::Result<i32> {
#[cfg(feature = "remote")]
{
if let Some(url) = &args.url {
return cmd_convert_url(args, url);
}
}
#[cfg(not(feature = "remote"))]
{
if args.url.is_some() {
bail!("--url requires the `remote` feature; rebuild with default features");
}
}
cmd_convert_tree(args)
}
fn cmd_convert_tree(args: &ConvertArgs) -> anyhow::Result<i32> {
let mut args = args.clone();
let config_file = merge_convert_config(&mut args)?;
let common = &args.common;
let target = args.target_or_default();
ensure_root(&common.root)?;
let filter = build_filter(common)?;
ensure_backup_dir(&args)?;
if let Some(path) = &common.report {
ensure_report_dir(path)?;
}
let console = console_cfg(common);
let dry_tag = if args.dry_run { " dry_run=true" } else { "" };
print_header(
console,
format!(
"mode=convert target={} root={}{}",
target,
common.root.display(),
dry_tag
),
);
let (ignore, ignore_recorded, ignore_warnings) = load_ignore(common)?;
let mut warnings = ignore_warnings;
let walk = {
let skip = skip_paths(
common.report.as_deref(),
ignore_recorded
.as_deref()
.map(std::path::PathBuf::from)
.as_deref(),
);
collect_files(&common.root, &filter, &skip, ignore.as_ref())
};
warnings.extend(walk.warnings);
print_warnings(&warnings, console);
let ctx = ConvertCtx {
root: common.root.clone(),
target,
dry_run: args.dry_run,
backup_dir: args.backup_dir.clone(),
max_file_size: common.effective_max_file_size(),
};
let started_at = wall_now();
let started = Instant::now();
let records = with_pool(common.threads, || {
walk.files
.par_iter()
.map(|p| convert_file(p, &ctx))
.collect::<Vec<_>>()
})?;
print_records(&records, console, Some(target.tag()));
let elapsed = started.elapsed();
let report = finalize_report(
"convert",
RunFacts {
root: Some(common.root.clone()),
url: None,
options: options_block(
common,
Some(&args as &dyn ConvertOptions),
&filter,
config_file.as_deref(),
ignore_recorded.as_deref(),
),
started_at,
verify_failed: None,
},
records,
elapsed,
);
let summary = match &report.summary {
Summary::Convert(s) => s.clone(),
_ => ConvertSummary::default(),
};
if let Some(path) = &common.report {
write_report(&report, path)?;
}
print_convert_summary(&summary, common.report.as_deref(), elapsed, console);
Ok(
if summary.unknown > 0 || summary.decode_errors > 0 || summary.io_errors > 0 {
1
} else {
0
},
)
}
fn cmd_verify(args: &TreeArgs) -> anyhow::Result<i32> {
let mut args = args.clone();
let config_file = merge_tree_config(&mut args)?;
ensure_root(&args.root)?;
let filter = build_filter(&args)?;
if let Some(path) = &args.report {
ensure_report_dir(path)?;
}
let console = console_cfg(&args);
print_header(console, format!("mode=verify root={}", args.root.display()));
let (ignore, ignore_recorded, ignore_warnings) = load_ignore(&args)?;
let mut warnings = Vec::new();
let walk = {
let skip = skip_paths(
args.report.as_deref(),
ignore_recorded
.as_deref()
.map(std::path::PathBuf::from)
.as_deref(),
);
let walk = collect_files(&args.root, &filter, &skip, ignore.as_ref());
warnings.extend(ignore_warnings);
walk
};
print_warnings(&walk.warnings, console);
let started_at = wall_now();
let started = Instant::now();
let records = with_pool(args.threads, || {
walk.files
.par_iter()
.map(|p| verify_file(p, args.effective_max_file_size()))
.collect::<Vec<_>>()
})?;
print_records(&records, console, None);
let elapsed = started.elapsed();
let report = finalize_report(
"verify",
RunFacts {
root: Some(args.root.clone()),
url: None,
options: options_block(
&args,
None,
&filter,
config_file.as_deref(),
ignore_recorded.as_deref(),
),
started_at,
verify_failed: None,
},
records,
elapsed,
);
let summary = match &report.summary {
Summary::Verify(s) => s.clone(),
_ => VerifySummary::default(),
};
if let Some(path) = &args.report {
write_report(&report, path)?;
}
if !console.quiet {
println!(
"[utf8-forge] checked={} passed={} failed={}",
summary.total_files, summary.passed, summary.failed
);
}
print_duration(args.report.as_deref(), elapsed, console);
Ok(if summary.failed > 0 { 3 } else { 0 })
}
#[cfg(feature = "remote")]
fn cmd_convert_url(args: &ConvertArgs, url: &str) -> anyhow::Result<i32> {
let common = &args.common;
if args.backup_dir.is_some() {
bail!("--backup-dir is not supported with --url");
}
let console = console_cfg(common);
print_header(
console,
format!("mode=fetch target={} url={url}", args.target_or_default()),
);
let started_at = wall_now();
let started = Instant::now();
let out_path = args
.output
.clone()
.unwrap_or_else(|| fetcher::derive_output_path(url));
let record = match fetcher::fetch_bytes(url, common.effective_max_file_size()) {
Err(e) => {
let action = match e {
ForgeError::TooLarge { .. } => "skipped_too_large",
_ => "error",
};
if action == "skipped_too_large" {
print_problem(format!("skipped_too_large {url} ({}: {e})", e.category()));
}
RecBuilder::new(&out_path).done(
None,
action,
None,
None,
Some(format!("{}: {e}", e.category())),
)
}
Ok(bytes) => {
let raw_len = bytes.len() as u64;
let det = detect(&bytes);
let tag = det.tag();
match transcode_detected(&bytes, det, args.target_or_default()) {
Err(e) => {
print_problem(format!("error {url} ({}: {e})", e.category()));
RecBuilder::new(&out_path).done(
Some(tag),
"error",
Some(raw_len),
None,
Some(format!("{}: {e}", e.category())),
)
}
Ok(outcome) => {
let new_bytes = match outcome {
TranscodeOutcome::Unchanged => bytes,
TranscodeOutcome::Changed(v) => v,
};
if fs::read(&out_path).is_ok_and(|existing| existing == new_bytes) {
RecBuilder::new(&out_path).done(
Some(tag),
"unchanged",
Some(raw_len),
Some(new_bytes.len() as u64),
None,
)
} else if args.dry_run {
RecBuilder::new(&out_path).done(
Some(tag),
"would_convert",
Some(raw_len),
Some(new_bytes.len() as u64),
None,
)
} else {
if let Some(parent) = out_path.parent()
&& !parent.as_os_str().is_empty()
{
fs::create_dir_all(parent).with_context(|| {
format!("cannot create output dir {}", parent.display())
})?;
}
match writer::atomic_write(&out_path, &new_bytes) {
Ok(()) => RecBuilder::new(&out_path).done(
Some(tag),
"converted",
Some(raw_len),
Some(new_bytes.len() as u64),
None,
),
Err(e) => RecBuilder::new(&out_path).done(
Some(tag),
"error",
Some(raw_len),
None,
Some(format!("{}: write failed: {e}", e.category())),
),
}
}
}
}
}
};
let elapsed = started.elapsed();
let options = OptionsBlock {
target: args.target_or_default().tag(),
dry_run: args.dry_run,
extensions: Vec::new(),
exclude_dirs: Vec::new(),
exclude_ext: Vec::new(),
exclude_globs: Vec::new(),
backup_dir: None,
config_file: None,
ignore_file: None,
};
let report = finalize_report(
"fetch",
RunFacts {
root: None,
url: Some(url.to_string()),
options,
started_at,
verify_failed: None,
},
vec![record],
elapsed,
);
let summary = match &report.summary {
Summary::Convert(s) => s.clone(),
_ => ConvertSummary::default(),
};
print_records(
std::slice::from_ref(&report.files[0]),
console,
Some(args.target_or_default().tag()),
);
if let Some(path) = &common.report {
write_report(&report, path)?;
}
print_convert_summary(&summary, common.report.as_deref(), elapsed, console);
Ok(
if summary.unknown > 0
|| summary.decode_errors > 0
|| summary.io_errors > 0
|| summary.fetch_error > 0
|| summary.too_large > 0
{
1
} else {
0
},
)
}
fn cmd_fix(args: &FixArgs) -> anyhow::Result<i32> {
let mut args = args.clone();
let config_file = merge_convert_config(&mut args)?;
let common = &args.common;
let target = args.target_or_default();
ensure_root(&common.root)?;
let filter = build_filter(common)?;
ensure_backup_dir(&args)?;
if let Some(path) = &common.report {
ensure_report_dir(path)?;
}
let console = console_cfg(common);
let dry_tag = if args.dry_run { " dry_run=true" } else { "" };
print_header(
console,
format!(
"mode=fix target={} root={}{}",
target,
common.root.display(),
dry_tag
),
);
let (ignore, ignore_recorded, ignore_warnings) = load_ignore(common)?;
let mut warnings = ignore_warnings;
let walk = {
let skip = skip_paths(
common.report.as_deref(),
ignore_recorded.as_deref().map(PathBuf::from).as_deref(),
);
collect_files(&common.root, &filter, &skip, ignore.as_ref())
};
warnings.extend(walk.warnings);
print_warnings(&warnings, console);
let started_at = wall_now();
let started = Instant::now();
let ctx = ConvertCtx {
root: common.root.clone(),
target,
dry_run: args.dry_run,
backup_dir: args.backup_dir.clone(),
max_file_size: common.effective_max_file_size(),
};
let convert_records = with_pool(common.threads, || {
walk.files
.par_iter()
.map(|p| convert_file(p, &ctx))
.collect::<Vec<_>>()
})?;
print_records(&convert_records, console, Some(target.tag()));
let verify_records = with_pool(common.threads, || {
walk.files
.par_iter()
.map(|p| verify_file(p, common.effective_max_file_size()))
.collect::<Vec<_>>()
})?;
let mut verify_failed: Vec<String> = verify_records
.iter()
.filter(|r| r.action == "failed")
.map(|r| r.path.clone())
.collect();
verify_failed.sort();
for r in &verify_records {
if r.action == "failed" {
let reason = r
.error
.clone()
.unwrap_or_else(|| format!("detected={}", r.detected.unwrap_or("?")));
print_problem(format!("failed {} ({reason})", r.path));
}
}
let elapsed = started.elapsed();
let mut report = finalize_report(
"fix",
RunFacts {
root: Some(common.root.clone()),
url: None,
options: options_block(
common,
Some(&args as &dyn ConvertOptions),
&filter,
config_file.as_deref(),
ignore_recorded.as_deref(),
),
started_at,
verify_failed: Some(verify_failed.clone()),
},
convert_records,
elapsed,
);
let verify_failed_count = verify_failed.len() as u64;
if let Summary::Fix(f) = &mut report.summary {
f.passed = verify_records
.iter()
.filter(|r| r.action == "passed")
.count() as u64;
f.failed = verify_failed_count;
}
let convert_issues = match &report.summary {
Summary::Fix(f) => f.unknown + f.decode_errors + f.io_errors,
_ => 0,
};
if let Some(path) = &common.report {
write_report(&report, path)?;
}
if let Summary::Fix(f) = &report.summary
&& !console.quiet
{
println!(
"[utf8-forge] scanned={} candidates={}",
f.total_files, f.candidates
);
println!(
"[utf8-forge] already_utf8={} converted={} unknown={} errors={}",
f.already_utf8,
f.converted,
f.unknown,
f.decode_errors + f.io_errors
);
if f.too_large > 0 {
println!("[utf8-forge] too_large={}", f.too_large);
}
println!(
"[utf8-forge] verify: checked={} passed={} failed={}",
f.passed + f.failed,
f.passed,
f.failed
);
}
print_duration(common.report.as_deref(), elapsed, console);
Ok(if verify_failed_count > 0 {
3
} else if convert_issues > 0 {
1
} else {
0
})
}
fn cmd_diff(args: &DiffArgs) -> anyhow::Result<i32> {
let load = |p: &Path, flag: &str| -> anyhow::Result<DiffInput> {
let text = fs::read_to_string(p)
.with_context(|| format!("cannot load {flag} report `{}`", p.display()))?;
serde_json::from_str(&text)
.with_context(|| format!("cannot load {flag} report `{}`", p.display()))
};
let before = load(&args.before, "--before")?;
let after = load(&args.after, "--after")?;
if let (Some(r1), Some(r2)) = (&before.root, &after.root)
&& r1 != r2
{
print_problem(format!(
"warning: roots differ (`{r1}` vs `{r2}`); path comparison assumes identical roots"
));
}
let mut doc = differ::compare(&before, &after);
doc.before = args.before.to_string_lossy().into_owned();
doc.after = args.after.to_string_lossy().into_owned();
let summary = &doc.summary;
if let Some(path) = &args.report {
ensure_report_dir(path)?;
let json = serde_json::to_string_pretty(&doc).context("diff serialization failed")?;
writer::atomic_write(path, (json + "\n").as_bytes())
.with_context(|| format!("cannot write diff report {}", path.display()))?;
println!(
"[utf8-forge] diff: added={} removed={} changed={} unchanged={} report={}",
summary.added,
summary.removed,
summary.action_changed,
summary.unchanged,
path.display()
);
} else {
println!(
"[utf8-forge] diff: added={} removed={} changed={} unchanged={}",
summary.added, summary.removed, summary.action_changed, summary.unchanged
);
let json = serde_json::to_string_pretty(&doc).context("diff serialization failed")?;
println!("{json}");
}
let drifted = summary.added + summary.removed + summary.action_changed;
Ok(if args.fail_on_change && drifted > 0 {
1
} else {
0
})
}
#[cfg(test)]
mod backup_placement_tests {
use super::check_backup_placement;
use std::path::Path;
#[test]
fn relative_backup_dir_resolves_against_cwd() {
let cwd = std::fs::canonicalize(std::env::current_dir().unwrap()).unwrap();
check_backup_placement(Path::new("backup"), &cwd.join("elsewhere-root")).unwrap();
check_backup_placement(Path::new("a/b/c"), &cwd.join("elsewhere-root")).unwrap();
let err = check_backup_placement(Path::new("backup"), &cwd).unwrap_err();
assert!(err.to_string().contains("inside"), "{err}");
}
}