use crate::{
CentralDirectoryEntry, Error, HostSystem, constants::version, invalid, record::array_at,
wire::wire_enum,
};
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum EntryKind {
File,
Directory,
SymbolicLink {
unix_target: Option<String>,
},
CharacterDevice {
numbers: Option<DeviceNumbers>,
},
BlockDevice {
numbers: Option<DeviceNumbers>,
},
Fifo,
Socket,
VolumeLabel,
Unknown {
mode: u16,
},
}
impl EntryKind {
pub(crate) fn resolve(
directory: &CentralDirectoryEntry,
unix_data: Option<Vec<u8>>,
attributes: &ExternalAttributes,
) -> Result<Self, Error> {
let position = directory.position();
let has_directory_suffix = directory.path().ends_with('/');
let file_type = match (
UnixFileType::from_mode(attributes.unix_mode),
has_directory_suffix || attributes.dos_directory,
) {
(UnixFileType::Unspecified | UnixFileType::Directory, true) => UnixFileType::Directory,
(file_type, false) => file_type,
_ => return Err(invalid(position, "inconsistent file attributes")),
};
if attributes.dos_volume_label && file_type != UnixFileType::Unspecified {
return Err(invalid(position, "inconsistent file attributes"));
}
if file_type == UnixFileType::Directory && !has_directory_suffix {
return Err(invalid(position, "directory filename must end with '/'"));
}
if (attributes.dos_volume_label
|| matches!(
file_type,
UnixFileType::Directory
| UnixFileType::CharacterDevice
| UnixFileType::BlockDevice
| UnixFileType::Fifo
| UnixFileType::Socket
))
&& directory.size() != 0
{
return Err(invalid(position, "non-file member has payload data"));
}
if file_type == UnixFileType::SymbolicLink && directory.size() == 0 && unix_data.is_none() {
return Err(invalid(position, "empty symbolic-link target"));
}
let has_type_data = unix_data.as_ref().is_some_and(|data| !data.is_empty());
Ok(match file_type {
UnixFileType::SymbolicLink => Self::SymbolicLink {
unix_target: unix_data
.map(|data| parse_link_target(data, position))
.transpose()?,
},
UnixFileType::CharacterDevice => Self::CharacterDevice {
numbers: DeviceNumbers::parse(unix_data, position)?,
},
UnixFileType::BlockDevice => Self::BlockDevice {
numbers: DeviceNumbers::parse(unix_data, position)?,
},
UnixFileType::Unspecified | UnixFileType::Regular
if has_type_data && !attributes.dos_volume_label =>
{
return Err(Error::Unsupported {
position,
feature: "ambiguous UNIX hard link",
});
}
_ if has_type_data => return Err(invalid(position, "unexpected UNIX file-type data")),
UnixFileType::Unspecified if attributes.dos_volume_label => Self::VolumeLabel,
UnixFileType::Unspecified | UnixFileType::Regular => Self::File,
UnixFileType::Directory => Self::Directory,
UnixFileType::Fifo => Self::Fifo,
UnixFileType::Socket => Self::Socket,
UnixFileType::Unknown(mode) => Self::Unknown { mode },
})
}
pub(crate) fn minimum_version(&self) -> u16 {
match self {
Self::VolumeLabel => version::V1_1,
Self::Directory => version::V2_0,
_ => version::BASE,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeviceNumbers {
pub major: u32,
pub minor: u32,
}
impl DeviceNumbers {
fn parse(data: Option<Vec<u8>>, position: u64) -> Result<Option<Self>, Error> {
let Some(data) = data else {
return Ok(None);
};
let bytes: [u8; 8] = data
.as_slice()
.try_into()
.map_err(|_| invalid(position, "invalid UNIX device numbers"))?;
Ok(Some(Self {
major: u32::from_le_bytes(array_at::<0, 4, _>(&bytes)),
minor: u32::from_le_bytes(array_at::<4, 4, _>(&bytes)),
}))
}
}
fn parse_link_target(data: Vec<u8>, position: u64) -> Result<String, Error> {
if data.is_empty() {
return Err(invalid(position, "empty symbolic-link target"));
}
let target =
String::from_utf8(data).map_err(|_| invalid(position, "non-UTF-8 UNIX link target"))?;
if target.contains('\0') {
return Err(invalid(position, "NUL in UNIX link target"));
}
Ok(target)
}
wire_enum! {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum UnixFileType(u16) {
Unspecified = 0,
Regular = 0o100000,
Directory = 0o040000,
SymbolicLink = 0o120000,
CharacterDevice = 0o020000,
BlockDevice = 0o060000,
Fifo = 0o010000,
Socket = 0o140000,
}
}
impl UnixFileType {
pub(crate) fn from_mode(mode: u16) -> Self {
Self::from(mode & 0o170000)
}
}
pub(crate) struct ExternalAttributes {
pub(crate) unix_mode: u16,
dos_directory: bool,
dos_volume_label: bool,
}
impl ExternalAttributes {
const DOS_VOLUME_LABEL: u32 = 0x08;
pub(crate) const DOS_DIRECTORY: u32 = 0x10;
pub(crate) const UNIX_MODE_SHIFT: u32 = 16;
pub(crate) fn new(host_system: HostSystem, raw: u32) -> Self {
let has_dos_attributes = matches!(
host_system,
HostSystem::MsDos
| HostSystem::Unix
| HostSystem::Os2Hpfs
| HostSystem::WindowsNtfs
| HostSystem::Vfat
| HostSystem::Darwin
);
Self {
unix_mode: if matches!(host_system, HostSystem::Unix | HostSystem::Darwin) {
(raw >> Self::UNIX_MODE_SHIFT) as u16
} else {
0
},
dos_directory: has_dos_attributes && raw & Self::DOS_DIRECTORY != 0,
dos_volume_label: has_dos_attributes && raw & Self::DOS_VOLUME_LABEL != 0,
}
}
}
#[cfg(test)]
mod tests {
use crate::HostSystem;
use super::ExternalAttributes;
#[test]
fn interprets_external_attributes_according_to_the_host() {
for (host, unix_mode, has_dos_attributes) in [
(HostSystem::MsDos, 0, true),
(HostSystem::Unix, 0o100755, true),
(HostSystem::Os2Hpfs, 0, true),
(HostSystem::WindowsNtfs, 0, true),
(HostSystem::Vfat, 0, true),
(HostSystem::Darwin, 0o100755, true),
(HostSystem::Amiga, 0, false),
(HostSystem::Unknown(255), 0, false),
] {
let attributes = ExternalAttributes::new(host, (0o100755 << 16) | 0x10);
assert_eq!(attributes.unix_mode, unix_mode, "host {host:?}");
assert_eq!(
attributes.dos_directory, has_dos_attributes,
"host {host:?}"
);
assert!(!attributes.dos_volume_label, "host {host:?}");
let attributes = ExternalAttributes::new(host, 0x08);
assert_eq!(attributes.unix_mode, 0, "host {host:?}");
assert!(!attributes.dos_directory, "host {host:?}");
assert_eq!(
attributes.dos_volume_label, has_dos_attributes,
"host {host:?}"
);
}
}
}