-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement basic exclusion list support (good enough for --exclude)
This hasn’t been tested extensively yet. Please report discrepancies (compared to original rsync) as you discover them.
- Loading branch information
1 parent
67193a5
commit 15c12f9
Showing
4 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package rsyncd | ||
|
||
import ( | ||
"io" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/gokrazy/rsync/internal/log" | ||
"github.com/gokrazy/rsync/internal/rsyncwire" | ||
) | ||
|
||
type filterRuleList struct { | ||
filters []*filterRule | ||
} | ||
|
||
// exclude.c:add_rule | ||
func (l *filterRuleList) addRule(fr *filterRule) { | ||
if strings.HasSuffix(fr.pattern, "/") { | ||
fr.flag |= filtruleDirectory | ||
fr.pattern = strings.TrimSuffix(fr.pattern, "/") | ||
} | ||
if strings.ContainsFunc(fr.pattern, func(r rune) bool { | ||
return r == '*' || r == '[' || r == '?' | ||
}) { | ||
fr.flag |= filtruleWild | ||
} | ||
l.filters = append(l.filters, fr) | ||
} | ||
|
||
// exclude.c:check_filter | ||
func (l *filterRuleList) matches(name string) bool { | ||
for _, fr := range l.filters { | ||
if fr.matches(name) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// exclude.c:recv_filter_list | ||
func recvFilterList(c *rsyncwire.Conn) (*filterRuleList, error) { | ||
var l filterRuleList | ||
const exclusionListEnd = 0 | ||
for { | ||
length, err := c.ReadInt32() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if length == exclusionListEnd { | ||
break | ||
} | ||
line := make([]byte, length) | ||
if _, err := io.ReadFull(c.Reader, line); err != nil { | ||
return nil, err | ||
} | ||
log.Printf("exclude: %q", string(line)) | ||
fr, err := parseFilter(string(line)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
l.addRule(fr) | ||
} | ||
return &l, nil | ||
} | ||
|
||
const ( | ||
filtruleInclude = 1 << iota | ||
filtruleClearList | ||
filtruleDirectory | ||
filtruleWild | ||
) | ||
|
||
type filterRule struct { | ||
flag int | ||
pattern string | ||
} | ||
|
||
// exclude.c:rule_matches | ||
func (fr *filterRule) matches(name string) bool { | ||
log.Printf("fr %v matches %q?", fr, name) | ||
if fr.flag&filtruleWild != 0 { | ||
panic("wildcard filter rules not yet implemented") | ||
} | ||
if !strings.ContainsRune(fr.pattern, '/') && | ||
fr.flag&filtruleWild == 0 { | ||
name = filepath.Base(name) | ||
} | ||
return fr.pattern == name | ||
} | ||
|
||
// exclude.c:parse_filter_str / exclude.c:parse_rule_tok | ||
func parseFilter(line string) (*filterRule, error) { | ||
rule := new(filterRule) | ||
|
||
// We only support what rsync calls XFLG_OLD_PREFIXES | ||
if strings.HasPrefix(line, "- ") { | ||
// clear include flag | ||
rule.flag &= ^filtruleInclude | ||
line = strings.TrimPrefix(line, "- ") | ||
} else if strings.HasPrefix(line, "+ ") { | ||
// set include flag | ||
rule.flag |= filtruleInclude | ||
line = strings.TrimPrefix(line, "+ ") | ||
} else if strings.HasPrefix(line, "!") { | ||
// set clear_list flag | ||
rule.flag |= filtruleClearList | ||
} | ||
|
||
rule.pattern = line | ||
|
||
return rule, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters