2017-03-15 11:32:44 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::Path;
|
2017-03-15 20:53:05 +00:00
|
|
|
use std::fs::{self, Metadata, File, Permissions};
|
2017-03-15 11:32:44 +00:00
|
|
|
use std::os::linux::fs::MetadataExt;
|
2017-03-15 20:53:05 +00:00
|
|
|
use std::os::unix::fs::{PermissionsExt, symlink};
|
2017-03-16 08:42:30 +00:00
|
|
|
use std::io::{Read, Write};
|
2017-03-15 11:32:44 +00:00
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
use ::util::*;
|
2017-03-18 14:41:59 +00:00
|
|
|
use super::{Repository, RepositoryError};
|
2017-03-16 08:42:30 +00:00
|
|
|
use super::integrity::RepositoryIntegrityError;
|
2017-03-17 06:15:19 +00:00
|
|
|
use ::bundle::BundleMode;
|
2017-03-15 11:32:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub enum FileType {
|
|
|
|
File,
|
|
|
|
Directory,
|
|
|
|
Symlink
|
|
|
|
}
|
|
|
|
serde_impl!(FileType(u8) {
|
|
|
|
File => 0,
|
|
|
|
Directory => 1,
|
|
|
|
Symlink => 2
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FileContents {
|
2017-03-16 08:42:30 +00:00
|
|
|
Inline(msgpack::Bytes),
|
2017-03-18 14:41:59 +00:00
|
|
|
ChunkedDirect(ChunkList),
|
|
|
|
ChunkedIndirect(ChunkList)
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|
|
|
|
serde_impl!(FileContents(u8) {
|
|
|
|
Inline(ByteBuf) => 0,
|
2017-03-16 08:42:30 +00:00
|
|
|
ChunkedDirect(Vec<Chunk>) => 1,
|
|
|
|
ChunkedIndirect(Vec<Chunk>) => 2
|
2017-03-15 11:32:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Inode {
|
|
|
|
pub name: String,
|
|
|
|
pub size: u64,
|
|
|
|
pub file_type: FileType,
|
|
|
|
pub mode: u32,
|
|
|
|
pub user: u32,
|
|
|
|
pub group: u32,
|
|
|
|
pub access_time: i64,
|
|
|
|
pub modify_time: i64,
|
|
|
|
pub create_time: i64,
|
|
|
|
pub symlink_target: Option<String>,
|
|
|
|
pub contents: Option<FileContents>,
|
2017-03-18 14:41:59 +00:00
|
|
|
pub children: Option<HashMap<String, ChunkList>>
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|
|
|
|
impl Default for Inode {
|
|
|
|
fn default() -> Self {
|
|
|
|
Inode {
|
|
|
|
name: "".to_string(),
|
|
|
|
size: 0,
|
|
|
|
file_type: FileType::File,
|
|
|
|
mode: 0o644,
|
|
|
|
user: 1000,
|
|
|
|
group: 1000,
|
|
|
|
access_time: 0,
|
|
|
|
modify_time: 0,
|
|
|
|
create_time: 0,
|
|
|
|
symlink_target: None,
|
|
|
|
contents: None,
|
|
|
|
children: None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serde_impl!(Inode(u8) {
|
|
|
|
name: String => 0,
|
|
|
|
size: u64 => 1,
|
|
|
|
file_type: FileType => 2,
|
|
|
|
mode: u32 => 3,
|
|
|
|
user: u32 => 4,
|
|
|
|
group: u32 => 5,
|
|
|
|
access_time: i64 => 6,
|
|
|
|
modify_time: i64 => 7,
|
|
|
|
create_time: i64 => 8,
|
|
|
|
symlink_target: Option<String> => 9,
|
|
|
|
contents: Option<FileContents> => 10,
|
2017-03-18 14:41:59 +00:00
|
|
|
children: HashMap<String, ChunkList> => 11
|
2017-03-15 11:32:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
impl Inode {
|
2017-03-17 06:15:19 +00:00
|
|
|
#[inline]
|
2017-03-16 08:42:30 +00:00
|
|
|
fn get_extended_attrs_from(&mut self, meta: &Metadata) -> Result<(), RepositoryError> {
|
2017-03-15 11:32:44 +00:00
|
|
|
self.mode = meta.st_mode();
|
|
|
|
self.user = meta.st_uid();
|
|
|
|
self.group = meta.st_gid();
|
|
|
|
self.access_time = meta.st_atime();
|
|
|
|
self.modify_time = meta.st_mtime();
|
|
|
|
self.create_time = meta.st_ctime();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
pub fn get_from<P: AsRef<Path>>(path: P) -> Result<Self, RepositoryError> {
|
|
|
|
let name = try!(path.as_ref().file_name()
|
|
|
|
.ok_or_else(|| RepositoryError::InvalidFileType(path.as_ref().to_owned())))
|
|
|
|
.to_string_lossy().to_string();
|
|
|
|
let meta = try!(fs::symlink_metadata(path.as_ref()));
|
2017-03-15 11:32:44 +00:00
|
|
|
let mut inode = Inode::default();
|
|
|
|
inode.name = name;
|
|
|
|
inode.size = meta.len();
|
|
|
|
inode.file_type = if meta.is_file() {
|
|
|
|
FileType::File
|
|
|
|
} else if meta.is_dir() {
|
|
|
|
FileType::Directory
|
|
|
|
} else if meta.file_type().is_symlink() {
|
|
|
|
FileType::Symlink
|
|
|
|
} else {
|
2017-03-16 08:42:30 +00:00
|
|
|
return Err(RepositoryError::InvalidFileType(path.as_ref().to_owned()));
|
2017-03-15 11:32:44 +00:00
|
|
|
};
|
|
|
|
if meta.file_type().is_symlink() {
|
2017-03-16 08:42:30 +00:00
|
|
|
inode.symlink_target = Some(try!(fs::read_link(path)).to_string_lossy().to_string());
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|
|
|
|
try!(inode.get_extended_attrs_from(&meta));
|
|
|
|
Ok(inode)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2017-03-16 08:42:30 +00:00
|
|
|
pub fn create_at<P: AsRef<Path>>(&self, path: P) -> Result<Option<File>, RepositoryError> {
|
2017-03-15 11:32:44 +00:00
|
|
|
let full_path = path.as_ref().join(&self.name);
|
2017-03-15 20:53:05 +00:00
|
|
|
let mut file = None;
|
2017-03-15 11:32:44 +00:00
|
|
|
match self.file_type {
|
|
|
|
FileType::File => {
|
2017-03-16 08:42:30 +00:00
|
|
|
file = Some(try!(File::create(&full_path)));
|
2017-03-15 11:32:44 +00:00
|
|
|
},
|
|
|
|
FileType::Directory => {
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(fs::create_dir(&full_path));
|
2017-03-15 11:32:44 +00:00
|
|
|
},
|
|
|
|
FileType::Symlink => {
|
|
|
|
if let Some(ref src) = self.symlink_target {
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(symlink(src, &full_path));
|
2017-03-15 11:32:44 +00:00
|
|
|
} else {
|
2017-03-16 08:42:30 +00:00
|
|
|
return Err(RepositoryIntegrityError::SymlinkWithoutTarget.into())
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(fs::set_permissions(&full_path, Permissions::from_mode(self.mode)));
|
2017-03-15 20:53:05 +00:00
|
|
|
//FIXME: set times and gid/uid
|
|
|
|
// https://crates.io/crates/filetime
|
|
|
|
Ok(file)
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Repository {
|
2017-03-16 11:33:10 +00:00
|
|
|
pub fn create_inode<P: AsRef<Path>>(&mut self, path: P) -> Result<Inode, RepositoryError> {
|
2017-03-15 11:32:44 +00:00
|
|
|
let mut inode = try!(Inode::get_from(path.as_ref()));
|
|
|
|
if inode.file_type == FileType::File && inode.size > 0 {
|
2017-03-16 08:42:30 +00:00
|
|
|
let mut file = try!(File::open(path));
|
2017-03-15 11:32:44 +00:00
|
|
|
if inode.size < 100 {
|
|
|
|
let mut data = Vec::with_capacity(inode.size as usize);
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(file.read_to_end(&mut data));
|
2017-03-15 11:32:44 +00:00
|
|
|
inode.contents = Some(FileContents::Inline(data.into()));
|
|
|
|
} else {
|
2017-03-17 06:15:19 +00:00
|
|
|
let mut chunks = try!(self.put_stream(BundleMode::Content, &mut file));
|
2017-03-16 08:42:30 +00:00
|
|
|
if chunks.len() < 10 {
|
|
|
|
inode.contents = Some(FileContents::ChunkedDirect(chunks));
|
|
|
|
} else {
|
2017-03-18 14:41:59 +00:00
|
|
|
let mut chunk_data = Vec::with_capacity(chunks.encoded_size());
|
|
|
|
chunks.write_to(&mut chunk_data).unwrap();
|
|
|
|
chunks = try!(self.put_data(BundleMode::Content, &chunk_data));
|
2017-03-16 08:42:30 +00:00
|
|
|
inode.contents = Some(FileContents::ChunkedIndirect(chunks));
|
|
|
|
}
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-16 11:33:10 +00:00
|
|
|
Ok(inode)
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:15:19 +00:00
|
|
|
#[inline]
|
2017-03-18 14:41:59 +00:00
|
|
|
pub fn put_inode(&mut self, inode: &Inode) -> Result<ChunkList, RepositoryError> {
|
2017-03-17 06:15:19 +00:00
|
|
|
self.put_data(BundleMode::Meta, &try!(msgpack::encode(inode)))
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-03-16 08:42:30 +00:00
|
|
|
pub fn get_inode(&mut self, chunks: &[Chunk]) -> Result<Inode, RepositoryError> {
|
|
|
|
Ok(try!(msgpack::decode(&try!(self.get_data(chunks)))))
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|
2017-03-15 20:53:05 +00:00
|
|
|
|
|
|
|
#[inline]
|
2017-03-16 08:42:30 +00:00
|
|
|
pub fn save_inode_at<P: AsRef<Path>>(&mut self, inode: &Inode, path: P) -> Result<(), RepositoryError> {
|
2017-03-15 20:53:05 +00:00
|
|
|
if let Some(mut file) = try!(inode.create_at(path.as_ref())) {
|
|
|
|
if let Some(ref contents) = inode.contents {
|
|
|
|
match *contents {
|
|
|
|
FileContents::Inline(ref data) => {
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(file.write_all(&data));
|
2017-03-15 20:53:05 +00:00
|
|
|
},
|
2017-03-16 08:42:30 +00:00
|
|
|
FileContents::ChunkedDirect(ref chunks) => {
|
2017-03-15 20:53:05 +00:00
|
|
|
try!(self.get_stream(chunks, &mut file));
|
2017-03-16 08:42:30 +00:00
|
|
|
},
|
|
|
|
FileContents::ChunkedIndirect(ref chunks) => {
|
|
|
|
let chunk_data = try!(self.get_data(chunks));
|
2017-03-18 14:41:59 +00:00
|
|
|
let chunks = ChunkList::read_from(&chunk_data);
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(self.get_stream(&chunks, &mut file));
|
2017-03-15 20:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-15 11:32:44 +00:00
|
|
|
}
|