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
|
//! `cachedir` is a tiny utility for tagging, finding, and untagging
//! directories as cache directories, according to the
//! <http://www.bford.info/cachedir/> specification.
//!
//! ~~~sh
//! $ cachedir find $HOME
//! $ cachedir tag $HOME/.cache
//! $ cachedir find $HOME
//! /home/liw/.cache
//! $ cachedir untag $HOME/.cache
//! $ cachedir find $HOME
//! $
//! ~~~
use cachedir::{is_cachedir_tag, CACHEDIR_TAG, TAG};
use clap::{Parser, Subcommand};
use std::fs::{remove_file, write};
use std::path::PathBuf;
use walkdir::{DirEntry, WalkDir};
/// The main program.
fn main() {
if let Err(e) = real_main() {
eprintln!("ERROR: {}", e);
std::process::exit(1);
}
}
/// Fallible main program.
fn real_main() -> anyhow::Result<()> {
let args = Args::parse();
match args.cmd {
Command::Find { dirs } => walk(&dirs, find)?,
Command::Tag { dirs } => walk(&dirs, tag)?,
Command::Untag { dirs } => walk(&dirs, untag)?,
Command::IsCache { dir } => is_cache(dir)?,
}
Ok(())
}
/// Command line arguments.
#[derive(Debug, Parser)]
struct Args {
#[clap(subcommand)]
cmd: Command,
}
/// Subcommands.
#[derive(Debug, Subcommand)]
enum Command {
/// Find cache directories starting at given directories.
Find { dirs: Vec<PathBuf> },
/// Add cache directory tag to given directories.
Tag { dirs: Vec<PathBuf> },
/// Remove cache directory tag from given directories.
Untag { dirs: Vec<PathBuf> },
/// Is the single given directory tagged as a cache directory?
IsCache { dir: PathBuf },
}
/// Is the given directory a cache?
fn is_cache(dir: PathBuf) -> Result<(), std::io::Error> {
let filename = dir.join(CACHEDIR_TAG);
if !is_cachedir_tag(&filename)? {
std::process::exit(1);
}
Ok(())
}
/// Is the given directory a cache?
fn find(entry: &DirEntry) -> Result<(), std::io::Error> {
let filename = tag_filename(entry);
if is_cachedir_tag(&filename)? {
println!("{}", entry.path().display());
}
Ok(())
}
/// Add a tag file to the given directory.
fn tag(entry: &DirEntry) -> Result<(), std::io::Error> {
let filename = tag_filename(entry);
if !is_cachedir_tag(&filename)? {
write(filename, TAG)?;
}
Ok(())
}
/// Remove a tag file from the given directory.
fn untag(entry: &DirEntry) -> Result<(), std::io::Error> {
let filename = tag_filename(entry);
if is_cachedir_tag(&filename)? {
remove_file(filename)?;
}
Ok(())
}
/// Iterate over all directories, call given function for each.
fn walk<F>(dirs: &[PathBuf], mut func: F) -> Result<(), std::io::Error>
where
F: FnMut(&DirEntry) -> Result<(), std::io::Error>,
{
for dir in dirs {
for entry in WalkDir::new(dir) {
let entry = entry?;
if entry.metadata()?.is_dir() {
func(&entry)?;
}
}
}
Ok(())
}
/// Return path to tag file in a directory.
fn tag_filename(entry: &DirEntry) -> PathBuf {
entry.path().join(CACHEDIR_TAG)
}
|