Skip to content

Commit 368dcf4

Browse files
committed
output "file" or "dir" when "--follow-symlink" option
1 parent 3e3cf87 commit 368dcf4

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

src/main.rs

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use anyhow::Result;
22
use clap::Clap;
33
use serde::Serialize;
44
use std::io::Write;
5-
use std::process::Output;
65
use std::rc::Rc;
76

87
#[derive(Debug, thiserror::Error)]
@@ -251,6 +250,42 @@ fn output_symlink_info<'a>(
251250
Ok((ctx, len))
252251
}
253252

253+
fn output_symlink_file_info<'a>(
254+
ctx: FindContext<'a>,
255+
path: &std::path::Path,
256+
link_target: &std::path::Path,
257+
link_path: &std::path::Path,
258+
meta: Option<std::fs::Metadata>,
259+
) -> Result<(FindContext<'a>, u64)> {
260+
if !check_include_exclude_path(path, &ctx.include, &ctx.exclude, &ctx.match_option) {
261+
return Ok((ctx, 0));
262+
}
263+
let parent = ctx.path.clone();
264+
let (l, modified) = match meta {
265+
Some(v) => {
266+
let l = v.len();
267+
let modified = match v.modified() {
268+
Ok(v) => Some(v),
269+
Err(e) => {
270+
eprintln!("failed to transform modified to datetime({}): {:?}", link_path.to_string_lossy(), e);
271+
None
272+
}
273+
};
274+
(Some(l), modified)
275+
},
276+
None => (None, None)
277+
};
278+
if !ctx.dir_only {
279+
write_record(ctx.output_stream,
280+
path.to_string_lossy().as_ref(),
281+
Some(link_target.to_string_lossy().as_ref()),
282+
"file",
283+
l,
284+
modified)?;
285+
}
286+
Ok((ctx.with_path(parent.as_path()), l.unwrap_or(0)))
287+
}
288+
254289
fn output_file_info<'a>(
255290
mut ctx: FindContext<'a>,
256291
path: &std::path::Path,
@@ -373,9 +408,9 @@ fn retrieve_symlink<'a>(
373408
meta: Option<std::fs::Metadata>,
374409
depth: i32,
375410
) -> Result<(FindContext<'a>, u64)> {
376-
// if depth >= ctx.max_depth {
377-
// return Ok((ctx.with_path(parent), 0));
378-
// }
411+
if depth >= ctx.max_depth + 1 {
412+
return Ok((ctx.with_path(parent), 0));
413+
}
379414
let path = ctx.path.clone();
380415
let link_target = match std::fs::read_link(path.clone()) {
381416
Ok(v) => v,
@@ -403,27 +438,28 @@ fn retrieve_symlink<'a>(
403438
return retrieve_symlink(ctx, parent, Some(v), depth + 1);
404439
}
405440
if ftype.is_file() {
406-
let (x, y) = output_file_info(ctx, link_path.as_path(), Some(v))?;
441+
let (x, y) = output_symlink_file_info(ctx, &path, link_target.as_path(), link_path.as_path(), Some(v))?;
407442
ctx = x;
408443
return Ok((ctx.with_path(parent), y));
409444
} else if ftype.is_dir() {
410-
ctx = ctx.with_path(link_path.as_path());
445+
// ctx = ctx.with_path(link_path.as_path());
411446
let modified = if let Some(meta) = meta {
412447
meta.modified().map(|x| Some(x)).unwrap_or(None)
413448
} else {
414449
None
415450
};
416-
if check_include_exclude_path(&path, &ctx.include, &ctx.exclude, &ctx.match_option) {
451+
let (mut ctx_tmp, len) = enum_files_recursive(ctx, parent, depth)?;
452+
if check_include_exclude_path(&path, &ctx_tmp.include, &ctx_tmp.exclude, &ctx_tmp.match_option) {
417453
write_record(
418-
&mut ctx.output_stream,
454+
&mut ctx_tmp.output_stream,
419455
path.to_string_lossy().as_ref(),
420456
Some(link_target.to_string_lossy().as_ref()),
421457
"dir",
422-
None,
458+
Some(len),
423459
modified,
424460
)?;
425461
}
426-
return enum_files_recursive(ctx, parent, depth + 1);
462+
return Ok((ctx_tmp, len));
427463
}
428464
}
429465
Err(e) => {
@@ -442,19 +478,6 @@ fn retrieve_symlink<'a>(
442478
{
443479
return Ok((ctx.with_path(parent), 0));
444480
}
445-
// let modified = meta
446-
// .map(|meta| match meta.modified() {
447-
// Ok(v) => Some(v),
448-
// Err(e) => {
449-
// eprintln!(
450-
// "get modified time failed({}): {:?}",
451-
// path.to_string_lossy(),
452-
// e
453-
// );
454-
// None
455-
// }
456-
// })
457-
// .unwrap_or(None);
458481
if check_include_exclude_path(path.as_path(), &ctx.include, &ctx.exclude, &ctx.match_option) {
459482
let link_target_relative_path = match path.parent() {
460483
Some(v) => v.join(link_target.clone()),
@@ -468,14 +491,6 @@ fn retrieve_symlink<'a>(
468491
}
469492
};
470493
return output_symlink_info(ctx, path.as_path(), link_target.to_string_lossy().as_ref(), meta.as_ref(), target_meta);
471-
// write_record(
472-
// &mut ctx.output_stream,
473-
// path.to_string_lossy().as_ref(),
474-
// Some(link_target.to_string_lossy().as_ref()),
475-
// "link",
476-
// None,
477-
// modified,
478-
// )?;
479494
}
480495
Ok((ctx.with_path(parent), 0))
481496
}

0 commit comments

Comments
 (0)