From 2a874aa522f228735da87353646c39981ff10fab Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Mar 2020 16:14:36 -0800 Subject: [PATCH] Warn when packaging files with Windows special names. --- src/cargo/ops/cargo_package.rs | 28 ++++++++++++++++++++++++---- tests/testsuite/package.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 1d7af7bf8..c4c2a0f9c 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -13,14 +13,14 @@ use log::debug; use tar::{Archive, Builder, EntryType, Header}; use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor}; -use crate::core::{Feature, Verbosity, Workspace}; +use crate::core::{Feature, Shell, Verbosity, Workspace}; use crate::core::{Package, PackageId, PackageSet, Resolve, Source, SourceId}; use crate::ops; use crate::sources::PathSource; use crate::util::errors::{CargoResult, CargoResultExt}; use crate::util::paths; use crate::util::toml::TomlManifest; -use crate::util::{self, Config, FileLock}; +use crate::util::{self, restricted_names, Config, FileLock}; pub struct PackageOpts<'cfg> { pub config: &'cfg Config, @@ -142,7 +142,7 @@ fn build_ar_list( let root = pkg.root(); for src_file in src_files { let rel_path = src_file.strip_prefix(&root)?.to_path_buf(); - check_filename(&rel_path)?; + check_filename(&rel_path, &mut ws.config().shell())?; let rel_str = rel_path .to_str() .ok_or_else(|| { @@ -804,7 +804,7 @@ fn report_hash_difference(orig: &HashMap, after: &HashMap CargoResult<()> { +fn check_filename(file: &Path, shell: &mut Shell) -> CargoResult<()> { let name = match file.file_name() { Some(name) => name, None => return Ok(()), @@ -825,5 +825,25 @@ fn check_filename(file: &Path) -> CargoResult<()> { file.display() ) } + let mut check_windows = |name| -> CargoResult<()> { + if restricted_names::is_windows_reserved(name) { + shell.warn(format!( + "file {} is a reserved Windows filename, \ + it will not work on Windows platforms", + file.display() + ))?; + } + Ok(()) + }; + for component in file.iter() { + if let Some(component) = component.to_str() { + check_windows(component)?; + } + } + if file.extension().is_some() { + if let Some(stem) = file.file_stem().and_then(|s| s.to_str()) { + check_windows(stem)?; + } + } Ok(()) } diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 668d20242..408b9b94b 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1663,3 +1663,37 @@ src/lib.rs let orig = read_to_string(p.root().join("target/package/foo-1.0.0/Cargo.toml.orig")).unwrap(); assert!(orig.contains("license-file = \"../LICENSE\"")); } + +#[cargo_test] +#[cfg(not(windows))] // Don't want to create invalid files on Windows. +fn package_restricted_windows() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + license = "MIT" + description = "foo" + homepage = "foo" + "#, + ) + .file("src/lib.rs", "pub mod con;\npub mod aux;") + .file("src/con.rs", "pub fn f() {}") + .file("src/aux/mod.rs", "pub fn f() {}") + .build(); + + p.cargo("package") + .with_stderr( + "\ +[WARNING] file src/aux/mod.rs is a reserved Windows filename, it will not work on Windows platforms +[WARNING] file src/con.rs is a reserved Windows filename, it will not work on Windows platforms +[PACKAGING] foo [..] +[VERIFYING] foo [..] +[COMPILING] foo [..] +[FINISHED] [..] +", + ) + .run(); +}