summaryrefslogtreecommitdiff
path: root/src/bin/cachedir.rs
blob: 4d35ae8d7ecbfc4c2f53183c29a638c7949d0a7b (plain)
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
//! `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 clap::{Parser, Subcommand};
use std::fs::{read, remove_file, write};
use std::path::{Path, PathBuf};
use walkdir::{DirEntry, WalkDir};

/// Name of tag file.
const CACHEDIR_TAG: &str = "CACHEDIR.TAG";

/// Prefix of contents of tag file.
const TAG: &str = "Signature: 8a477f597d28d172789f06886806bc55";

/// 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(())
}

/// Is the given file a cache directory tag?
fn is_cachedir_tag(filename: &Path) -> Result<bool, std::io::Error> {
    if filename.exists() {
        let data = read(filename)?;
        if data.len() >= TAG.len() {
            Ok(&data[..TAG.len()] == TAG.as_bytes())
        } else {
            Ok(false)
        }
    } else {
        Ok(false)
    }
}

/// Return path to tag file in a directory.
fn tag_filename(entry: &DirEntry) -> PathBuf {
    entry.path().join(CACHEDIR_TAG)
}