feat(filter): per-directory merge rules at the receiver; implement e/n/w/- modifiers

This commit is contained in:
2026-09-23 22:46:14 +02:00
parent 29f8be161c
commit b414f197af
22 changed files with 1154 additions and 192 deletions
+56 -11
View File
@@ -25,11 +25,12 @@
* Modifiers: '/' absolute anchor, '!' negate match, 'C' inject CVS defaults,
* 's' sender side, 'r' receiver side, 'p' perishable. The rsync 'x'
* (xattr-name) modifier is not implemented and is rejected explicitly
* everywhere. The merge-file modifiers 'e' (exclude the merge file itself),
* 'n' (do not inherit the merge file), 'w' (word-split the merge file) and '-'
* (do not transfer the merge file) are accepted and consumed only on merge/
* dir-merge rules (rejected on every other rule, matching rsync); their
* semantics are not implemented and they are otherwise ignored.
* everywhere. The merge-only modifiers are accepted only on merge/dir-merge
* rules (rejected on every other rule, matching rsync): 'e' excludes the merge
* file itself, 'n' makes the merged rules non-inheriting (they apply only to
* the directory that holds the merge file), 'w' word-splits the merge file on
* whitespace instead of lines, and '-' reads the merge file as a list of bare
* exclude patterns with no rule prefixes.
* A trailing '/' makes a pattern match directories only. A leading '/' anchors
* the pattern to its owner directory.
*/
@@ -55,18 +56,32 @@ typedef struct {
bool dir_only; /* pattern had a trailing '/': matches directories only */
bool negate; /* '!' modifier: match succeeds when the pattern does not */
bool perishable; /* 'p' modifier (ignored in deleted directories) */
bool no_inherit; /* 'n' on the owning dir-merge: applies only in `owner` */
char* owner; /* owning directory rel path ("" == transfer root) */
char* pattern; /* cleaned glob pattern (no leading '/', no trailing '/') */
} FilterRule;
/* One per-directory merge-file registration ("dir-merge NAME"/": NAME", "merge
* NAME"/". NAME" and -F's .rsync-filter) together with the merge-only modifiers
* parsed from the rule. The scanner reads `name` in every directory it
* traverses and applies `no_prefixes`/`include`/`word_split`/`no_inherit`/
* `exclude_self` while merging the file's rules. */
typedef struct {
char* name;
bool no_prefixes; /* '-' : file holds only bare exclude patterns */
bool include; /* '+' : file holds only bare include patterns */
bool word_split; /* 'w' : split the file on whitespace, not lines */
bool no_inherit; /* 'n' : the merged rules do not inherit below their dir */
bool exclude_self; /* 'e' : exclude the merge file itself from the transfer */
} FilterDirMerge;
typedef struct FilterRuleList {
FilterRule** items; /* owned array of rule pointers */
int count;
int capacity;
/* Per-directory merge-file basenames registered by "dir-merge NAME"/": NAME"
* or by -F (.rsync-filter). Owned strings; the scanner reads each name in
* every directory it traverses. */
char** dir_merge_names;
/* Per-directory merge-file registrations. Owned; the scanner reads each
* name in every directory it traverses. */
FilterDirMerge* dir_merges;
int dir_merge_count;
int dir_merge_capacity;
} FilterRuleList;
@@ -83,13 +98,23 @@ typedef struct {
FilterRule* filter_rule_parse(const char* line, const FilterParseOptions* opts, char* err,
size_t err_size);
void filter_rule_free(FilterRule* rule);
/* Deep-copy a rule (owned pattern/owner). Returns NULL on allocation failure. */
FilterRule* filter_rule_clone(const FilterRule* rule);
FilterRuleList* filter_rule_list_create(void);
/* Append a fully-parsed rule (takes ownership). Returns false on OOM. */
bool filter_rule_list_add(FilterRuleList* list, FilterRule* rule);
/* Register a per-directory merge-file basename (idempotent). Returns false on
* OOM. Used by the scanner to read custom "dir-merge" files. */
/* Register a per-directory merge-file basename (idempotent, no modifiers).
* Returns false on OOM. Used by the scanner to read custom "dir-merge" files. */
bool filter_rule_list_add_dir_merge(FilterRuleList* list, const char* name);
/* Register a per-directory merge file with its merge-only modifiers. On a
* duplicate name the existing registration is kept (rsync's first wins) and true
* is returned. When `exclude_self` is set an implicit exclude rule for the
* merge file's basename is appended to the list at this position, matching
* rsync's `e` modifier. Returns false on OOM. */
bool filter_rule_list_add_dir_merge_ex(FilterRuleList* list, const char* name, bool no_prefixes,
bool include, bool word_split, bool no_inherit,
bool exclude_self);
/* Parse `line` and append it. Handles "clear"/"!" (resets the list), "merge
* FILE"/". FILE" (splices the file's rules) and "dir-merge NAME"/": NAME"
* (registers a per-directory filename). Returns false and fills `err` on bad
@@ -122,6 +147,15 @@ bool filter_file_append(FilterRuleList* list, const char* dir_path, const char*
const char* owner_rel, const FilterParseOptions* opts, bool* exists,
char* err, size_t err_size);
/* Append a per-directory merge file honoring its merge-only modifiers: `-`
* reads every (word-split, when `w`) token as a bare exclude, `+` as a bare
* include, and `n` marks each read rule non-inheriting. A plain name behaves
* like filter_file_append. A missing file yields *exists=false with no error;
* returns false only on a parse/allocation failure (message in `err`). */
bool filter_dir_merge_append(FilterRuleList* list, const char* dir_path, const FilterDirMerge* spec,
const char* owner_rel, const FilterParseOptions* opts, bool* exists,
char* err, size_t err_size);
/* filter_file_read_named with the default ".rsync-filter" name. */
FilterRuleList* filter_file_read(const char* dir_path, const char* owner_rel, bool* exists,
char* err, size_t err_size);
@@ -135,4 +169,15 @@ FilterRuleList* filter_file_read(const char* dir_path, const char* owner_rel, bo
FilterAction filter_rules_apply_side(const FilterRuleList* list, const char* rel_path,
const char* leaf, bool is_dir, unsigned side);
/* Evaluate a received per-directory rule set for the receiver side, using
* rsync's per-directory-before-ancestors order: the entry's containing
* directory's rules are tried first, then each ancestor's, then the receive
* root's. `dir_rules` is a flat list whose rules carry `owner`; a rule applies
* only when `owner` is exactly the directory being examined (a no-inherit rule
* therefore applies only to that directory's direct children). Returns the
* first matching rule's receiver verdict (PROTECT/RISK) or FILTER_ACTION_NONE.
* The caller evaluates the command-line base rules after this chain. */
FilterAction filter_dir_rules_apply_side(const FilterRuleList* dir_rules, const char* rel_path,
const char* leaf, bool is_dir);
#endif