Handle invalid file names (Windows) (fixes #238)

This commit is contained in:
Jakob Borg 2014-06-04 10:09:27 +02:00
parent 5065d1d0b4
commit a477989950

View File

@ -6,23 +6,43 @@
package protocol package protocol
// Windows uses backslashes as file separator // Windows uses backslashes as file separator and disallows a bunch of
// characters in the filename
import "path/filepath" import (
"path/filepath"
"strings"
)
var disallowedCharacters = string([]rune{
'<', '>', ':', '"', '|', '?', '*',
0, 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,
})
type nativeModel struct { type nativeModel struct {
next Model next Model
} }
func (m nativeModel) Index(nodeID string, repo string, files []FileInfo) { func (m nativeModel) Index(nodeID string, repo string, files []FileInfo) {
for i := range files { for i, f := range files {
files[i].Name = filepath.FromSlash(files[i].Name) if strings.ContainsAny(f.Name, disallowedCharacters) {
files[i].Flags |= FlagInvalid
l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name)
}
files[i].Name = filepath.FromSlash(f.Name)
} }
m.next.Index(nodeID, repo, files) m.next.Index(nodeID, repo, files)
} }
func (m nativeModel) IndexUpdate(nodeID string, repo string, files []FileInfo) { func (m nativeModel) IndexUpdate(nodeID string, repo string, files []FileInfo) {
for i := range files { for i, f := range files {
if strings.ContainsAny(f.Name, disallowedCharacters) {
files[i].Flags |= FlagInvalid
l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name)
}
files[i].Name = filepath.FromSlash(files[i].Name) files[i].Name = filepath.FromSlash(files[i].Name)
} }
m.next.IndexUpdate(nodeID, repo, files) m.next.IndexUpdate(nodeID, repo, files)