use alloc::vec::Vec;
use core::fmt;
use core::ops::{BitOr, BitOrAssign};
pub const HEADER_SIZE: usize = 8;
pub const SYNC_DATA_MAX: usize = 64 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SyncId {
Stat,
Sta2,
List,
Lis2,
Send,
Snd2,
Recv,
Rcv2,
Data,
Done,
Okay,
Fail,
Dent,
Dnt2,
Quit,
}
impl SyncId {
pub(super) const ALL: [Self; 15] = [
Self::Stat,
Self::Sta2,
Self::List,
Self::Lis2,
Self::Send,
Self::Snd2,
Self::Recv,
Self::Rcv2,
Self::Data,
Self::Done,
Self::Okay,
Self::Fail,
Self::Dent,
Self::Dnt2,
Self::Quit,
];
pub(crate) fn as_bytes(self) -> [u8; 4] {
match self {
Self::Stat => *b"STAT",
Self::Sta2 => *b"STA2",
Self::List => *b"LIST",
Self::Lis2 => *b"LIS2",
Self::Send => *b"SEND",
Self::Snd2 => *b"SND2",
Self::Recv => *b"RECV",
Self::Rcv2 => *b"RCV2",
Self::Data => *b"DATA",
Self::Done => *b"DONE",
Self::Okay => *b"OKAY",
Self::Fail => *b"FAIL",
Self::Dent => *b"DENT",
Self::Dnt2 => *b"DNT2",
Self::Quit => *b"QUIT",
}
}
pub(crate) fn from_bytes(bytes: [u8; 4]) -> Option<Self> {
Self::ALL.into_iter().find(|id| id.as_bytes() == bytes)
}
}
pub(super) const STAT_V1_SIZE: usize = 16;
pub const STAT_V2_SIZE: usize = 72;
pub const STAT_V2_BODY_SIZE: usize = STAT_V2_SIZE - 4;
pub(super) const DENT_V1_SIZE: usize = 20;
pub(super) const DENT_V2_SIZE: usize = 76;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(transparent)]
pub struct FileMode(pub u32);
impl fmt::Display for FileMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Octal::fmt(&self.0, f)
}
}
impl fmt::Debug for FileMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "FileMode({:#o})", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct StatV1 {
pub mode: FileMode,
pub size: u32,
pub mtime: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct StatV2 {
pub error: u32,
pub dev: u64,
pub ino: u64,
pub mode: FileMode,
pub nlink: u32,
pub uid: u32,
pub gid: u32,
pub size: u64,
pub atime: i64,
pub mtime: i64,
pub ctime: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirEntry {
pub name: Vec<u8>,
pub mode: FileMode,
pub size: u32,
pub mtime: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirEntryV2 {
pub name: Vec<u8>,
pub stat: StatV2,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct SyncFlags(u32);
impl SyncFlags {
pub const NONE: Self = Self(0);
pub const BROTLI: Self = Self(1);
pub const LZ4: Self = Self(2);
pub const ZSTD: Self = Self(4);
pub const DRY_RUN: Self = Self(0x8000_0000);
pub const fn bits(self) -> u32 {
self.0
}
pub(crate) const fn contains(self, flags: Self) -> bool {
self.0 & flags.0 == flags.0
}
}
impl BitOr for SyncFlags {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl BitOrAssign for SyncFlags {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
pub(super) fn u32_at(buf: &[u8], off: usize) -> u32 {
u32::from_le_bytes(buf[off..off + 4].try_into().unwrap())
}
pub(super) fn u64_at(buf: &[u8], off: usize) -> u64 {
u64::from_le_bytes(buf[off..off + 8].try_into().unwrap())
}
pub(super) fn i64_at(buf: &[u8], off: usize) -> i64 {
i64::from_le_bytes(buf[off..off + 8].try_into().unwrap())
}
pub fn parse_stat_v2_body(b: &[u8; STAT_V2_BODY_SIZE]) -> StatV2 {
StatV2 {
error: u32_at(b, 0),
dev: u64_at(b, 4),
ino: u64_at(b, 12),
mode: FileMode(u32_at(b, 20)),
nlink: u32_at(b, 24),
uid: u32_at(b, 28),
gid: u32_at(b, 32),
size: u64_at(b, 36),
atime: i64_at(b, 44),
mtime: i64_at(b, 52),
ctime: i64_at(b, 60),
}
}