]> git.proxmox.com Git - cargo.git/commitdiff
Warn when packaging files with Windows special names.
authorEric Huss <eric@huss.org>
Tue, 3 Mar 2020 00:14:36 +0000 (16:14 -0800)
committerEric Huss <eric@huss.org>
Tue, 3 Mar 2020 00:14:36 +0000 (16:14 -0800)
src/cargo/ops/cargo_package.rs
tests/testsuite/package.rs

index 1d7af7bf8136f9a8a91b98da32b1e09edc8044e8..c4c2a0f9c66c6bae18504ea05246f79081615c9d 100644 (file)
@@ -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<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(()),
@@ -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(())
 }
index 668d2024237c4187c7f4b4ffbe3ec3ff0a1f457f..408b9b94b9d44378826598e4759ab1b102cfc530 100644 (file)
@@ -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();
+}