refactor: rename FileMetadata to ipfs_unixfs::Metadata

This commit is contained in:
Joonas Koivunen 2020-06-16 19:22:58 +03:00
parent 9b18427e16
commit 948eeff48f
8 changed files with 96 additions and 95 deletions

View File

@ -11,7 +11,7 @@ use warp::{path, query, Filter, Rejection, Reply};
use bytes::{Bytes, BytesMut, buf::BufMut};
use tar::{Header, EntryType};
use futures::stream::TryStream;
use ipfs::unixfs::ll::file::FileMetadata;
use ipfs::unixfs::ll::Metadata;
use ipfs::unixfs::{ll::file::FileReadFailed, TraversalFailed};
use crate::v0::refs::{walk_path, IpfsPath};
use ipfs::unixfs::ll::walk::{self, Walker, ContinuedWalk};
@ -338,7 +338,7 @@ impl TarHelper {
long_filename_header
}
fn apply_file(&mut self, path: &Path, metadata: &FileMetadata, total_size: u64) -> Result<[Option<Bytes>; 4], GetError> {
fn apply_file(&mut self, path: &Path, metadata: &Metadata, total_size: u64) -> Result<[Option<Bytes>; 4], GetError> {
let mut ret: [Option<Bytes>; 4] = Default::default();
if let Err(e) = self.header.set_path(path) {
@ -382,7 +382,7 @@ impl TarHelper {
ret
}
fn apply_directory(&mut self, path: &Path, metadata: &FileMetadata) -> Result<[Option<Bytes>; 4], GetError> {
fn apply_directory(&mut self, path: &Path, metadata: &Metadata) -> Result<[Option<Bytes>; 4], GetError> {
let mut ret: [Option<Bytes>; 4] = Default::default();
if let Err(e) = self.header.set_path(path) {
@ -413,7 +413,7 @@ impl TarHelper {
Ok(ret)
}
fn apply_symlink(&mut self, path: &Path, target: &Path, metadata: &FileMetadata) -> Result<[Option<Bytes>; 7], GetError> {
fn apply_symlink(&mut self, path: &Path, target: &Path, metadata: &Metadata) -> Result<[Option<Bytes>; 7], GetError> {
let mut ret: [Option<Bytes>; 7] = Default::default();
if let Err(e) = self.header.set_path(path) {
@ -476,7 +476,7 @@ impl TarHelper {
}
}
fn set_metadata(header: &mut tar::Header, metadata: &FileMetadata, default_mode: u32) {
fn set_metadata(header: &mut tar::Header, metadata: &Metadata, default_mode: u32) {
header.set_mode(metadata.mode()
.map(|mode| mode & 0o7777)
.unwrap_or(default_mode));

View File

@ -88,7 +88,7 @@ fn walk(blocks: ShardedBlockStore, start: &Cid) -> Result<(u64, u64), Error> {
read_bytes += blocks.as_file(&first.to_bytes())?.read_to_end(&mut buf)? as u64;
// Similar to first step, except we no longer get the file metadata. It is still accessible
// from the `visit` via `AsRef<ipfs_unixfs::file::FileMetadata>` but likely only needed in
// from the `visit` via `AsRef<ipfs_unixfs::file::Metadata>` but likely only needed in
// the first step.
let (content, next_step) = visit.continue_walk(&buf, &mut None)?;
stdout.write_all(content)?;

View File

@ -5,7 +5,7 @@ use std::io::{Error as IoError, Read};
use std::path::{Path, PathBuf};
use std::io;
use std::borrow::Cow;
use ipfs_unixfs::file::FileMetadata;
use ipfs_unixfs::Metadata;
fn main() {
let cid = match std::env::args().nth(1).map(Cid::try_from) {
@ -273,7 +273,7 @@ fn walk(blocks: ShardedBlockStore, start: &Cid) -> Result<(), Error> {
Ok(())
}
fn apply_file(header: &mut tar::Header, metadata: &FileMetadata, total_size: u64) {
fn apply_file(header: &mut tar::Header, metadata: &Metadata, total_size: u64) {
header.set_mode(metadata.mode()
.map(|mode| mode & 0o7777)
.unwrap_or(0o0644));

View File

@ -2,8 +2,8 @@
///!
///! Most usable for walking UnixFS file trees provided by the `visit::IdleFileVisit` and
///! `visit::FileVisit` types.
use crate::pb::{ParsingFailed, UnixFs};
use crate::{InvalidCidInLink, UnexpectedNodeType};
use crate::pb::ParsingFailed;
use crate::{InvalidCidInLink, UnexpectedNodeType, Metadata};
use std::borrow::Cow;
use std::fmt;
@ -13,54 +13,6 @@ pub mod reader;
/// Higher level API for visiting the file tree.
pub mod visit;
/// Container for the unixfs metadata, which can be present at the root of the file trees.
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct FileMetadata {
mode: Option<u32>,
mtime: Option<(i64, u32)>,
}
impl FileMetadata {
/// Returns the full file mode, if one has been specified.
///
/// The full file mode is originally read through `st_mode` field of `stat` struct defined in
/// `sys/stat.h` and it's defining OpenGroup standard. Lowest 3 bytes will correspond to read,
/// write, and execute rights per user, group, and other and 4th byte determines sticky bits,
/// set user id or set group id. Following two bytes correspond to the different file types, as
/// defined by the same OpenGroup standard:
/// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html
pub fn mode(&self) -> Option<u32> {
self.mode
}
/// Returns the raw timestamp of last modification time, if specified.
///
/// The timestamp is `(seconds, nanos)` similar to `std::time::Duration` with the exception of
/// allowing seconds to be negative. The seconds are calculated from `1970-01-01 00:00:00` or
/// the common "unix epoch".
pub fn mtime(&self) -> Option<(i64, u32)> {
self.mtime
}
/// Returns the mtime metadata as an `FileTime`. Enabled only on feature `filetime`.
#[cfg(feature = "filetime")]
pub fn mtime_as_filetime(&self) -> Option<filetime::FileTime> {
self.mtime().map(|(seconds, nanos)| filetime::FileTime::from_unix_time(seconds, nanos))
}
}
impl<'a> From<&'a UnixFs<'_>> for FileMetadata {
fn from(data: &'a UnixFs<'_>) -> Self {
let mode = data.mode;
let mtime = data
.mtime
.clone()
.map(|ut| (ut.Seconds, ut.FractionalNanoseconds.unwrap_or(0)));
FileMetadata { mode, mtime }
}
}
/// Describes the errors which can happen during a visit or lower level block-by-block walking of
/// the DAG.
#[derive(Debug)]
@ -122,7 +74,7 @@ pub enum FileError {
/// Errored when the filesize is non-zero.
NoLinksNoContent,
/// Unsupported: non-root block defines metadata.
NonRootDefinesMetadata(FileMetadata),
NonRootDefinesMetadata(Metadata),
/// A non-leaf node in the tree has no filesize value which is used to determine the file range
/// for this tree.
IntermediateNodeWithoutFileSize,

View File

@ -3,7 +3,7 @@ use std::convert::TryFrom;
use std::fmt;
use std::ops::Range;
use crate::file::{FileError, FileMetadata, FileReadFailed, UnwrapBorrowedExt};
use crate::file::{FileError, Metadata, FileReadFailed, UnwrapBorrowedExt};
/// Navigates the UnixFs files, which are either:
/// - single block files which have everything needed to all of the contents
@ -22,12 +22,12 @@ pub struct FileReader<'a> {
links: Vec<PBLink<'a>>,
data: &'a [u8],
blocksizes: Vec<u64>,
metadata: FileMetadata,
metadata: Metadata,
file_size: u64,
}
impl AsRef<FileMetadata> for FileReader<'_> {
fn as_ref(&self) -> &FileMetadata {
impl AsRef<Metadata> for FileReader<'_> {
fn as_ref(&self) -> &Metadata {
&self.metadata
}
}
@ -75,12 +75,12 @@ impl<'a> FileReader<'a> {
/// Method for starting the file traversal. `data` is the raw data from unixfs block.
pub fn from_block(data: &'a [u8]) -> Result<Self, FileReadFailed> {
let inner = FlatUnixFs::try_from(data)?;
let metadata = FileMetadata::from(&inner.data);
let metadata = Metadata::from(&inner.data);
Self::from_parts(inner, 0, metadata)
}
pub(crate) fn from_parsed(inner: FlatUnixFs<'a>) -> Result<Self, FileReadFailed> {
let metadata = FileMetadata::from(&inner.data);
let metadata = Metadata::from(&inner.data);
Self::from_parts(inner, 0, metadata)
}
@ -93,7 +93,7 @@ impl<'a> FileReader<'a> {
let inner = FlatUnixFs::try_from(data)?;
if inner.data.mode.is_some() || inner.data.mtime.is_some() {
let metadata = FileMetadata::from(&inner.data);
let metadata = Metadata::from(&inner.data);
return Err(FileError::NonRootDefinesMetadata(metadata).into());
}
@ -103,7 +103,7 @@ impl<'a> FileReader<'a> {
fn from_parts(
inner: FlatUnixFs<'a>,
offset: u64,
metadata: FileMetadata,
metadata: Metadata,
) -> Result<Self, FileReadFailed> {
let empty_or_no_content = inner
.data
@ -196,7 +196,7 @@ pub struct Traversal {
last_offset: u64,
file_size: u64,
metadata: FileMetadata,
metadata: Metadata,
}
impl Traversal {
@ -223,8 +223,8 @@ impl Traversal {
}
}
impl AsRef<FileMetadata> for Traversal {
fn as_ref(&self) -> &FileMetadata {
impl AsRef<Metadata> for Traversal {
fn as_ref(&self) -> &Metadata {
&self.metadata
}
}

View File

@ -3,7 +3,7 @@ use std::convert::TryFrom;
use std::ops::Range;
use crate::file::reader::{FileContent, FileReader, Traversal};
use crate::file::{FileMetadata, FileReadFailed};
use crate::file::{Metadata, FileReadFailed};
use crate::pb::{merkledag::PBLink, FlatUnixFs};
use crate::InvalidCidInLink;
@ -27,7 +27,7 @@ impl IdleFileVisit {
pub fn start(
self,
block: &[u8],
) -> Result<(&[u8], u64, FileMetadata, Option<FileVisit>), FileReadFailed> {
) -> Result<(&[u8], u64, Metadata, Option<FileVisit>), FileReadFailed> {
let fr = FileReader::from_block(block)?;
self.start_from_reader(fr, &mut None)
}
@ -36,7 +36,7 @@ impl IdleFileVisit {
self,
block: FlatUnixFs<'a>,
cache: &'_ mut Option<Cache>,
) -> Result<(&'a [u8], u64, FileMetadata, Option<FileVisit>), FileReadFailed> {
) -> Result<(&'a [u8], u64, Metadata, Option<FileVisit>), FileReadFailed> {
let fr = FileReader::from_parsed(block)?;
self.start_from_reader(fr, cache)
}
@ -45,7 +45,7 @@ impl IdleFileVisit {
self,
fr: FileReader<'a>,
cache: &'_ mut Option<Cache>,
) -> Result<(&'a [u8], u64, FileMetadata, Option<FileVisit>), FileReadFailed> {
) -> Result<(&'a [u8], u64, Metadata, Option<FileVisit>), FileReadFailed> {
let metadata = fr.as_ref().to_owned();
let (content, traversal) = fr.content();
@ -195,8 +195,8 @@ impl FileVisit {
}
}
impl AsRef<FileMetadata> for FileVisit {
fn as_ref(&self) -> &FileMetadata {
impl AsRef<Metadata> for FileVisit {
fn as_ref(&self) -> &Metadata {
self.state.as_ref()
}
}

View File

@ -13,7 +13,7 @@ pub mod dir;
pub use dir::{resolve, LookupError, MaybeResolved, ResolveError};
mod pb;
use crate::pb::UnixFsType;
use pb::{UnixFs, UnixFsType};
/// Support operations for the dag-pb, the outer shell of UnixFS.
pub mod dagpb;
@ -116,3 +116,51 @@ impl UnexpectedNodeType {
}
}
}
/// Container for the unixfs metadata, which can be present at the root of the file trees.
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct Metadata {
mode: Option<u32>,
mtime: Option<(i64, u32)>,
}
impl Metadata {
/// Returns the full file mode, if one has been specified.
///
/// The full file mode is originally read through `st_mode` field of `stat` struct defined in
/// `sys/stat.h` and it's defining OpenGroup standard. Lowest 3 bytes will correspond to read,
/// write, and execute rights per user, group, and other and 4th byte determines sticky bits,
/// set user id or set group id. Following two bytes correspond to the different file types, as
/// defined by the same OpenGroup standard:
/// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html
pub fn mode(&self) -> Option<u32> {
self.mode
}
/// Returns the raw timestamp of last modification time, if specified.
///
/// The timestamp is `(seconds, nanos)` similar to `std::time::Duration` with the exception of
/// allowing seconds to be negative. The seconds are calculated from `1970-01-01 00:00:00` or
/// the common "unix epoch".
pub fn mtime(&self) -> Option<(i64, u32)> {
self.mtime
}
/// Returns the mtime metadata as an `FileTime`. Enabled only on feature `filetime`.
#[cfg(feature = "filetime")]
pub fn mtime_as_filetime(&self) -> Option<filetime::FileTime> {
self.mtime().map(|(seconds, nanos)| filetime::FileTime::from_unix_time(seconds, nanos))
}
}
impl<'a> From<&'a UnixFs<'_>> for Metadata {
fn from(data: &'a UnixFs<'_>) -> Self {
let mode = data.mode;
let mtime = data
.mtime
.clone()
.map(|ut| (ut.Seconds, ut.FractionalNanoseconds.unwrap_or(0)));
Metadata { mode, mtime }
}
}

View File

@ -4,7 +4,8 @@ use std::borrow::Cow;
use std::convert::TryFrom;
use std::fmt;
use crate::pb::{FlatUnixFs, PBLink, PBNode, ParsingFailed, UnixFsType};
use crate::file::{FileMetadata, FileReadFailed, FileError};
use crate::Metadata;
use crate::file::{FileReadFailed, FileError};
use crate::file::visit::{IdleFileVisit, FileVisit, Cache};
use crate::{InvalidCidInLink, UnexpectedNodeType};
use std::path::{Path, PathBuf};
@ -86,7 +87,7 @@ impl Walker {
/// data.
pub fn start<'a>(data: &'a [u8], root_name: &str, cache: &mut Option<Cache>) -> Result<ContinuedWalk<'a>, Error> {
let flat = FlatUnixFs::try_from(data)?;
let metadata = FileMetadata::from(&flat.data);
let metadata = Metadata::from(&flat.data);
match flat.data.Type {
UnixFsType::Directory => {
@ -229,7 +230,7 @@ impl Walker {
}
let flat = FlatUnixFs::try_from(bytes)?;
let metadata = FileMetadata::from(&flat.data);
let metadata = Metadata::from(&flat.data);
match flat.data.Type {
UnixFsType::Directory => {
@ -379,11 +380,11 @@ impl Walker {
struct InnerEntry {
kind: InnerKind,
path: PathBuf,
metadata: FileMetadata,
metadata: Metadata,
depth: usize,
}
impl From<InnerEntry> for FileMetadata {
impl From<InnerEntry> for Metadata {
fn from(e: InnerEntry) -> Self {
e.metadata
}
@ -412,16 +413,16 @@ enum InnerKind {
#[derive(Debug)]
pub enum Entry<'a> {
/// Current item is the root directory (HAMTShard or plain Directory).
RootDirectory(&'a Path, &'a FileMetadata),
RootDirectory(&'a Path, &'a Metadata),
/// Current item is a continuation of a HAMTShard directory. Only the root HAMTShard will have
/// file metadata.
Bucket(&'a Cid, &'a Path),
/// Current item is a non-root plain directory or a HAMTShard root directory.
Directory(&'a Cid, &'a Path, &'a FileMetadata),
Directory(&'a Cid, &'a Path, &'a Metadata),
/// Current item is a possibly root file with a path, metadata, and total file size.
File(Option<&'a Cid>, &'a Path, &'a FileMetadata, u64),
File(Option<&'a Cid>, &'a Path, &'a Metadata, u64),
/// Current item is a possibly root symlink.
Symlink(Option<&'a Cid>, &'a Path, &'a FileMetadata),
Symlink(Option<&'a Cid>, &'a Path, &'a Metadata),
}
impl<'a> Entry<'a> {
@ -440,7 +441,7 @@ impl<'a> Entry<'a> {
/// Returns the metadata for the latest entry. It exists for initial directory entries, files,
/// and symlinks but not continued HamtShards.
pub fn metadata(&self) -> Option<&'a FileMetadata> {
pub fn metadata(&self) -> Option<&'a Metadata> {
use Entry::*;
match self {
Bucket(_, _) => None,
@ -476,7 +477,7 @@ impl<'a> Entry<'a> {
}
impl InnerEntry {
fn new_root_dir(metadata: FileMetadata, name: &str) -> Self {
fn new_root_dir(metadata: Metadata, name: &str) -> Self {
let mut path = PathBuf::new();
path.push(name);
Self {
@ -487,7 +488,7 @@ impl InnerEntry {
}
}
fn new_root_bucket(metadata: FileMetadata, name: &str) -> Self {
fn new_root_bucket(metadata: Metadata, name: &str) -> Self {
let mut path = PathBuf::new();
path.push(name);
Self {
@ -498,7 +499,7 @@ impl InnerEntry {
}
}
fn new_root_file(metadata: FileMetadata, name: &str, step: Option<FileVisit>, file_size: u64) -> Self {
fn new_root_file(metadata: Metadata, name: &str, step: Option<FileVisit>, file_size: u64) -> Self {
let mut path = PathBuf::new();
path.push(name);
Self {
@ -509,7 +510,7 @@ impl InnerEntry {
}
}
fn new_root_symlink(metadata: FileMetadata, name: &str) -> Self {
fn new_root_symlink(metadata: Metadata, name: &str) -> Self {
let mut path = PathBuf::new();
path.push(name);
Self {
@ -548,7 +549,7 @@ impl InnerEntry {
cid: Cid,
name: &str,
depth: usize,
metadata: FileMetadata,
metadata: Metadata,
) {
use InnerKind::*;
match self.kind {
@ -572,7 +573,7 @@ impl InnerEntry {
cid: Cid,
name: &str,
depth: usize,
metadata: FileMetadata,
metadata: Metadata,
) {
use InnerKind::*;
match self.kind {
@ -629,7 +630,7 @@ impl InnerEntry {
cid: Cid,
name: &str,
depth: usize,
metadata: FileMetadata,
metadata: Metadata,
step: Option<FileVisit>,
file_size: u64,
) {
@ -655,7 +656,7 @@ impl InnerEntry {
cid: Cid,
name: &str,
depth: usize,
metadata: FileMetadata
metadata: Metadata
) {
use InnerKind::*;
match self.kind {