Skip to content
This repository was archived by the owner on Jul 23, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub trait Metadata {
/// Returns true if the path is a file.
fn is_file(&self) -> bool;

/// Returns true if the path is a symlink.
fn is_symlink(&self) -> bool;

/// Returns the last modified time of the path.
fn modified(&self) -> Result<SystemTime>;

Expand Down Expand Up @@ -49,19 +52,25 @@ where
M: Metadata,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let modified: DateTime<Local> =
let modified: DateTime<Utc> =
DateTime::from(self.metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH));
write!(
f,
"{filetype}{permissions} {owner} {group} {size} {modified} {path}",
filetype = if self.metadata.is_dir() { "d" } else { "-" },
"{filetype}{permissions} {owner:>12} {group:>12} {size:#14} {modified} {path}",
filetype = if self.metadata.is_dir() {
"d"
} else if self.metadata.is_symlink() {
"l"
} else {
"-"
},
// TODO: Don't hardcode permissions ;)
permissions = "rwxr-xr-x",
// TODO: Consider showing canonical names here
owner = self.metadata.uid(),
group = self.metadata.gid(),
size = self.metadata.len(),
modified = modified.format("%b %d %Y"),
modified = modified.format("%b %d %H:%M"),
path = self
.path
.as_ref()
Expand Down Expand Up @@ -303,7 +312,7 @@ impl StorageBackend for Filesystem {
let path = dir_entry.path();
let relpath = path.strip_prefix(prefix).unwrap();
let relpath = std::path::PathBuf::from(relpath);
match std::fs::metadata(dir_entry.path()) {
match std::fs::symlink_metadata(dir_entry.path()) {
Ok(stat) => Some(Fileinfo {
path: relpath,
metadata: stat,
Expand Down Expand Up @@ -384,7 +393,7 @@ impl StorageBackend for Filesystem {
};

let from_rename = from.clone(); // Alright, borrow checker, have it your way.
let fut = tokio::fs::metadata(from)
let fut = tokio::fs::symlink_metadata(from)
.map_err(|_| Error::IOError)
.and_then(move |metadata| {
if metadata.is_file() {
Expand Down Expand Up @@ -417,6 +426,10 @@ impl Metadata for std::fs::Metadata {
self.is_file()
}

fn is_symlink(&self) -> bool {
self.file_type().is_symlink()
}

fn modified(&self) -> Result<SystemTime> {
self.modified().map_err(|e| e.into())
}
Expand Down Expand Up @@ -500,6 +513,7 @@ mod tests {

assert_eq!(meta.is_dir(), my_meta.is_dir());
assert_eq!(meta.is_file(), my_meta.is_file());
assert_eq!(meta.is_symlink(), my_meta.is_symlink());
assert_eq!(meta.len(), my_meta.len());
assert_eq!(meta.modified().unwrap(), my_meta.modified().unwrap());
}
Expand Down Expand Up @@ -527,6 +541,7 @@ mod tests {
assert_eq!(my_fileinfo.path, relpath);
assert_eq!(my_fileinfo.metadata.is_dir(), meta.is_dir());
assert_eq!(my_fileinfo.metadata.is_file(), meta.is_file());
assert_eq!(my_fileinfo.metadata.is_symlink(), meta.is_symlink());
assert_eq!(my_fileinfo.metadata.len(), meta.len());
assert_eq!(
my_fileinfo.metadata.modified().unwrap(),
Expand Down Expand Up @@ -622,6 +637,9 @@ mod tests {
fn is_file(&self) -> bool {
true
}
fn is_symlink(&self) -> bool {
false
}
fn modified(&self) -> Result<SystemTime> {
Ok(std::time::SystemTime::UNIX_EPOCH)
}
Expand All @@ -644,7 +662,10 @@ mod tests {
.file_name()
.unwrap()
.to_string_lossy();
let format = format!("-rwxr-xr-x 1 2 5 Jan 01 1970 {}", basename);
let format = format!(
"-rwxr-xr-x 1 2 5 Jan 01 00:00 {}",
basename
);
assert_eq!(my_format, format);
}

Expand All @@ -661,7 +682,7 @@ mod tests {
rt.block_on(fs.mkd(new_dir_name)).expect("Failed to mkd");

let full_path = root.join(new_dir_name);
let metadata = std::fs::metadata(full_path).unwrap();
let metadata = std::fs::symlink_metadata(full_path).unwrap();
assert!(metadata.is_dir());
}

Expand All @@ -686,6 +707,7 @@ mod tests {
.is_file());

let old_full_path = root.join(old_filename);
std::fs::metadata(old_full_path).expect_err("Old filename should not exists anymore");
std::fs::symlink_metadata(old_full_path)
.expect_err("Old filename should not exists anymore");
}
}