Skip to content

Commit

Permalink
Merge branch 'main' of gitlab.com:shank/utils-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
the-shank committed Jan 23, 2023
2 parents d912f96 + 68cc4e8 commit 1ad5997
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions src/bin/broken_symlinks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use color_eyre::eyre::{Context, Result};
use color_eyre::eyre::Result;
use std::env;
use std::fs::read_dir;
use std::fs::{self, read_dir};
use std::path::PathBuf;
use std::time::Instant;

Expand Down Expand Up @@ -52,32 +52,50 @@ fn main() -> Result<()> {
Ok(())
}

// TODO: collect directories that cannot be read
// TODO: collect entries that cannot be read
fn process_entry(entry: &fs::DirEntry, opts: &Opts) -> Result<()> {
if opts.verbose {
println!("Processing entry: {:?}", entry.file_name());
}

let file_type = entry.file_type()?;

if file_type.is_dir() {
// process directory
process_dir(&entry.path(), opts)?;
} else if file_type.is_symlink() {
// process symlink
let target = entry.path();
match target.try_exists() {
Ok(exists) => {
if !exists {
println!("[Broken]: {}", target.display());
}
}
Err(e) => {
eprintln!("[error ({})]: {}", e.kind(), target.display(),);
}
}
}

Ok(())
}

fn process_dir(dir: &PathBuf, opts: &Opts) -> Result<()> {
if opts.verbose {
println!("Processing: {}", dir.display());
println!("Processing directory: {}", dir.display());
}

for entry in
read_dir(dir).with_context(|| format!("error processing dir: {}", dir.display()))?
{
let entry =
entry.with_context(|| format!("error processing some file in {}", dir.display()))?;

let file_type = entry.file_type()?;

if file_type.is_dir() {
// process directory
process_dir(&entry.path(), opts)?;
} else if file_type.is_symlink() {
// process symlink
let path = entry.path();
if !path.try_exists().with_context(|| {
format!("Failed to check if symlink exists: {}", path.display())
})? {
println!("Broken symlink: {}", path.display());
};
match read_dir(dir) {
Ok(dir) => {
// process the entries
for entry in dir {
let entry = entry?;
let _ = process_entry(&entry, opts);
}
}
Err(e) => {
// print the failures to stderr
eprintln!("[error ({})]: {}", e.kind(), dir.display(),);
}
}

Expand Down

0 comments on commit 1ad5997

Please sign in to comment.