Rinomina: crate akode-*, cartelle akode-rs/ e akode-cli/, binario akode, ~/.akode, AKODE_* (A)
Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
120
akode-rs/windows-sandbox-rs/src/helper_materialization/copy.rs
Normal file
120
akode-rs/windows-sandbox-rs/src/helper_materialization/copy.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
//! Copies legacy helpers using destination-inherited ACLs and the existing freshness check.
|
||||
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use anyhow::anyhow;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(super) enum CopyOutcome {
|
||||
Reused,
|
||||
ReCopied,
|
||||
}
|
||||
|
||||
pub(super) fn copy_from_source_if_needed(source: &Path, destination: &Path) -> Result<CopyOutcome> {
|
||||
if destination_is_fresh(source, destination)? {
|
||||
return Ok(CopyOutcome::Reused);
|
||||
}
|
||||
|
||||
let destination_dir = destination.parent().ok_or_else(|| {
|
||||
anyhow!(
|
||||
"helper destination has no parent: {}",
|
||||
destination.display()
|
||||
)
|
||||
})?;
|
||||
fs::create_dir_all(destination_dir).with_context(|| {
|
||||
format!(
|
||||
"create helper destination directory {}",
|
||||
destination_dir.display()
|
||||
)
|
||||
})?;
|
||||
|
||||
let temp_path = NamedTempFile::new_in(destination_dir)
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"create temporary helper file in {}",
|
||||
destination_dir.display()
|
||||
)
|
||||
})?
|
||||
.into_temp_path();
|
||||
let temp_path_buf = temp_path.to_path_buf();
|
||||
|
||||
let mut source_file = fs::File::open(source)
|
||||
.with_context(|| format!("open helper source for read {}", source.display()))?;
|
||||
let mut temp_file = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.open(&temp_path_buf)
|
||||
.with_context(|| format!("open temporary helper file {}", temp_path_buf.display()))?;
|
||||
|
||||
// Write into a temp file created inside `.sandbox-bin` so the copied helper keeps the
|
||||
// destination directory's inherited ACLs instead of reusing the source file's descriptor.
|
||||
std::io::copy(&mut source_file, &mut temp_file).with_context(|| {
|
||||
format!(
|
||||
"copy helper from {} to {}",
|
||||
source.display(),
|
||||
temp_path_buf.display()
|
||||
)
|
||||
})?;
|
||||
temp_file
|
||||
.flush()
|
||||
.with_context(|| format!("flush temporary helper file {}", temp_path_buf.display()))?;
|
||||
drop(temp_file);
|
||||
|
||||
if destination.exists() {
|
||||
fs::remove_file(destination).with_context(|| {
|
||||
format!("remove stale helper destination {}", destination.display())
|
||||
})?;
|
||||
}
|
||||
|
||||
match fs::rename(&temp_path_buf, destination) {
|
||||
Ok(()) => Ok(CopyOutcome::ReCopied),
|
||||
Err(rename_err) => {
|
||||
if destination_is_fresh(source, destination)? {
|
||||
Ok(CopyOutcome::Reused)
|
||||
} else {
|
||||
Err(rename_err).with_context(|| {
|
||||
format!(
|
||||
"rename helper temp file {} to {}",
|
||||
temp_path_buf.display(),
|
||||
destination.display()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn destination_is_fresh(source: &Path, destination: &Path) -> Result<bool> {
|
||||
let source_meta = fs::metadata(source)
|
||||
.with_context(|| format!("read helper source metadata {}", source.display()))?;
|
||||
let destination_meta = match fs::metadata(destination) {
|
||||
Ok(meta) => meta,
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false),
|
||||
Err(err) => {
|
||||
return Err(err).with_context(|| {
|
||||
format!("read helper destination metadata {}", destination.display())
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if source_meta.len() != destination_meta.len() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let source_modified = source_meta
|
||||
.modified()
|
||||
.with_context(|| format!("read helper source mtime {}", source.display()))?;
|
||||
let destination_modified = destination_meta
|
||||
.modified()
|
||||
.with_context(|| format!("read helper destination mtime {}", destination.display()))?;
|
||||
|
||||
Ok(destination_modified >= source_modified)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "copy_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,58 @@
|
||||
//! Tests the legacy helper copy and freshness contract.
|
||||
|
||||
use super::CopyOutcome;
|
||||
use super::copy_from_source_if_needed;
|
||||
use super::destination_is_fresh;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn copy_from_source_if_needed_copies_missing_destination() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let source = tmp.path().join("source.exe");
|
||||
let destination = tmp.path().join("bin").join("helper.exe");
|
||||
|
||||
fs::write(&source, b"runner-v1").expect("write source");
|
||||
|
||||
let outcome = copy_from_source_if_needed(&source, &destination).expect("copy helper");
|
||||
|
||||
assert_eq!(CopyOutcome::ReCopied, outcome);
|
||||
assert_eq!(
|
||||
b"runner-v1".as_slice(),
|
||||
fs::read(&destination).expect("read destination")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn destination_is_fresh_uses_size_and_mtime() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let source = tmp.path().join("source.exe");
|
||||
let destination = tmp.path().join("destination.exe");
|
||||
|
||||
fs::write(&destination, b"same-size").expect("write destination");
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
fs::write(&source, b"same-size").expect("write source");
|
||||
assert!(!destination_is_fresh(&source, &destination).expect("stale metadata"));
|
||||
|
||||
fs::write(&destination, b"same-size").expect("rewrite destination");
|
||||
assert!(destination_is_fresh(&source, &destination).expect("fresh metadata"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_from_source_if_needed_reuses_fresh_destination() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let source = tmp.path().join("source.exe");
|
||||
let destination = tmp.path().join("bin").join("helper.exe");
|
||||
|
||||
fs::write(&source, b"runner-v1").expect("write source");
|
||||
copy_from_source_if_needed(&source, &destination).expect("initial copy");
|
||||
|
||||
let outcome = copy_from_source_if_needed(&source, &destination).expect("revalidate helper");
|
||||
|
||||
assert_eq!(CopyOutcome::Reused, outcome);
|
||||
assert_eq!(
|
||||
b"runner-v1".as_slice(),
|
||||
fs::read(&destination).expect("read destination")
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user