Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rust] Use file lock to protect concurrent accesses to cache #14898

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 111 additions & 2 deletions rust/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "778a62d50d430548a64ececb679a81afba2ffa2ac1553d2e95807253aa723ac7",
"checksum": "ba73e39704c230ab36d29779338a27060fadfde0a7e9b957428668f5fdb9c271",
"crates": {
"addr2line 0.21.0": {
"name": "addr2line",
Expand Down Expand Up @@ -4539,6 +4539,100 @@
],
"license_file": "LICENSE-APACHE"
},
"fs2 0.4.3": {
"name": "fs2",
"version": "0.4.3",
"package_url": "https://github.com/danburkert/fs2-rs",
"repository": {
"Http": {
"url": "https://static.crates.io/crates/fs2/0.4.3/download",
"sha256": "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
}
},
"targets": [
{
"Library": {
"crate_name": "fs2",
"crate_root": "src/lib.rs",
"srcs": {
"allow_empty": true,
"include": [
"**/*.rs"
]
}
}
}
],
"library_target_name": "fs2",
"common_attrs": {
"compile_data_glob": [
"**"
],
"deps": {
"common": [],
"selects": {
"cfg(unix)": [
{
"id": "libc 0.2.168",
"target": "libc"
}
],
"cfg(windows)": [
{
"id": "winapi 0.3.9",
"target": "winapi"
}
]
}
},
"edition": "2015",
"version": "0.4.3"
},
"license": "MIT/Apache-2.0",
"license_ids": [
"Apache-2.0",
"MIT"
],
"license_file": "LICENSE-APACHE"
},
"fs_extra 1.3.0": {
"name": "fs_extra",
"version": "1.3.0",
"package_url": "https://github.com/webdesus/fs_extra",
"repository": {
"Http": {
"url": "https://static.crates.io/crates/fs_extra/1.3.0/download",
"sha256": "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
}
},
"targets": [
{
"Library": {
"crate_name": "fs_extra",
"crate_root": "src/lib.rs",
"srcs": {
"allow_empty": true,
"include": [
"**/*.rs"
]
}
}
}
],
"library_target_name": "fs_extra",
"common_attrs": {
"compile_data_glob": [
"**"
],
"edition": "2018",
"version": "1.3.0"
},
"license": "MIT",
"license_ids": [
"MIT"
],
"license_file": "LICENSE"
},
"futures 0.3.30": {
"name": "futures",
"version": "0.3.30",
Expand Down Expand Up @@ -13320,6 +13414,14 @@
"id": "flate2 1.0.35",
"target": "flate2"
},
{
"id": "fs2 0.4.3",
"target": "fs2"
},
{
"id": "fs_extra 1.3.0",
"target": "fs_extra"
},
{
"id": "infer 0.16.0",
"target": "infer"
Expand Down Expand Up @@ -18553,7 +18655,12 @@
],
"crate_features": {
"common": [
"winbase"
"fileapi",
"handleapi",
"processthreadsapi",
"std",
"winbase",
"winerror"
],
"selects": {}
},
Expand Down Expand Up @@ -22185,6 +22292,8 @@
"env_logger 0.11.5",
"exitcode 1.1.2",
"flate2 1.0.35",
"fs2 0.4.3",
"fs_extra 1.3.0",
"infer 0.16.0",
"log 0.4.22",
"regex 1.11.1",
Expand Down
20 changes: 19 additions & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ debpkg = "0.6.0"
anyhow = { version = "1.0.94", default-features = false, features = ["backtrace", "std"] }
apple-flat-package = "0.20.0"
which = "7.0.0"
fs2 = "0.4.3"
fs_extra = "1.3.0"

[dev-dependencies]
assert_cmd = "2.0.16"
Expand Down
57 changes: 30 additions & 27 deletions rust/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use apple_flat_package::PkgReader;
use bzip2::read::BzDecoder;
use directories::BaseDirs;
use flate2::read::GzDecoder;
use fs_extra::dir::{move_dir, CopyOptions};
use regex::Regex;
use std::fs;
use std::fs::File;
Expand Down Expand Up @@ -143,7 +144,7 @@ pub fn uncompress(
} else if extension.eq_ignore_ascii_case(EXE) {
uncompress_sfx(compressed_file, target, log)?
} else if extension.eq_ignore_ascii_case(DEB) {
uncompress_deb(compressed_file, target, log, os, volume.unwrap_or_default())?
uncompress_deb(compressed_file, target, log, volume.unwrap_or_default())?
} else if extension.eq_ignore_ascii_case(MSI) {
install_msi(compressed_file, log, os)?
} else if extension.eq_ignore_ascii_case(XML) || extension.eq_ignore_ascii_case(HTML) {
Expand All @@ -158,6 +159,7 @@ pub fn uncompress(
extension
)));
}

Ok(())
}

Expand All @@ -176,15 +178,23 @@ pub fn uncompress_sfx(compressed_file: &str, target: &Path, log: &Logger) -> Res
sevenz_rust::decompress(file_reader, zip_parent).unwrap();

let zip_parent_str = path_to_string(zip_parent);
let target_str = path_to_string(target);
let core_str = format!(r"{}\core", zip_parent_str);
move_folder_content(&core_str, &target, &log)?;

Ok(())
}

pub fn move_folder_content(source: &str, target: &Path, log: &Logger) -> Result<(), Error> {
log.trace(format!(
"Moving extracted files and folders from {} to {}",
core_str, target_str
"Moving files and folders from {} to {}",
source,
target.display()
));
create_parent_path_if_not_exists(target)?;
fs::rename(&core_str, &target_str)?;

let mut options = CopyOptions::new();
options.content_only = true;
options.skip_exist = true;
move_dir(source, target, &options)?;
Ok(())
}

Expand Down Expand Up @@ -261,7 +271,6 @@ pub fn uncompress_deb(
compressed_file: &str,
target: &Path,
log: &Logger,
os: &str,
label: &str,
) -> Result<(), Error> {
let zip_parent = Path::new(compressed_file).parent().unwrap();
Expand All @@ -276,21 +285,17 @@ pub fn uncompress_deb(
deb_pkg.data()?.unpack(zip_parent)?;

let zip_parent_str = path_to_string(zip_parent);
let target_str = path_to_string(target);
let opt_edge_str = format!("{}/opt/microsoft/{}", zip_parent_str, label);
let opt_edge_mv = format!("mv {} {}", opt_edge_str, target_str);
let command = Command::new_single(opt_edge_mv.clone());
log.trace(format!(
"Moving extracted files and folders from {} to {}",
opt_edge_str, target_str
));
create_parent_path_if_not_exists(target)?;
run_shell_command_by_os(os, command)?;
let target_path = Path::new(target);
if target_path.parent().unwrap().read_dir()?.next().is_none() {
fs::rename(&opt_edge_str, &target_str)?;

// Exception due to bad symbolic link in unstable distributions. For example:
// microsoft-edge -> /opt/microsoft/msedge-beta/microsoft-edge-beta
if !label.eq("msedge") {
let link = format!("{}/microsoft-edge", opt_edge_str);
fs::remove_file(Path::new(&link)).unwrap_or_default();
}

move_folder_content(&opt_edge_str, &target, &log)?;

Ok(())
}

Expand Down Expand Up @@ -337,14 +342,12 @@ pub fn uncompress_tar(decoder: &mut dyn Read, target: &Path, log: &Logger) -> Re
let mut buffer: Vec<u8> = Vec::new();
decoder.read_to_end(&mut buffer)?;
let mut archive = Archive::new(Cursor::new(buffer));
if !target.exists() {
for entry in archive.entries()? {
let mut entry_decoder = entry?;
let entry_path: PathBuf = entry_decoder.path()?.iter().skip(1).collect();
let entry_target = target.join(entry_path);
fs::create_dir_all(entry_target.parent().unwrap())?;
entry_decoder.unpack(entry_target)?;
}
for entry in archive.entries()? {
let mut entry_decoder = entry?;
let entry_path: PathBuf = entry_decoder.path()?.iter().skip(1).collect();
let entry_target = target.join(entry_path);
fs::create_dir_all(entry_target.parent().unwrap())?;
entry_decoder.unpack(entry_target)?;
}
Ok(())
}
Expand Down
Loading
Loading