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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2017 Gilad Naaman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//! A cross platform crate for recursively walking over paths matching a Glob pattern.
//!
//! This crate inherits many features from both `walkdir` and `globset`,
//! like  optionally following symbolic links, limiting of open file descriptors,
//! and more.
//!
//! Glob related options can be set by supplying your own `GlobSet` to `GlobWalk::from_globset`.
//!
//! # Example: Finding image files in the current directory.
//!
//! ```ignore
//! for img in GlobWalker::from_patterns(&["*.{png,jpg,gif}"], ".") {
//!     if let Ok(img) = img {
//!         remove_file(img.path()).unwrap();
//!     }
//! }
//! ```

extern crate walkdir;
extern crate globset;

#[cfg(test)]
extern crate tempdir;

use std::path::{PathBuf, Path};
use std::cmp::Ordering;
use globset::{GlobSetBuilder, Glob, GlobSet};
use walkdir::{WalkDir, DirEntry};

type GlobError = globset::Error;
type WalkError = walkdir::Error;

/// An iterator for recursively yielding glob matches.
///
/// The order of elements yielded by this iterator is unspecified.
pub struct GlobWalker {
    glob: GlobSet,
    base: PathBuf,

    min_depth: usize,
    max_depth: usize,
    follow_links: bool,
    max_open: usize,
    sort_by: Option<Box<
        FnMut(&DirEntry,&DirEntry) -> Ordering + Send + Sync + 'static
    >>,
    contents_first: bool,
}

impl GlobWalker {
    pub fn new<S: AsRef<str>>(pattern: S) -> Result<Self, GlobError> {
        GlobWalker::from_patterns(&[pattern])
    }

    /// Construct a new `GlobWalker` from a list of patterns.
    ///
    /// When iterated, the base directory will be recursively searched for paths
    /// matching `pats`.
    pub fn from_patterns<S: AsRef<str>>(pats: &[S]) -> Result<Self, GlobError> {

        let mut builder = GlobSetBuilder::new();
        for pattern in pats {
            builder.add(Glob::new(pattern.as_ref())?);
        }

        let set = builder.build()?;

        Ok(Self::from_globset(set))
    }

    /// Construct a new `GlobWalker` from a GlobSet
    ///
    /// When iterated, the base directory will be recursively searched for paths
    /// matching `glob`.
    pub fn from_globset(glob: GlobSet) -> Self {
        GlobWalker {
            glob,
            base: ".".into(),
            follow_links: false,
            max_open: 10,
            min_depth: 0,
            max_depth: ::std::usize::MAX,
            sort_by: None,
            contents_first: false,
        }
    }

    /// Change the root dir of the walker.
    pub fn base_dir<P: AsRef<Path>>(mut self, base: P) -> Self {
        self.base = base.as_ref().into();
        self
    }

    /// Set the minimum depth of entries yielded by the iterator.
    ///
    /// The smallest depth is `0` and always corresponds to the path given
    /// to the `new` function on this type. Its direct descendents have depth
    /// `1`, and their descendents have depth `2`, and so on.
    pub fn min_depth(mut self, depth: usize) -> Self {
        self.min_depth = depth;
        self
    }

    /// Set the maximum depth of entries yield by the iterator.
    ///
    /// The smallest depth is `0` and always corresponds to the path given
    /// to the `new` function on this type. Its direct descendents have depth
    /// `1`, and their descendents have depth `2`, and so on.
    ///
    /// Note that this will not simply filter the entries of the iterator, but
    /// it will actually avoid descending into directories when the depth is
    /// exceeded.
    pub fn max_depth(mut self, depth: usize) -> Self {
        self.max_depth = depth;
        self
    }

    /// Follow symbolic links. By default, this is disabled.
    ///
    /// When `yes` is `true`, symbolic links are followed as if they were
    /// normal directories and files. If a symbolic link is broken or is
    /// involved in a loop, an error is yielded.
    ///
    /// When enabled, the yielded [`DirEntry`] values represent the target of
    /// the link while the path corresponds to the link. See the [`DirEntry`]
    /// type for more details.
    ///
    /// [`DirEntry`]: struct.DirEntry.html
    pub fn follow_links(mut self, yes: bool) -> Self {
        self.follow_links = yes;
        self
    }

    /// Set the maximum number of simultaneously open file descriptors used
    /// by the iterator.
    ///
    /// `n` must be greater than or equal to `1`. If `n` is `0`, then it is set
    /// to `1` automatically. If this is not set, then it defaults to some
    /// reasonably low number.
    ///
    /// This setting has no impact on the results yielded by the iterator
    /// (even when `n` is `1`). Instead, this setting represents a trade off
    /// between scarce resources (file descriptors) and memory. Namely, when
    /// the maximum number of file descriptors is reached and a new directory
    /// needs to be opened to continue iteration, then a previous directory
    /// handle is closed and has its unyielded entries stored in memory. In
    /// practice, this is a satisfying trade off because it scales with respect
    /// to the *depth* of your file tree. Therefore, low values (even `1`) are
    /// acceptable.
    ///
    /// Note that this value does not impact the number of system calls made by
    /// an exhausted iterator.
    ///
    /// # Platform behavior
    ///
    /// On Windows, if `follow_links` is enabled, then this limit is not
    /// respected. In particular, the maximum number of file descriptors opened
    /// is proportional to the depth of the directory tree traversed.
    pub fn max_open(mut self, n: usize) -> Self {
        self.max_open = n;
        self
    }

// FIXME: See next FIXME
//    /// Set a function for sorting directory entries.
//    ///
//    /// If a compare function is set, the resulting iterator will return all
//    /// paths in sorted order. The compare function will be called to compare
//    /// entries from the same directory.
//    pub fn sort_by<F>(mut self, cmp: F) -> Self
//        where F: FnMut(&DirEntry, &DirEntry) -> Ordering + Send + Sync + 'static
//    {
//        self.sort_by = Some(Box::new(cmp));
//        self
//    }

    /// Yield a directory's contents before the directory itself. By default,
    /// this is disabled.
    ///
    /// When `yes` is `false` (as is the default), the directory is yielded
    /// before its contents are read. This is useful when, e.g. you want to
    /// skip processing of some directories.
    ///
    /// When `yes` is `true`, the iterator yields the contents of a directory
    /// before yielding the directory itself. This is useful when, e.g. you
    /// want to recursively delete a directory.
    pub fn contents_first(mut self, yes: bool) -> Self {
        self.contents_first = yes;
        self
    }
}

impl IntoIterator for GlobWalker {
    type Item = Result<DirEntry, WalkError>;
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        let walker = WalkDir::new(&self.base)
            .min_depth(self.min_depth)
            .max_depth(self.max_depth)
            .max_open(self.max_open)
            .follow_links(self.follow_links)
            .contents_first(self.contents_first);

        // FIXME: This cannot compile right now.
        // let walker = if let Some(sorter) = self.sort_by.take() {
        //     walker.sort_by(move |a, b| sorter(a, b))
        // }
        // else {
        //     walker
        // };

        IntoIter {
            glob: self.glob,
            base: self.base,
            walker: walker.into_iter()
        }
    }
}

/// An iterator which emits glob-matched patterns.
///
/// An instance of this type must be constructed through `GlobWalker`,
/// which uses a builder-style pattern.
///
/// The order of the yielded paths is undefined, unless specified by the user
/// using `GlobWalker::sort_by`.
pub struct IntoIter {
    glob: GlobSet,
    base: PathBuf,
    walker: walkdir::IntoIter,
}

impl Iterator for IntoIter {
    type Item = Result<DirEntry, WalkError>;

    // Possible optimization - Do not descend into directory that will never be a match
    fn next(&mut self) -> Option<Self::Item> {
        for entry in &mut self.walker {
            match entry {
                Ok(e) => {
                    // Strip the common base directory so that the matcher will be
                    // able to recognize the file name.
                    // `unwrap` here is safe, since walkdir returns the files with relation
                    // to the given base-dir.
                    if self.glob.is_match((&e).path().strip_prefix(&*self.base).unwrap()) {
                        return Some(Ok(e));
                    }
                },
                Err(e) => {
                    return Some(Err(e));
                }
            }
        }

        None
    }
}

pub fn glob<S: AsRef<str>>(pattern: S) -> Result<GlobWalker, GlobError> {
    GlobWalker::new(pattern)
}


#[cfg(test)]
mod tests {
    use super::*;
    use ::tempdir::TempDir;
    use ::std::fs::{File, create_dir_all};

    fn touch(dir: &TempDir, names: &[&str]) {
        for name in names {
            let name = normalize_path_sep(name);
            File::create(dir.path().join(name)).expect("Failed to create a test file");
        }
    }

    fn normalize_path_sep<S: AsRef<str>>(s: S) -> String {
        s.as_ref().replace("[/]", if cfg!(windows) {"\\"} else {"/"})
    }

    #[test]
    fn do_the_globwalk() {
        let dir = TempDir::new("globset_walkdir").expect("Failed to create temporary folder");
        let dir_path = dir.path();
        create_dir_all(dir_path.join("src/some_mod")).expect("");
        create_dir_all(dir_path.join("tests")).expect("");
        create_dir_all(dir_path.join("contrib")).expect("");

        touch(&dir, &[
            "a.rs",
            "b.rs",
            "avocado.rs",
            "lib.c",
            "src[/]hello.rs",
            "src[/]world.rs",
            "src[/]some_mod[/]unexpected.rs",
            "src[/]cruel.txt",
            "contrib[/]README.md",
            "contrib[/]README.rst",
            "contrib[/]lib.rs",
        ][..]);


        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("src/**/*.rs").unwrap());
        builder.add(Glob::new("*.c").unwrap());
        builder.add(Glob::new("**/lib.rs").unwrap());
        builder.add(Glob::new("**/*.{md,rst}").unwrap());
        let set = builder.build().unwrap();

        let mut expected: Vec<_> = ["src[/]some_mod[/]unexpected.rs",
                                    "src[/]world.rs",
                                    "src[/]hello.rs",
                                    "lib.c",
                                    "contrib[/]lib.rs",
                                    "contrib[/]README.md",
                                    "contrib[/]README.rst"].iter().map(normalize_path_sep).collect();

        for matched_file in GlobWalker::from_globset(set)
                                        .base_dir(dir_path)
                                        .into_iter()
                                        .filter_map(Result::ok) {
            let path = matched_file.path().strip_prefix(dir_path).unwrap().to_str().unwrap();
            let path = path.replace("[/]", if cfg!(windows) {"\\"} else {"/"});

            println!("path = {}", path);

            let del_idx = if let Some(idx) = expected.iter().position(|n| &path == n) {
                idx
            } else {
                panic!("Iterated file is unexpected: {}", path);
            };
            expected.remove(del_idx);
        }

        let empty: &[&str] = &[][..];
        assert_eq!(expected, empty);
    }

    #[test]
    fn find_image_files() {
        let dir = TempDir::new("globset_walkdir").expect("Failed to create temporary folder");
        let dir_path = dir.path();

        touch(&dir, &[
            "a.rs",
            "a.jpg",
            "a.png",
            "b.docx",
        ][..]);


        let mut expected = vec!["a.jpg", "a.png"];

        for matched_file in GlobWalker::new("*.{png,jpg,gif}")
                                        .unwrap()
                                        .base_dir(dir_path)
                                        .into_iter()
                                        .filter_map(Result::ok) {
            let path = matched_file.path().strip_prefix(dir_path).unwrap().to_str().unwrap();
            let path = path.replace("[/]", if cfg!(windows) {"\\"} else {"/"});

            println!("path = {}", path);

            let del_idx = if let Some(idx) = expected.iter().position(|n| &path == n) {
                idx
            } else {
                panic!("Iterated file is unexpected: {}", path);
            };
            expected.remove(del_idx);
        }

        let empty: &[&str] = &[][..];
        assert_eq!(expected, empty);
    }
}