Skip to content
Myr

send DirEntries to output instead of just path

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Jonathan Goren · committed by Jonathan Goren · parent 81669f4 · Browse files · 7 files+88 added−80 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+88 added−80 removed

  • other:+88 added−80 removed

7 files+88 added−80 removed

modified:src/exec/job.rs+5 added-5 removed
1use std::path::PathBuf;1use std::sync::{Arc, Mutex};23use crossbeam_channel::Receiver;45use crate::entry::DirEntry;6use crate::error::print_error;7use crate::exit_codes::{merge_exitcodes, ExitCode};8use crate::walk::WorkerResult;9
15 unmodified lines
25 let lock = rx.lock().unwrap();2627 // Obtain the next result from the receiver, else if the channel28 // has closed, exit from the loop29 let value: PathBuf = match lock.recv() {30 Ok(WorkerResult::Entry(path)) => path,29 let value: DirEntry = match lock.recv() {30 Ok(WorkerResult::Entry(val)) => val,31 Ok(WorkerResult::Error(err)) => {32 if show_filesystem_errors {33 print_error(err.to_string());34 }
4 unmodified lines
3940 // Drop the lock so that other threads can read from the receiver.41 drop(lock);42 // Generate a command, execute it and store its exit code.43 results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm), buffer_output))43 results.push(cmd.generate_and_execute(value.path(), Arc::clone(&out_perm), buffer_output))44 }45 // Returns error in case of any error.46 merge_exitcodes(results)47}
5 unmodified lines
53 buffer_output: bool,54 limit: usize,55) -> ExitCode {56 let paths = rx.iter().filter_map(|value| match value {57 WorkerResult::Entry(path) => Some(path),57 WorkerResult::Entry(val) => Some(val.path().to_owned()),58 WorkerResult::Error(err) => {59 if show_filesystem_errors {60 print_error(err.to_string());61 }
17 unmodified lines
added:src/entry.rs+62 added
1use std::{2 fs::{FileType, Metadata},3 path::{Path, PathBuf},4};56use once_cell::unsync::OnceCell;78enum DirEntryInner {9 Normal(ignore::DirEntry),10 BrokenSymlink(PathBuf),11}1213pub struct DirEntry {14 inner: DirEntryInner,15 metadata: OnceCell<Option<Metadata>>,16}1718impl DirEntry {19 pub fn normal(e: ignore::DirEntry) -> Self {20 Self {21 inner: DirEntryInner::Normal(e),22 metadata: OnceCell::new(),23 }24 }2526 pub fn broken_symlink(path: PathBuf) -> Self {27 Self {28 inner: DirEntryInner::BrokenSymlink(path),29 metadata: OnceCell::new(),30 }31 }3233 pub fn path(&self) -> &Path {34 match &self.inner {35 DirEntryInner::Normal(e) => e.path(),36 DirEntryInner::BrokenSymlink(pathbuf) => pathbuf.as_path(),37 }38 }3940 pub fn file_type(&self) -> Option<FileType> {41 match &self.inner {42 DirEntryInner::Normal(e) => e.file_type(),43 DirEntryInner::BrokenSymlink(_) => self.metadata().map(|m| m.file_type()),44 }45 }4647 pub fn metadata(&self) -> Option<&Metadata> {48 self.metadata49 .get_or_init(|| match &self.inner {50 DirEntryInner::Normal(e) => e.metadata().ok(),51 DirEntryInner::BrokenSymlink(path) => path.symlink_metadata().ok(),52 })53 .as_ref()54 }5556 pub fn depth(&self) -> Option<usize> {57 match &self.inner {58 DirEntryInner::Normal(e) => Some(e.depth()),59 DirEntryInner::BrokenSymlink(_) => None,60 }61 }62}
modified:src/filesystem.rs+2 added-2 removed
7 unmodified lines
8use std::path::{Path, PathBuf};910use normpath::PathExt;1112use crate::walk;12use crate::entry;1314pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {15 if path.is_absolute() {16 return Ok(path.to_path_buf());
33 unmodified lines
50pub fn is_executable(_: &fs::Metadata) -> bool {51 false52}5354pub fn is_empty(entry: &walk::DirEntry) -> bool {54pub fn is_empty(entry: &entry::DirEntry) -> bool {55 if let Some(file_type) = entry.file_type() {56 if file_type.is_dir() {57 if let Ok(mut entries) = fs::read_dir(entry.path()) {58 entries.next().is_none()
90 unmodified lines
modified:src/filetypes.rs+2 added-2 removed
1use crate::entry;2use crate::filesystem;2use crate::walk;34/// Whether or not to show5#[derive(Default)]6pub struct FileTypes {
6 unmodified lines
13 pub empty_only: bool,14}1516impl FileTypes {17 pub fn should_ignore(&self, entry: &walk::DirEntry) -> bool {17 pub fn should_ignore(&self, entry: &entry::DirEntry) -> bool {18 if let Some(ref entry_type) = entry.file_type() {19 (!self.files && entry_type.is_file())20 || (!self.directories && entry_type.is_dir())21 || (!self.symlinks && entry_type.is_symlink())
18 unmodified lines
modified:src/main.rs+1 added
1mod app;2mod config;3mod entry;4mod error;5mod exec;6mod exit_codes;7mod filesystem;
563 unmodified lines
modified:src/output.rs+5 added-3 removed
3 unmodified lines
45use lscolors::{Indicator, LsColors, Style};67use crate::config::Config;8use crate::entry::DirEntry;9use crate::error::print_error;10use crate::exit_codes::ExitCode;11use crate::filesystem::strip_current_dir;12
1 unmodified line
14 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)15}1617// TODO: this function is performance critical and can probably be optimized17pub fn print_entry<W: Write>(stdout: &mut W, entry: &Path, config: &Config) {18pub fn print_entry<W: Write>(stdout: &mut W, entry: &DirEntry, config: &Config) {19 let path = entry.path();20 let path = if config.strip_cwd_prefix {19 strip_current_dir(entry)21 strip_current_dir(path)22 } else {21 entry23 path24 };2526 let r = if let Some(ref ls_colors) = config.ls_colors {27 print_entry_colorized(stdout, path, config, ls_colors)
107 unmodified lines
modified:src/walk.rs+11 added-68 removed
1use std::ffi::OsStr;2use std::fs::{FileType, Metadata};2use std::io;3use std::mem;5use std::path::{Path, PathBuf};4use std::path::PathBuf;5use std::sync::atomic::{AtomicBool, Ordering};6use std::sync::{Arc, Mutex};7use std::thread;8use std::time::{Duration, Instant};
2 unmodified lines
11use anyhow::{anyhow, Result};12use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender};13use ignore::overrides::OverrideBuilder;14use ignore::{self, WalkBuilder};16use once_cell::unsync::OnceCell;15use regex::bytes::Regex;1617use crate::config::Config;18use crate::entry::DirEntry;19use crate::error::print_error;20use crate::exec;21use crate::exit_codes::{merge_exitcodes, ExitCode};22use crate::filesystem;
11 unmodified lines
34}3536/// The Worker threads can result in a valid entry having PathBuf or an error.37pub enum WorkerResult {39 Entry(PathBuf),38 Entry(DirEntry),39 Error(ignore::Error),40}4142/// Maximum size of the output buffer before flushing results to the console
142 unmodified lines
185 mode: ReceiverMode,186 /// The deadline to switch to streaming mode.187 deadline: Instant,188 /// The buffer of quickly received paths.190 buffer: Vec<PathBuf>,189 buffer: Vec<DirEntry>,190 /// Result count.191 num_results: usize,192}193
48 unmodified lines
242243 /// Wait for a result or state change.244 fn poll(&mut self) -> Result<(), ExitCode> {245 match self.recv() {247 Ok(WorkerResult::Entry(path)) => {246 Ok(WorkerResult::Entry(dir_entry)) => {247 if self.config.quiet {248 return Err(ExitCode::HasResults(true));249 }250251 match self.mode {252 ReceiverMode::Buffering => {254 self.buffer.push(path);253 self.buffer.push(dir_entry);254 if self.buffer.len() > MAX_BUFFER_LENGTH {255 self.stream()?;256 }257 }258 ReceiverMode::Streaming => {260 self.print(&path)?;259 self.print(&dir_entry)?;260 self.flush()?;261 }262 }263
20 unmodified lines
284 Ok(())285 }286287 /// Output a path.289 fn print(&mut self, path: &Path) -> Result<(), ExitCode> {290 output::print_entry(&mut self.stdout, path, &self.config);288 fn print(&mut self, entry: &DirEntry) -> Result<(), ExitCode> {289 output::print_entry(&mut self.stdout, entry, &self.config);290291 if self.interrupt_flag.load(Ordering::Relaxed) {292 // Ignore any errors on flush, because we're about to exit anyway293 let _ = self.flush();
17 unmodified lines
311312 /// Stop looping.313 fn stop(&mut self) -> Result<(), ExitCode> {314 if self.mode == ReceiverMode::Buffering {316 self.buffer.sort();315 self.buffer.sort_by(|e1, e2| e1.path().cmp(e2.path()));316 self.stream()?;317 }318319 if self.config.quiet {
82 unmodified lines
402 }403 })404}405407enum DirEntryInner {408 Normal(ignore::DirEntry),409 BrokenSymlink(PathBuf),410}411412pub struct DirEntry {413 inner: DirEntryInner,414 metadata: OnceCell<Option<Metadata>>,415}416417impl DirEntry {418 fn normal(e: ignore::DirEntry) -> Self {419 Self {420 inner: DirEntryInner::Normal(e),421 metadata: OnceCell::new(),422 }423 }424425 fn broken_symlink(path: PathBuf) -> Self {426 Self {427 inner: DirEntryInner::BrokenSymlink(path),428 metadata: OnceCell::new(),429 }430 }431432 pub fn path(&self) -> &Path {433 match &self.inner {434 DirEntryInner::Normal(e) => e.path(),435 DirEntryInner::BrokenSymlink(pathbuf) => pathbuf.as_path(),436 }437 }438439 pub fn file_type(&self) -> Option<FileType> {440 match &self.inner {441 DirEntryInner::Normal(e) => e.file_type(),442 DirEntryInner::BrokenSymlink(_) => self.metadata().map(|m| m.file_type()),443 }444 }445446 pub fn metadata(&self) -> Option<&Metadata> {447 self.metadata448 .get_or_init(|| match &self.inner {449 DirEntryInner::Normal(e) => e.metadata().ok(),450 DirEntryInner::BrokenSymlink(path) => path.symlink_metadata().ok(),451 })452 .as_ref()453 }454455 pub fn depth(&self) -> Option<usize> {456 match &self.inner {457 DirEntryInner::Normal(e) => Some(e.depth()),458 DirEntryInner::BrokenSymlink(_) => None,459 }460 }461}462406fn spawn_senders(407 config: &Arc<Config>,408 quit_flag: &Arc<AtomicBool>,409 pattern: Arc<Regex>,
142 unmodified lines
552 return ignore::WalkState::Continue;553 }554 }555613 let send_result = tx_thread.send(WorkerResult::Entry(entry_path.to_owned()));556 let send_result = tx_thread.send(WorkerResult::Entry(entry));557558 if send_result.is_err() {559 return ignore::WalkState::Quit;560 }
10 unmodified lines