From 0f276b6ec8383cea3f3da726682594e5e683033a Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Sat, 27 Mar 2021 07:13:39 +0300 Subject: Resolve symlinks when listing This has the benefit of showing the size and modification date of the pointed-to file. Symlink to directories now respects '--dirs-first' option and broken symlinks don't show in directory listing. --- src/listing.rs | 22 +++------------------- src/renderer.rs | 4 ---- 2 files changed, 3 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index 66aea6b..2fe583c 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -65,9 +65,6 @@ pub enum EntryType { /// Entry is a file File, - - /// Entry is a symlink - Symlink, } /// Entry @@ -114,11 +111,6 @@ impl Entry { pub fn is_file(&self) -> bool { self.entry_type == EntryType::File } - - /// Returns wether the entry is a symlink - pub fn is_symlink(&self) -> bool { - self.entry_type == EntryType::Symlink - } } /// One entry in the path to the listed directory @@ -260,7 +252,7 @@ pub fn directory_listing( let file_name = entry.file_name().to_string_lossy().to_string(); // if file is a directory, add '/' to the end of the name - if let Ok(metadata) = entry.metadata() { + if let Ok(metadata) = std::fs::metadata(entry.path()) { if skip_symlinks && metadata.file_type().is_symlink() { continue; } @@ -269,15 +261,7 @@ pub fn directory_listing( Err(_) => None, }; - if metadata.file_type().is_symlink() { - entries.push(Entry::new( - file_name, - EntryType::Symlink, - file_url, - None, - last_modification_date, - )); - } else if metadata.is_dir() { + if metadata.is_dir() { entries.push(Entry::new( file_name, EntryType::Directory, @@ -285,7 +269,7 @@ pub fn directory_listing( None, last_modification_date, )); - } else { + } else if metadata.is_file() { entries.push(Entry::new( file_name, EntryType::File, diff --git a/src/renderer.rs b/src/renderer.rs index c9ec9cd..2487696 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -341,10 +341,6 @@ fn entry_row( } } } - } @else if entry.is_symlink() { - a.symlink href=(parametrized_link(&entry.link, sort_method, sort_order)) { - (entry.name) span.symlink-symbol { "⇢" } - } } } } -- cgit v1.2.3 From 81e65554b4da37399ef9b0ac6c647198a6e81ebf Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Sat, 27 Mar 2021 07:19:59 +0300 Subject: Bring back the symlink symbol --- src/listing.rs | 11 +++++++++++ src/renderer.rs | 6 ++++++ 2 files changed, 17 insertions(+) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index 2fe583c..c5eefe6 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -75,6 +75,9 @@ pub struct Entry { /// Type of the entry pub entry_type: EntryType, + /// Entry is symlink. Not mutually exclusive with entry_type + pub is_symlink: bool, + /// URL of the entry pub link: String, @@ -89,6 +92,7 @@ impl Entry { fn new( name: String, entry_type: EntryType, + is_symlink: bool, link: String, size: Option, last_modification_date: Option, @@ -96,6 +100,7 @@ impl Entry { Entry { name, entry_type, + is_symlink, link, size, last_modification_date, @@ -250,6 +255,10 @@ pub fn directory_listing( // show file url as relative to static path let file_url = utf8_percent_encode(&p.to_string_lossy(), FRAGMENT).to_string(); let file_name = entry.file_name().to_string_lossy().to_string(); + let is_symlink = match entry.metadata() { + Ok(metadata) => metadata.file_type().is_symlink(), + Err(_) => continue, + }; // if file is a directory, add '/' to the end of the name if let Ok(metadata) = std::fs::metadata(entry.path()) { @@ -265,6 +274,7 @@ pub fn directory_listing( entries.push(Entry::new( file_name, EntryType::Directory, + is_symlink, file_url, None, last_modification_date, @@ -273,6 +283,7 @@ pub fn directory_listing( entries.push(Entry::new( file_name, EntryType::File, + is_symlink, file_url, Some(ByteSize::b(metadata.len())), last_modification_date, diff --git a/src/renderer.rs b/src/renderer.rs index 2487696..266f035 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -329,11 +329,17 @@ fn entry_row( @if entry.is_dir() { a.directory href=(parametrized_link(&entry.link, sort_method, sort_order)) { (entry.name) "/" + @if entry.is_symlink { + span.symlink-symbol { "⇢" } + } } } @else if entry.is_file() { div.file-entry { a.file href=(&entry.link) { (entry.name) + @if entry.is_symlink { + span.symlink-symbol { "⇢" } + } } @if let Some(size) = entry.size { span.mobile-info.size { -- cgit v1.2.3 From 57ec279491db97d2933f07127235d44e918472ce Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Mon, 29 Mar 2021 12:11:58 +0300 Subject: Move symlink symbol from html to css This should facilitate testing because this symbol will no longer a part of the entry text shown in html. --- src/renderer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/renderer.rs b/src/renderer.rs index 266f035..c51f364 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -330,7 +330,7 @@ fn entry_row( a.directory href=(parametrized_link(&entry.link, sort_method, sort_order)) { (entry.name) "/" @if entry.is_symlink { - span.symlink-symbol { "⇢" } + span.symlink-symbol { } } } } @else if entry.is_file() { @@ -338,7 +338,7 @@ fn entry_row( a.file href=(&entry.link) { (entry.name) @if entry.is_symlink { - span.symlink-symbol { "⇢" } + span.symlink-symbol { } } } @if let Some(size) = entry.size { -- cgit v1.2.3 From 97f5772ca5b1d23ef623a0aea7a5a7f84b9345f1 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Mon, 5 Apr 2021 23:21:26 +0300 Subject: Honor --no-symlinks option when listing --- src/listing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index c5eefe6..53d7233 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -262,7 +262,7 @@ pub fn directory_listing( // if file is a directory, add '/' to the end of the name if let Ok(metadata) = std::fs::metadata(entry.path()) { - if skip_symlinks && metadata.file_type().is_symlink() { + if skip_symlinks && is_symlink { continue; } let last_modification_date = match metadata.modified() { -- cgit v1.2.3 From 60695fac0957a4032ee84057de676ab8e3ddbb5d Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Mon, 5 Apr 2021 23:21:34 +0300 Subject: Avoid unneccessary syscalls for entry metadata For non-symlink files and directories, there is no need to call `std::fs::metadata()` as the metadata are already obtained via `entry.metadata()` --- src/listing.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index 53d7233..9d74906 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -255,13 +255,16 @@ pub fn directory_listing( // show file url as relative to static path let file_url = utf8_percent_encode(&p.to_string_lossy(), FRAGMENT).to_string(); let file_name = entry.file_name().to_string_lossy().to_string(); - let is_symlink = match entry.metadata() { - Ok(metadata) => metadata.file_type().is_symlink(), - Err(_) => continue, + let (is_symlink, metadata) = match entry.metadata() { + Ok(metadata) if metadata.file_type().is_symlink() => { + // for symlinks, get the metadata of the original file + (true, std::fs::metadata(entry.path())) + } + res => (false, res), }; // if file is a directory, add '/' to the end of the name - if let Ok(metadata) = std::fs::metadata(entry.path()) { + if let Ok(metadata) = metadata { if skip_symlinks && is_symlink { continue; } -- cgit v1.2.3