syncthing/proto/bep/bep.proto
Jakob Borg 77970d5113
refactor: use modern Protobuf encoder (#9817)
At a high level, this is what I've done and why:

- I'm moving the protobuf generation for the `protocol`, `discovery` and
`db` packages to the modern alternatives, and using `buf` to generate
because it's nice and simple.
- After trying various approaches on how to integrate the new types with
the existing code, I opted for splitting off our own data model types
from the on-the-wire generated types. This means we can have a
`FileInfo` type with nicer ergonomics and lots of methods, while the
protobuf generated type stays clean and close to the wire protocol. It
does mean copying between the two when required, which certainly adds a
small amount of inefficiency. If we want to walk this back in the future
and use the raw generated type throughout, that's possible, this however
makes the refactor smaller (!) as it doesn't change everything about the
type for everyone at the same time.
- I have simply removed in cold blood a significant number of old
database migrations. These depended on previous generations of generated
messages of various kinds and were annoying to support in the new
fashion. The oldest supported database version now is the one from
Syncthing 1.9.0 from Sep 7, 2020.
- I changed config structs to be regular manually defined structs.

For the sake of discussion, some things I tried that turned out not to
work...

### Embedding / wrapping

Embedding the protobuf generated structs in our existing types as a data
container and keeping our methods and stuff:

```
package protocol

type FileInfo struct {
  *generated.FileInfo
}
```

This generates a lot of problems because the internal shape of the
generated struct is quite different (different names, different types,
more pointers), because initializing it doesn't work like you'd expect
(i.e., you end up with an embedded nil pointer and a panic), and because
the types of child types don't get wrapped. That is, even if we also
have a similar wrapper around a `Vector`, that's not the type you get
when accessing `someFileInfo.Version`, you get the `*generated.Vector`
that doesn't have methods, etc.

### Aliasing

```
package protocol

type FileInfo = generated.FileInfo
```

Doesn't help because you can't attach methods to it, plus all the above.

### Generating the types into the target package like we do now and
attaching methods

This fails because of the different shape of the generated type (as in
the embedding case above) plus the generated struct already has a bunch
of methods that we can't necessarily override properly (like `String()`
and a bunch of getters).

### Methods to functions

I considered just moving all the methods we attach to functions in a
specific package, so that for example

```
package protocol

func (f FileInfo) Equal(other FileInfo) bool
```

would become

```
package fileinfos

func Equal(a, b *generated.FileInfo) bool
```

and this would mostly work, but becomes quite verbose and cumbersome,
and somewhat limits discoverability (you can't see what methods are
available on the type in auto completions, etc). In the end I did this
in some cases, like in the database layer where a lot of things like
`func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv
*generated.FileVersion)` because they were anyway just internal methods.

Fixes #8247
2024-12-01 16:50:17 +01:00

254 lines
5.2 KiB
Protocol Buffer

syntax = "proto3";
package bep;
// --- Pre-auth ---
message Hello {
string device_name = 1;
string client_name = 2;
string client_version = 3;
int32 num_connections = 4;
int64 timestamp = 5;
}
// --- Header ---
message Header {
MessageType type = 1;
MessageCompression compression = 2;
}
enum MessageType {
MESSAGE_TYPE_CLUSTER_CONFIG = 0;
MESSAGE_TYPE_INDEX = 1;
MESSAGE_TYPE_INDEX_UPDATE = 2;
MESSAGE_TYPE_REQUEST = 3;
MESSAGE_TYPE_RESPONSE = 4;
MESSAGE_TYPE_DOWNLOAD_PROGRESS = 5;
MESSAGE_TYPE_PING = 6;
MESSAGE_TYPE_CLOSE = 7;
}
enum MessageCompression {
MESSAGE_COMPRESSION_NONE = 0;
MESSAGE_COMPRESSION_LZ4 = 1;
}
// --- Actual messages ---
// Cluster Config
message ClusterConfig {
repeated Folder folders = 1;
bool secondary = 2;
}
message Folder {
string id = 1;
string label = 2;
bool read_only = 3;
bool ignore_permissions = 4;
bool ignore_delete = 5;
bool disable_temp_indexes = 6;
bool paused = 7;
repeated Device devices = 16;
}
message Device {
bytes id = 1;
string name = 2;
repeated string addresses = 3;
Compression compression = 4;
string cert_name = 5;
int64 max_sequence = 6;
bool introducer = 7;
uint64 index_id = 8;
bool skip_introduction_removals = 9;
bytes encryption_password_token = 10;
}
enum Compression {
COMPRESSION_METADATA = 0;
COMPRESSION_NEVER = 1;
COMPRESSION_ALWAYS = 2;
}
// Index and Index Update
message Index {
string folder = 1;
repeated FileInfo files = 2;
int64 last_sequence = 3; // the highest sequence in this batch
}
message IndexUpdate {
string folder = 1;
repeated FileInfo files = 2;
int64 last_sequence = 3; // the highest sequence in this batch
int64 prev_sequence = 4; // the highest sequence in the previous batch
}
message FileInfo {
// The field ordering here optimizes for struct size / alignment --
// large types come before smaller ones.
string name = 1;
int64 size = 3;
int64 modified_s = 5;
uint64 modified_by = 12;
Vector version = 9;
int64 sequence = 10;
repeated BlockInfo blocks = 16;
string symlink_target = 17;
bytes blocks_hash = 18;
bytes encrypted = 19;
FileInfoType type = 2;
uint32 permissions = 4;
int32 modified_ns = 11;
int32 block_size = 13;
PlatformData platform = 14;
// The local_flags fields stores flags that are relevant to the local
// host only. It is not part of the protocol, doesn't get sent or
// received (we make sure to zero it), nonetheless we need it on our
// struct and to be able to serialize it to/from the database.
uint32 local_flags = 1000;
// The version_hash is an implementation detail and not part of the wire
// format.
bytes version_hash = 1001;
// The time when the inode was last changed (i.e., permissions, xattrs
// etc changed). This is host-local, not sent over the wire.
int64 inode_change_ns = 1002;
// The size of the data appended to the encrypted file on disk. This is
// host-local, not sent over the wire.
int32 encryption_trailer_size = 1003;
bool deleted = 6;
bool invalid = 7;
bool no_permissions = 8;
}
enum FileInfoType {
FILE_INFO_TYPE_FILE = 0;
FILE_INFO_TYPE_DIRECTORY = 1;
FILE_INFO_TYPE_SYMLINK_FILE = 2 [deprecated = true];
FILE_INFO_TYPE_SYMLINK_DIRECTORY = 3 [deprecated = true];
FILE_INFO_TYPE_SYMLINK = 4;
}
message BlockInfo {
bytes hash = 3;
int64 offset = 1;
int32 size = 2;
uint32 weak_hash = 4;
}
message Vector {
repeated Counter counters = 1;
}
message Counter {
uint64 id = 1;
uint64 value = 2;
}
message PlatformData {
UnixData unix = 1;
WindowsData windows = 2;
XattrData linux = 3;
XattrData darwin = 4;
XattrData freebsd = 5;
XattrData netbsd = 6;
}
message UnixData {
// The owner name and group name are set when known (i.e., could be
// resolved on the source device), while the UID and GID are always set
// as they come directly from the stat() call.
string owner_name = 1;
string group_name = 2;
int32 uid = 3;
int32 gid = 4;
}
message WindowsData {
// Windows file objects have a single owner, which may be a user or a
// group. We keep the name of that account, and a flag to indicate what
// type it is.
string owner_name = 1;
bool owner_is_group = 2;
}
message XattrData {
repeated Xattr xattrs = 1;
}
message Xattr {
string name = 1;
bytes value = 2;
}
// Request
message Request {
int32 id = 1;
string folder = 2;
string name = 3;
int64 offset = 4;
int32 size = 5;
bytes hash = 6;
bool from_temporary = 7;
uint32 weak_hash = 8;
int32 block_no = 9;
}
// Response
message Response {
int32 id = 1;
bytes data = 2;
ErrorCode code = 3;
}
enum ErrorCode {
ERROR_CODE_NO_ERROR = 0;
ERROR_CODE_GENERIC = 1;
ERROR_CODE_NO_SUCH_FILE = 2;
ERROR_CODE_INVALID_FILE = 3;
}
// DownloadProgress
message DownloadProgress {
string folder = 1;
repeated FileDownloadProgressUpdate updates = 2;
}
message FileDownloadProgressUpdate {
FileDownloadProgressUpdateType update_type = 1;
string name = 2;
Vector version = 3;
repeated int32 block_indexes = 4 [packed = false];
int32 block_size = 5;
}
enum FileDownloadProgressUpdateType {
FILE_DOWNLOAD_PROGRESS_UPDATE_TYPE_APPEND = 0;
FILE_DOWNLOAD_PROGRESS_UPDATE_TYPE_FORGET = 1;
}
// Ping
message Ping {}
// Close
message Close {
string reason = 1;
}