Skip to content

Commit

Permalink
Warn when packaging files with Windows special names.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Mar 3, 2020
1 parent 15ac82b commit 2a874aa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(|| {
Expand Down Expand Up @@ -804,7 +804,7 @@ fn report_hash_difference(orig: &HashMap<PathBuf, u64>, after: &HashMap<PathBuf,
//
// To help out in situations like this, issue about weird filenames when
// packaging as a "heads up" that something may not work on other platforms.
fn check_filename(file: &Path) -> CargoResult<()> {
fn check_filename(file: &Path, shell: &mut Shell) -> CargoResult<()> {
let name = match file.file_name() {
Some(name) => name,
None => return Ok(()),
Expand All @@ -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(())
}
34 changes: 34 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 2a874aa

Please sign in to comment.