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
//! Simple library to handle configuration fragments.
//!
//! This crate provides helpers to scan configuration fragments on disk.
//! The goal is to help in writing Linux services which are shipped as part of a [Reproducible OS][reproducible].
//! Its name derives from **over**lays and **drop**ins (base directories and configuration fragments).
//!
//! The main entrypoint is [`scan`](fn.scan.html). It scans
//! for configuration fragments across multiple directories (with increasing priority),
//! following these rules:
//!
//!  * fragments are identified by unique filenames, lexicographically (e.g. `50-default-limits.conf`).
//!  * in case of name duplication, last directory wins (e.g. `/etc/svc/custom.conf` can override `/usr/lib/svc/custom.conf`).
//!  * a fragment symlinked to `/dev/null` is used to ignore any previous fragment with the same filename.
//!
//! [reproducible]: http://0pointer.net/blog/projects/stateless.html
//!
//! # Example
//!
//! ```rust,no_run
//! # use liboverdrop;
//! // Scan for fragments under:
//! //  * /usr/lib/my-crate/config.d/*.toml
//! //  * /run/my-crate/config.d/*.toml
//! //  * /etc/my-crate/config.d/*.toml
//!
//! let base_dirs = [
//!     "/usr/lib",
//!     "/run",
//!     "/etc",
//! ];
//! let fragments = liboverdrop::scan(&base_dirs, "my-crate/config.d", &["toml"], false);
//!
//! for (filename, filepath) in fragments {
//!     println!("fragment '{}' located at '{}'", filename.to_string_lossy(), filepath.display());
//! }
//! ```
//!
//! # Migrating from liboverdrop 0.0.x
//!
//! The signature changed from
//! ```rust,compile_fail
//! # use liboverdrop::FragmentScanner;
//! let base_dirs: Vec<String> = vec![/**/];
//! let shared_path: &str = "config.d";
//! let allowed_extensions: Vec<String> = vec![/**/];
//! for (basename, filepath) in FragmentScanner::new(
//!                                 base_dirs, shared_path, false, allowed_extensions).scan() {
//!     // basename: String
//!     // filepath: PathBuf
//! }
//! ```
//! to
//! ```rust,no_run
//! # use liboverdrop;
//! # /*
//! let base_dirs: IntoIterator<Item = AsRef<Path>> = /* could be anything */;
//! let shared_path: AsRef<Path> = /* ... */;
//! let allowed_extensions: &[AsRef<OsStr>] = &[/* ... */];
//! # */
//! # let base_dirs = [""];
//! # let shared_path = "";
//! # let allowed_extensions = &[""];
//! for (basename, filepath) in liboverdrop::scan(
//!                                 base_dirs, shared_path, allowed_extensions, false) {
//!     // basename: OsString
//!     // filepath: PathBuf
//! }
//! ```
//!
//! When updating, re-consider if you need to allocate any argument now,
//! since they can all be literals or borrowed.

use log::trace;
use std::collections::BTreeMap;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::{Path, PathBuf};

/// The well-known path to the null device used for overrides.
const DEVNULL: &str = "/dev/null";

/// Scan unique configuration fragments from the configuration directories specified.
///
/// # Arguments
///
/// * `base_dirs` - Base components of directories where configuration fragments are located.
/// * `shared_path` - Common relative path from each entry in `base_dirs` to the directory
///                   holding configuration fragments.
/// * `allowed_extensions` - Only scan files that have an extension listed in `allowed_extensions`.
///                          If an empty slice is passed, then all extensions are allowed.
/// * `ignore_dotfiles` - Whether to ignore dotfiles (hidden files with name prefixed with '.').
///
/// `shared_path` is joined onto each entry in `base_dirs` to form the directory paths to scan.
///
/// Returns a `BTreeMap` indexed by configuration fragment filename,
/// holding the path where the unique configuration fragment is located.
///
/// Configuration fragments are stored in the `BTreeMap` in alphanumeric order by filename.
/// Configuration fragments existing in directories that are scanned later override fragments
/// of the same filename in directories that are scanned earlier.
pub fn scan<BdS: AsRef<Path>, BdI: IntoIterator<Item = BdS>, Sp: AsRef<Path>, As: AsRef<OsStr>>(
    base_dirs: BdI,
    shared_path: Sp,
    allowed_extensions: &[As],
    ignore_dotfiles: bool,
) -> BTreeMap<OsString, PathBuf> {
    let shared_path = shared_path.as_ref();

    let mut files_map = BTreeMap::new();
    for dir in base_dirs {
        let dir = dir.as_ref().join(shared_path);
        trace!("Scanning directory '{}'", dir.display());

        let dir_iter = match fs::read_dir(dir) {
            Ok(iter) => iter,
            _ => continue,
        };
        for entry in dir_iter.flatten() {
            let fpath = entry.path();
            let fname = entry.file_name();

            // If hidden files not allowed, ignore dotfiles.
            // Rust RFC 900 &c.: there's no way to check if a Path/OsStr starts with a prefix;
            // instead, we check via to_string_lossy(), which will only allocate if the basename wasn't UTF-8,
            // and the lossiness doesn't bother us; https://github.com/rust-lang/rfcs/issues/900
            if ignore_dotfiles && fname.to_string_lossy().starts_with('.') {
                continue;
            }

            // If extensions are specified, proceed only if filename has one of the allowed
            // extensions.
            if !allowed_extensions.is_empty() {
                if let Some(extension) = fpath.extension() {
                    if !allowed_extensions.iter().any(|ae| ae.as_ref() == extension) {
                        continue;
                    }
                } else {
                    continue;
                }
            }

            // Check filetype, ignore non-file.
            let meta = match entry.metadata() {
                Ok(m) => m,
                _ => continue,
            };
            if !meta.file_type().is_file() {
                if let Ok(target) = fs::read_link(&fpath) {
                    // A devnull symlink is a special case to ignore previous file-names.
                    if target == Path::new(DEVNULL) {
                        trace!("Nulled config file '{}'", fpath.display());
                        files_map.remove(&fname);
                    }
                }
                continue;
            }

            trace!(
                "Found config file '{}' at '{}'",
                Path::new(&fname).display(),
                fpath.display()
            );
            files_map.insert(fname, fpath);
        }
    }

    files_map
}

#[cfg(test)]
mod tests {
    use super::*;

    fn assert_fragments_match(
        fragments: &BTreeMap<OsString, PathBuf>,
        filename: &OsStr,
        filepath: &Path,
    ) {
        assert_eq!(fragments.get(filename).unwrap(), filepath);
    }

    fn assert_fragments_hit<T: AsRef<OsStr>>(fragments: &BTreeMap<OsString, PathBuf>, filename: T) {
        assert!(fragments.get(filename.as_ref()).is_some());
    }

    fn assert_fragments_miss<T: AsRef<OsStr>>(
        fragments: &BTreeMap<OsString, PathBuf>,
        filename: T,
    ) {
        assert!(fragments.get(filename.as_ref()).is_none());
    }

    #[test]
    fn basic_override() {
        let treedir = "tests/fixtures/tree-basic";
        let dirs = [
            format!("{}/{}", treedir, "usr/lib"),
            format!("{}/{}", treedir, "run"),
            format!("{}/{}", treedir, "etc"),
        ];

        let expected_fragments = [
            (
                OsString::from("01-config-a.toml"),
                Path::new(treedir).join("etc/liboverdrop.d/01-config-a.toml"),
            ),
            (
                OsString::from("02-config-b.toml"),
                Path::new(treedir).join("run/liboverdrop.d/02-config-b.toml"),
            ),
            (
                OsString::from("03-config-c.toml"),
                Path::new(treedir).join("etc/liboverdrop.d/03-config-c.toml"),
            ),
            (
                OsString::from("04-config-d.toml"),
                Path::new(treedir).join("usr/lib/liboverdrop.d/04-config-d.toml"),
            ),
            (
                OsString::from("05-config-e.toml"),
                Path::new(treedir).join("etc/liboverdrop.d/05-config-e.toml"),
            ),
            (
                OsString::from("06-config-f.toml"),
                Path::new(treedir).join("run/liboverdrop.d/06-config-f.toml"),
            ),
            (
                OsString::from("07-config-g.toml"),
                Path::new(treedir).join("etc/liboverdrop.d/07-config-g.toml"),
            ),
        ];

        let fragments = scan(&dirs, "liboverdrop.d", &["toml"], false);

        for (name, path) in &expected_fragments {
            assert_fragments_match(&fragments, &name, &path);
        }

        // Check keys are stored in the correct order.
        let expected_keys: Vec<_> = expected_fragments
            .into_iter()
            .map(|(name, _)| name)
            .collect();
        let fragments_keys: Vec<_> = fragments.keys().cloned().collect();
        assert_eq!(fragments_keys, expected_keys);
    }

    #[test]
    fn basic_override_restrict_extensions() {
        let treedir = "tests/fixtures/tree-basic";
        let dirs = [format!("{}/{}", treedir, "etc")];

        let fragments = scan(&dirs, "liboverdrop.d", &["toml"], false);

        assert_fragments_hit(&fragments, "01-config-a.toml");
        assert_fragments_miss(&fragments, "08-config-h.conf");
        assert_fragments_miss(&fragments, "noextension");
    }

    #[test]
    fn basic_override_allow_all_extensions() {
        let treedir = "tests/fixtures/tree-basic";
        let dirs = [format!("{}/{}", treedir, "etc")];

        let fragments = scan::<_, _, _, &str>(&dirs, "liboverdrop.d", &[], false);

        assert_fragments_hit(&fragments, "01-config-a.toml");
        assert_fragments_hit(&fragments, "config.conf");
        assert_fragments_hit(&fragments, "noextension");
    }

    #[test]
    fn basic_override_ignore_hidden() {
        let treedir = "tests/fixtures/tree-basic";
        let dirs = [format!("{}/{}", treedir, "etc")];

        let fragments = scan::<_, _, _, &str>(&dirs, "liboverdrop.d", &[], true);

        assert_fragments_hit(&fragments, "config.conf");
        assert_fragments_miss(&fragments, ".hidden.conf");
    }

    #[test]
    fn basic_override_allow_hidden() {
        let treedir = "tests/fixtures/tree-basic";
        let dirs = [format!("{}/{}", treedir, "etc")];

        let fragments = scan::<_, _, _, &OsStr>(&dirs, "liboverdrop.d", &[], false);

        assert_fragments_hit(&fragments, "config.conf");
        assert_fragments_hit(&fragments, ".hidden.conf");
    }
}