Skip to content

Commit 38a0883

Browse files
kelvinou01Kelvin Ou
andauthored
feat: add support for layered package cache (#1003)
Co-authored-by: Kelvin Ou <[email protected]>
1 parent 11de1e8 commit 38a0883

File tree

2 files changed

+520
-101
lines changed

2 files changed

+520
-101
lines changed

crates/rattler_cache/src/package_cache/cache_lock.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use fs4::fs_std::FileExt;
1111
use parking_lot::Mutex;
1212
use rattler_digest::Sha256Hash;
1313

14-
use crate::package_cache::PackageCacheError;
14+
use crate::package_cache::PackageCacheLayerError;
1515

1616
/// A lock on the cache entry. As long as this lock is held, no other process is
1717
/// allowed to modify the cache entry. This however, does not guarantee that the
@@ -60,7 +60,7 @@ impl Drop for CacheRwLock {
6060
}
6161

6262
impl CacheRwLock {
63-
pub async fn acquire_read(path: &Path) -> Result<Self, PackageCacheError> {
63+
pub async fn acquire_read(path: &Path) -> Result<Self, PackageCacheLayerError> {
6464
let lock_file_path = path.to_path_buf();
6565

6666
let acquire_lock_fut = simple_spawn_blocking::tokio::run_blocking_task(move || {
@@ -71,7 +71,7 @@ impl CacheRwLock {
7171
.write(true)
7272
.open(&lock_file_path)
7373
.map_err(|e| {
74-
PackageCacheError::LockError(
74+
PackageCacheLayerError::LockError(
7575
format!(
7676
"failed to open cache lock for reading: '{}'",
7777
lock_file_path.display()
@@ -81,7 +81,7 @@ impl CacheRwLock {
8181
})?;
8282

8383
fs4::fs_std::FileExt::lock_shared(&file).map_err(move |e| {
84-
PackageCacheError::LockError(
84+
PackageCacheLayerError::LockError(
8585
format!(
8686
"failed to acquire read lock on cache lock file: '{}'",
8787
lock_file_path.display()
@@ -108,7 +108,7 @@ impl CacheRwLock {
108108
}
109109

110110
impl CacheRwLock {
111-
pub async fn acquire_write(path: &Path) -> Result<Self, PackageCacheError> {
111+
pub async fn acquire_write(path: &Path) -> Result<Self, PackageCacheLayerError> {
112112
let lock_file_path = path.to_path_buf();
113113
let acquire_lock_fut = simple_spawn_blocking::tokio::run_blocking_task(move || {
114114
let file = std::fs::OpenOptions::new()
@@ -118,7 +118,7 @@ impl CacheRwLock {
118118
.read(true)
119119
.open(&lock_file_path)
120120
.map_err(|e| {
121-
PackageCacheError::LockError(
121+
PackageCacheLayerError::LockError(
122122
format!(
123123
"failed to open cache lock for writing: '{}",
124124
lock_file_path.display()
@@ -128,7 +128,7 @@ impl CacheRwLock {
128128
})?;
129129

130130
file.lock_exclusive().map_err(move |e| {
131-
PackageCacheError::LockError(
131+
PackageCacheLayerError::LockError(
132132
format!(
133133
"failed to acquire write lock on cache lock file: '{}'",
134134
lock_file_path.display()
@@ -159,15 +159,15 @@ impl CacheRwLock {
159159
&mut self,
160160
revision: u64,
161161
sha256: Option<&Sha256Hash>,
162-
) -> Result<(), PackageCacheError> {
162+
) -> Result<(), PackageCacheLayerError> {
163163
let file = self.file.clone();
164164
let sha256 = sha256.cloned();
165165
simple_spawn_blocking::tokio::run_blocking_task(move || {
166166
let mut file = file.lock();
167167

168168
// Ensure we write from the start of the file
169169
file.rewind().map_err(|e| {
170-
PackageCacheError::LockError(
170+
PackageCacheLayerError::LockError(
171171
"failed to rewind cache lock for reading revision".to_string(),
172172
e,
173173
)
@@ -176,7 +176,7 @@ impl CacheRwLock {
176176
// Write the bytes of the revision
177177
let revision_bytes = revision.to_be_bytes();
178178
file.write_all(&revision_bytes).map_err(|e| {
179-
PackageCacheError::LockError(
179+
PackageCacheLayerError::LockError(
180180
"failed to write revision from cache lock".to_string(),
181181
e,
182182
)
@@ -187,7 +187,7 @@ impl CacheRwLock {
187187
let len = sha.len();
188188
let sha = &sha[..];
189189
file.write_all(sha).map_err(|e| {
190-
PackageCacheError::LockError(
190+
PackageCacheLayerError::LockError(
191191
"failed to write sha256 from cache lock".to_string(),
192192
e,
193193
)
@@ -199,7 +199,7 @@ impl CacheRwLock {
199199

200200
// Ensure all bytes are written to disk
201201
file.flush().map_err(|e| {
202-
PackageCacheError::LockError(
202+
PackageCacheLayerError::LockError(
203203
"failed to flush cache lock after writing revision".to_string(),
204204
e,
205205
)
@@ -208,7 +208,7 @@ impl CacheRwLock {
208208
// Update the length of the file
209209
let file_length = revision_bytes.len() + sha_bytes;
210210
file.set_len(file_length as u64).map_err(|e| {
211-
PackageCacheError::LockError(
211+
PackageCacheLayerError::LockError(
212212
"failed to truncate cache lock after writing revision".to_string(),
213213
e,
214214
)
@@ -222,10 +222,10 @@ impl CacheRwLock {
222222

223223
impl CacheRwLock {
224224
/// Reads the revision from the cache lock file.
225-
pub fn read_revision(&mut self) -> Result<u64, PackageCacheError> {
225+
pub fn read_revision(&mut self) -> Result<u64, PackageCacheLayerError> {
226226
let mut file = self.file.lock();
227227
file.rewind().map_err(|e| {
228-
PackageCacheError::LockError(
228+
PackageCacheLayerError::LockError(
229229
"failed to rewind cache lock for reading revision".to_string(),
230230
e,
231231
)
@@ -237,7 +237,7 @@ impl CacheRwLock {
237237
return Ok(0);
238238
}
239239
Err(e) => {
240-
return Err(PackageCacheError::LockError(
240+
return Err(PackageCacheLayerError::LockError(
241241
"failed to read revision from cache lock".to_string(),
242242
e,
243243
));
@@ -247,27 +247,30 @@ impl CacheRwLock {
247247
}
248248

249249
/// Reads the sha256 hash from the cache lock file.
250-
pub fn read_sha256(&mut self) -> Result<Option<Sha256Hash>, PackageCacheError> {
250+
pub fn read_sha256(&mut self) -> Result<Option<Sha256Hash>, PackageCacheLayerError> {
251251
const SHA256_LEN: usize = 32;
252252
const REVISION_LEN: u64 = 8;
253253
let mut file = self.file.lock();
254254
file.rewind().map_err(|e| {
255-
PackageCacheError::LockError(
255+
PackageCacheLayerError::LockError(
256256
"failed to rewind cache lock for reading sha256".to_string(),
257257
e,
258258
)
259259
})?;
260260
let mut buf = [0; SHA256_LEN];
261261
let _ = file.seek(SeekFrom::Start(REVISION_LEN)).map_err(|e| {
262-
PackageCacheError::LockError("failed to seek to sha256 in cache lock".to_string(), e)
262+
PackageCacheLayerError::LockError(
263+
"failed to seek to sha256 in cache lock".to_string(),
264+
e,
265+
)
263266
})?;
264267
match file.read_exact(&mut buf) {
265268
Ok(_) => {}
266269
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
267270
return Ok(None);
268271
}
269272
Err(e) => {
270-
return Err(PackageCacheError::LockError(
273+
return Err(PackageCacheLayerError::LockError(
271274
"failed to read sha256 from cache lock".to_string(),
272275
e,
273276
));

0 commit comments

Comments
 (0)