2016-07-04 03:40:29 -07:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
|
|
|
|
|
|
|
//go:generate go run ../../script/protofmt.go bep.proto
|
2017-01-02 17:16:21 -07:00
|
|
|
//go:generate protoc -I ../../vendor/ -I ../../vendor/github.com/gogo/protobuf/protobuf -I . --gogofast_out=. bep.proto
|
2016-07-04 03:40:29 -07:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-07-23 05:46:31 -07:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2016-07-04 03:40:29 -07:00
|
|
|
"fmt"
|
2018-02-25 01:39:00 -07:00
|
|
|
"runtime"
|
2016-08-06 06:05:59 -07:00
|
|
|
"time"
|
2016-07-23 05:46:31 -07:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2016-09-23 12:33:54 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
2016-07-04 03:40:29 -07:00
|
|
|
)
|
|
|
|
|
2016-09-01 23:45:46 -07:00
|
|
|
const (
|
|
|
|
SyntheticDirectorySize = 128
|
|
|
|
)
|
|
|
|
|
2016-07-04 03:40:29 -07:00
|
|
|
var (
|
|
|
|
sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
|
|
|
|
HelloMessageMagic = uint32(0x2EA7D90B)
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m Hello) Magic() uint32 {
|
|
|
|
return HelloMessageMagic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) String() string {
|
2016-12-09 11:02:18 -07:00
|
|
|
switch f.Type {
|
|
|
|
case FileInfoTypeDirectory:
|
|
|
|
return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Deleted:%v, Invalid:%v, NoPermissions:%v}",
|
|
|
|
f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Deleted, f.Invalid, f.NoPermissions)
|
|
|
|
case FileInfoTypeFile:
|
|
|
|
return fmt.Sprintf("File{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Length:%d, Deleted:%v, Invalid:%v, NoPermissions:%v, Blocks:%v}",
|
|
|
|
f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.Invalid, f.NoPermissions, f.Blocks)
|
2017-02-07 01:34:24 -07:00
|
|
|
case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
|
2016-12-09 11:02:18 -07:00
|
|
|
return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, Deleted:%v, Invalid:%v, NoPermissions:%v, SymlinkTarget:%q}",
|
|
|
|
f.Name, f.Type, f.Sequence, f.Version, f.Deleted, f.Invalid, f.NoPermissions, f.SymlinkTarget)
|
|
|
|
default:
|
|
|
|
panic("mystery file type detected")
|
|
|
|
}
|
2016-07-04 03:40:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsDeleted() bool {
|
|
|
|
return f.Deleted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsInvalid() bool {
|
|
|
|
return f.Invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsDirectory() bool {
|
|
|
|
return f.Type == FileInfoTypeDirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsSymlink() bool {
|
|
|
|
switch f.Type {
|
2017-02-07 01:34:24 -07:00
|
|
|
case FileInfoTypeSymlink, FileInfoTypeDeprecatedSymlinkDirectory, FileInfoTypeDeprecatedSymlinkFile:
|
2016-07-04 03:40:29 -07:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) HasPermissionBits() bool {
|
|
|
|
return !f.NoPermissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) FileSize() int64 {
|
2016-09-01 23:45:46 -07:00
|
|
|
if f.Deleted {
|
|
|
|
return 0
|
|
|
|
}
|
2016-12-09 02:38:36 -07:00
|
|
|
if f.IsDirectory() || f.IsSymlink() {
|
2016-09-01 23:45:46 -07:00
|
|
|
return SyntheticDirectorySize
|
2016-07-04 03:40:29 -07:00
|
|
|
}
|
|
|
|
return f.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) FileName() string {
|
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
2016-08-06 06:05:59 -07:00
|
|
|
func (f FileInfo) ModTime() time.Time {
|
|
|
|
return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
|
|
|
|
}
|
|
|
|
|
2017-12-14 02:51:17 -07:00
|
|
|
func (f FileInfo) SequenceNo() int64 {
|
|
|
|
return f.Sequence
|
|
|
|
}
|
|
|
|
|
2016-07-04 03:40:29 -07: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-02-10 11:40:57 -07:00
|
|
|
// If only one of the files is invalid, that one loses
|
|
|
|
if f.IsInvalid() != other.IsInvalid() {
|
|
|
|
return !f.IsInvalid()
|
|
|
|
}
|
|
|
|
|
2016-07-04 03:40:29 -07: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 06:05:59 -07:00
|
|
|
if f.ModTime().After(other.ModTime()) {
|
2016-07-04 03:40:29 -07:00
|
|
|
return true
|
|
|
|
}
|
2016-08-06 06:05:59 -07:00
|
|
|
if f.ModTime().Before(other.ModTime()) {
|
2016-07-04 03:40:29 -07: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 01:39:00 -07:00
|
|
|
func (f FileInfo) IsEmpty() bool {
|
|
|
|
return f.Version.Counters == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEquivalent checks that the two file infos represent the same actual file content,
|
|
|
|
// 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
|
|
|
|
// - modification time
|
|
|
|
// - 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.
|
|
|
|
func (f FileInfo) IsEquivalent(other FileInfo, ignorePerms bool, ignoreBlocks bool) bool {
|
|
|
|
if f.Name != other.Name || f.Type != other.Type || f.Deleted != other.Deleted || f.Invalid != other.Invalid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ignorePerms && !f.NoPermissions && !other.NoPermissions && !PermsEqual(f.Permissions, other.Permissions) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Type {
|
|
|
|
case FileInfoTypeFile:
|
|
|
|
return f.Size == other.Size && f.ModTime().Equal(other.ModTime()) && (ignoreBlocks || BlocksEqual(f.Blocks, other.Blocks))
|
|
|
|
case FileInfoTypeSymlink:
|
|
|
|
return f.SymlinkTarget == other.SymlinkTarget
|
|
|
|
case FileInfoTypeDirectory:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
func (f *FileInfo) Invalidate(invalidatedBy ShortID) {
|
|
|
|
f.Invalid = true
|
|
|
|
f.ModifiedBy = invalidatedBy
|
|
|
|
f.Blocks = nil
|
|
|
|
f.Sequence = 0
|
|
|
|
}
|
|
|
|
|
2016-07-04 03:40:29 -07:00
|
|
|
func (b BlockInfo) String() string {
|
2016-12-14 16:30:29 -07:00
|
|
|
return fmt.Sprintf("Block{%d/%d/%d/%x}", b.Offset, b.Size, b.WeakHash, b.Hash)
|
2016-07-04 03:40:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if the block is a full block of zeroes.
|
|
|
|
func (b BlockInfo) IsEmpty() bool {
|
|
|
|
return b.Size == BlockSize && bytes.Equal(b.Hash, sha256OfEmptyBlock[:])
|
|
|
|
}
|
2016-07-23 05:46:31 -07: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 00:36:14 -07:00
|
|
|
|
|
|
|
func (f Folder) Description() string {
|
|
|
|
// used by logging stuff
|
2016-12-19 02:12:06 -07:00
|
|
|
if f.Label == "" {
|
|
|
|
return f.ID
|
|
|
|
}
|
2016-11-22 00:36:14 -07:00
|
|
|
return fmt.Sprintf("%q (%s)", f.Label, f.ID)
|
|
|
|
}
|