Skip to main content

nu_command/filesystem/
ls.rs

1use crate::{DirBuilder, DirInfo};
2use chrono::{DateTime, Local, LocalResult, TimeZone, Utc};
3use nu_engine::{command_prelude::*, glob_from};
4use nu_glob::MatchOptions;
5use nu_path::{expand_path_with, expand_to_real_path};
6use nu_protocol::{
7    NuGlob, PipelineMetadata, Signals,
8    shell_error::{self, generic::GenericError, io::IoError},
9};
10use pathdiff::diff_paths;
11use rayon::prelude::*;
12#[cfg(unix)]
13use std::os::unix::fs::PermissionsExt;
14use std::{
15    cmp::Ordering,
16    fs::{DirEntry, Metadata},
17    path::PathBuf,
18    sync::{Arc, Mutex, mpsc},
19    time::{SystemTime, UNIX_EPOCH},
20};
21
22/// Entry from directory listing with cached metadata/file type to avoid repeated syscalls.
23/// On Windows, DirEntry::metadata() is free (no extra syscalls).
24/// On Unix, DirEntry::file_type() is usually free, but metadata requires stat().
25struct LsEntry {
26    path: PathBuf,
27    /// Cached metadata - on Windows this is free from DirEntry, on Unix we may need to fetch it later
28    #[cfg(windows)]
29    metadata: Option<Metadata>,
30    /// Cached file type - free on most platforms from DirEntry::file_type()
31    #[cfg(not(windows))]
32    file_type: Option<std::fs::FileType>,
33}
34
35impl LsEntry {
36    fn from_dir_entry(entry: &DirEntry) -> Self {
37        let path = entry.path();
38        #[cfg(windows)]
39        {
40            // On Windows, DirEntry::metadata() is free (no extra syscalls)
41            let metadata = entry.metadata().ok();
42            LsEntry { path, metadata }
43        }
44        #[cfg(not(windows))]
45        {
46            // On Unix, DirEntry::file_type() is free, but metadata requires stat()
47            let file_type = entry.file_type().ok();
48            LsEntry { path, file_type }
49        }
50    }
51
52    fn from_path(path: PathBuf) -> Self {
53        LsEntry {
54            path,
55            #[cfg(windows)]
56            metadata: None,
57            #[cfg(not(windows))]
58            file_type: None,
59        }
60    }
61
62    /// Check if this is a directory. Uses cached info if available.
63    fn is_dir(&self) -> bool {
64        #[cfg(windows)]
65        {
66            if let Some(ref md) = self.metadata {
67                return md.is_dir();
68            }
69        }
70        #[cfg(not(windows))]
71        {
72            if let Some(ref ft) = self.file_type {
73                return ft.is_dir();
74            }
75        }
76        // Fallback: need to query
77        self.path
78            .symlink_metadata()
79            .map(|m| m.file_type().is_dir())
80            .unwrap_or(false)
81    }
82
83    /// Check if this is hidden on the current platform.
84    #[cfg(windows)]
85    fn is_hidden(&self) -> bool {
86        use std::os::windows::fs::MetadataExt;
87        // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
88        const FILE_ATTRIBUTE_HIDDEN: u32 = 0x2;
89        if let Some(ref md) = self.metadata {
90            (md.file_attributes() & FILE_ATTRIBUTE_HIDDEN) != 0
91        } else {
92            // Fallback
93            self.path
94                .metadata()
95                .map(|m| (m.file_attributes() & FILE_ATTRIBUTE_HIDDEN) != 0)
96                .unwrap_or(false)
97        }
98    }
99
100    #[cfg(not(windows))]
101    fn is_hidden(&self) -> bool {
102        self.path
103            .file_name()
104            .map(|name| name.to_string_lossy().starts_with('.'))
105            .unwrap_or(false)
106    }
107
108    /// Get metadata, fetching it if not cached.
109    /// On Windows this should always be cached from DirEntry.
110    /// On Unix this will call symlink_metadata() if needed.
111    fn get_metadata(&self) -> Option<Metadata> {
112        #[cfg(windows)]
113        {
114            // If metadata was cached from DirEntry, use it; otherwise fetch it
115            // (needed for entries created via from_path, e.g., from glob results)
116            if self.metadata.is_some() {
117                self.metadata.clone()
118            } else {
119                std::fs::symlink_metadata(&self.path).ok()
120            }
121        }
122        #[cfg(not(windows))]
123        {
124            std::fs::symlink_metadata(&self.path).ok()
125        }
126    }
127}
128
129#[derive(Clone)]
130pub struct Ls;
131
132#[derive(Clone, Copy)]
133struct Args {
134    all: bool,
135    long: bool,
136    short_names: bool,
137    full_paths: bool,
138    du: bool,
139    directory: bool,
140    use_mime_type: bool,
141    use_threads: bool,
142    call_span: Span,
143}
144
145impl Command for Ls {
146    fn name(&self) -> &str {
147        "ls"
148    }
149
150    fn description(&self) -> &str {
151        "List the filenames, sizes, and modification times of items in a directory."
152    }
153
154    fn search_terms(&self) -> Vec<&str> {
155        vec!["dir"]
156    }
157
158    fn signature(&self) -> nu_protocol::Signature {
159        Signature::build("ls")
160            .input_output_types(vec![(Type::Nothing, Type::table())])
161            // LsGlobPattern is similar to string, it won't auto-expand
162            // and we use it to track if the user input is quoted.
163            .rest("pattern", SyntaxShape::OneOf(vec![SyntaxShape::GlobPattern, SyntaxShape::String]), "The glob pattern to use.")
164            .switch("all", "Show hidden files.", Some('a'))
165            .switch(
166                "long",
167                "Get all available columns for each entry (slower; columns are platform-dependent).",
168                Some('l'),
169            )
170            .switch(
171                "short-names",
172                "Only print the file names, and not the path.",
173                Some('s'),
174            )
175            .switch("full-paths", "Display paths as absolute paths.", Some('f'))
176            .switch(
177                "du",
178                "Display the apparent directory size (\"disk usage\") in place of the directory metadata size.",
179                Some('d'),
180            )
181            .switch(
182                "directory",
183                "List the specified directory itself instead of its contents.",
184                Some('D'),
185            )
186            .switch("mime-type", "Show mime-type in type column instead of 'file' (based on filenames only; files' contents are not examined).", Some('m'))
187            .switch("threads", "Use multiple threads to list contents. Output will be non-deterministic.", Some('t'))
188            .category(Category::FileSystem)
189    }
190
191    fn run(
192        &self,
193        engine_state: &EngineState,
194        stack: &mut Stack,
195        call: &Call,
196        _input: PipelineData,
197    ) -> Result<PipelineData, ShellError> {
198        let all = call.has_flag(engine_state, stack, "all")?;
199        let long = call.has_flag(engine_state, stack, "long")?;
200        let short_names = call.has_flag(engine_state, stack, "short-names")?;
201        let full_paths = call.has_flag(engine_state, stack, "full-paths")?;
202        let du = call.has_flag(engine_state, stack, "du")?;
203        let directory = call.has_flag(engine_state, stack, "directory")?;
204        let use_mime_type = call.has_flag(engine_state, stack, "mime-type")?;
205        let use_threads = call.has_flag(engine_state, stack, "threads")?;
206        let call_span = call.head;
207        let cwd = engine_state.cwd(Some(stack))?.into_std_path_buf();
208
209        let args = Args {
210            all,
211            long,
212            short_names,
213            full_paths,
214            du,
215            directory,
216            use_mime_type,
217            use_threads,
218            call_span,
219        };
220
221        let pattern_arg = call.rest::<Spanned<NuGlob>>(engine_state, stack, 0)?;
222        let input_pattern_arg = if !call.has_positional_args(stack, 0) {
223            None
224        } else {
225            Some(pattern_arg)
226        };
227        match input_pattern_arg {
228            None => Ok(
229                ls_for_one_pattern(None, args, engine_state.signals().clone(), cwd)?
230                    .into_pipeline_data_with_metadata(
231                        call_span,
232                        engine_state.signals().clone(),
233                        ls_pipeline_metadata(call_span, long),
234                    ),
235            ),
236            Some(pattern) => {
237                let mut result_iters = vec![];
238                for pat in pattern {
239                    result_iters.push(ls_for_one_pattern(
240                        Some(pat),
241                        args,
242                        engine_state.signals().clone(),
243                        cwd.clone(),
244                    )?)
245                }
246
247                // Here nushell needs to use
248                // use `flatten` to chain all iterators into one.
249                Ok(result_iters
250                    .into_iter()
251                    .flatten()
252                    .into_pipeline_data_with_metadata(
253                        call_span,
254                        engine_state.signals().clone(),
255                        ls_pipeline_metadata(call_span, long),
256                    ))
257            }
258        }
259    }
260
261    fn examples(&self) -> Vec<Example<'_>> {
262        vec![
263            Example {
264                description: "List visible files in the current directory.",
265                example: "ls",
266                result: None,
267            },
268            Example {
269                description: "List visible files in a subdirectory.",
270                example: "ls subdir",
271                result: None,
272            },
273            Example {
274                description: "List visible files with full path in the parent directory.",
275                example: "ls -f ..",
276                result: None,
277            },
278            Example {
279                description: "List Rust files.",
280                example: "ls *.rs",
281                result: None,
282            },
283            Example {
284                description: "List files and directories whose name do not contain 'bar'.",
285                example: "ls | where name !~ bar",
286                result: None,
287            },
288            Example {
289                description: "List the full path of all dirs in your home directory.",
290                example: "ls -a ~ | where type == dir",
291                result: None,
292            },
293            Example {
294                description: "List only the names (not paths) of all dirs in your home directory which have not been modified in 7 days.",
295                example: "ls -as ~ | where type == dir and modified < ((date now) - 7day)",
296                result: None,
297            },
298            Example {
299                description: "Recursively list all files and subdirectories under the current directory using a glob pattern.",
300                example: "ls -a **/*",
301                result: None,
302            },
303            Example {
304                description: "Recursively list *.rs and *.toml files using the glob command.",
305                example: "ls ...(glob **/*.{rs,toml})",
306                result: None,
307            },
308            Example {
309                description: "List given paths and show directories themselves.",
310                example: "['/path/to/directory' '/path/to/file'] | each {|| ls -D $in } | flatten",
311                result: None,
312            },
313        ]
314    }
315}
316
317/// Builds `ls` output metadata, including width-priority hints for compact views.
318fn ls_pipeline_metadata(span: Span, long: bool) -> PipelineMetadata {
319    let mut metadata = PipelineMetadata {
320        path_columns: vec!["name".to_string()],
321        ..Default::default()
322    };
323
324    // Keep long listings close to legacy layout; priority hints are most useful in compact views.
325    if !long {
326        metadata.set_table_width_priority_columns(span, ["name"]);
327    }
328
329    metadata
330}
331
332fn ls_for_one_pattern(
333    pattern_arg: Option<Spanned<NuGlob>>,
334    args: Args,
335    signals: Signals,
336    cwd: PathBuf,
337) -> Result<PipelineData, ShellError> {
338    fn create_pool(num_threads: usize, call_span: Span) -> Result<rayon::ThreadPool, ShellError> {
339        match rayon::ThreadPoolBuilder::new()
340            .num_threads(num_threads)
341            .build()
342        {
343            Err(e) => Err(e).map_err(|e| {
344                ShellError::Generic(GenericError::new(
345                    "Error creating thread pool",
346                    e.to_string(),
347                    call_span,
348                ))
349            }),
350            Ok(pool) => Ok(pool),
351        }
352    }
353
354    let (tx, rx) = mpsc::channel();
355
356    let Args {
357        all,
358        long,
359        short_names,
360        full_paths,
361        du,
362        directory,
363        use_mime_type,
364        use_threads,
365        call_span,
366    } = args;
367    let pattern_arg = {
368        if let Some(path) = pattern_arg {
369            // it makes no sense to list an empty string.
370            if path.item.as_ref().is_empty() {
371                return Err(ShellError::Io(IoError::new_with_additional_context(
372                    shell_error::io::ErrorKind::from_std(std::io::ErrorKind::NotFound),
373                    path.span,
374                    PathBuf::from(path.item.to_string()),
375                    "empty string('') directory or file does not exist",
376                )));
377            }
378            Some(path.map(NuGlob::strip_ansi_string_unlikely))
379        } else {
380            pattern_arg
381        }
382    };
383
384    let mut just_read_dir = false;
385    let p_tag: Span = pattern_arg.as_ref().map(|p| p.span).unwrap_or(call_span);
386    let (pattern_arg, absolute_path) = match pattern_arg {
387        Some(pat) => {
388            // expand with cwd here is only used for checking
389            let tmp_expanded =
390                nu_path::expand_path_with(pat.item.as_ref(), &cwd, pat.item.is_expand());
391            // Avoid checking and pushing "*" to the path when directory (do not show contents) flag is true
392            if !directory && tmp_expanded.is_dir() {
393                if read_dir(tmp_expanded, p_tag, use_threads, signals.clone())?
394                    .next()
395                    .is_none()
396                {
397                    return Ok(Value::test_nothing().into_pipeline_data());
398                }
399                just_read_dir =
400                    !(pat.item.is_expand() && nu_glob::is_glob_with_backend(pat.item.as_ref()));
401            }
402
403            // it's absolute path if:
404            // 1. pattern is absolute.
405            // 2. pattern can be expanded, and after expands to real_path, it's absolute.
406            //    here `expand_to_real_path` call is required, because `~/aaa` should be absolute
407            //    path.
408            let absolute_path = Path::new(pat.item.as_ref()).is_absolute()
409                || (pat.item.is_expand() && expand_to_real_path(pat.item.as_ref()).is_absolute());
410            (pat.item, absolute_path)
411        }
412        None => {
413            // Avoid pushing "*" to the default path when directory (do not show contents) flag is true
414            if directory {
415                (NuGlob::Expand(".".to_string()), false)
416            } else if read_dir(cwd.clone(), p_tag, use_threads, signals.clone())?
417                .next()
418                .is_none()
419            {
420                return Ok(Value::test_nothing().into_pipeline_data());
421            } else {
422                (NuGlob::Expand("*".to_string()), false)
423            }
424        }
425    };
426
427    let hidden_dir_specified = is_hidden_dir(pattern_arg.as_ref());
428
429    let path = pattern_arg.into_spanned(p_tag);
430    let (prefix, paths): (
431        Option<PathBuf>,
432        Box<dyn Iterator<Item = Result<LsEntry, ShellError>> + Send>,
433    ) = if just_read_dir {
434        let expanded = nu_path::expand_path_with(path.item.as_ref(), &cwd, path.item.is_expand());
435        let paths = read_dir(expanded.clone(), p_tag, use_threads, signals.clone())?;
436        // just need to read the directory, so prefix is path itself.
437        (Some(expanded), paths)
438    } else {
439        let glob_options = if all {
440            None
441        } else {
442            let glob_options = MatchOptions {
443                recursive_match_hidden_dir: false,
444                ..Default::default()
445            };
446            Some(glob_options)
447        };
448        let (prefix, glob_paths) =
449            glob_from(&path, &cwd, call_span, glob_options, signals.clone())?;
450        // Convert PathBuf results to LsEntry (without cached file type from glob)
451        let paths = glob_paths.map(|r| r.map(LsEntry::from_path));
452        (prefix, Box::new(paths))
453    };
454
455    let mut paths_peek = paths.peekable();
456    let no_matches = paths_peek.peek().is_none();
457    signals.check(&call_span)?;
458    if no_matches {
459        return Err(ShellError::Generic(
460            GenericError::new(
461                format!("No matches found for {:?}", path.item),
462                "Pattern, file or folder not found",
463                p_tag,
464            )
465            .with_help("no matches found"),
466        ));
467    }
468
469    let hidden_dirs = Arc::new(Mutex::new(Vec::new()));
470
471    let signals_clone = signals.clone();
472
473    let pool = if use_threads {
474        let count = std::thread::available_parallelism()
475            .map_err(|err| {
476                IoError::new_with_additional_context(
477                    err,
478                    call_span,
479                    None,
480                    "Could not get available parallelism",
481                )
482            })?
483            .get();
484        create_pool(count, call_span)?
485    } else {
486        create_pool(1, call_span)?
487    };
488
489    pool.install(|| {
490        rayon::spawn(move || {
491            let result = paths_peek
492                .par_bridge()
493                .filter_map(move |x| match x {
494                    Ok(entry) => {
495                        let hidden_dir_clone = Arc::clone(&hidden_dirs);
496                        let mut hidden_dir_mutex = hidden_dir_clone
497                            .lock()
498                            .expect("Unable to acquire lock for hidden_dirs");
499                        if path_contains_hidden_folder(&entry.path, &hidden_dir_mutex) {
500                            return None;
501                        }
502
503                        if !all && !hidden_dir_specified && entry.is_hidden() {
504                            if entry.is_dir() {
505                                hidden_dir_mutex.push(entry.path.clone());
506                                drop(hidden_dir_mutex);
507                            }
508                            return None;
509                        }
510                        // Get reference to path first for display_name calculation
511                        let path = &entry.path;
512
513                        let display_name = if short_names {
514                            path.file_name().map(|os| os.to_string_lossy().to_string())
515                        } else if full_paths || absolute_path {
516                            Some(path.to_string_lossy().to_string())
517                        } else if let Some(prefix) = &prefix {
518                            if let Ok(remainder) = path.strip_prefix(prefix) {
519                                if directory {
520                                    // When the path is the same as the cwd, path_diff should be "."
521                                    let path_diff = if let Some(path_diff_not_dot) =
522                                        diff_paths(path, &cwd)
523                                    {
524                                        let path_diff_not_dot = path_diff_not_dot.to_string_lossy();
525                                        if path_diff_not_dot.is_empty() {
526                                            ".".to_string()
527                                        } else {
528                                            path_diff_not_dot.to_string()
529                                        }
530                                    } else {
531                                        path.to_string_lossy().to_string()
532                                    };
533
534                                    Some(path_diff)
535                                } else {
536                                    let new_prefix = if let Some(pfx) = diff_paths(prefix, &cwd) {
537                                        pfx
538                                    } else {
539                                        prefix.to_path_buf()
540                                    };
541
542                                    Some(new_prefix.join(remainder).to_string_lossy().to_string())
543                                }
544                            } else {
545                                Some(path.to_string_lossy().to_string())
546                            }
547                        } else {
548                            Some(path.to_string_lossy().to_string())
549                        }
550                        .ok_or_else(|| {
551                            ShellError::Generic(GenericError::new(
552                                format!("Invalid file name: {:}", path.to_string_lossy()),
553                                "invalid file name",
554                                call_span,
555                            ))
556                        });
557
558                        match display_name {
559                            Ok(name) => {
560                                // Use cached metadata from LsEntry when available (free on Windows)
561                                // On Unix, this will call symlink_metadata() but only once per entry
562                                let metadata = entry.get_metadata();
563                                // When full_paths is enabled, ensure path is absolute for symlink target expansion
564                                let path_for_dict = if full_paths && !path.is_absolute() {
565                                    std::borrow::Cow::Owned(cwd.join(path))
566                                } else {
567                                    std::borrow::Cow::Borrowed(path)
568                                };
569                                let result = dir_entry_dict(
570                                    &path_for_dict,
571                                    &name,
572                                    metadata.as_ref(),
573                                    call_span,
574                                    long,
575                                    du,
576                                    &signals_clone,
577                                    use_mime_type,
578                                    full_paths,
579                                );
580                                match result {
581                                    Ok(value) => Some(value),
582                                    Err(err) => Some(Value::error(err, call_span)),
583                                }
584                            }
585                            Err(err) => Some(Value::error(err, call_span)),
586                        }
587                    }
588                    Err(err) => Some(Value::error(err, call_span)),
589                })
590                .try_for_each(|stream| {
591                    tx.send(stream).map_err(|e| {
592                        ShellError::Generic(GenericError::new(
593                            "Error streaming data",
594                            e.to_string(),
595                            call_span,
596                        ))
597                    })
598                })
599                .map_err(|err| {
600                    ShellError::Generic(GenericError::new(
601                        "Unable to create a rayon pool",
602                        err.to_string(),
603                        call_span,
604                    ))
605                });
606
607            if let Err(error) = result {
608                let _ = tx.send(Value::error(error, call_span));
609            }
610        });
611    });
612
613    Ok(rx
614        .into_iter()
615        .into_pipeline_data(call_span, signals.clone()))
616}
617
618fn is_hidden_dir(dir: impl AsRef<Path>) -> bool {
619    #[cfg(windows)]
620    {
621        use std::os::windows::fs::MetadataExt;
622
623        if let Ok(metadata) = dir.as_ref().metadata() {
624            let attributes = metadata.file_attributes();
625            // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
626            (attributes & 0x2) != 0
627        } else {
628            false
629        }
630    }
631
632    #[cfg(not(windows))]
633    {
634        dir.as_ref()
635            .file_name()
636            .map(|name| name.to_string_lossy().starts_with('.'))
637            .unwrap_or(false)
638    }
639}
640
641fn path_contains_hidden_folder(path: &Path, folders: &[PathBuf]) -> bool {
642    if folders.iter().any(|p| path.starts_with(p.as_path())) {
643        return true;
644    }
645    false
646}
647
648#[cfg(unix)]
649use std::os::unix::fs::FileTypeExt;
650use std::path::Path;
651
652pub fn get_file_type(md: &std::fs::Metadata, display_name: &str, use_mime_type: bool) -> String {
653    let ft = md.file_type();
654    let mut file_type = "unknown";
655    if ft.is_dir() {
656        file_type = "dir";
657    } else if ft.is_file() {
658        file_type = "file";
659    } else if ft.is_symlink() {
660        file_type = "symlink";
661    } else {
662        #[cfg(unix)]
663        {
664            if ft.is_block_device() {
665                file_type = "block device";
666            } else if ft.is_char_device() {
667                file_type = "char device";
668            } else if ft.is_fifo() {
669                file_type = "pipe";
670            } else if ft.is_socket() {
671                file_type = "socket";
672            }
673        }
674    }
675    if use_mime_type {
676        let guess = mime_guess::from_path(display_name);
677        let mime_guess = match guess.first() {
678            Some(mime_type) => mime_type.essence_str().to_string(),
679            None => "unknown".to_string(),
680        };
681        if file_type == "file" {
682            mime_guess
683        } else {
684            file_type.to_string()
685        }
686    } else {
687        file_type.to_string()
688    }
689}
690
691/// Escape control characters in filenames so they are displayed visibly
692/// rather than being interpreted by the terminal.
693fn escape_filename_control_chars(name: &str) -> String {
694    if !name.chars().any(|c| c.is_control()) {
695        return name.to_string();
696    }
697
698    let mut buf = String::with_capacity(name.len());
699    for c in name.chars() {
700        if c.is_control() {
701            buf.extend(c.escape_unicode());
702        } else {
703            buf.push(c);
704        }
705    }
706    buf
707}
708
709#[allow(clippy::too_many_arguments)]
710pub(crate) fn dir_entry_dict(
711    filename: &std::path::Path, // absolute path
712    display_name: &str,         // file name to be displayed
713    metadata: Option<&std::fs::Metadata>,
714    span: Span,
715    long: bool,
716    du: bool,
717    signals: &Signals,
718    use_mime_type: bool,
719    full_symlink_target: bool,
720) -> Result<Value, ShellError> {
721    #[cfg(windows)]
722    if metadata.is_none() {
723        return Ok(windows_helper::dir_entry_dict_windows_fallback(
724            filename,
725            display_name,
726            span,
727            long,
728        ));
729    }
730
731    let mut record = Record::new();
732    let mut file_type = "unknown".to_string();
733
734    record.push(
735        "name",
736        Value::string(escape_filename_control_chars(display_name), span),
737    );
738
739    if let Some(md) = metadata {
740        file_type = get_file_type(md, display_name, use_mime_type);
741        record.push("type", Value::string(file_type.clone(), span));
742    } else {
743        record.push("type", Value::nothing(span));
744    }
745
746    if long && let Some(md) = metadata {
747        record.push(
748            "target",
749            if md.file_type().is_symlink() {
750                if let Ok(path_to_link) = filename.read_link() {
751                    // Actually `filename` should always have a parent because it's a symlink.
752                    // But for safety, we check `filename.parent().is_some()` first.
753                    if full_symlink_target && filename.parent().is_some() {
754                        Value::string(
755                            expand_path_with(
756                                path_to_link,
757                                filename
758                                    .parent()
759                                    .expect("already check the filename have a parent"),
760                                true,
761                            )
762                            .to_string_lossy(),
763                            span,
764                        )
765                    } else {
766                        Value::string(path_to_link.to_string_lossy(), span)
767                    }
768                } else {
769                    Value::string("Could not obtain target file's path", span)
770                }
771            } else {
772                Value::nothing(span)
773            },
774        )
775    }
776
777    if long && let Some(md) = metadata {
778        record.push("readonly", Value::bool(md.permissions().readonly(), span));
779
780        #[cfg(unix)]
781        {
782            use nu_utils::filesystem::users;
783            use std::os::unix::fs::MetadataExt;
784
785            let mode = md.permissions().mode();
786            record.push(
787                "mode",
788                Value::string(umask::Mode::from(mode).to_string(), span),
789            );
790
791            let nlinks = md.nlink();
792            record.push("num_links", Value::int(nlinks as i64, span));
793
794            let inode = md.ino();
795            record.push("inode", Value::int(inode as i64, span));
796
797            record.push(
798                "user",
799                if let Some(user) = users::get_user_by_uid(md.uid().into()) {
800                    Value::string(user.name, span)
801                } else {
802                    Value::int(md.uid().into(), span)
803                },
804            );
805
806            record.push(
807                "group",
808                if let Some(group) = users::get_group_by_gid(md.gid().into()) {
809                    Value::string(group.name, span)
810                } else {
811                    Value::int(md.gid().into(), span)
812                },
813            );
814        }
815    }
816
817    record.push(
818        "size",
819        if let Some(md) = metadata {
820            let zero_sized = file_type == "pipe"
821                || file_type == "socket"
822                || file_type == "char device"
823                || file_type == "block device";
824
825            if md.is_dir() {
826                if du {
827                    let params = DirBuilder::new(Span::new(0, 2), None, false, None, false);
828                    let dir_size = DirInfo::new(filename, &params, None, span, signals)?.get_size();
829
830                    Value::filesize(dir_size as i64, span)
831                } else {
832                    let dir_size: u64 = md.len();
833
834                    Value::filesize(dir_size as i64, span)
835                }
836            } else if md.is_file() {
837                Value::filesize(md.len() as i64, span)
838            } else if md.file_type().is_symlink() {
839                if let Ok(symlink_md) = filename.symlink_metadata() {
840                    Value::filesize(symlink_md.len() as i64, span)
841                } else {
842                    Value::nothing(span)
843                }
844            } else if zero_sized {
845                Value::filesize(0, span)
846            } else {
847                Value::nothing(span)
848            }
849        } else {
850            Value::nothing(span)
851        },
852    );
853
854    if let Some(md) = metadata {
855        if long {
856            record.push("created", {
857                let mut val = Value::nothing(span);
858                if let Ok(c) = md.created()
859                    && let Some(local) = try_convert_to_local_date_time(c)
860                {
861                    val = Value::date(local.with_timezone(local.offset()), span);
862                }
863                val
864            });
865
866            record.push("accessed", {
867                let mut val = Value::nothing(span);
868                if let Ok(a) = md.accessed()
869                    && let Some(local) = try_convert_to_local_date_time(a)
870                {
871                    val = Value::date(local.with_timezone(local.offset()), span)
872                }
873                val
874            });
875        }
876
877        record.push("modified", {
878            let mut val = Value::nothing(span);
879            if let Ok(m) = md.modified()
880                && let Some(local) = try_convert_to_local_date_time(m)
881            {
882                val = Value::date(local.with_timezone(local.offset()), span);
883            }
884            val
885        })
886    } else {
887        if long {
888            record.push("created", Value::nothing(span));
889            record.push("accessed", Value::nothing(span));
890        }
891
892        record.push("modified", Value::nothing(span));
893    }
894
895    Ok(Value::record(record, span))
896}
897
898// TODO: can we get away from local times in `ls`? internals might be cleaner if we worked in UTC
899// and left the conversion to local time to the display layer
900fn try_convert_to_local_date_time(t: SystemTime) -> Option<DateTime<Local>> {
901    // Adapted from https://github.com/chronotope/chrono/blob/v0.4.19/src/datetime.rs#L755-L767.
902    let (sec, nsec) = match t.duration_since(UNIX_EPOCH) {
903        Ok(dur) => (dur.as_secs() as i64, dur.subsec_nanos()),
904        Err(e) => {
905            // unlikely but should be handled
906            let dur = e.duration();
907            let (sec, nsec) = (dur.as_secs() as i64, dur.subsec_nanos());
908            if nsec == 0 {
909                (-sec, 0)
910            } else {
911                (-sec - 1, 1_000_000_000 - nsec)
912            }
913        }
914    };
915
916    const NEG_UNIX_EPOCH: i64 = -11644473600; // t was invalid 0, UNIX_EPOCH subtracted above.
917    if sec == NEG_UNIX_EPOCH {
918        // do not tz lookup invalid SystemTime
919        return None;
920    }
921    match Utc.timestamp_opt(sec, nsec) {
922        LocalResult::Single(t) => Some(t.with_timezone(&Local)),
923        _ => None,
924    }
925}
926
927// #[cfg(windows)] is just to make Clippy happy, remove if you ever want to use this on other platforms
928#[cfg(windows)]
929fn unix_time_to_local_date_time(secs: i64) -> Option<DateTime<Local>> {
930    match Utc.timestamp_opt(secs, 0) {
931        LocalResult::Single(t) => Some(t.with_timezone(&Local)),
932        _ => None,
933    }
934}
935
936#[cfg(windows)]
937mod windows_helper {
938    use super::*;
939
940    use nu_protocol::shell_error;
941    use std::os::windows::prelude::OsStrExt;
942    use windows::Win32::Foundation::FILETIME;
943    use windows::Win32::Storage::FileSystem::{
944        FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_REPARSE_POINT, FindClose,
945        FindFirstFileW, WIN32_FIND_DATAW,
946    };
947    use windows::Win32::System::SystemServices::{
948        IO_REPARSE_TAG_MOUNT_POINT, IO_REPARSE_TAG_SYMLINK,
949    };
950
951    /// A secondary way to get file info on Windows, for when std::fs::symlink_metadata() fails.
952    /// dir_entry_dict depends on metadata, but that can't be retrieved for some Windows system files:
953    /// https://github.com/rust-lang/rust/issues/96980
954    pub fn dir_entry_dict_windows_fallback(
955        filename: &Path,
956        display_name: &str,
957        span: Span,
958        long: bool,
959    ) -> Value {
960        let mut record = Record::new();
961
962        record.push(
963            "name",
964            Value::string(escape_filename_control_chars(display_name), span),
965        );
966
967        let find_data = match find_first_file(filename, span) {
968            Ok(fd) => fd,
969            Err(e) => {
970                // Sometimes this happens when the file name is not allowed on Windows (ex: ends with a '.', pipes)
971                // For now, we just log it and give up on returning metadata columns
972                // TODO: find another way to get this data (like cmd.exe, pwsh, and MINGW bash can)
973                log::error!("ls: '{}' {}", filename.to_string_lossy(), e);
974                return Value::record(record, span);
975            }
976        };
977
978        record.push(
979            "type",
980            Value::string(get_file_type_windows_fallback(&find_data), span),
981        );
982
983        if long {
984            record.push(
985                "target",
986                if is_symlink(&find_data) {
987                    if let Ok(path_to_link) = filename.read_link() {
988                        Value::string(path_to_link.to_string_lossy(), span)
989                    } else {
990                        Value::string("Could not obtain target file's path", span)
991                    }
992                } else {
993                    Value::nothing(span)
994                },
995            );
996
997            record.push(
998                "readonly",
999                Value::bool(
1000                    find_data.dwFileAttributes & FILE_ATTRIBUTE_READONLY.0 != 0,
1001                    span,
1002                ),
1003            );
1004        }
1005
1006        let file_size = ((find_data.nFileSizeHigh as u64) << 32) | find_data.nFileSizeLow as u64;
1007        record.push("size", Value::filesize(file_size as i64, span));
1008
1009        if long {
1010            record.push("created", {
1011                let mut val = Value::nothing(span);
1012                let seconds_since_unix_epoch = unix_time_from_filetime(&find_data.ftCreationTime);
1013                if let Some(local) = unix_time_to_local_date_time(seconds_since_unix_epoch) {
1014                    val = Value::date(local.with_timezone(local.offset()), span);
1015                }
1016                val
1017            });
1018
1019            record.push("accessed", {
1020                let mut val = Value::nothing(span);
1021                let seconds_since_unix_epoch = unix_time_from_filetime(&find_data.ftLastAccessTime);
1022                if let Some(local) = unix_time_to_local_date_time(seconds_since_unix_epoch) {
1023                    val = Value::date(local.with_timezone(local.offset()), span);
1024                }
1025                val
1026            });
1027        }
1028
1029        record.push("modified", {
1030            let mut val = Value::nothing(span);
1031            let seconds_since_unix_epoch = unix_time_from_filetime(&find_data.ftLastWriteTime);
1032            if let Some(local) = unix_time_to_local_date_time(seconds_since_unix_epoch) {
1033                val = Value::date(local.with_timezone(local.offset()), span);
1034            }
1035            val
1036        });
1037
1038        Value::record(record, span)
1039    }
1040
1041    fn unix_time_from_filetime(ft: &FILETIME) -> i64 {
1042        /// January 1, 1970 as Windows file time
1043        const EPOCH_AS_FILETIME: u64 = 116444736000000000;
1044        const HUNDREDS_OF_NANOSECONDS: u64 = 10000000;
1045
1046        let time_u64 = ((ft.dwHighDateTime as u64) << 32) | (ft.dwLowDateTime as u64);
1047        if time_u64 > 0 {
1048            let rel_to_linux_epoch = time_u64.saturating_sub(EPOCH_AS_FILETIME);
1049            let seconds_since_unix_epoch = rel_to_linux_epoch / HUNDREDS_OF_NANOSECONDS;
1050            return seconds_since_unix_epoch as i64;
1051        }
1052        0
1053    }
1054
1055    // wrapper around the FindFirstFileW Win32 API
1056    fn find_first_file(filename: &Path, span: Span) -> Result<WIN32_FIND_DATAW, ShellError> {
1057        unsafe {
1058            let mut find_data = WIN32_FIND_DATAW::default();
1059            // The windows crate really needs a nicer way to do string conversions
1060            let filename_wide: Vec<u16> = filename
1061                .as_os_str()
1062                .encode_wide()
1063                .chain(std::iter::once(0))
1064                .collect();
1065
1066            match FindFirstFileW(
1067                windows::core::PCWSTR(filename_wide.as_ptr()),
1068                &mut find_data,
1069            ) {
1070                Ok(handle) => {
1071                    // Don't forget to close the Find handle
1072                    // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew#remarks
1073                    // Assumption: WIN32_FIND_DATAW is a pure data struct, so we can let our
1074                    // find_data outlive the handle.
1075                    let _ = FindClose(handle);
1076                    Ok(find_data)
1077                }
1078                Err(e) => Err(ShellError::Io(IoError::new_with_additional_context(
1079                    shell_error::io::ErrorKind::from_std(std::io::ErrorKind::Other),
1080                    span,
1081                    PathBuf::from(filename),
1082                    format!("Could not read metadata: {e}"),
1083                ))),
1084            }
1085        }
1086    }
1087
1088    fn get_file_type_windows_fallback(find_data: &WIN32_FIND_DATAW) -> String {
1089        if find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY.0 != 0 {
1090            return "dir".to_string();
1091        }
1092
1093        if is_symlink(find_data) {
1094            return "symlink".to_string();
1095        }
1096
1097        "file".to_string()
1098    }
1099
1100    fn is_symlink(find_data: &WIN32_FIND_DATAW) -> bool {
1101        if find_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT.0 != 0 {
1102            // Follow Golang's lead in treating mount points as symlinks.
1103            // https://github.com/golang/go/blob/016d7552138077741a9c3fdadc73c0179f5d3ff7/src/os/types_windows.go#L104-L105
1104            if find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK
1105                || find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT
1106            {
1107                return true;
1108            }
1109        }
1110        false
1111    }
1112}
1113
1114#[allow(clippy::type_complexity)]
1115fn read_dir(
1116    f: PathBuf,
1117    span: Span,
1118    use_threads: bool,
1119    signals: Signals,
1120) -> Result<Box<dyn Iterator<Item = Result<LsEntry, ShellError>> + Send>, ShellError> {
1121    let signals_clone = signals.clone();
1122    let items = f
1123        .read_dir()
1124        .map_err(|err| IoError::new(err, span, f.clone()))?
1125        .map(move |d| {
1126            signals_clone.check(&span)?;
1127            d.map(|entry| LsEntry::from_dir_entry(&entry))
1128                .map_err(|err| IoError::new(err, span, f.clone()))
1129                .map_err(ShellError::from)
1130        });
1131    if !use_threads {
1132        let mut collected = items.collect::<Vec<_>>();
1133        signals.check(&span)?;
1134        collected.sort_by(|a, b| match (a, b) {
1135            (Ok(a), Ok(b)) => a.path.cmp(&b.path),
1136            (Ok(_), Err(_)) => Ordering::Greater,
1137            (Err(_), Ok(_)) => Ordering::Less,
1138            (Err(_), Err(_)) => Ordering::Equal,
1139        });
1140        return Ok(Box::new(collected.into_iter()));
1141    }
1142    Ok(Box::new(items))
1143}
1144
1145#[cfg(test)]
1146mod tests {
1147    use super::escape_filename_control_chars;
1148
1149    #[test]
1150    fn escape_filename_control_chars_renders_control_chars_visibly() {
1151        // Normal filenames pass through unchanged
1152        assert_eq!(escape_filename_control_chars("hello.txt"), "hello.txt");
1153        // ESC (0x1b) is escaped to its unicode representation
1154        assert_eq!(escape_filename_control_chars("hooks\x1bE"), "hooks\\u{1b}E");
1155        // NUL byte
1156        assert_eq!(
1157            escape_filename_control_chars("file\x00name"),
1158            "file\\u{0}name"
1159        );
1160        // Multiple control characters
1161        assert_eq!(
1162            escape_filename_control_chars("\x01a\x02b"),
1163            "\\u{1}a\\u{2}b"
1164        );
1165    }
1166}