Move filename conversion into osutil

This commit is contained in:
Audrius Butkevicius 2014-11-05 23:41:51 +00:00 committed by Jakob Borg
parent 938e287501
commit c891999e1d
5 changed files with 20 additions and 18 deletions

View File

@ -29,6 +29,7 @@ import (
"sync" "sync"
"github.com/syncthing/syncthing/internal/config" "github.com/syncthing/syncthing/internal/config"
"github.com/syncthing/syncthing/internal/osutil"
"github.com/syncthing/syncthing/internal/protocol" "github.com/syncthing/syncthing/internal/protocol"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
@ -171,7 +172,7 @@ func (f *BlockFinder) Iterate(hash []byte, iterFn func(string, string, uint32) b
for iter.Next() && iter.Error() == nil { for iter.Next() && iter.Error() == nil {
folder, file := fromBlockKey(iter.Key()) folder, file := fromBlockKey(iter.Key())
index := binary.BigEndian.Uint32(iter.Value()) index := binary.BigEndian.Uint32(iter.Value())
if iterFn(folder, nativeFilename(file), index) { if iterFn(folder, osutil.NativeFilename(file), index) {
return true return true
} }
} }

View File

@ -25,6 +25,7 @@ import (
"sync" "sync"
"github.com/syncthing/syncthing/internal/lamport" "github.com/syncthing/syncthing/internal/lamport"
"github.com/syncthing/syncthing/internal/osutil"
"github.com/syncthing/syncthing/internal/protocol" "github.com/syncthing/syncthing/internal/protocol"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
) )
@ -174,19 +175,19 @@ func (s *Set) WithGlobalTruncated(fn fileIterator) {
} }
func (s *Set) Get(device protocol.DeviceID, file string) protocol.FileInfo { func (s *Set) Get(device protocol.DeviceID, file string) protocol.FileInfo {
f := ldbGet(s.db, []byte(s.folder), device[:], []byte(normalizedFilename(file))) f := ldbGet(s.db, []byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
f.Name = nativeFilename(f.Name) f.Name = osutil.NativeFilename(f.Name)
return f return f
} }
func (s *Set) GetGlobal(file string) protocol.FileInfo { func (s *Set) GetGlobal(file string) protocol.FileInfo {
f := ldbGetGlobal(s.db, []byte(s.folder), []byte(normalizedFilename(file))) f := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
f.Name = nativeFilename(f.Name) f.Name = osutil.NativeFilename(f.Name)
return f return f
} }
func (s *Set) Availability(file string) []protocol.DeviceID { func (s *Set) Availability(file string) []protocol.DeviceID {
return ldbAvailability(s.db, []byte(s.folder), []byte(normalizedFilename(file))) return ldbAvailability(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
} }
func (s *Set) LocalVersion(device protocol.DeviceID) uint64 { func (s *Set) LocalVersion(device protocol.DeviceID) uint64 {
@ -213,7 +214,7 @@ func DropFolder(db *leveldb.DB, folder string) {
func normalizeFilenames(fs []protocol.FileInfo) { func normalizeFilenames(fs []protocol.FileInfo) {
for i := range fs { for i := range fs {
fs[i].Name = normalizedFilename(fs[i].Name) fs[i].Name = osutil.NormalizedFilename(fs[i].Name)
} }
} }
@ -221,10 +222,10 @@ func nativeFileIterator(fn fileIterator) fileIterator {
return func(fi protocol.FileIntf) bool { return func(fi protocol.FileIntf) bool {
switch f := fi.(type) { switch f := fi.(type) {
case protocol.FileInfo: case protocol.FileInfo:
f.Name = nativeFilename(f.Name) f.Name = osutil.NativeFilename(f.Name)
return fn(f) return fn(f)
case protocol.FileInfoTruncated: case protocol.FileInfoTruncated:
f.Name = nativeFilename(f.Name) f.Name = osutil.NativeFilename(f.Name)
return fn(f) return fn(f)
default: default:
panic("unknown interface type") panic("unknown interface type")

View File

@ -13,14 +13,14 @@
// You should have received a copy of the GNU General Public License along // You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>. // with this program. If not, see <http://www.gnu.org/licenses/>.
package files package osutil
import "code.google.com/p/go.text/unicode/norm" import "code.google.com/p/go.text/unicode/norm"
func normalizedFilename(s string) string { func NormalizedFilename(s string) string {
return norm.NFC.String(s) return norm.NFC.String(s)
} }
func nativeFilename(s string) string { func NativeFilename(s string) string {
return norm.NFD.String(s) return norm.NFD.String(s)
} }

View File

@ -15,14 +15,14 @@
// +build !windows,!darwin // +build !windows,!darwin
package files package osutil
import "code.google.com/p/go.text/unicode/norm" import "code.google.com/p/go.text/unicode/norm"
func normalizedFilename(s string) string { func NormalizedFilename(s string) string {
return norm.NFC.String(s) return norm.NFC.String(s)
} }
func nativeFilename(s string) string { func NativeFilename(s string) string {
return s return s
} }

View File

@ -13,7 +13,7 @@
// You should have received a copy of the GNU General Public License along // You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>. // with this program. If not, see <http://www.gnu.org/licenses/>.
package files package osutil
import ( import (
"path/filepath" "path/filepath"
@ -21,10 +21,10 @@ import (
"code.google.com/p/go.text/unicode/norm" "code.google.com/p/go.text/unicode/norm"
) )
func normalizedFilename(s string) string { func NormalizedFilename(s string) string {
return norm.NFC.String(filepath.ToSlash(s)) return norm.NFC.String(filepath.ToSlash(s))
} }
func nativeFilename(s string) string { func NativeFilename(s string) string {
return filepath.FromSlash(s) return filepath.FromSlash(s)
} }