2016-07-04 10:40:29 +00:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
|
|
|
|
|
|
|
//go:generate go run ../../script/protofmt.go bep.proto
|
2018-12-18 11:36:38 +00:00
|
|
|
//go:generate protoc -I ../../ -I . --gogofast_out=. bep.proto
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-07-23 12:46:31 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2016-07-04 10:40:29 +00:00
|
|
|
"fmt"
|
2018-02-25 08:39:00 +00:00
|
|
|
"runtime"
|
2016-08-06 13:05:59 +00:00
|
|
|
"time"
|
2016-07-23 12:46:31 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2016-07-04 10:40:29 +00:00
|
|
|
)
|
|
|
|
|
2016-09-02 06:45:46 +00:00
|
|
|
const (
|
2019-01-17 19:48:43 +00:00
|
|
|
SyntheticDirectorySize = 128
|
|
|
|
HelloMessageMagic uint32 = 0x2EA7D90B
|
|
|
|
Version13HelloMagic uint32 = 0x9F79BC40 // old
|
2016-07-04 10:40:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (m Hello) Magic() uint32 {
|
|
|
|
return HelloMessageMagic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) String() string {
|
2016-12-09 18:02:18 +00:00
|
|
|
switch f.Type {
|
|
|
|
case FileInfoTypeDirectory:
|
2018-06-24 07:50:18 +00:00
|
|
|
return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v}",
|
|
|
|
f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions)
|
2016-12-09 18:02:18 +00:00
|
|
|
case FileInfoTypeFile:
|
2018-06-24 07:50:18 +00:00
|
|
|
return fmt.Sprintf("File{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Length:%d, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, BlockSize:%d, Blocks:%v}",
|
|
|
|
f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.RawBlockSize, f.Blocks)
|
2017-02-07 08:34:24 +00:00
|
|
|
case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
|
2018-06-24 07:50:18 +00:00
|
|
|
return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, SymlinkTarget:%q}",
|
|
|
|
f.Name, f.Type, f.Sequence, f.Version, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.SymlinkTarget)
|
2016-12-09 18:02:18 +00:00
|
|
|
default:
|
|
|
|
panic("mystery file type detected")
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsDeleted() bool {
|
|
|
|
return f.Deleted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsInvalid() bool {
|
2018-06-24 07:50:18 +00:00
|
|
|
return f.RawInvalid || f.LocalFlags&LocalInvalidFlags != 0
|
|
|
|
}
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
func (f FileInfo) IsUnsupported() bool {
|
|
|
|
return f.LocalFlags&FlagLocalUnsupported != 0
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
func (f FileInfo) IsIgnored() bool {
|
|
|
|
return f.LocalFlags&FlagLocalIgnored != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) MustRescan() bool {
|
|
|
|
return f.LocalFlags&FlagLocalMustRescan != 0
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
func (f FileInfo) IsReceiveOnlyChanged() bool {
|
|
|
|
return f.LocalFlags&FlagLocalReceiveOnly != 0
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (f FileInfo) IsDirectory() bool {
|
|
|
|
return f.Type == FileInfoTypeDirectory
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
func (f FileInfo) ShouldConflict() bool {
|
|
|
|
return f.LocalFlags&LocalConflictFlags != 0
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (f FileInfo) IsSymlink() bool {
|
|
|
|
switch f.Type {
|
2017-02-07 08:34:24 +00:00
|
|
|
case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
|
2016-07-04 10:40:29 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) HasPermissionBits() bool {
|
|
|
|
return !f.NoPermissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) FileSize() int64 {
|
2016-09-02 06:45:46 +00:00
|
|
|
if f.Deleted {
|
|
|
|
return 0
|
|
|
|
}
|
2016-12-09 09:38:36 +00:00
|
|
|
if f.IsDirectory() || f.IsSymlink() {
|
2016-09-02 06:45:46 +00:00
|
|
|
return SyntheticDirectorySize
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
return f.Size
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:08:50 +00:00
|
|
|
func (f FileInfo) BlockSize() int {
|
|
|
|
if f.RawBlockSize == 0 {
|
|
|
|
return MinBlockSize
|
|
|
|
}
|
|
|
|
return int(f.RawBlockSize)
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (f FileInfo) FileName() string {
|
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
func (f FileInfo) FileLocalFlags() uint32 {
|
|
|
|
return f.LocalFlags
|
|
|
|
}
|
|
|
|
|
2016-08-06 13:05:59 +00:00
|
|
|
func (f FileInfo) ModTime() time.Time {
|
|
|
|
return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
|
|
|
|
}
|
|
|
|
|
2017-12-14 09:51:17 +00:00
|
|
|
func (f FileInfo) SequenceNo() int64 {
|
|
|
|
return f.Sequence
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:08:32 +00:00
|
|
|
func (f FileInfo) FileVersion() Vector {
|
|
|
|
return f.Version
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:25:12 +00:00
|
|
|
func (f FileInfo) FileType() FileInfoType {
|
|
|
|
return f.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) FilePermissions() uint32 {
|
|
|
|
return f.Permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) FileModifiedBy() ShortID {
|
|
|
|
return f.ModifiedBy
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// WinsConflict returns true if "f" is the one to choose when it is in
|
|
|
|
// conflict with "other".
|
|
|
|
func (f FileInfo) WinsConflict(other FileInfo) bool {
|
2018-07-12 08:15:57 +00:00
|
|
|
// If only one of the files is invalid, that one loses.
|
2018-02-10 18:40:57 +00:00
|
|
|
if f.IsInvalid() != other.IsInvalid() {
|
|
|
|
return !f.IsInvalid()
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// If a modification is in conflict with a delete, we pick the
|
|
|
|
// modification.
|
|
|
|
if !f.IsDeleted() && other.IsDeleted() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if f.IsDeleted() && !other.IsDeleted() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The one with the newer modification time wins.
|
2016-08-06 13:05:59 +00:00
|
|
|
if f.ModTime().After(other.ModTime()) {
|
2016-07-04 10:40:29 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-08-06 13:05:59 +00:00
|
|
|
if f.ModTime().Before(other.ModTime()) {
|
2016-07-04 10:40:29 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The modification times were equal. Use the device ID in the version
|
|
|
|
// vector as tie breaker.
|
|
|
|
return f.Version.Compare(other.Version) == ConcurrentGreater
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
func (f FileInfo) IsEmpty() bool {
|
|
|
|
return f.Version.Counters == nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:48:53 +00:00
|
|
|
func (f FileInfo) IsEquivalent(other FileInfo, modTimeWindow time.Duration) bool {
|
|
|
|
return f.isEquivalent(other, modTimeWindow, false, false, 0)
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 19:48:53 +00:00
|
|
|
func (f FileInfo) IsEquivalentOptional(other FileInfo, modTimeWindow time.Duration, ignorePerms bool, ignoreBlocks bool, ignoreFlags uint32) bool {
|
|
|
|
return f.isEquivalent(other, modTimeWindow, ignorePerms, ignoreBlocks, ignoreFlags)
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isEquivalent checks that the two file infos represent the same actual file content,
|
2018-02-25 08:39:00 +00:00
|
|
|
// i.e. it does purposely not check only selected (see below) struct members.
|
|
|
|
// Permissions (config) and blocks (scanning) can be excluded from the comparison.
|
|
|
|
// Any file info is not "equivalent", if it has different
|
|
|
|
// - type
|
|
|
|
// - deleted flag
|
|
|
|
// - invalid flag
|
|
|
|
// - permissions, unless they are ignored
|
|
|
|
// A file is not "equivalent", if it has different
|
2019-07-23 19:48:53 +00:00
|
|
|
// - modification time (difference bigger than modTimeWindow)
|
2018-02-25 08:39:00 +00:00
|
|
|
// - size
|
|
|
|
// - blocks, unless there are no blocks to compare (scanning)
|
|
|
|
// A symlink is not "equivalent", if it has different
|
|
|
|
// - target
|
|
|
|
// A directory does not have anything specific to check.
|
2019-07-23 19:48:53 +00:00
|
|
|
func (f FileInfo) isEquivalent(other FileInfo, modTimeWindow time.Duration, ignorePerms bool, ignoreBlocks bool, ignoreFlags uint32) bool {
|
2018-06-24 07:50:18 +00:00
|
|
|
if f.MustRescan() || other.MustRescan() {
|
|
|
|
// These are per definition not equivalent because they don't
|
|
|
|
// represent a valid state, even if both happen to have the
|
|
|
|
// MustRescan bit set.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
// Mask out the ignored local flags before checking IsInvalid() below
|
|
|
|
f.LocalFlags &^= ignoreFlags
|
|
|
|
other.LocalFlags &^= ignoreFlags
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
if f.Name != other.Name || f.Type != other.Type || f.Deleted != other.Deleted || f.IsInvalid() != other.IsInvalid() {
|
2018-02-25 08:39:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ignorePerms && !f.NoPermissions && !other.NoPermissions && !PermsEqual(f.Permissions, other.Permissions) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Type {
|
|
|
|
case FileInfoTypeFile:
|
2019-07-23 19:48:53 +00:00
|
|
|
return f.Size == other.Size && ModTimeEqual(f.ModTime(), other.ModTime(), modTimeWindow) && (ignoreBlocks || BlocksEqual(f.Blocks, other.Blocks))
|
2018-02-25 08:39:00 +00:00
|
|
|
case FileInfoTypeSymlink:
|
|
|
|
return f.SymlinkTarget == other.SymlinkTarget
|
|
|
|
case FileInfoTypeDirectory:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:48:53 +00:00
|
|
|
func ModTimeEqual(a, b time.Time, modTimeWindow time.Duration) bool {
|
|
|
|
if a.Equal(b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
diff := a.Sub(b)
|
|
|
|
if diff < 0 {
|
|
|
|
diff *= -1
|
|
|
|
}
|
|
|
|
return diff < modTimeWindow
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
func PermsEqual(a, b uint32) bool {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
// There is only writeable and read only, represented for user, group
|
|
|
|
// and other equally. We only compare against user.
|
|
|
|
return a&0600 == b&0600
|
|
|
|
default:
|
|
|
|
// All bits count
|
|
|
|
return a&0777 == b&0777
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlocksEqual returns whether two slices of blocks are exactly the same hash
|
|
|
|
// and index pair wise.
|
|
|
|
func BlocksEqual(a, b []BlockInfo) bool {
|
|
|
|
if len(b) != len(a) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, sblk := range a {
|
|
|
|
if !bytes.Equal(sblk.Hash, b[i].Hash) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
func (f *FileInfo) SetMustRescan(by ShortID) {
|
|
|
|
f.LocalFlags = FlagLocalMustRescan
|
|
|
|
f.ModifiedBy = by
|
|
|
|
f.Blocks = nil
|
|
|
|
f.Sequence = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileInfo) SetIgnored(by ShortID) {
|
|
|
|
f.LocalFlags = FlagLocalIgnored
|
|
|
|
f.ModifiedBy = by
|
|
|
|
f.Blocks = nil
|
|
|
|
f.Sequence = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileInfo) SetUnsupported(by ShortID) {
|
|
|
|
f.LocalFlags = FlagLocalUnsupported
|
|
|
|
f.ModifiedBy = by
|
2017-11-11 19:18:17 +00:00
|
|
|
f.Blocks = nil
|
|
|
|
f.Sequence = 0
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (b BlockInfo) String() string {
|
2016-12-14 23:30:29 +00:00
|
|
|
return fmt.Sprintf("Block{%d/%d/%d/%x}", b.Offset, b.Size, b.WeakHash, b.Hash)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if the block is a full block of zeroes.
|
|
|
|
func (b BlockInfo) IsEmpty() bool {
|
2018-04-16 18:08:50 +00:00
|
|
|
if v, ok := sha256OfEmptyBlock[int(b.Size)]; ok {
|
|
|
|
return bytes.Equal(b.Hash, v[:])
|
|
|
|
}
|
|
|
|
return false
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2016-07-23 12:46:31 +00:00
|
|
|
|
|
|
|
type IndexID uint64
|
|
|
|
|
|
|
|
func (i IndexID) String() string {
|
|
|
|
return fmt.Sprintf("0x%16X", uint64(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i IndexID) Marshal() ([]byte, error) {
|
|
|
|
bs := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(bs, uint64(i))
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexID) Unmarshal(bs []byte) error {
|
|
|
|
if len(bs) != 8 {
|
|
|
|
return errors.New("incorrect IndexID length")
|
|
|
|
}
|
|
|
|
*i = IndexID(binary.BigEndian.Uint64(bs))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIndexID() IndexID {
|
|
|
|
return IndexID(rand.Int64())
|
|
|
|
}
|
2016-11-22 07:36:14 +00:00
|
|
|
|
|
|
|
func (f Folder) Description() string {
|
|
|
|
// used by logging stuff
|
2016-12-19 09:12:06 +00:00
|
|
|
if f.Label == "" {
|
|
|
|
return f.ID
|
|
|
|
}
|
2016-11-22 07:36:14 +00:00
|
|
|
return fmt.Sprintf("%q (%s)", f.Label, f.ID)
|
|
|
|
}
|