lib/protocol, lib/discover, lib/db: Use protocol buffer serialization (fixes #3080)
This changes the BEP protocol to use protocol buffer serialization instead of XDR, and therefore also the database format. The local discovery protocol is also updated to be protocol buffer format. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3276 LGTM: AudriusButkevicius
This commit is contained in:
committed by
Audrius Butkevicius
parent
21f5b16e47
commit
fa0101bd60
@@ -71,7 +71,7 @@ func hashFiles(dir string, blockSize int, outbox, inbox chan protocol.FileInfo,
|
||||
panic("Bug. Asked to hash a directory or a deleted file.")
|
||||
}
|
||||
|
||||
blocks, err := HashFile(filepath.Join(dir, f.Name), blockSize, f.CachedSize, counter)
|
||||
blocks, err := HashFile(filepath.Join(dir, f.Name), blockSize, f.Size, counter)
|
||||
if err != nil {
|
||||
l.Debugln("hash error:", f.Name, err)
|
||||
continue
|
||||
|
||||
@@ -168,7 +168,7 @@ func (w *walker) walk() (chan protocol.FileInfo, error) {
|
||||
|
||||
for file := range toHashChan {
|
||||
filesToHash = append(filesToHash, file)
|
||||
total += int64(file.CachedSize)
|
||||
total += int64(file.Size)
|
||||
}
|
||||
|
||||
realToHashChan := make(chan protocol.FileInfo)
|
||||
@@ -309,25 +309,22 @@ func (w *walker) walkRegular(relPath string, info os.FileInfo, mtime time.Time,
|
||||
// - was not invalid (since it looks valid now)
|
||||
// - has the same size as previously
|
||||
cf, ok := w.CurrentFiler.CurrentFile(relPath)
|
||||
permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, curMode)
|
||||
permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Permissions, curMode)
|
||||
if ok && permUnchanged && !cf.IsDeleted() && cf.Modified == mtime.Unix() && !cf.IsDirectory() &&
|
||||
!cf.IsSymlink() && !cf.IsInvalid() && cf.Size() == info.Size() {
|
||||
!cf.IsSymlink() && !cf.IsInvalid() && cf.Size == info.Size() {
|
||||
return nil
|
||||
}
|
||||
|
||||
l.Debugln("rescan:", cf, mtime.Unix(), info.Mode()&os.ModePerm)
|
||||
|
||||
var flags = curMode & uint32(maskModePerm)
|
||||
if w.IgnorePerms {
|
||||
flags = protocol.FlagNoPermBits | 0666
|
||||
}
|
||||
|
||||
f := protocol.FileInfo{
|
||||
Name: relPath,
|
||||
Version: cf.Version.Update(w.ShortID),
|
||||
Flags: flags,
|
||||
Modified: mtime.Unix(),
|
||||
CachedSize: info.Size(),
|
||||
Name: relPath,
|
||||
Type: protocol.FileInfoTypeFile,
|
||||
Version: cf.Version.Update(w.ShortID),
|
||||
Permissions: curMode & uint32(maskModePerm),
|
||||
NoPermissions: w.IgnorePerms,
|
||||
Modified: mtime.Unix(),
|
||||
Size: info.Size(),
|
||||
}
|
||||
l.Debugln("to hash:", relPath, f)
|
||||
|
||||
@@ -349,22 +346,18 @@ func (w *walker) walkDir(relPath string, info os.FileInfo, mtime time.Time, dcha
|
||||
// - was not a symlink (since it's a directory now)
|
||||
// - was not invalid (since it looks valid now)
|
||||
cf, ok := w.CurrentFiler.CurrentFile(relPath)
|
||||
permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, uint32(info.Mode()))
|
||||
permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Permissions, uint32(info.Mode()))
|
||||
if ok && permUnchanged && !cf.IsDeleted() && cf.IsDirectory() && !cf.IsSymlink() && !cf.IsInvalid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
flags := uint32(protocol.FlagDirectory)
|
||||
if w.IgnorePerms {
|
||||
flags |= protocol.FlagNoPermBits | 0777
|
||||
} else {
|
||||
flags |= uint32(info.Mode() & maskModePerm)
|
||||
}
|
||||
f := protocol.FileInfo{
|
||||
Name: relPath,
|
||||
Version: cf.Version.Update(w.ShortID),
|
||||
Flags: flags,
|
||||
Modified: mtime.Unix(),
|
||||
Name: relPath,
|
||||
Type: protocol.FileInfoTypeDirectory,
|
||||
Version: cf.Version.Update(w.ShortID),
|
||||
Permissions: uint32(info.Mode() & maskModePerm),
|
||||
NoPermissions: w.IgnorePerms,
|
||||
Modified: mtime.Unix(),
|
||||
}
|
||||
l.Debugln("dir:", relPath, f)
|
||||
|
||||
@@ -420,11 +413,12 @@ func (w *walker) walkSymlink(absPath, relPath string, dchan chan protocol.FileIn
|
||||
}
|
||||
|
||||
f := protocol.FileInfo{
|
||||
Name: relPath,
|
||||
Version: cf.Version.Update(w.ShortID),
|
||||
Flags: uint32(protocol.FlagSymlink | protocol.FlagNoPermBits | 0666 | SymlinkFlags(targetType)),
|
||||
Modified: 0,
|
||||
Blocks: blocks,
|
||||
Name: relPath,
|
||||
Type: SymlinkType(targetType),
|
||||
Version: cf.Version.Update(w.ShortID),
|
||||
Modified: 0,
|
||||
NoPermissions: true, // Symlinks don't have permissions of their own
|
||||
Blocks: blocks,
|
||||
}
|
||||
|
||||
l.Debugln("symlink changedb:", absPath, f)
|
||||
@@ -519,21 +513,21 @@ func SymlinkTypeEqual(disk symlinks.TargetType, f protocol.FileInfo) bool {
|
||||
case symlinks.TargetUnknown:
|
||||
return true
|
||||
case symlinks.TargetDirectory:
|
||||
return f.IsDirectory() && f.Flags&protocol.FlagSymlinkMissingTarget == 0
|
||||
return f.Type == protocol.FileInfoTypeSymlinkDirectory
|
||||
case symlinks.TargetFile:
|
||||
return !f.IsDirectory() && f.Flags&protocol.FlagSymlinkMissingTarget == 0
|
||||
return f.Type == protocol.FileInfoTypeSymlinkFile
|
||||
}
|
||||
panic("unknown symlink TargetType")
|
||||
}
|
||||
|
||||
func SymlinkFlags(t symlinks.TargetType) uint32 {
|
||||
func SymlinkType(t symlinks.TargetType) protocol.FileInfoType {
|
||||
switch t {
|
||||
case symlinks.TargetFile:
|
||||
return 0
|
||||
return protocol.FileInfoTypeSymlinkFile
|
||||
case symlinks.TargetDirectory:
|
||||
return protocol.FlagDirectory
|
||||
return protocol.FileInfoTypeSymlinkDirectory
|
||||
case symlinks.TargetUnknown:
|
||||
return protocol.FlagSymlinkMissingTarget
|
||||
return protocol.FileInfoTypeSymlinkUnknown
|
||||
}
|
||||
panic("unknown symlink TargetType")
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ import (
|
||||
)
|
||||
|
||||
type testfile struct {
|
||||
name string
|
||||
size int
|
||||
hash string
|
||||
name string
|
||||
length int64
|
||||
hash string
|
||||
}
|
||||
|
||||
type testfileList []testfile
|
||||
@@ -322,7 +322,7 @@ func (l fileList) testfiles() testfileList {
|
||||
if len(f.Blocks) > 1 {
|
||||
panic("simple test case stuff only supports a single block per file")
|
||||
}
|
||||
testfiles[i] = testfile{name: f.Name, size: int(f.Size())}
|
||||
testfiles[i] = testfile{name: f.Name, length: f.FileSize()}
|
||||
if len(f.Blocks) == 1 {
|
||||
testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
|
||||
}
|
||||
@@ -334,7 +334,7 @@ func (l testfileList) String() string {
|
||||
var b bytes.Buffer
|
||||
b.WriteString("{\n")
|
||||
for _, f := range l {
|
||||
fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.size, f.hash)
|
||||
fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.length, f.hash)
|
||||
}
|
||||
b.WriteString("}")
|
||||
return b.String()
|
||||
@@ -342,28 +342,28 @@ func (l testfileList) String() string {
|
||||
|
||||
func TestSymlinkTypeEqual(t *testing.T) {
|
||||
testcases := []struct {
|
||||
onDiskType symlinks.TargetType
|
||||
inIndexFlags uint32
|
||||
equal bool
|
||||
onDiskType symlinks.TargetType
|
||||
fiType protocol.FileInfoType
|
||||
equal bool
|
||||
}{
|
||||
// File is only equal to file
|
||||
{symlinks.TargetFile, 0, true},
|
||||
{symlinks.TargetFile, protocol.FlagDirectory, false},
|
||||
{symlinks.TargetFile, protocol.FlagSymlinkMissingTarget, false},
|
||||
{symlinks.TargetFile, protocol.FileInfoTypeSymlinkFile, true},
|
||||
{symlinks.TargetFile, protocol.FileInfoTypeSymlinkDirectory, false},
|
||||
{symlinks.TargetFile, protocol.FileInfoTypeSymlinkUnknown, false},
|
||||
// Directory is only equal to directory
|
||||
{symlinks.TargetDirectory, 0, false},
|
||||
{symlinks.TargetDirectory, protocol.FlagDirectory, true},
|
||||
{symlinks.TargetDirectory, protocol.FlagSymlinkMissingTarget, false},
|
||||
{symlinks.TargetDirectory, protocol.FileInfoTypeSymlinkFile, false},
|
||||
{symlinks.TargetDirectory, protocol.FileInfoTypeSymlinkDirectory, true},
|
||||
{symlinks.TargetDirectory, protocol.FileInfoTypeSymlinkUnknown, false},
|
||||
// Unknown is equal to anything
|
||||
{symlinks.TargetUnknown, 0, true},
|
||||
{symlinks.TargetUnknown, protocol.FlagDirectory, true},
|
||||
{symlinks.TargetUnknown, protocol.FlagSymlinkMissingTarget, true},
|
||||
{symlinks.TargetUnknown, protocol.FileInfoTypeSymlinkFile, true},
|
||||
{symlinks.TargetUnknown, protocol.FileInfoTypeSymlinkDirectory, true},
|
||||
{symlinks.TargetUnknown, protocol.FileInfoTypeSymlinkUnknown, true},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
res := SymlinkTypeEqual(tc.onDiskType, protocol.FileInfo{Flags: tc.inIndexFlags})
|
||||
res := SymlinkTypeEqual(tc.onDiskType, protocol.FileInfo{Type: tc.fiType})
|
||||
if res != tc.equal {
|
||||
t.Errorf("Incorrect result %v for %v, %v", res, tc.onDiskType, tc.inIndexFlags)
|
||||
t.Errorf("Incorrect result %v for %v, %v", res, tc.onDiskType, tc.fiType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user