mac download (#77)

* mac download

* clippy
This commit is contained in:
Wyatt Verchere
2023-04-11 07:15:37 -07:00
committed by GitHub
parent d23bc4450d
commit ac6ad0ef2e
10 changed files with 63 additions and 25 deletions

View File

@@ -41,7 +41,9 @@ pub struct State {
/// Information on the location of files used in the launcher
pub directories: DirectoryInfo,
/// Semaphore used to limit concurrent I/O and avoid errors
pub io_semaphore: Semaphore,
pub io_semaphore: RwLock<Semaphore>,
/// Stored maximum number of sempahores of current io_semaphore
pub io_semaphore_max: RwLock<u32>,
/// Launcher metadata
pub metadata: Metadata,
// TODO: settings API
@@ -80,8 +82,10 @@ impl State {
Settings::init(&directories.settings_file()).await?;
// Loose initializations
let io_semaphore_max = settings.max_concurrent_downloads;
let io_semaphore =
Semaphore::new(settings.max_concurrent_downloads);
RwLock::new(Semaphore::new(io_semaphore_max));
// Launcher data
let (metadata, profiles) = tokio::try_join! {
@@ -112,6 +116,7 @@ impl State {
Ok(Arc::new(Self {
directories,
io_semaphore,
io_semaphore_max: RwLock::new(io_semaphore_max as u32),
metadata,
settings: RwLock::new(settings),
profiles: RwLock::new(profiles),
@@ -158,4 +163,21 @@ impl State {
Ok(())
}
/// Reset semaphores to default values
/// This will block until all uses of the semaphore are complete, so it should only be called
/// when we are not in the middle of downloading something (ie: changing the settings!)
pub async fn reset_semaphore(&self) {
let settings = self.settings.read().await;
let mut io_semaphore = self.io_semaphore.write().await;
let mut total_permits = self.io_semaphore_max.write().await;
// Wait to get all permits back
let _ = io_semaphore.acquire_many(*total_permits).await;
// Reset the semaphore
io_semaphore.close();
*total_permits = settings.max_concurrent_downloads as u32;
*io_semaphore = Semaphore::new(settings.max_concurrent_downloads);
}
}

View File

@@ -10,8 +10,8 @@ use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use tokio::fs;
use tokio::sync::Semaphore;
use tokio::{fs, sync::RwLock};
const PROFILE_JSON_PATH: &str = "profile.json";
@@ -119,7 +119,7 @@ impl Profile {
pub async fn set_icon<'a>(
&'a mut self,
cache_dir: &Path,
semaphore: &Semaphore,
semaphore: &RwLock<Semaphore>,
icon: bytes::Bytes,
file_name: &str,
) -> crate::Result<&'a mut Self> {
@@ -166,11 +166,11 @@ impl Profiles {
#[tracing::instrument]
pub async fn init(
dirs: &DirectoryInfo,
io_sempahore: &Semaphore,
io_sempahore: &RwLock<Semaphore>,
) -> crate::Result<Self> {
let mut profiles = HashMap::new();
fs::create_dir_all(dirs.profiles_dir()).await?;
let mut entries = fs::read_dir(dirs.profiles_dir()).await?;
while let Some(entry) = entries.next_entry().await? {
let path = entry.path();
if path.is_dir() {

View File

@@ -10,7 +10,7 @@ use sha2::Digest;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::io::AsyncReadExt;
use tokio::sync::Semaphore;
use tokio::sync::{RwLock, Semaphore};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Project {
@@ -160,7 +160,7 @@ async fn read_icon_from_file(
icon_path: Option<String>,
cache_dir: &Path,
path: &PathBuf,
io_semaphore: &Semaphore,
io_semaphore: &RwLock<Semaphore>,
) -> crate::Result<Option<PathBuf>> {
if let Some(icon_path) = icon_path {
// we have to repoen the zip twice here :(
@@ -208,7 +208,7 @@ async fn read_icon_from_file(
pub async fn infer_data_from_files(
paths: Vec<PathBuf>,
cache_dir: PathBuf,
io_semaphore: &Semaphore,
io_semaphore: &RwLock<Semaphore>,
) -> crate::Result<HashMap<PathBuf, Project>> {
let mut file_path_hashes = HashMap::new();