nu_command/filesystem/
ls.rs

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