-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ls.rs
716 lines (652 loc) · 22.6 KB
/
ls.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// This file is part of the uutils coreutils package.
//
// (c) Jeremiah Peschka <jeremiah.peschka@gmail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
// spell-checker:ignore (ToDO) cpio svgz webm somegroup nlink rmvb xspf
extern crate getopts;
#[cfg(unix)]
extern crate isatty;
extern crate number_prefix;
extern crate term_grid;
extern crate termsize;
extern crate time;
extern crate unicode_width;
#[cfg(unix)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate uucore;
#[cfg(unix)]
use isatty::stdout_isatty;
use number_prefix::{decimal_prefix, Prefixed, Standalone};
use std::cmp::Reverse;
#[cfg(unix)]
use std::collections::HashMap;
use std::fs;
use std::fs::{DirEntry, FileType, Metadata};
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::fs::MetadataExt;
#[cfg(windows)]
use std::os::windows::fs::MetadataExt;
use std::path::{Path, PathBuf};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use time::{strftime, Timespec};
#[cfg(unix)]
use unicode_width::UnicodeWidthStr;
#[cfg(unix)]
use uucore::libc::{mode_t, S_ISGID, S_ISUID, S_ISVTX, S_IWOTH, S_IXGRP, S_IXOTH, S_IXUSR};
static NAME: &str = "ls";
static SUMMARY: &str = "";
static LONG_HELP: &str = "
By default, ls will list the files and contents of any directories on
the command line, expect that it will ignore files and directories
whose names start with '.'
";
#[cfg(unix)]
static DEFAULT_COLORS: &str = "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:";
#[cfg(unix)]
lazy_static! {
static ref LS_COLORS: String =
std::env::var("LS_COLORS").unwrap_or_else(|_| DEFAULT_COLORS.to_string());
static ref COLOR_MAP: HashMap<&'static str, &'static str> = {
let codes = LS_COLORS.split(':');
let mut map = HashMap::new();
for c in codes {
let p: Vec<_> = c.split('=').collect();
if p.len() == 2 {
map.insert(p[0], p[1]);
}
}
map
};
static ref RESET_CODE: &'static str = COLOR_MAP.get("rs").unwrap_or(&"0");
static ref LEFT_CODE: &'static str = COLOR_MAP.get("lc").unwrap_or(&"\x1b[");
static ref RIGHT_CODE: &'static str = COLOR_MAP.get("rc").unwrap_or(&"m");
static ref END_CODE: &'static str = COLOR_MAP.get("ec").unwrap_or(&"");
}
pub fn uumain(args: impl uucore::Args) -> i32 {
let args = args.collect_str();
let syntax = format!(
"[OPTION]... DIRECTORY
{0} [OPTION]... [FILE]...",
NAME
);
let matches = app!(&syntax, SUMMARY, LONG_HELP)
.optflag("1", "", "list one file per line.")
.optflag(
"a",
"all",
"Do not ignore hidden files (files with names that start with '.').",
)
.optflag(
"A",
"almost-all",
"In a directory, do not ignore all file names that start with '.', only ignore \
'.' and '..'.",
)
.optflag("B", "ignore-backups", "Ignore entries which end with ~.")
.optflag(
"c",
"",
"If the long listing format (e.g., -l, -o) is being used, print the status \
change time (the ‘ctime’ in the inode) instead of the modification time. When \
explicitly sorting by time (--sort=time or -t) or when not using a long listing \
format, sort according to the status change time.",
)
.optflag(
"d",
"directory",
"Only list the names of directories, rather than listing directory contents. \
This will not follow symbolic links unless one of `--dereference-command-line \
(-H)`, `--dereference (-L)`, or `--dereference-command-line-symlink-to-dir` is \
specified.",
)
.optflag(
"F",
"classify",
"Append a character to each file name indicating the file type. Also, for \
regular files that are executable, append '*'. The file type indicators are \
'/' for directories, '@' for symbolic links, '|' for FIFOs, '=' for sockets, \
'>' for doors, and nothing for regular files.",
)
.optflag(
"h",
"human-readable",
"Print human readable file sizes (e.g. 1K 234M 56G).",
)
.optflag("i", "inode", "print the index number of each file")
.optflag(
"L",
"dereference",
"When showing file information for a symbolic link, show information for the \
file the link references rather than the link itself.",
)
.optflag("l", "long", "Display detailed information.")
.optflag("n", "numeric-uid-gid", "-l with numeric UIDs and GIDs.")
.optflag(
"r",
"reverse",
"Reverse whatever the sorting method is--e.g., list files in reverse \
alphabetical order, youngest first, smallest first, or whatever.",
)
.optflag(
"R",
"recursive",
"List the contents of all directories recursively.",
)
.optflag("S", "", "Sort by file size, largest first.")
.optflag(
"t",
"",
"Sort by modification time (the 'mtime' in the inode), newest first.",
)
.optflag(
"U",
"",
"Do not sort; list the files in whatever order they are stored in the \
directory. This is especially useful when listing very large directories, \
since not doing any sorting can be noticeably faster.",
)
.optflagopt(
"",
"color",
"Color output based on file type.",
"always|auto|never",
)
.parse(args);
list(matches);
0
}
fn list(options: getopts::Matches) {
let locs: Vec<String> = if options.free.is_empty() {
vec![String::from(".")]
} else {
options.free.to_vec()
};
let mut files = Vec::<PathBuf>::new();
let mut dirs = Vec::<PathBuf>::new();
for loc in locs {
let p = PathBuf::from(&loc);
let mut dir = false;
if p.is_dir() && !options.opt_present("d") {
dir = true;
if options.opt_present("l") && !(options.opt_present("L")) {
if let Ok(md) = p.symlink_metadata() {
if md.file_type().is_symlink() && !p.ends_with("/") {
dir = false;
}
}
}
}
if dir {
dirs.push(p);
} else {
files.push(p);
}
}
sort_entries(&mut files, &options);
display_items(&files, None, &options);
sort_entries(&mut dirs, &options);
for dir in dirs {
if options.free.len() > 1 {
println!("\n{}:", dir.to_string_lossy());
}
enter_directory(&dir, &options);
}
}
#[cfg(any(unix, target_os = "redox"))]
fn sort_entries(entries: &mut Vec<PathBuf>, options: &getopts::Matches) {
let mut reverse = options.opt_present("r");
if options.opt_present("t") {
if options.opt_present("c") {
entries.sort_by_key(|k| {
Reverse(get_metadata(k, options).map(|md| md.ctime()).unwrap_or(0))
});
} else {
entries.sort_by_key(|k| {
// Newest first
Reverse(
get_metadata(k, options)
.and_then(|md| md.modified())
.unwrap_or(std::time::UNIX_EPOCH),
)
});
}
} else if options.opt_present("S") {
entries.sort_by_key(|k| get_metadata(k, options).map(|md| md.size()).unwrap_or(0));
reverse = !reverse;
} else if !options.opt_present("U") {
entries.sort();
}
if reverse {
entries.reverse();
}
}
#[cfg(windows)]
fn sort_entries(entries: &mut Vec<PathBuf>, options: &getopts::Matches) {
let mut reverse = options.opt_present("r");
if options.opt_present("t") {
entries.sort_by_key(|k| {
// Newest first
Reverse(
get_metadata(k, options)
.and_then(|md| md.modified())
.unwrap_or(std::time::UNIX_EPOCH),
)
});
} else if options.opt_present("S") {
entries.sort_by_key(|k| {
get_metadata(k, options)
.map(|md| md.file_size())
.unwrap_or(0)
});
reverse = !reverse;
} else if !options.opt_present("U") {
entries.sort();
}
if reverse {
entries.reverse();
}
}
fn max(lhs: usize, rhs: usize) -> usize {
if lhs > rhs {
lhs
} else {
rhs
}
}
fn should_display(entry: &DirEntry, options: &getopts::Matches) -> bool {
let ffi_name = entry.file_name();
let name = ffi_name.to_string_lossy();
if !options.opt_present("a") && !options.opt_present("A") && name.starts_with('.') {
return false;
}
if options.opt_present("B") && name.ends_with('~') {
return false;
}
true
}
fn enter_directory(dir: &PathBuf, options: &getopts::Matches) {
let mut entries: Vec<_> = safe_unwrap!(fs::read_dir(dir).and_then(Iterator::collect));
entries.retain(|e| should_display(e, options));
let mut entries: Vec<_> = entries.iter().map(DirEntry::path).collect();
sort_entries(&mut entries, options);
if options.opt_present("a") {
let mut display_entries = entries.clone();
display_entries.insert(0, dir.join(".."));
display_entries.insert(0, dir.join("."));
display_items(&display_entries, Some(dir), options);
} else {
display_items(&entries, Some(dir), options);
}
if options.opt_present("R") {
for e in entries.iter().filter(|p| p.is_dir()) {
println!("\n{}:", e.to_string_lossy());
enter_directory(&e, options);
}
}
}
fn get_metadata(entry: &PathBuf, options: &getopts::Matches) -> std::io::Result<Metadata> {
if options.opt_present("L") {
entry.metadata().or_else(|_| entry.symlink_metadata())
} else {
entry.symlink_metadata()
}
}
fn display_dir_entry_size(entry: &PathBuf, options: &getopts::Matches) -> (usize, usize) {
if let Ok(md) = get_metadata(entry, options) {
(
display_symlink_count(&md).len(),
display_file_size(&md, options).len(),
)
} else {
(0, 0)
}
}
fn pad_left(string: String, count: usize) -> String {
if count > string.len() {
let pad = count - string.len();
let pad = String::from_utf8(vec![b' '; pad]).unwrap();
format!("{}{}", pad, string)
} else {
string
}
}
fn display_items(items: &[PathBuf], strip: Option<&Path>, options: &getopts::Matches) {
if options.opt_present("long") || options.opt_present("numeric-uid-gid") {
let (mut max_links, mut max_size) = (1, 1);
for item in items {
let (links, size) = display_dir_entry_size(item, options);
max_links = max(links, max_links);
max_size = max(size, max_size);
}
for item in items {
display_item_long(item, strip, max_links, max_size, options);
}
} else {
if !options.opt_present("1") {
let names = items.iter().filter_map(|i| {
let md = get_metadata(i, options);
match md {
Err(e) => {
let filename = get_file_name(i, strip);
show_error!("{}: {}", filename, e);
None
}
Ok(md) => Some(display_file_name(&i, strip, &md, options)),
}
});
if let Some(size) = termsize::get() {
let mut grid = Grid::new(GridOptions {
filling: Filling::Spaces(2),
direction: Direction::TopToBottom,
});
for name in names {
grid.add(name);
}
if let Some(output) = grid.fit_into_width(size.cols as usize) {
print!("{}", output);
return;
}
}
}
// Couldn't display a grid, either because we don't know
// the terminal width or because fit_into_width failed
for i in items {
let md = get_metadata(i, options);
if let Ok(md) = md {
println!("{}", display_file_name(&i, strip, &md, options).contents);
}
}
}
}
use uucore::fs::display_permissions;
fn display_item_long(
item: &PathBuf,
strip: Option<&Path>,
max_links: usize,
max_size: usize,
options: &getopts::Matches,
) {
let md = match get_metadata(item, options) {
Err(e) => {
let filename = get_file_name(&item, strip);
show_error!("{}: {}", filename, e);
return;
}
Ok(md) => md,
};
println!(
"{}{}{} {} {} {} {} {} {}",
get_inode(&md, options),
display_file_type(md.file_type()),
display_permissions(&md),
pad_left(display_symlink_count(&md), max_links),
display_uname(&md, options),
display_group(&md, options),
pad_left(display_file_size(&md, options), max_size),
display_date(&md, options),
display_file_name(&item, strip, &md, options).contents
);
}
#[cfg(unix)]
fn get_inode(metadata: &Metadata, options: &getopts::Matches) -> String {
if options.opt_present("inode") {
format!("{:8} ", metadata.ino())
} else {
"".to_string()
}
}
#[cfg(not(unix))]
fn get_inode(_metadata: &Metadata, _options: &getopts::Matches) -> String {
"".to_string()
}
// Currently getpwuid is `linux` target only. If it's broken out into
// a posix-compliant attribute this can be updated...
#[cfg(unix)]
use uucore::entries;
#[cfg(unix)]
fn display_uname(metadata: &Metadata, options: &getopts::Matches) -> String {
if options.opt_present("numeric-uid-gid") {
metadata.uid().to_string()
} else {
entries::uid2usr(metadata.uid()).unwrap_or_else(|_| metadata.uid().to_string())
}
}
#[cfg(unix)]
fn display_group(metadata: &Metadata, options: &getopts::Matches) -> String {
if options.opt_present("numeric-uid-gid") {
metadata.gid().to_string()
} else {
entries::gid2grp(metadata.gid()).unwrap_or_else(|_| metadata.gid().to_string())
}
}
#[cfg(not(unix))]
#[allow(unused_variables)]
fn display_uname(metadata: &Metadata, _options: &getopts::Matches) -> String {
"somebody".to_string()
}
#[cfg(not(unix))]
#[allow(unused_variables)]
fn display_group(metadata: &Metadata, _options: &getopts::Matches) -> String {
"somegroup".to_string()
}
#[cfg(unix)]
fn display_date(metadata: &Metadata, options: &getopts::Matches) -> String {
let secs = if options.opt_present("c") {
metadata.ctime()
} else {
metadata.mtime()
};
let time = time::at(Timespec::new(secs, 0));
strftime("%F %R", &time).unwrap()
}
#[cfg(not(unix))]
#[allow(unused_variables)]
fn display_date(metadata: &Metadata, options: &getopts::Matches) -> String {
if let Ok(mtime) = metadata.modified() {
let time = time::at(Timespec::new(
mtime
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64,
0,
));
strftime("%F %R", &time).unwrap()
} else {
"???".to_string()
}
}
fn display_file_size(metadata: &Metadata, options: &getopts::Matches) -> String {
if options.opt_present("human-readable") {
match decimal_prefix(metadata.len() as f64) {
Standalone(bytes) => bytes.to_string(),
Prefixed(prefix, bytes) => format!("{:.2}{}", bytes, prefix).to_uppercase(),
}
} else {
metadata.len().to_string()
}
}
fn display_file_type(file_type: FileType) -> String {
if file_type.is_dir() {
"d".to_string()
} else if file_type.is_symlink() {
"l".to_string()
} else {
"-".to_string()
}
}
fn get_file_name(name: &Path, strip: Option<&Path>) -> String {
let mut name = match strip {
Some(prefix) => name.strip_prefix(prefix).unwrap_or(name),
None => name,
};
if name.as_os_str().is_empty() {
name = Path::new(".");
}
name.to_string_lossy().into_owned()
}
#[cfg(not(unix))]
fn display_file_name(
path: &Path,
strip: Option<&Path>,
metadata: &Metadata,
options: &getopts::Matches,
) -> Cell {
let mut name = get_file_name(path, strip);
if !options.opt_present("long") {
name = get_inode(metadata, options) + &name;
}
if options.opt_present("classify") {
let file_type = metadata.file_type();
if file_type.is_dir() {
name.push('/');
} else if file_type.is_symlink() {
name.push('@');
}
}
if options.opt_present("long") && metadata.file_type().is_symlink() {
if let Ok(target) = path.read_link() {
// We don't bother updating width here because it's not used for long listings
let target_name = target.to_string_lossy().to_string();
name.push_str(" -> ");
name.push_str(&target_name);
}
}
name.into()
}
#[cfg(unix)]
fn color_name(name: String, typ: &str) -> String {
let mut typ = typ;
if !COLOR_MAP.contains_key(typ) {
if typ == "or" {
typ = "ln";
} else if typ == "mi" {
typ = "fi";
}
};
if let Some(code) = COLOR_MAP.get(typ) {
format!(
"{}{}{}{}{}{}{}{}",
*LEFT_CODE, code, *RIGHT_CODE, name, *END_CODE, *LEFT_CODE, *RESET_CODE, *RIGHT_CODE,
)
} else {
name
}
}
#[cfg(unix)]
macro_rules! has {
($mode:expr, $perm:expr) => {
$mode & ($perm as mode_t) != 0
};
}
#[cfg(unix)]
#[allow(clippy::cognitive_complexity)]
fn display_file_name(
path: &Path,
strip: Option<&Path>,
metadata: &Metadata,
options: &getopts::Matches,
) -> Cell {
let mut name = get_file_name(path, strip);
if !options.opt_present("long") {
name = get_inode(metadata, options) + &name;
}
let mut width = UnicodeWidthStr::width(&*name);
let color = match options.opt_str("color") {
None => true,
Some(val) => match val.as_ref() {
"always" | "yes" | "force" => true,
"auto" | "tty" | "if-tty" => stdout_isatty(),
/* "never" | "no" | "none" | */ _ => false,
},
};
let classify = options.opt_present("classify");
let ext;
if color || classify {
let file_type = metadata.file_type();
let (code, sym) = if file_type.is_dir() {
("di", Some('/'))
} else if file_type.is_symlink() {
if path.exists() {
("ln", Some('@'))
} else {
("or", Some('@'))
}
} else if file_type.is_socket() {
("so", Some('='))
} else if file_type.is_fifo() {
("pi", Some('|'))
} else if file_type.is_block_device() {
("bd", None)
} else if file_type.is_char_device() {
("cd", None)
} else if file_type.is_file() {
let mode = metadata.mode() as mode_t;
let sym = if has!(mode, S_IXUSR | S_IXGRP | S_IXOTH) {
Some('*')
} else {
None
};
if has!(mode, S_ISUID) {
("su", sym)
} else if has!(mode, S_ISGID) {
("sg", sym)
} else if has!(mode, S_ISVTX) && has!(mode, S_IWOTH) {
("tw", sym)
} else if has!(mode, S_ISVTX) {
("st", sym)
} else if has!(mode, S_IWOTH) {
("ow", sym)
} else if has!(mode, S_IXUSR | S_IXGRP | S_IXOTH) {
("ex", sym)
} else if metadata.nlink() > 1 {
("mh", sym)
} else if let Some(e) = path.extension() {
ext = format!("*.{}", e.to_string_lossy());
(ext.as_str(), None)
} else {
("fi", None)
}
} else {
("", None)
};
if color {
name = color_name(name, code);
}
if classify {
if let Some(s) = sym {
name.push(s);
width += 1;
}
}
}
if options.opt_present("long") && metadata.file_type().is_symlink() {
if let Ok(target) = path.read_link() {
// We don't bother updating width here because it's not used for long listings
let code = if target.exists() { "fi" } else { "mi" };
let target_name = color_name(target.to_string_lossy().to_string(), code);
name.push_str(" -> ");
name.push_str(&target_name);
}
}
Cell {
contents: name,
width,
}
}
#[cfg(not(unix))]
#[allow(unused_variables)]
fn display_symlink_count(metadata: &Metadata) -> String {
// Currently not sure of how to get this on Windows, so I'm punting.
// Git Bash looks like it may do the same thing.
String::from("1")
}
#[cfg(unix)]
fn display_symlink_count(metadata: &Metadata) -> String {
metadata.nlink().to_string()
}