2016-07-04 12:40:29 +02:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 07:52:18 +01:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-07-04 12:40:29 +02:00
|
|
|
|
|
|
|
//go:generate go run ../../script/protofmt.go structs.proto
|
2018-12-18 12:36:38 +01:00
|
|
|
//go:generate protoc -I ../../ -I . --gogofast_out=Mlib/protocol/bep.proto=github.com/syncthing/syncthing/lib/protocol:. structs.proto
|
2016-07-04 12:40:29 +02:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-10-10 11:48:21 +02:00
|
|
|
"bytes"
|
2016-07-04 12:40:29 +02:00
|
|
|
"fmt"
|
2018-10-10 12:43:07 +02:00
|
|
|
"sort"
|
2016-08-06 15:05:59 +02:00
|
|
|
"time"
|
2016-07-04 12:40:29 +02:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) String() string {
|
2018-06-24 09:50:18 +02:00
|
|
|
switch f.Type {
|
|
|
|
case protocol.FileInfoTypeDirectory:
|
|
|
|
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)
|
|
|
|
case protocol.FileInfoTypeFile:
|
|
|
|
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}",
|
|
|
|
f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.RawBlockSize)
|
|
|
|
case protocol.FileInfoTypeSymlink, protocol.FileInfoTypeDeprecatedSymlinkDirectory, protocol.FileInfoTypeDeprecatedSymlinkFile:
|
|
|
|
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)
|
|
|
|
default:
|
|
|
|
panic("mystery file type detected")
|
|
|
|
}
|
2016-07-04 12:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) IsDeleted() bool {
|
|
|
|
return f.Deleted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) IsInvalid() bool {
|
2018-06-24 09:50:18 +02:00
|
|
|
return f.RawInvalid || f.LocalFlags&protocol.LocalInvalidFlags != 0
|
|
|
|
}
|
|
|
|
|
2018-07-12 10:15:57 +02:00
|
|
|
func (f FileInfoTruncated) IsUnsupported() bool {
|
|
|
|
return f.LocalFlags&protocol.FlagLocalUnsupported != 0
|
|
|
|
}
|
|
|
|
|
2018-06-24 09:50:18 +02:00
|
|
|
func (f FileInfoTruncated) IsIgnored() bool {
|
|
|
|
return f.LocalFlags&protocol.FlagLocalIgnored != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) MustRescan() bool {
|
|
|
|
return f.LocalFlags&protocol.FlagLocalMustRescan != 0
|
2016-07-04 12:40:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 10:15:57 +02:00
|
|
|
func (f FileInfoTruncated) IsReceiveOnlyChanged() bool {
|
|
|
|
return f.LocalFlags&protocol.FlagLocalReceiveOnly != 0
|
|
|
|
}
|
|
|
|
|
2016-07-04 12:40:29 +02:00
|
|
|
func (f FileInfoTruncated) IsDirectory() bool {
|
|
|
|
return f.Type == protocol.FileInfoTypeDirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) IsSymlink() bool {
|
|
|
|
switch f.Type {
|
2017-02-07 09:34:24 +01:00
|
|
|
case protocol.FileInfoTypeSymlink, protocol.FileInfoTypeDeprecatedSymlinkDirectory, protocol.FileInfoTypeDeprecatedSymlinkFile:
|
2016-07-04 12:40:29 +02:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 10:32:35 +02:00
|
|
|
func (f FileInfoTruncated) ShouldConflict() bool {
|
|
|
|
return f.LocalFlags&protocol.LocalConflictFlags != 0
|
|
|
|
}
|
|
|
|
|
2016-07-04 12:40:29 +02:00
|
|
|
func (f FileInfoTruncated) HasPermissionBits() bool {
|
|
|
|
return !f.NoPermissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) FileSize() int64 {
|
2016-09-02 08:45:46 +02:00
|
|
|
if f.Deleted {
|
|
|
|
return 0
|
|
|
|
}
|
2016-12-09 10:38:36 +01:00
|
|
|
if f.IsDirectory() || f.IsSymlink() {
|
2016-09-02 08:45:46 +02:00
|
|
|
return protocol.SyntheticDirectorySize
|
2016-07-04 12:40:29 +02:00
|
|
|
}
|
|
|
|
return f.Size
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:08:50 +02:00
|
|
|
func (f FileInfoTruncated) BlockSize() int {
|
|
|
|
if f.RawBlockSize == 0 {
|
|
|
|
return protocol.MinBlockSize
|
|
|
|
}
|
|
|
|
return int(f.RawBlockSize)
|
|
|
|
}
|
|
|
|
|
2016-07-04 12:40:29 +02:00
|
|
|
func (f FileInfoTruncated) FileName() string {
|
|
|
|
return f.Name
|
|
|
|
}
|
2016-08-06 15:05:59 +02:00
|
|
|
|
2018-07-12 10:15:57 +02:00
|
|
|
func (f FileInfoTruncated) FileLocalFlags() uint32 {
|
|
|
|
return f.LocalFlags
|
|
|
|
}
|
|
|
|
|
2016-08-06 15:05:59 +02:00
|
|
|
func (f FileInfoTruncated) ModTime() time.Time {
|
|
|
|
return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
|
|
|
|
}
|
2017-11-11 20:18:17 +01:00
|
|
|
|
2017-12-14 10:51:17 +01:00
|
|
|
func (f FileInfoTruncated) SequenceNo() int64 {
|
|
|
|
return f.Sequence
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:08:32 +02:00
|
|
|
func (f FileInfoTruncated) FileVersion() protocol.Vector {
|
|
|
|
return f.Version
|
|
|
|
}
|
|
|
|
|
2019-10-15 11:25:12 +02:00
|
|
|
func (f FileInfoTruncated) FileType() protocol.FileInfoType {
|
|
|
|
return f.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) FilePermissions() uint32 {
|
|
|
|
return f.Permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) FileModifiedBy() protocol.ShortID {
|
|
|
|
return f.ModifiedBy
|
|
|
|
}
|
2020-02-10 10:48:30 +01:00
|
|
|
|
2018-06-24 09:50:18 +02:00
|
|
|
func (f FileInfoTruncated) ConvertToIgnoredFileInfo(by protocol.ShortID) protocol.FileInfo {
|
2020-02-19 16:58:09 +01:00
|
|
|
file := f.copyToFileInfo()
|
|
|
|
file.SetIgnored(by)
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfoTruncated) ConvertToDeletedFileInfo(by protocol.ShortID) protocol.FileInfo {
|
|
|
|
file := f.copyToFileInfo()
|
|
|
|
file.SetDeleted(by)
|
|
|
|
return file
|
2017-11-11 20:18:17 +01:00
|
|
|
}
|
2018-07-12 10:15:57 +02:00
|
|
|
|
2020-02-19 16:58:09 +01:00
|
|
|
// copyToFileInfo just copies all members of FileInfoTruncated to protocol.FileInfo
|
|
|
|
func (f FileInfoTruncated) copyToFileInfo() protocol.FileInfo {
|
2020-02-10 10:48:30 +01:00
|
|
|
return protocol.FileInfo{
|
2020-02-19 16:58:09 +01:00
|
|
|
Name: f.Name,
|
|
|
|
Size: f.Size,
|
|
|
|
ModifiedS: f.ModifiedS,
|
|
|
|
ModifiedBy: f.ModifiedBy,
|
|
|
|
Version: f.Version,
|
|
|
|
Sequence: f.Sequence,
|
|
|
|
SymlinkTarget: f.SymlinkTarget,
|
|
|
|
BlocksHash: f.BlocksHash,
|
|
|
|
Type: f.Type,
|
|
|
|
Permissions: f.Permissions,
|
|
|
|
ModifiedNs: f.ModifiedNs,
|
|
|
|
RawBlockSize: f.RawBlockSize,
|
|
|
|
LocalFlags: f.LocalFlags,
|
|
|
|
Deleted: f.Deleted,
|
|
|
|
RawInvalid: f.RawInvalid,
|
|
|
|
NoPermissions: f.NoPermissions,
|
2020-02-10 10:48:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 10:15:57 +02:00
|
|
|
func (c Counts) Add(other Counts) Counts {
|
|
|
|
return Counts{
|
|
|
|
Files: c.Files + other.Files,
|
|
|
|
Directories: c.Directories + other.Directories,
|
|
|
|
Symlinks: c.Symlinks + other.Symlinks,
|
|
|
|
Deleted: c.Deleted + other.Deleted,
|
|
|
|
Bytes: c.Bytes + other.Bytes,
|
|
|
|
Sequence: c.Sequence + other.Sequence,
|
|
|
|
DeviceID: protocol.EmptyDeviceID[:],
|
|
|
|
LocalFlags: c.LocalFlags | other.LocalFlags,
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 11:48:21 +02:00
|
|
|
|
2018-12-11 09:59:04 +01:00
|
|
|
func (c Counts) TotalItems() int32 {
|
|
|
|
return c.Files + c.Directories + c.Symlinks + c.Deleted
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:48:21 +02:00
|
|
|
func (vl VersionList) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
var id protocol.DeviceID
|
|
|
|
b.WriteString("{")
|
|
|
|
for i, v := range vl.Versions {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
copy(id[:], v.Device)
|
2018-10-10 12:43:07 +02:00
|
|
|
fmt.Fprintf(&b, "{%v, %v}", v.Version, id)
|
2018-10-10 11:48:21 +02:00
|
|
|
}
|
|
|
|
b.WriteString("}")
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// update brings the VersionList up to date with file. It returns the updated
|
|
|
|
// VersionList, a potentially removed old FileVersion and its index, as well as
|
|
|
|
// the index where the new FileVersion was inserted.
|
2019-11-29 09:11:52 +01:00
|
|
|
func (vl VersionList) update(folder, device []byte, file protocol.FileInfo, t readOnlyTransaction) (_ VersionList, removedFV FileVersion, removedAt int, insertedAt int, err error) {
|
2018-10-30 05:40:51 +01:00
|
|
|
vl, removedFV, removedAt = vl.pop(device)
|
2018-10-10 11:48:21 +02:00
|
|
|
|
|
|
|
nv := FileVersion{
|
|
|
|
Device: device,
|
|
|
|
Version: file.Version,
|
|
|
|
Invalid: file.IsInvalid(),
|
|
|
|
}
|
2018-10-10 12:43:07 +02:00
|
|
|
i := 0
|
|
|
|
if nv.Invalid {
|
|
|
|
i = sort.Search(len(vl.Versions), func(j int) bool {
|
|
|
|
return vl.Versions[j].Invalid
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for ; i < len(vl.Versions); i++ {
|
|
|
|
switch vl.Versions[i].Version.Compare(file.Version) {
|
2018-10-10 11:48:21 +02:00
|
|
|
case protocol.Equal:
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case protocol.Lesser:
|
|
|
|
// The version at this point in the list is equal to or lesser
|
|
|
|
// ("older") than us. We insert ourselves in front of it.
|
|
|
|
vl = vl.insertAt(i, nv)
|
2019-11-29 09:11:52 +01:00
|
|
|
return vl, removedFV, removedAt, i, nil
|
2018-10-10 11:48:21 +02:00
|
|
|
|
|
|
|
case protocol.ConcurrentLesser, protocol.ConcurrentGreater:
|
|
|
|
// The version at this point is in conflict with us. We must pull
|
|
|
|
// the actual file metadata to determine who wins. If we win, we
|
|
|
|
// insert ourselves in front of the loser here. (The "Lesser" and
|
|
|
|
// "Greater" in the condition above is just based on the device
|
|
|
|
// IDs in the version vector, which is not the only thing we use
|
|
|
|
// to determine the winner.)
|
|
|
|
//
|
|
|
|
// A surprise missing file entry here is counted as a win for us.
|
2019-11-29 09:11:52 +01:00
|
|
|
if of, ok, err := t.getFile(folder, vl.Versions[i].Device, []byte(file.Name)); err != nil {
|
|
|
|
return vl, removedFV, removedAt, i, err
|
|
|
|
} else if !ok || file.WinsConflict(of) {
|
2018-10-10 11:48:21 +02:00
|
|
|
vl = vl.insertAt(i, nv)
|
2019-11-29 09:11:52 +01:00
|
|
|
return vl, removedFV, removedAt, i, nil
|
2018-10-10 11:48:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find a position for an insert above, so append to the end.
|
|
|
|
vl.Versions = append(vl.Versions, nv)
|
|
|
|
|
2019-11-29 09:11:52 +01:00
|
|
|
return vl, removedFV, removedAt, len(vl.Versions) - 1, nil
|
2018-10-10 11:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vl VersionList) insertAt(i int, v FileVersion) VersionList {
|
|
|
|
vl.Versions = append(vl.Versions, FileVersion{})
|
|
|
|
copy(vl.Versions[i+1:], vl.Versions[i:])
|
|
|
|
vl.Versions[i] = v
|
|
|
|
return vl
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:40:51 +01:00
|
|
|
// pop returns the VersionList without the entry for the given device, as well
|
|
|
|
// as the removed FileVersion and the position, where that FileVersion was.
|
|
|
|
// If there is no FileVersion for the given device, the position is -1.
|
|
|
|
func (vl VersionList) pop(device []byte) (VersionList, FileVersion, int) {
|
|
|
|
removedAt := -1
|
|
|
|
for i, v := range vl.Versions {
|
|
|
|
if bytes.Equal(v.Device, device) {
|
|
|
|
vl.Versions = append(vl.Versions[:i], vl.Versions[i+1:]...)
|
|
|
|
return vl, v, i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vl, FileVersion{}, removedAt
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:48:21 +02:00
|
|
|
func (vl VersionList) Get(device []byte) (FileVersion, bool) {
|
|
|
|
for _, v := range vl.Versions {
|
|
|
|
if bytes.Equal(v.Device, device) {
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FileVersion{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileList []protocol.FileInfo
|
|
|
|
|
|
|
|
func (fl fileList) Len() int {
|
|
|
|
return len(fl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fl fileList) Swap(a, b int) {
|
|
|
|
fl[a], fl[b] = fl[b], fl[a]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fl fileList) Less(a, b int) bool {
|
|
|
|
return fl[a].Name < fl[b].Name
|
|
|
|
}
|