From 4b1b77ea849b1e01f5a74a779e7fb156044452fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=A4ggstr=C3=B6m?= Date: Tue, 20 Nov 2018 11:55:52 +0100 Subject: [PATCH] Match directories with a trailing slash. Append a slash on directories, to be able to match them separately from files. --- rofs-filtered.c | 29 ++++++++++++++++++++++++----- rofs-filtered.rc | 6 +++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/rofs-filtered.c b/rofs-filtered.c index 848e3ed..0eb388f 100644 --- a/rofs-filtered.c +++ b/rofs-filtered.c @@ -407,10 +407,29 @@ static int should_hide(const char *name, mode_t mode) { } if (conf.invert && mode != S_IFREG && mode != S_IFDIR) return conf.invert; - if (!regexec(&pattern, name, 0, NULL, 0)) { - // We have a match. - log_msg(LOG_DEBUG, "match: %s", name); - return !conf.invert; + + /* Append slash if directory */ + if (mode == S_IFDIR) { + char *name_and_slash = malloc(strlen(name) + 2); + if (!name_and_slash) { + log_msg(LOG_DEBUG, "out of memory while handling %s", name); + return 0; + } + strcpy(name_and_slash, name); + strcat(name_and_slash, "/"); + if (!regexec(&pattern, name_and_slash, 0, NULL, 0)) { + // We have a match. + log_msg(LOG_DEBUG, "match: %s", name_and_slash); + free(name_and_slash); + return !conf.invert; + } + free(name_and_slash); + } else { + if (!regexec(&pattern, name, 0, NULL, 0)) { + // We have a match. + log_msg(LOG_DEBUG, "match: %s", name); + return !conf.invert; + } } return conf.invert; } @@ -464,7 +483,7 @@ static int callback_readlink(const char *path, char *buf, size_t size) { static int callback_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { - if (should_hide(path, S_IFREG)) return -ENOENT; + if (should_hide(path, S_IFDIR)) return -ENOENT; DIR *dp; struct dirent *de; diff --git a/rofs-filtered.rc b/rofs-filtered.rc index 7ce4a50..2c3a207 100644 --- a/rofs-filtered.rc +++ b/rofs-filtered.rc @@ -41,7 +41,7 @@ # /file1.flac # /file1.mp3 # /file2.mp3 -# /subDir1 +# /subDir1/ # /subDir1/file3.flac # etc... # @@ -52,10 +52,10 @@ .*\.flac$ # Hide subDir2 (and all its contents), no matter where it appears in the tree: -/subDir2$ +/subDir2/$ # Hide the subDir2 but only if it occurs at the root of the tree: -^/subDir2$ +^/subDir2/$ # Since a RegEx can not start with '|' (vertical bar), this symbol is used to # escape out of RegEx mode and introduce special configuration flags.