use std::io;
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use ostrya_core::{Checksum, FileHeader, ObjectType, RepoMode, Xattrs, loose_path};
use ostrya_rt::File as RtFile;
use rustix::fs::{AtFlags, FileType, Mode, OFlags, Stat};
use rustix::io::Errno;
use crate::error::{Error, Result};
use crate::inflate::{ArchiveDecoder, archive_decoder};
use crate::object::{self, MAX_FILE_HEADER_SIZE, MAX_METADATA_SIZE};
use crate::repo::Repo;
use crate::write::FileMeta;
const SYMLINK_READ_CAP: u64 = 64 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FileKind {
Regular {
size: u64,
},
Symlink {
target: String,
},
}
#[derive(Debug, Clone)]
enum ReaderSource {
None,
Plain,
Archive { payload_offset: u64 },
}
#[derive(Debug, Clone)]
enum ObjectStore {
Repo,
Staging {
dir: Arc<OwnedFd>,
name: String,
},
}
#[derive(Debug, Clone)]
pub struct FileObject {
repo: Repo,
checksum: Checksum,
pub uid: u32,
pub gid: u32,
pub mode: u32,
pub xattrs: Xattrs,
pub kind: FileKind,
source: ReaderSource,
store: ObjectStore,
}
impl FileObject {
pub fn checksum(&self) -> &Checksum {
&self.checksum
}
pub fn is_symlink(&self) -> bool {
matches!(self.kind, FileKind::Symlink { .. })
}
pub fn header(&self) -> FileHeader {
FileHeader {
uid: self.uid,
gid: self.gid,
mode: self.mode,
symlink_target: match &self.kind {
FileKind::Symlink { target } => target.clone(),
FileKind::Regular { .. } => String::new(),
},
xattrs: self.xattrs.clone(),
}
}
pub(crate) fn meta(&self) -> FileMeta {
FileMeta {
uid: self.uid,
gid: self.gid,
mode: self.mode,
xattrs: self.xattrs.clone(),
}
}
pub async fn reader(&self) -> Result<ContentReader> {
let inner = match &self.source {
ReaderSource::None => ContentReaderInner::Empty,
ReaderSource::Plain => ContentReaderInner::Plain(self.open_payload(0).await?),
ReaderSource::Archive { payload_offset } => {
let file = self.open_payload(*payload_offset).await?;
ContentReaderInner::Inflate(Box::new(archive_decoder(file)))
}
};
Ok(ContentReader { inner })
}
pub async fn write_to<W: futures_io::AsyncWrite + Unpin>(&self, writer: &mut W) -> Result<()> {
let reader = self.reader().await?;
crate::write::copy_stream(reader, writer)
.await
.map_err(Error::Io)
}
fn payload_location(&self) -> Result<(OwnedFd, String)> {
match &self.store {
ObjectStore::Repo => {
let path = loose_path(&self.checksum, ObjectType::File, self.repo.mode());
Ok((self.repo.objects_fd().try_clone_to_owned()?, path))
}
ObjectStore::Staging { dir, name } => {
Ok((dir.as_fd().try_clone_to_owned()?, name.clone()))
}
}
}
async fn open_payload(&self, payload_offset: u64) -> Result<RtFile> {
let (dir, path) = self.payload_location()?;
let file = ostrya_rt::unblock(move || {
object::open_content_file(dir.as_fd(), &path, payload_offset)
})
.await
.map_err(Error::Io)?;
Ok(RtFile::from(file))
}
}
impl Repo {
pub async fn load_file(&self, checksum: &Checksum) -> Result<FileObject> {
let mode = self.mode();
let path = loose_path(checksum, ObjectType::File, mode);
let repo = self.clone();
let key = *checksum;
let loaded =
ostrya_rt::unblock(move || load_by_mode(repo.objects_fd(), &path, &key, mode)).await?;
Ok(FileObject {
repo: self.clone(),
checksum: *checksum,
uid: loaded.uid,
gid: loaded.gid,
mode: loaded.mode,
xattrs: loaded.xattrs,
kind: loaded.kind,
source: loaded.source,
store: ObjectStore::Repo,
})
}
}
pub(crate) async fn load_staged_file(
repo: &Repo,
staging_fd: BorrowedFd<'_>,
checksum: &Checksum,
) -> Result<FileObject> {
let mode = repo.mode();
let name = crate::write::flat_name(checksum, ObjectType::File, mode);
let dir = Arc::new(staging_fd.try_clone_to_owned()?);
let key = *checksum;
let load_dir = dir.clone();
let load_name = name.clone();
let loaded =
ostrya_rt::unblock(move || load_by_mode(load_dir.as_fd(), &load_name, &key, mode)).await?;
Ok(FileObject {
repo: repo.clone(),
checksum: *checksum,
uid: loaded.uid,
gid: loaded.gid,
mode: loaded.mode,
xattrs: loaded.xattrs,
kind: loaded.kind,
source: loaded.source,
store: ObjectStore::Staging { dir, name },
})
}
struct Loaded {
uid: u32,
gid: u32,
mode: u32,
xattrs: Xattrs,
kind: FileKind,
source: ReaderSource,
}
fn load_by_mode(
dir_fd: BorrowedFd<'_>,
object_path: &str,
checksum: &Checksum,
mode: RepoMode,
) -> Result<Loaded> {
match mode {
RepoMode::Archive => load_archive(dir_fd, object_path, checksum),
RepoMode::BareUser | RepoMode::BareUserShared => {
load_bare_user(dir_fd, object_path, checksum)
}
RepoMode::Bare => load_bare(dir_fd, object_path, checksum),
RepoMode::BareUserOnly => load_bare_user_only(dir_fd, object_path, checksum),
RepoMode::BareSplitXattrs => load_bare_split_xattrs(dir_fd, object_path, checksum),
}
}
fn map_object_error(err: Errno, checksum: &Checksum, ty: ObjectType) -> Error {
if err == Errno::NOENT {
Error::ObjectNotFound {
checksum: *checksum,
ty,
}
} else {
Error::Io(err.into())
}
}
fn open_object(objects_fd: BorrowedFd<'_>, path: &str, checksum: &Checksum) -> Result<OwnedFd> {
rustix::fs::openat(
objects_fd,
path,
OFlags::RDONLY | OFlags::CLOEXEC,
Mode::empty(),
)
.map_err(|e| map_object_error(e, checksum, ObjectType::File))
}
fn stat_object(
objects_fd: BorrowedFd<'_>,
path: &str,
checksum: &Checksum,
ty: ObjectType,
) -> Result<Stat> {
rustix::fs::statat(objects_fd, path, AtFlags::SYMLINK_NOFOLLOW)
.map_err(|e| map_object_error(e, checksum, ty))
}
fn read_link_target(objects_fd: BorrowedFd<'_>, path: &str) -> Result<String> {
let link = rustix::fs::readlinkat(objects_fd, path, Vec::new())?;
link.into_string()
.map_err(|_| Error::InvalidFormat("symlink target is not valid UTF-8".into()))
}
fn symlink_target_from_content(content: &[u8]) -> Result<String> {
let bytes = content.strip_suffix(&[0]).unwrap_or(content);
if bytes.contains(&0) {
return Err(Error::InvalidFormat(
"symlink target has an interior NUL".into(),
));
}
std::str::from_utf8(bytes)
.map(str::to_owned)
.map_err(|_| Error::InvalidFormat("symlink target is not valid UTF-8".into()))
}
fn load_archive(dir_fd: BorrowedFd<'_>, path: &str, checksum: &Checksum) -> Result<Loaded> {
use std::io::Read;
let fd = open_object(dir_fd, path, checksum)?;
let mut file = std::fs::File::from(fd);
let mut prefix = [0u8; 8];
file.read_exact(&mut prefix)?;
if prefix[4..8] != [0u8; 4] {
return Err(Error::InvalidFormat(
"content framing padding is not zero".into(),
));
}
let header_len = u32::from_be_bytes(prefix[..4].try_into().unwrap()) as u64;
if header_len > MAX_FILE_HEADER_SIZE {
return Err(Error::InvalidFormat(
"content header exceeds the size cap".into(),
));
}
let mut header_bytes = vec![0u8; header_len as usize];
file.read_exact(&mut header_bytes)?;
let (header, uncompressed_size) = FileHeader::parse_archive(&header_bytes)?;
let (kind, source) = if header.is_symlink() {
(
FileKind::Symlink {
target: header.symlink_target,
},
ReaderSource::None,
)
} else {
(
FileKind::Regular {
size: uncompressed_size,
},
ReaderSource::Archive {
payload_offset: 8 + header_len,
},
)
};
Ok(Loaded {
uid: header.uid,
gid: header.gid,
mode: header.mode,
xattrs: header.xattrs,
kind,
source,
})
}
fn load_bare_user(dir_fd: BorrowedFd<'_>, path: &str, checksum: &Checksum) -> Result<Loaded> {
use std::io::Read;
let fd = open_object(dir_fd, path, checksum)?;
let meta = object::read_xattr(fd.as_fd(), "user.ostreemeta")
.map_err(Error::Io)?
.ok_or_else(|| {
Error::InvalidFormat("bare-user .file is missing its user.ostreemeta xattr".into())
})?;
let header = FileHeader::parse_stat_metadata(&meta)?;
let stat = rustix::fs::fstat(&fd)?;
let (kind, source) = if header.is_symlink() {
let mut content = Vec::new();
std::fs::File::from(fd)
.take(SYMLINK_READ_CAP)
.read_to_end(&mut content)?;
(
FileKind::Symlink {
target: symlink_target_from_content(&content)?,
},
ReaderSource::None,
)
} else {
(
FileKind::Regular {
size: stat.st_size.max(0) as u64,
},
ReaderSource::Plain,
)
};
Ok(Loaded {
uid: header.uid,
gid: header.gid,
mode: header.mode,
xattrs: header.xattrs,
kind,
source,
})
}
fn load_bare(dir_fd: BorrowedFd<'_>, path: &str, checksum: &Checksum) -> Result<Loaded> {
let stat = stat_object(dir_fd, path, checksum, ObjectType::File)?;
let uid = stat.st_uid;
let gid = stat.st_gid;
let mode = stat.st_mode;
match FileType::from_raw_mode(stat.st_mode) {
FileType::Symlink => Ok(Loaded {
uid,
gid,
mode,
xattrs: object::read_link_xattrs(dir_fd, path)?,
kind: FileKind::Symlink {
target: read_link_target(dir_fd, path)?,
},
source: ReaderSource::None,
}),
FileType::RegularFile => {
let fd = rustix::fs::openat(
dir_fd,
path,
OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)
.map_err(|e| map_object_error(e, checksum, ObjectType::File))?;
let xattrs = object::read_all_xattrs(fd.as_fd())?;
Ok(Loaded {
uid,
gid,
mode,
xattrs,
kind: FileKind::Regular {
size: stat.st_size.max(0) as u64,
},
source: ReaderSource::Plain,
})
}
_ => Err(Error::InvalidFormat(
"bare object is neither a regular file nor a symlink".into(),
)),
}
}
fn load_bare_user_only(dir_fd: BorrowedFd<'_>, path: &str, checksum: &Checksum) -> Result<Loaded> {
let stat = stat_object(dir_fd, path, checksum, ObjectType::File)?;
let mode = stat.st_mode;
match FileType::from_raw_mode(stat.st_mode) {
FileType::Symlink => Ok(Loaded {
uid: 0,
gid: 0,
mode,
xattrs: Xattrs::empty(),
kind: FileKind::Symlink {
target: read_link_target(dir_fd, path)?,
},
source: ReaderSource::None,
}),
FileType::RegularFile => Ok(Loaded {
uid: 0,
gid: 0,
mode,
xattrs: Xattrs::empty(),
kind: FileKind::Regular {
size: stat.st_size.max(0) as u64,
},
source: ReaderSource::Plain,
}),
_ => Err(Error::InvalidFormat(
"bare-user-only object is neither a regular file nor a symlink".into(),
)),
}
}
fn load_bare_split_xattrs(
dir_fd: BorrowedFd<'_>,
path: &str,
checksum: &Checksum,
) -> Result<Loaded> {
let stat = stat_object(dir_fd, path, checksum, ObjectType::File)?;
let uid = stat.st_uid;
let gid = stat.st_gid;
let mode = stat.st_mode;
let xattrs = load_split_xattrs(dir_fd, checksum)?;
match FileType::from_raw_mode(stat.st_mode) {
FileType::Symlink => Ok(Loaded {
uid,
gid,
mode,
xattrs,
kind: FileKind::Symlink {
target: read_link_target(dir_fd, path)?,
},
source: ReaderSource::None,
}),
FileType::RegularFile => Ok(Loaded {
uid,
gid,
mode,
xattrs,
kind: FileKind::Regular {
size: stat.st_size.max(0) as u64,
},
source: ReaderSource::Plain,
}),
_ => Err(Error::InvalidFormat(
"bare-split-xattrs object is neither a regular file nor a symlink".into(),
)),
}
}
fn load_split_xattrs(objects_fd: BorrowedFd<'_>, checksum: &Checksum) -> Result<Xattrs> {
let path = loose_path(
checksum,
ObjectType::FileXattrsLink,
RepoMode::BareSplitXattrs,
);
let bytes = object::read_meta_object(objects_fd, &path, MAX_METADATA_SIZE).map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
Error::InvalidFormat("bare-split-xattrs .file is missing its .file-xattrs-link".into())
} else {
Error::Io(e)
}
})?;
Ok(Xattrs::from_gvariant(&bytes)?)
}
pub struct ContentReader {
inner: ContentReaderInner,
}
enum ContentReaderInner {
Empty,
Plain(RtFile),
Inflate(Box<ArchiveDecoder>),
}
impl ContentReader {
fn poll_read_bytes(&mut self, cx: &mut Context<'_>, out: &mut [u8]) -> Poll<io::Result<usize>> {
use futures_io::AsyncRead;
match &mut self.inner {
ContentReaderInner::Empty => Poll::Ready(Ok(0)),
ContentReaderInner::Plain(inner) => Pin::new(inner).poll_read(cx, out),
ContentReaderInner::Inflate(inner) => Pin::new(&mut **inner).poll_read(cx, out),
}
}
}
impl futures_io::AsyncRead for ContentReader {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
self.get_mut().poll_read_bytes(cx, buf)
}
}
#[cfg(feature = "tokio")]
impl ostrya_rt::tokio_io::AsyncRead for ContentReader {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ostrya_rt::tokio_io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let out = buf.initialize_unfilled();
match self.get_mut().poll_read_bytes(cx, out) {
Poll::Ready(Ok(n)) => {
buf.advance(n);
Poll::Ready(Ok(()))
}
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
Poll::Pending => Poll::Pending,
}
}
}
const _: fn() = || {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<FileObject>();
assert_send_sync::<ContentReader>();
};
#[cfg(feature = "tokio")]
const _: fn() = || {
fn assert_tokio_read<T: ostrya_rt::tokio_io::AsyncRead>() {}
assert_tokio_read::<ContentReader>();
};