refactor: Do read check first instead

Signed-off-by: Xuanwo <github@xuanwo.io>
This commit is contained in:
Xuanwo 2023-01-09 14:25:48 +08:00 коммит произвёл Sylvestre Ledru
Родитель f618a28157
Коммит 762f5c60b5
1 изменённых файлов: 11 добавлений и 7 удалений

18
src/cache/cache.rs поставляемый
Просмотреть файл

@ -392,13 +392,7 @@ impl Storage for opendal::Operator {
let path = ".sccache_check"; let path = ".sccache_check";
let can_write = match self.object(path).write("Hello, World!").await { // Read is required, return error directly if we can't read .
Ok(_) => true,
Err(err) if err.kind() == ErrorKind::ObjectAlreadyExists => true,
Err(err) if err.kind() == ErrorKind::ObjectPermissionDenied => false,
Err(err) => bail!("cache storage failed to write: {:?}", err),
};
match self.object(path).read().await { match self.object(path).read().await {
Ok(_) => (), Ok(_) => (),
// Read not exist file with not found is ok. // Read not exist file with not found is ok.
@ -406,6 +400,16 @@ impl Storage for opendal::Operator {
Err(err) => bail!("cache storage failed to read: {:?}", err), Err(err) => bail!("cache storage failed to read: {:?}", err),
}; };
let can_write = match self.object(path).write("Hello, World!").await {
Ok(_) => true,
Err(err) if err.kind() == ErrorKind::ObjectAlreadyExists => true,
// Toralte all other write errors because we can do read as least.
Err(err) => {
warn!("storage write check failed: {err:?}");
false
}
};
if can_write { if can_write {
Ok(CacheMode::ReadWrite) Ok(CacheMode::ReadWrite)
} else { } else {