mirror of
https://github.com/octoleo/syncthing.git
synced 2025-02-08 23:08:27 +00:00
Refactor: break out normalizePath method
This commit is contained in:
parent
d7a3cc505c
commit
dc96849718
@ -252,46 +252,11 @@ func (w *Walker) walkAndHashFiles(fchan, dchan chan protocol.FileInfo) filepath.
|
|||||||
return skip
|
return skip
|
||||||
}
|
}
|
||||||
|
|
||||||
var normalizedRn string
|
relPath, shouldSkip := w.normalizePath(absPath, relPath)
|
||||||
if runtime.GOOS == "darwin" {
|
if shouldSkip {
|
||||||
// Mac OS X file names should always be NFD normalized.
|
|
||||||
normalizedRn = norm.NFD.String(relPath)
|
|
||||||
} else {
|
|
||||||
// Every other OS in the known universe uses NFC or just plain
|
|
||||||
// doesn't bother to define an encoding. In our case *we* do care,
|
|
||||||
// so we enforce NFC regardless.
|
|
||||||
normalizedRn = norm.NFC.String(relPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if relPath != normalizedRn {
|
|
||||||
// The file name was not normalized.
|
|
||||||
|
|
||||||
if !w.AutoNormalize {
|
|
||||||
// We're not authorized to do anything about it, so complain and skip.
|
|
||||||
|
|
||||||
l.Warnf("File name %q is not in the correct UTF8 normalization form; skipping.", relPath)
|
|
||||||
return skip
|
return skip
|
||||||
}
|
}
|
||||||
|
|
||||||
// We will attempt to normalize it.
|
|
||||||
normalizedPath := filepath.Join(w.Dir, normalizedRn)
|
|
||||||
if _, err := osutil.Lstat(normalizedPath); os.IsNotExist(err) {
|
|
||||||
// Nothing exists with the normalized filename. Good.
|
|
||||||
if err = os.Rename(absPath, normalizedPath); err != nil {
|
|
||||||
l.Infof(`Error normalizing UTF8 encoding of file "%s": %v`, relPath, err)
|
|
||||||
return skip
|
|
||||||
}
|
|
||||||
l.Infof(`Normalized UTF8 encoding of file name "%s".`, relPath)
|
|
||||||
} else {
|
|
||||||
// There is something already in the way at the normalized
|
|
||||||
// file name.
|
|
||||||
l.Infof(`File "%s" has UTF8 encoding conflict with another file; ignoring.`, relPath)
|
|
||||||
return skip
|
|
||||||
}
|
|
||||||
|
|
||||||
relPath = normalizedRn
|
|
||||||
}
|
|
||||||
|
|
||||||
var cf protocol.FileInfo
|
var cf protocol.FileInfo
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
@ -445,6 +410,51 @@ func (w *Walker) walkAndHashFiles(fchan, dchan chan protocol.FileInfo) filepath.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// normalizePath returns the normalized relative path (possibly after fixing
|
||||||
|
// it on disk), or skip is true.
|
||||||
|
func (w *Walker) normalizePath(absPath, relPath string) (normPath string, skip bool) {
|
||||||
|
if runtime.GOOS == "darwin" {
|
||||||
|
// Mac OS X file names should always be NFD normalized.
|
||||||
|
normPath = norm.NFD.String(relPath)
|
||||||
|
} else {
|
||||||
|
// Every other OS in the known universe uses NFC or just plain
|
||||||
|
// doesn't bother to define an encoding. In our case *we* do care,
|
||||||
|
// so we enforce NFC regardless.
|
||||||
|
normPath = norm.NFC.String(relPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
if relPath != normPath {
|
||||||
|
// The file name was not normalized.
|
||||||
|
|
||||||
|
if !w.AutoNormalize {
|
||||||
|
// We're not authorized to do anything about it, so complain and skip.
|
||||||
|
|
||||||
|
l.Warnf("File name %q is not in the correct UTF8 normalization form; skipping.", relPath)
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
|
||||||
|
// We will attempt to normalize it.
|
||||||
|
normalizedPath := filepath.Join(w.Dir, normPath)
|
||||||
|
if _, err := osutil.Lstat(normalizedPath); os.IsNotExist(err) {
|
||||||
|
// Nothing exists with the normalized filename. Good.
|
||||||
|
if err = os.Rename(absPath, normalizedPath); err != nil {
|
||||||
|
l.Infof(`Error normalizing UTF8 encoding of file "%s": %v`, relPath, err)
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
l.Infof(`Normalized UTF8 encoding of file name "%s".`, relPath)
|
||||||
|
} else {
|
||||||
|
// There is something already in the way at the normalized
|
||||||
|
// file name.
|
||||||
|
l.Infof(`File "%s" has UTF8 encoding conflict with another file; ignoring.`, relPath)
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath = normPath
|
||||||
|
}
|
||||||
|
|
||||||
|
return normPath, false
|
||||||
|
}
|
||||||
|
|
||||||
func checkDir(dir string) error {
|
func checkDir(dir string) error {
|
||||||
if info, err := osutil.Lstat(dir); err != nil {
|
if info, err := osutil.Lstat(dir); err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user