Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Windows support for the lib #634

Merged
merged 17 commits into from
Nov 12, 2024
Merged
2 changes: 1 addition & 1 deletion backhand/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
//! with references back to the inodes that describe those entries.

use std::ffi::OsStr;
use std::os::unix::prelude::OsStrExt;
use std::path::{Component, Path};

use deku::prelude::*;

use crate::inode::InodeId;
use crate::unix_string::OsStrExt;
use crate::BackhandError;

#[derive(Debug, DekuRead, DekuWrite, Clone, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion backhand/src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ffi::OsStr;
use std::fmt;
use std::os::unix::prelude::OsStrExt;

use crate::data::Added;
use crate::dir::{Dir, DirEntry};
Expand All @@ -11,6 +10,7 @@ use crate::inode::{
use crate::kinds::Kind;
use crate::metadata::MetadataWriter;
use crate::squashfs::SuperBlock;
use crate::unix_string::OsStrExt;
use crate::{Id, NodeHeader, SquashfsBlockDevice, SquashfsCharacterDevice, SquashfsSymlink};

#[derive(Clone)]
Expand Down
1 change: 1 addition & 0 deletions backhand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ mod kinds;
mod metadata;
mod reader;
mod squashfs;
mod unix_string;

pub use crate::data::DataSize;
pub use crate::error::BackhandError;
Expand Down
2 changes: 1 addition & 1 deletion backhand/src/squashfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::ffi::OsString;
use std::io::{Cursor, Seek, SeekFrom};
use std::os::unix::prelude::OsStringExt;
use std::path::PathBuf;
use std::sync::Mutex;
use std::sync::{Arc, RwLock};
Expand All @@ -19,6 +18,7 @@ use crate::fragment::Fragment;
use crate::inode::{Inode, InodeId, InodeInner};
use crate::kinds::{Kind, LE_V4_0};
use crate::reader::{BufReadSeek, SquashFsReader, SquashfsReaderWithOffset};
use crate::unix_string::OsStringExt;
use crate::{
metadata, Export, FilesystemReader, Id, Node, NodeHeader, SquashfsBlockDevice,
SquashfsCharacterDevice, SquashfsDir, SquashfsFileReader, SquashfsSymlink,
Expand Down
54 changes: 54 additions & 0 deletions backhand/src/unix_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::ffi::OsStr;
use std::ffi::OsString;

#[cfg(unix)]
use std::os::unix::ffi::OsStrExt as OsStrExtUnix;

#[cfg(unix)]
use std::os::unix::ffi::OsStringExt as OsStringExtUnix;

pub trait OsStrExt {
fn as_bytes(&self) -> &[u8];
fn from_bytes(slice: &[u8]) -> &Self;
}

#[cfg(unix)]
impl OsStrExt for OsStr {
fn as_bytes(&self) -> &[u8] {
OsStrExtUnix::as_bytes(self)
}

fn from_bytes(slice: &[u8]) -> &Self {
OsStrExtUnix::from_bytes(slice)
}
}

#[cfg(windows)]
impl OsStrExt for OsStr {
fn as_bytes(&self) -> &[u8] {
self.to_str().unwrap().as_bytes()
}

fn from_bytes(slice: &[u8]) -> &Self {
let string = std::str::from_utf8(slice).unwrap();
OsStr::new(string)
}
}

pub trait OsStringExt {
fn from_vec(vec: Vec<u8>) -> Self;
}

#[cfg(unix)]
impl OsStringExt for OsString {
fn from_vec(vec: Vec<u8>) -> Self {
OsStringExtUnix::from_vec(vec)
}
}

#[cfg(windows)]
impl OsStringExt for OsString {
fn from_vec(vec: Vec<u8>) -> Self {
OsStr::from_bytes(vec.as_slice()).into()
}
}
Loading