mod helpers;
use std::path::Path;
use helpers::{InstallPackage, prepare_dir_upload, prepare_file_upload};
use crate::{
IdeviceError, IdeviceService, provider::IdeviceProvider,
services::installation_proxy::InstallationProxyClient,
};
#[cfg(feature = "rsd")]
use crate::{RsdService, provider::RsdProvider, rsd};
#[cfg(feature = "rsd")]
use helpers::{prepare_dir_upload_rsd, prepare_file_upload_rsd};
pub async fn install_package<P: AsRef<Path>>(
provider: &dyn IdeviceProvider,
local_path: P,
options: Option<plist::Value>,
) -> Result<(), IdeviceError> {
install_package_with_callback(provider, local_path, options, |_| async {}, ()).await
}
#[cfg(feature = "rsd")]
pub async fn install_package_rsd<P: AsRef<Path>>(
provider: &mut impl RsdProvider,
handshake: &mut rsd::RsdHandshake,
local_path: P,
options: Option<plist::Value>,
) -> Result<(), IdeviceError> {
install_package_with_callback_rsd(provider, handshake, local_path, options, |_| async {}, ())
.await
}
pub async fn install_package_with_callback<P: AsRef<Path>, Fut, S>(
provider: &dyn IdeviceProvider,
local_path: P,
options: Option<plist::Value>,
callback: impl Fn((u64, S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let metadata = tokio::fs::metadata(&local_path).await?;
if metadata.is_dir() {
let InstallPackage {
remote_package_path,
options,
} = prepare_dir_upload(provider, local_path, options).await?;
let mut inst = InstallationProxyClient::connect(provider).await?;
inst.upgrade_with_callback(remote_package_path, Some(options), callback, state)
.await
} else {
let data = tokio::fs::read(&local_path).await?;
install_bytes_with_callback(provider, data, options, callback, state).await
}
}
#[cfg(feature = "rsd")]
pub async fn install_package_with_callback_rsd<P: AsRef<Path>, Fut, S>(
provider: &mut impl RsdProvider,
handshake: &mut rsd::RsdHandshake,
local_path: P,
options: Option<plist::Value>,
callback: impl Fn((u64, S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let metadata = tokio::fs::metadata(&local_path).await?;
if metadata.is_dir() {
let InstallPackage {
remote_package_path,
options,
} = prepare_dir_upload_rsd(provider, handshake, local_path, options).await?;
let mut inst = InstallationProxyClient::connect_rsd(provider, handshake).await?;
inst.upgrade_with_callback(remote_package_path, Some(options), callback, state)
.await
} else {
let data = tokio::fs::read(&local_path).await?;
install_bytes_with_callback_rsd(provider, handshake, data, options, callback, state).await
}
}
pub async fn upgrade_package<P: AsRef<Path>>(
provider: &dyn IdeviceProvider,
local_path: P,
options: Option<plist::Value>,
) -> Result<(), IdeviceError> {
upgrade_package_with_callback(provider, local_path, options, |_| async {}, ()).await
}
#[cfg(feature = "rsd")]
pub async fn upgrade_package_rsd<P: AsRef<Path>>(
provider: &mut impl RsdProvider,
handshake: &mut rsd::RsdHandshake,
local_path: P,
options: Option<plist::Value>,
) -> Result<(), IdeviceError> {
upgrade_package_with_callback_rsd(provider, handshake, local_path, options, |_| async {}, ())
.await
}
pub async fn upgrade_package_with_callback<P: AsRef<Path>, Fut, S>(
provider: &dyn IdeviceProvider,
local_path: P,
options: Option<plist::Value>,
callback: impl Fn((u64, S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let metadata = tokio::fs::metadata(&local_path).await?;
if metadata.is_dir() {
let InstallPackage {
remote_package_path,
options,
} = prepare_dir_upload(provider, local_path, options).await?;
let mut inst = InstallationProxyClient::connect(provider).await?;
inst.upgrade_with_callback(remote_package_path, Some(options), callback, state)
.await
} else {
let data = tokio::fs::read(&local_path).await?;
upgrade_bytes_with_callback(provider, data, options, callback, state).await
}
}
#[cfg(feature = "rsd")]
pub async fn upgrade_package_with_callback_rsd<P: AsRef<Path>, Fut, S>(
provider: &mut impl RsdProvider,
handshake: &mut rsd::RsdHandshake,
local_path: P,
options: Option<plist::Value>,
callback: impl Fn((u64, S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let metadata = tokio::fs::metadata(&local_path).await?;
if metadata.is_dir() {
let InstallPackage {
remote_package_path,
options,
} = prepare_dir_upload_rsd(provider, handshake, local_path, options).await?;
let mut inst = InstallationProxyClient::connect_rsd(provider, handshake).await?;
inst.upgrade_with_callback(remote_package_path, Some(options), callback, state)
.await
} else {
let data = tokio::fs::read(&local_path).await?;
upgrade_bytes_with_callback_rsd(provider, handshake, data, options, callback, state).await
}
}
pub async fn install_bytes(
provider: &dyn IdeviceProvider,
data: impl AsRef<[u8]>,
options: Option<plist::Value>,
) -> Result<(), IdeviceError> {
install_bytes_with_callback(provider, data, options, |_| async {}, ()).await
}
#[cfg(feature = "rsd")]
pub async fn install_bytes_rsd(
provider: &mut impl RsdProvider,
handshake: &mut rsd::RsdHandshake,
data: impl AsRef<[u8]>,
options: Option<plist::Value>,
) -> Result<(), IdeviceError> {
install_bytes_with_callback_rsd(provider, handshake, data, options, |_| async {}, ()).await
}
pub async fn install_bytes_with_callback<Fut, S>(
provider: &dyn IdeviceProvider,
data: impl AsRef<[u8]>,
options: Option<plist::Value>,
callback: impl Fn((u64, S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let InstallPackage {
remote_package_path,
options,
} = prepare_file_upload(provider, data, options).await?;
let mut inst = InstallationProxyClient::connect(provider).await?;
inst.install_with_callback(remote_package_path, Some(options), callback, state)
.await
}
#[cfg(feature = "rsd")]
pub async fn install_bytes_with_callback_rsd<Fut, S>(
provider: &mut impl RsdProvider,
handshake: &mut rsd::RsdHandshake,
data: impl AsRef<[u8]>,
options: Option<plist::Value>,
callback: impl Fn((u64, S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let InstallPackage {
remote_package_path,
options,
} = prepare_file_upload_rsd(provider, handshake, data, options).await?;
let mut inst = InstallationProxyClient::connect_rsd(provider, handshake).await?;
inst.install_with_callback(remote_package_path, Some(options), callback, state)
.await
}
pub async fn upgrade_bytes(
provider: &dyn IdeviceProvider,
data: impl AsRef<[u8]>,
options: Option<plist::Value>,
) -> Result<(), IdeviceError> {
upgrade_bytes_with_callback(provider, data, options, |_| async {}, ()).await
}
#[cfg(feature = "rsd")]
pub async fn upgrade_bytes_rsd(
provider: &mut impl RsdProvider,
handshake: &mut rsd::RsdHandshake,
data: impl AsRef<[u8]>,
options: Option<plist::Value>,
) -> Result<(), IdeviceError> {
upgrade_bytes_with_callback_rsd(provider, handshake, data, options, |_| async {}, ()).await
}
pub async fn upgrade_bytes_with_callback<Fut, S>(
provider: &dyn IdeviceProvider,
data: impl AsRef<[u8]>,
options: Option<plist::Value>,
callback: impl Fn((u64, S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let InstallPackage {
remote_package_path,
options,
} = prepare_file_upload(provider, data, options).await?;
let mut inst = InstallationProxyClient::connect(provider).await?;
inst.upgrade_with_callback(remote_package_path, Some(options), callback, state)
.await
}
#[cfg(feature = "rsd")]
pub async fn upgrade_bytes_with_callback_rsd<Fut, S>(
provider: &mut impl RsdProvider,
handshake: &mut rsd::RsdHandshake,
data: impl AsRef<[u8]>,
options: Option<plist::Value>,
callback: impl Fn((u64, S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let InstallPackage {
remote_package_path,
options,
} = prepare_file_upload_rsd(provider, handshake, data, options).await?;
let mut inst = InstallationProxyClient::connect_rsd(provider, handshake).await?;
inst.upgrade_with_callback(remote_package_path, Some(options), callback, state)
.await
}