Skip to content

Commit

Permalink
core/cli: Make async optional (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
antangelo authored Aug 11, 2023
1 parent fc2d833 commit e37f91e
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: -p xdvdfs-cli --release
args: -p xdvdfs-cli --release --features=sync

- name: Package artifact
run: zip -j xdvdfs-linux-${{ github.sha }}.zip LICENSE target/release/xdvdfs
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: -p xdvdfs-cli --release --target=x86_64-pc-windows-gnu
args: -p xdvdfs-cli --release --target=x86_64-pc-windows-gnu --features=sync
use-cross: true

- name: Package artifact
Expand Down
4 changes: 4 additions & 0 deletions xdvdfs-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ md-5 = { version = "0.10.5", default-features = false }
futures = "0.3.28"
anyhow = "1.0.71"
env_logger = "0.10.0"
maybe-async = "0.2.7"

[features]
sync = ["xdvdfs/sync"]

[[bin]]
name = "xdvdfs"
Expand Down
2 changes: 2 additions & 0 deletions xdvdfs-cli/src/cmd_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::Path;

use maybe_async::maybe_async;
use xdvdfs::layout::{DirectoryEntryNode, VolumeDescriptor};

fn print_volume(volume: &VolumeDescriptor) {
Expand Down Expand Up @@ -48,6 +49,7 @@ fn print_dirent(dirent: &DirectoryEntryNode) -> Result<(), anyhow::Error> {
Ok(())
}

#[maybe_async(?Send)]
pub async fn cmd_info(img_path: &String, entry: Option<&String>) -> Result<(), anyhow::Error> {
let mut img = crate::cmd_read::open_image(Path::new(img_path)).await?;
let volume = xdvdfs::read::read_volume(&mut img).await?;
Expand Down
6 changes: 6 additions & 0 deletions xdvdfs-cli/src/cmd_md5.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use maybe_async::maybe_async;
use md5::{Digest, Md5};
use std::fs::File;
use xdvdfs::util;

#[maybe_async(?Send)]
async fn md5_file_dirent<E>(
img: &mut impl xdvdfs::blockdev::BlockDeviceRead<E>,
file: xdvdfs::layout::DirectoryEntryNode,
Expand All @@ -15,6 +17,7 @@ async fn md5_file_dirent<E>(
Ok(format!("{:x}", result))
}

#[maybe_async(?Send)]
async fn md5_file_tree<E>(
img: &mut impl xdvdfs::blockdev::BlockDeviceRead<E>,
tree: &Vec<(String, xdvdfs::layout::DirectoryEntryNode)>,
Expand All @@ -36,6 +39,7 @@ async fn md5_file_tree<E>(
Ok(())
}

#[maybe_async(?Send)]
async fn md5_from_file_path<E>(
volume: &xdvdfs::layout::VolumeDescriptor,
img: &mut impl xdvdfs::blockdev::BlockDeviceRead<E>,
Expand All @@ -53,6 +57,7 @@ async fn md5_from_file_path<E>(
Ok(())
}

#[maybe_async(?Send)]
async fn md5_from_root_tree<E>(
volume: &xdvdfs::layout::VolumeDescriptor,
img: &mut impl xdvdfs::blockdev::BlockDeviceRead<E>,
Expand All @@ -61,6 +66,7 @@ async fn md5_from_root_tree<E>(
md5_file_tree(img, &tree, "").await
}

#[maybe_async(?Send)]
pub async fn cmd_md5(img_path: &str, path: Option<&str>) -> Result<(), anyhow::Error> {
let mut img = File::options().read(true).open(img_path)?;
let volume = xdvdfs::read::read_volume(&mut img).await?;
Expand Down
2 changes: 2 additions & 0 deletions xdvdfs-cli/src/cmd_pack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};

use maybe_async::maybe_async;
use xdvdfs::write::{self, img::ProgressInfo};

fn get_default_image_path(source_path: &Path) -> Option<PathBuf> {
Expand All @@ -13,6 +14,7 @@ fn get_default_image_path(source_path: &Path) -> Option<PathBuf> {
Some(output)
}

#[maybe_async(?Send)]
pub async fn cmd_pack(
source_path: &String,
image_path: &Option<String>,
Expand Down
6 changes: 6 additions & 0 deletions xdvdfs-cli/src/cmd_read.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use maybe_async::maybe_async;
use std::{
fs::File,
io::{BufReader, Read, Write},
Expand All @@ -6,6 +7,8 @@ use std::{
};
use xdvdfs::blockdev::OffsetWrapper;

#[maybe_async(?Send)]
#[maybe_async(?Send)]
pub async fn open_image(
path: &Path,
) -> Result<OffsetWrapper<BufReader<File>, std::io::Error>, anyhow::Error> {
Expand All @@ -14,6 +17,7 @@ pub async fn open_image(
Ok(xdvdfs::blockdev::OffsetWrapper::new(img).await?)
}

#[maybe_async(?Send)]
pub async fn cmd_ls(img_path: &str, dir_path: &str) -> Result<(), anyhow::Error> {
let mut img = open_image(Path::new(img_path)).await?;
let volume = xdvdfs::read::read_volume(&mut img).await?;
Expand Down Expand Up @@ -41,6 +45,7 @@ pub async fn cmd_ls(img_path: &str, dir_path: &str) -> Result<(), anyhow::Error>
Ok(())
}

#[maybe_async(?Send)]
pub async fn cmd_tree(img_path: &str) -> Result<(), anyhow::Error> {
let mut img = open_image(Path::new(img_path)).await?;
let volume = xdvdfs::read::read_volume(&mut img).await?;
Expand Down Expand Up @@ -68,6 +73,7 @@ pub async fn cmd_tree(img_path: &str) -> Result<(), anyhow::Error> {
Ok(())
}

#[maybe_async(?Send)]
pub async fn cmd_unpack(img_path: &str, target_dir: &Option<String>) -> Result<(), anyhow::Error> {
let target_dir = match target_dir {
Some(path) => PathBuf::from_str(path).unwrap(),
Expand Down
15 changes: 13 additions & 2 deletions xdvdfs-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{Parser, Subcommand};
use maybe_async::maybe_async;

mod cmd_info;
mod cmd_md5;
Expand Down Expand Up @@ -67,6 +68,7 @@ enum Cmd {
},
}

#[maybe_async]
async fn run_command(cmd: &Cmd) -> Result<(), anyhow::Error> {
use Cmd::*;
match cmd {
Expand All @@ -85,13 +87,22 @@ async fn run_command(cmd: &Cmd) -> Result<(), anyhow::Error> {
}
}

#[cfg(feature = "sync")]
fn run_program(cmd: &Cmd) -> anyhow::Result<()> {
run_command(&cmd)
}

#[cfg(not(feature = "sync"))]
fn run_program(cmd: &Cmd) -> anyhow::Result<()> {
futures::executor::block_on(run_command(cmd))
}

fn main() {
use futures::executor::block_on;
env_logger::init();

let cli = Args::parse();
if let Some(cmd) = cli.command {
let res = block_on(run_command(&cmd));
let res = run_program(&cmd);
if let Err(err) = res {
eprintln!("Error: {}", err);
std::process::exit(1);
Expand Down
2 changes: 2 additions & 0 deletions xdvdfs-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ async-trait = { version = "0.1.68" }
encoding_rs = "0.8.32"
log = { version = "0.4.17", optional = true }
rustversion = "1.0.14"
maybe-async = "0.2.7"

[features]
default = ["std", "read", "write", "logging"]
std = ["serde/std", "itertools/use_std", "arrayvec/std"]
read = []
write = ["std"]
logging = ["log"]
sync = ["maybe-async/is_sync"]

[lib]

Expand Down
23 changes: 14 additions & 9 deletions xdvdfs-core/src/blockdev.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use maybe_async::maybe_async;

#[cfg(not(feature = "sync"))]
use alloc::boxed::Box;
use async_trait::async_trait;

const XDVD_OFFSETS: &[u64] = &[0, 387 * 1024 * 1024];

/// Trait for read operations on some block device containing an XDVDFS filesystem
/// Calls to `read` will always be thread safe (that is, no two calls to `read` will
/// be made on the same blockdevice at the same time)
#[cfg(feature = "read")]
#[async_trait(?Send)]
//#[async_trait(?Send)]
#[maybe_async(?Send)]
pub trait BlockDeviceRead<E> {
async fn read(&mut self, offset: u64, buffer: &mut [u8]) -> Result<(), E>;
}
Expand All @@ -16,7 +19,8 @@ pub trait BlockDeviceRead<E> {
/// Calls to trait methods will always be thread safe (that is, no two calls within the trait will
/// be made on the same blockdevice at the same time)
#[cfg(feature = "write")]
#[async_trait(?Send)]
//#[async_trait(?Send)]
#[maybe_async(?Send)]
pub trait BlockDeviceWrite<E> {
async fn write(&mut self, offset: u64, buffer: &[u8]) -> Result<(), E>;
async fn len(&mut self) -> Result<u64, E>;
Expand All @@ -29,7 +33,7 @@ pub trait BlockDeviceWrite<E> {
#[derive(Copy, Clone, Debug)]
pub struct OutOfBounds;

#[async_trait(?Send)]
#[maybe_async(?Send)]
impl<T: AsRef<[u8]>> BlockDeviceRead<OutOfBounds> for T {
async fn read(&mut self, offset: u64, buffer: &mut [u8]) -> Result<(), OutOfBounds> {
let offset = offset as usize;
Expand Down Expand Up @@ -57,6 +61,7 @@ impl<T, E> OffsetWrapper<T, E>
where
T: BlockDeviceRead<E> + Sized,
{
#[maybe_async(?Send)]
pub async fn new(dev: T) -> Result<Self, crate::util::Error<E>> {
let mut s = Self {
inner: dev,
Expand Down Expand Up @@ -85,7 +90,7 @@ where
}
}

#[async_trait(?Send)]
#[maybe_async(?Send)]
impl<T, E> BlockDeviceRead<E> for OffsetWrapper<T, E>
where
T: BlockDeviceRead<E>,
Expand All @@ -96,7 +101,7 @@ where
}

#[cfg(feature = "write")]
#[async_trait(?Send)]
#[maybe_async(?Send)]
impl<T, E> BlockDeviceWrite<E> for OffsetWrapper<T, E>
where
T: BlockDeviceRead<E> + BlockDeviceWrite<E>,
Expand All @@ -111,7 +116,7 @@ where
}

#[cfg(all(feature = "std", feature = "read"))]
#[async_trait(?Send)]
#[maybe_async(?Send)]
impl<R> BlockDeviceRead<std::io::Error> for R
where
R: std::io::Read + std::io::Seek,
Expand All @@ -125,7 +130,7 @@ where
}

#[cfg(all(feature = "std", feature = "write"))]
#[async_trait(?Send)]
#[maybe_async(?Send)]
impl BlockDeviceWrite<std::io::Error> for std::fs::File {
async fn write(&mut self, offset: u64, buffer: &[u8]) -> Result<(), std::io::Error> {
use std::io::Seek;
Expand All @@ -141,7 +146,7 @@ impl BlockDeviceWrite<std::io::Error> for std::fs::File {
}

#[cfg(all(feature = "std", feature = "write"))]
#[async_trait(?Send)]
#[maybe_async(?Send)]
impl BlockDeviceWrite<std::io::Error> for std::io::BufWriter<std::fs::File> {
async fn write(&mut self, offset: u64, buffer: &[u8]) -> Result<(), std::io::Error> {
use std::io::Seek;
Expand Down
4 changes: 4 additions & 0 deletions xdvdfs-core/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use proc_bitfield::bitfield;
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;

use maybe_async::maybe_async;

pub const SECTOR_SIZE: u64 = 2048;
pub const VOLUME_HEADER_MAGIC: [u8; 0x14] = *b"MICROSOFT*XBOX*MEDIA";

Expand Down Expand Up @@ -227,6 +229,7 @@ impl DirectoryEntryDiskData {
}

#[cfg(feature = "read")]
#[maybe_async(?Send)]
pub async fn read_data<E>(
&self,
dev: &mut impl super::blockdev::BlockDeviceRead<E>,
Expand All @@ -244,6 +247,7 @@ impl DirectoryEntryDiskData {
}

#[cfg(feature = "read")]
#[maybe_async(?Send)]
pub async fn read_data_all<E>(
&self,
dev: &mut impl super::blockdev::BlockDeviceRead<E>,
Expand Down
8 changes: 8 additions & 0 deletions xdvdfs-core/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use super::layout::{
self, DirectoryEntryDiskNode, DirectoryEntryNode, DirectoryEntryTable, VolumeDescriptor,
};
use super::util;
use maybe_async::maybe_async;

/// Read the XDVDFS volume descriptor from sector 32 of the drive
/// Returns None if the volume descriptor is invalid
#[maybe_async(?Send)]
pub async fn read_volume<E>(
dev: &mut impl BlockDeviceRead<E>,
) -> Result<VolumeDescriptor, util::Error<E>> {
Expand All @@ -22,6 +24,7 @@ pub async fn read_volume<E>(
}
}

#[maybe_async(?Send)]
async fn read_dirent<E>(
dev: &mut impl BlockDeviceRead<E>,
offset: u64,
Expand Down Expand Up @@ -53,6 +56,7 @@ async fn read_dirent<E>(
}

impl VolumeDescriptor {
#[maybe_async(?Send)]
pub async fn root_dirent<E>(
&self,
dev: &mut impl BlockDeviceRead<E>,
Expand All @@ -66,6 +70,7 @@ impl VolumeDescriptor {
}

impl DirectoryEntryTable {
#[maybe_async(?Send)]
async fn find_dirent<E>(
&self,
dev: &mut impl BlockDeviceRead<E>,
Expand Down Expand Up @@ -104,6 +109,7 @@ impl DirectoryEntryTable {
///
/// Returns None if the root path is provided (root has no dirent)
/// or the path does not exist.
#[maybe_async(?Send)]
pub async fn walk_path<E>(
&self,
dev: &mut impl BlockDeviceRead<E>,
Expand Down Expand Up @@ -139,6 +145,7 @@ impl DirectoryEntryTable {
// FIXME: walk_dirent_tree variant that uses dirtab as an array instead of walking the tree

/// Walks the directory entry table in preorder, returning all directory entries.
#[maybe_async(?Send)]
pub async fn walk_dirent_tree<E>(
&self,
dev: &mut impl BlockDeviceRead<E>,
Expand Down Expand Up @@ -182,6 +189,7 @@ impl DirectoryEntryTable {
Ok(dirents)
}

#[maybe_async(?Send)]
pub async fn file_tree<E>(
&self,
dev: &mut impl BlockDeviceRead<E>,
Expand Down
Loading

0 comments on commit e37f91e

Please sign in to comment.