2015-01-13 13:31:14 +01:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 21:42:11 +02:00
|
|
|
|
2016-03-09 13:43:16 +01:00
|
|
|
//go:generate -command genxdr go run ../../vendor/github.com/calmh/xdr/cmd/genxdr/main.go
|
2014-12-06 14:23:10 +01:00
|
|
|
//go:generate genxdr -o message_xdr.go message.go
|
|
|
|
|
2014-09-22 21:42:11 +02:00
|
|
|
package protocol
|
|
|
|
|
2015-11-21 16:30:53 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2016-03-25 21:29:07 +01:00
|
|
|
var (
|
|
|
|
sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
|
|
|
|
HelloMessageMagic uint32 = 0x9F79BC40
|
|
|
|
)
|
|
|
|
|
|
|
|
type HelloMessage struct {
|
|
|
|
DeviceName string // max:64
|
|
|
|
ClientName string // max:64
|
|
|
|
ClientVersion string // max:64
|
|
|
|
}
|
2014-09-22 21:42:11 +02:00
|
|
|
|
|
|
|
type IndexMessage struct {
|
2015-09-24 19:51:59 +02:00
|
|
|
Folder string // max:256
|
2015-08-18 08:41:34 +02:00
|
|
|
Files []FileInfo // max:1000000
|
2015-01-08 10:28:39 +01:00
|
|
|
Flags uint32
|
|
|
|
Options []Option // max:64
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type FileInfo struct {
|
|
|
|
Name string // max:8192
|
|
|
|
Flags uint32
|
|
|
|
Modified int64
|
2015-03-20 09:58:32 +01:00
|
|
|
Version Vector
|
2015-01-18 01:26:52 +01:00
|
|
|
LocalVersion int64
|
2015-08-26 23:33:03 +02:00
|
|
|
CachedSize int64 // noencode (cache only)
|
2016-02-19 20:47:31 +01:00
|
|
|
Blocks []BlockInfo // max:10000000
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) String() string {
|
2015-03-20 09:58:32 +01:00
|
|
|
return fmt.Sprintf("File{Name:%q, Flags:0%o, Modified:%d, Version:%v, Size:%d, Blocks:%v}",
|
2014-09-22 21:42:11 +02:00
|
|
|
f.Name, f.Flags, f.Modified, f.Version, f.Size(), f.Blocks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) Size() (bytes int64) {
|
2014-11-05 00:22:15 +01:00
|
|
|
if f.IsDeleted() || f.IsDirectory() {
|
2014-09-22 21:42:11 +02:00
|
|
|
return 128
|
|
|
|
}
|
2015-10-21 23:33:04 +02:00
|
|
|
if f.CachedSize > 0 {
|
|
|
|
return f.CachedSize
|
|
|
|
}
|
2014-09-22 21:42:11 +02:00
|
|
|
for _, b := range f.Blocks {
|
|
|
|
bytes += int64(b.Size)
|
|
|
|
}
|
2015-10-21 23:33:04 +02:00
|
|
|
f.CachedSize = bytes
|
2014-09-22 21:42:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsDeleted() bool {
|
2014-11-05 00:22:15 +01:00
|
|
|
return f.Flags&FlagDeleted != 0
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsInvalid() bool {
|
2014-11-05 00:22:15 +01:00
|
|
|
return f.Flags&FlagInvalid != 0
|
2014-10-06 22:57:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsDirectory() bool {
|
2014-11-05 00:22:15 +01:00
|
|
|
return f.Flags&FlagDirectory != 0
|
|
|
|
}
|
|
|
|
|
2014-11-08 00:06:04 +01:00
|
|
|
func (f FileInfo) IsSymlink() bool {
|
|
|
|
return f.Flags&FlagSymlink != 0
|
|
|
|
}
|
|
|
|
|
2014-11-05 00:22:15 +01:00
|
|
|
func (f FileInfo) HasPermissionBits() bool {
|
|
|
|
return f.Flags&FlagNoPermBits == 0
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:12:19 +02: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 {
|
|
|
|
// 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.
|
|
|
|
if f.Modified > other.Modified {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if f.Modified < other.Modified {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-22 21:42:11 +02:00
|
|
|
type BlockInfo struct {
|
|
|
|
Offset int64 // noencode (cache only)
|
2015-01-18 01:26:52 +01:00
|
|
|
Size int32
|
2014-09-22 21:42:11 +02:00
|
|
|
Hash []byte // max:64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BlockInfo) String() string {
|
|
|
|
return fmt.Sprintf("Block{%d/%d/%x}", b.Offset, b.Size, b.Hash)
|
|
|
|
}
|
|
|
|
|
2015-11-21 16:30:53 +01: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[:])
|
|
|
|
}
|
|
|
|
|
2014-09-22 21:42:11 +02:00
|
|
|
type RequestMessage struct {
|
2015-09-24 19:51:59 +02:00
|
|
|
Folder string // max:256
|
2015-01-08 10:28:39 +01:00
|
|
|
Name string // max:8192
|
2015-01-18 01:26:52 +01:00
|
|
|
Offset int64
|
|
|
|
Size int32
|
2015-01-08 10:28:39 +01:00
|
|
|
Hash []byte // max:64
|
|
|
|
Flags uint32
|
|
|
|
Options []Option // max:64
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseMessage struct {
|
2015-02-06 22:34:51 +01:00
|
|
|
Data []byte
|
|
|
|
Code int32
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ClusterConfigMessage struct {
|
2016-03-25 21:29:07 +01:00
|
|
|
Folders []Folder // max:1000000
|
|
|
|
Options []Option // max:64
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ClusterConfigMessage) GetOption(key string) string {
|
|
|
|
for _, option := range o.Options {
|
|
|
|
if option.Key == key {
|
|
|
|
return option.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-09-28 13:00:38 +02:00
|
|
|
type Folder struct {
|
2015-09-24 19:51:59 +02:00
|
|
|
ID string // max:256
|
2016-03-11 10:48:46 +01:00
|
|
|
Label string // max:256
|
2015-08-18 08:41:34 +02:00
|
|
|
Devices []Device // max:1000000
|
2015-03-25 21:20:04 +01:00
|
|
|
Flags uint32
|
|
|
|
Options []Option // max:64
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 13:00:38 +02:00
|
|
|
type Device struct {
|
2015-09-27 12:39:02 +02:00
|
|
|
ID []byte // max:32
|
|
|
|
Name string // max:64
|
|
|
|
Addresses []string // max:64,2083
|
|
|
|
Compression uint32
|
|
|
|
CertName string // max:64
|
2015-01-18 01:26:52 +01:00
|
|
|
MaxLocalVersion int64
|
2015-03-25 21:20:04 +01:00
|
|
|
Flags uint32
|
|
|
|
Options []Option // max:64
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option struct {
|
|
|
|
Key string // max:64
|
|
|
|
Value string // max:1024
|
|
|
|
}
|
|
|
|
|
|
|
|
type CloseMessage struct {
|
|
|
|
Reason string // max:1024
|
2015-01-18 01:26:52 +01:00
|
|
|
Code int32
|
2014-09-22 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type EmptyMessage struct{}
|