Skip to content

Commit 74435f0

Browse files
committed
[rust] Refactor lock logic in a separate module
1 parent 71b95da commit 74435f0

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

rust/src/files.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use crate::config::OS;
1919
use crate::config::OS::WINDOWS;
20+
use crate::lock::Lock;
2021
use crate::{
2122
format_one_arg, format_three_args, run_shell_command_by_os, Command, Logger, CP_VOLUME_COMMAND,
2223
HDIUTIL_ATTACH_COMMAND, HDIUTIL_DETACH_COMMAND, MACOS, MSIEXEC_INSTALL_COMMAND,
@@ -27,7 +28,6 @@ use apple_flat_package::PkgReader;
2728
use bzip2::read::BzDecoder;
2829
use directories::BaseDirs;
2930
use flate2::read::GzDecoder;
30-
use fs2::FileExt;
3131
use fs_extra::dir::{move_dir, CopyOptions};
3232
use regex::Regex;
3333
use std::fs;
@@ -55,7 +55,6 @@ const MSI: &str = "msi";
5555
const XZ: &str = "xz";
5656
const SEVEN_ZIP_HEADER: &[u8; 6] = b"7z\xBC\xAF\x27\x1C";
5757
const UNCOMPRESS_MACOS_ERR_MSG: &str = "{} files are only supported in macOS";
58-
const LOCK_FILE: &str = "sm.lock";
5958

6059
#[derive(Hash, Eq, PartialEq, Debug)]
6160
pub struct BrowserPath {
@@ -72,39 +71,6 @@ impl BrowserPath {
7271
}
7372
}
7473

75-
pub struct Lock {
76-
file: File,
77-
path: PathBuf,
78-
}
79-
80-
impl Lock {
81-
fn acquire(log: &Logger, target: &Path, single_file: Option<String>) -> Result<Self, Error> {
82-
let lock_folder = if single_file.is_some() {
83-
create_parent_path_if_not_exists(target)?;
84-
target.parent().unwrap()
85-
} else {
86-
create_path_if_not_exists(target)?;
87-
target
88-
};
89-
let path = lock_folder.join(LOCK_FILE);
90-
let file = File::create(&path)?;
91-
92-
log.trace(format!("Using lock file at {}", path.display()));
93-
file.lock_exclusive().unwrap_or_default();
94-
95-
Ok(Self { file, path })
96-
}
97-
98-
fn release(&mut self) {
99-
self.file.unlock().unwrap_or_default();
100-
fs::remove_file(&self.path).unwrap_or_default();
101-
}
102-
103-
fn exists(&mut self) -> bool {
104-
self.path.exists()
105-
}
106-
}
107-
10874
pub fn create_parent_path_if_not_exists(path: &Path) -> Result<(), Error> {
10975
if let Some(p) = path.parent() {
11076
create_path_if_not_exists(p)?;

rust/src/lock.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::logger::Logger;
2+
use anyhow::Error;
3+
use std::fs::File;
4+
use std::path::{Path, PathBuf};
5+
6+
use crate::files::{create_parent_path_if_not_exists, create_path_if_not_exists};
7+
use fs2::FileExt;
8+
use std::fs;
9+
10+
const LOCK_FILE: &str = "sm.lock";
11+
12+
pub struct Lock {
13+
file: File,
14+
path: PathBuf,
15+
}
16+
17+
impl Lock {
18+
pub fn acquire(
19+
log: &Logger,
20+
target: &Path,
21+
single_file: Option<String>,
22+
) -> Result<Self, Error> {
23+
let lock_folder = if single_file.is_some() {
24+
create_parent_path_if_not_exists(target)?;
25+
target.parent().unwrap()
26+
} else {
27+
create_path_if_not_exists(target)?;
28+
target
29+
};
30+
let path = lock_folder.join(LOCK_FILE);
31+
let file = File::create(&path)?;
32+
33+
log.trace(format!("Using lock file at {}", path.display()));
34+
file.lock_exclusive().unwrap_or_default();
35+
36+
Ok(Self { file, path })
37+
}
38+
39+
pub fn release(&mut self) {
40+
self.file.unlock().unwrap_or_default();
41+
fs::remove_file(&self.path).unwrap_or_default();
42+
}
43+
44+
pub fn exists(&mut self) -> bool {
45+
self.path.exists()
46+
}
47+
}

0 commit comments

Comments
 (0)