From f046ff5d0c9ccba5457c71a995905f6e85fa51d4 Mon Sep 17 00:00:00 2001 From: Alex Huszagh Date: Wed, 22 Jun 2022 14:28:53 -0500 Subject: [PATCH] Add a cache dir tag when creating a target directory. Ensure `cross` is similar in functionality to `cargo`, and this minimizes network traffic by applications that may copy the project but do not wish to copy compiled code. Closes #835. --- CHANGELOG.md | 1 + src/docker/shared.rs | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67752ebe9..c6c127911 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- #836 - write a `CACHEDIR.TAG` when creating the target directory, similar to `cargo`. - #804 - allow usage of env `CARGO_BUILD_TARGET` as an alias for `CROSS_BUILD_TARGET` - #792 - fixed container-in-container support when using podman. - #781 - ensure `target.$(...)` config options override `build` ones. diff --git a/src/docker/shared.rs b/src/docker/shared.rs index 85b462a40..ac3b51a17 100644 --- a/src/docker/shared.rs +++ b/src/docker/shared.rs @@ -62,7 +62,7 @@ impl Directories { // otherwise `docker` will create them but they will be owned by `root` fs::create_dir(&cargo).ok(); fs::create_dir(&xargo).ok(); - fs::create_dir(&target).ok(); + create_target_dir(target)?; let cargo = mount_finder.find_mount_path(cargo); let xargo = mount_finder.find_mount_path(xargo); @@ -111,6 +111,24 @@ impl Directories { } } +const CACHEDIR_TAG: &str = "Signature: 8a477f597d28d172789f06886806bc55 +# This file is a cache directory tag created by cross. +# For information about cache directory tags see https://bford.info/cachedir/"; + +fn create_target_dir(path: &Path) -> Result<()> { + // cargo creates all paths to the target directory, and writes + // a cache dir tag only if the path doesn't previously exist. + if !path.exists() { + fs::create_dir_all(&path)?; + fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&path.join("CACHEDIR.TAG"))? + .write_all(CACHEDIR_TAG.as_bytes())?; + } + Ok(()) +} + pub fn command(engine: &Engine) -> Command { Command::new(&engine.path) }