Using require_literal_leading_dot with glob_with

Hello Rust Community,

I decided to learn Rust this year and been enjoying it so far. I've still got lots to learn for sure.

I copied this program from the docs for the glob crate and changed it to set require_literal_leading_dot: true. The docs say * require that . appears literally in the pattern; * , ? , ** , or [...] will not match."

use glob::glob_with;
use glob::MatchOptions;

fn main() {
    let options = MatchOptions {
        case_sensitive: false,
        require_literal_separator: false,
        require_literal_leading_dot: true,
    };
    for entry in glob_with("**/*", options).unwrap() {
        if let Ok(path) = entry {
            println!("{:?}", path.display())
        }
    }

}

Files starting with a period are ignored but not directories found by **. I expected these to be skipped as well.

If I do cargo run then I get output like this:

"target/release/.fingerprint/hello-d2fa6f5979bb38b8"
"target/release/.fingerprint/hello-d2fa6f5979bb38b8/bin-hello"
"target/release/.fingerprint/hello-d2fa6f5979bb38b8/bin-hello.json"
"target/release/.fingerprint/hello-d2fa6f5979bb38b8/dep-bin-hello"

My .git directory does not get listed or other .xxxxx files in the sub directories but sub directories with periods do. Is this a bug or expected in some way I'm not interpreting the docs?

If I go into the target/release directory, .fingerprint only gets shown with ls -a so it must be a proper period. Now I look further if I create other sub directories with a period they don't get listed either.

It is a pretty straight forward program to try out so I hope you guys can tell me what is going on please?

Thanks

Please read the pinned code formatting and syntax highlighting guide.

Scanning through the open issues, I think it's fair to say that the glob crate has a lot of flaws and will probably be deprecated some day. That said, there was a recent PR that I believe fixes this particular issue. And apparently the first release in 2 years was just a month ago. So your best bet may be to wait for another release.

One of the issues mentioned globset as a replacement (probably in combination with walkdir), but that doesn't support the "ignore .hidden" functionality so far.

Another mentioned globwalk, but that seems to have it's own problems too.

Thanks for taking the time to look into my question and reply quinedot. I did not spot the pinned post since I went straight to the help section :(. Thanks for letting me know. Thanks for the link to the github repository. I see it might be fixed and yes grumbles about the status of the project.

Now I know my understanding of how it should work is correct and it is a bug. It was a nice idea to be able to do UNIX glob pattern matching on Windows but not essential for what I'm trying to achieve. It looks like WalkDir is more reputable API so I shall have a play with that. Thank you.

use walkdir::{DirEntry, WalkDir};
use std::env;
use regex::Regex;

fn main() {
    let re1 = Regex::new(r".+\.rs$").unwrap();
    let re2 = Regex::new(r".+\.py$").unwrap();
    let res = vec![re1, re2];

    let name_filter = |entry: &DirEntry| {
        if entry.file_type().is_file() {
            res.iter().any(|re| re.is_match(&entry.file_name().to_string_lossy()))
        } else {
            if entry.file_type().is_dir(){
                !entry.file_name().to_str().unwrap().starts_with(".")
            } else {
                true
            }
        }
    };

    let current_dir = env::current_dir().unwrap();
    let walker = WalkDir::new(current_dir).into_iter();

    for entry in walker.filter_entry(name_filter) {
        match &entry {
            Ok(e) => {
                if e.file_type().is_file() {
                    println!("{}", &entry.unwrap().path().display());
                }
           }
           Err(e) => {
               eprintln!("{}", e);
           }
       }
    }
}

I played around with Walkdir and with a little help from a book on Rust I think I've basically achieved what I want. I can scan all directories for files with specific patterns and avoid hidden directories :). All I need to do is figure out out how to turn this into a vector of strings and I'll be very happy.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.