2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2015-01-12 13:50:30 +00:00
|
|
|
// Package db provides a set type to track local/remote files with newness
|
2014-08-15 10:52:16 +00:00
|
|
|
// checks. We must do a certain amount of normalization in here. We will get
|
|
|
|
// fed paths with either native or wire-format separators and encodings
|
|
|
|
// depending on who calls us. We transform paths to wire-format (NFC and
|
|
|
|
// slashes) on the way to the database, and transform to native format
|
|
|
|
// (varying separator and encoding) on the way back out.
|
2015-01-12 13:50:30 +00:00
|
|
|
package db
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
import (
|
2015-10-20 13:58:18 +00:00
|
|
|
stdsync "sync"
|
2016-07-23 12:46:31 +00:00
|
|
|
"sync/atomic"
|
2015-10-20 13:58:18 +00:00
|
|
|
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-03-28 13:36:57 +00:00
|
|
|
)
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
type FileSet struct {
|
2016-07-29 19:54:24 +00:00
|
|
|
sequence int64 // Our local sequence number
|
|
|
|
folder string
|
|
|
|
db *Instance
|
|
|
|
blockmap *BlockMap
|
|
|
|
localSize sizeTracker
|
|
|
|
globalSize sizeTracker
|
|
|
|
|
|
|
|
remoteSequence map[protocol.DeviceID]int64 // Highest seen sequence numbers for other devices
|
|
|
|
updateMutex sync.Mutex // protects remoteSequence and database updates
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 07:18:42 +00:00
|
|
|
// FileIntf is the set of methods implemented by both protocol.FileInfo and
|
2016-07-04 10:40:29 +00:00
|
|
|
// FileInfoTruncated.
|
2015-01-09 07:18:42 +00:00
|
|
|
type FileIntf interface {
|
2016-07-04 10:40:29 +00:00
|
|
|
FileSize() int64
|
|
|
|
FileName() string
|
2015-01-09 07:18:42 +00:00
|
|
|
IsDeleted() bool
|
|
|
|
IsInvalid() bool
|
|
|
|
IsDirectory() bool
|
|
|
|
IsSymlink() bool
|
|
|
|
HasPermissionBits() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Iterator is called with either a protocol.FileInfo or a
|
2016-07-04 10:40:29 +00:00
|
|
|
// FileInfoTruncated (depending on the method) and returns true to
|
2015-01-09 07:18:42 +00:00
|
|
|
// continue iteration, false to stop.
|
|
|
|
type Iterator func(f FileIntf) bool
|
|
|
|
|
2015-10-20 13:58:18 +00:00
|
|
|
type sizeTracker struct {
|
|
|
|
files int
|
|
|
|
deleted int
|
|
|
|
bytes int64
|
|
|
|
mut stdsync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sizeTracker) addFile(f FileIntf) {
|
|
|
|
if f.IsInvalid() {
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 07:10:26 +00:00
|
|
|
|
2015-10-20 13:58:18 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
if f.IsDeleted() {
|
|
|
|
s.deleted++
|
|
|
|
} else {
|
|
|
|
s.files++
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
s.bytes += f.FileSize()
|
2015-10-20 13:58:18 +00:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sizeTracker) removeFile(f FileIntf) {
|
|
|
|
if f.IsInvalid() {
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 07:10:26 +00:00
|
|
|
|
2015-10-20 13:58:18 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
if f.IsDeleted() {
|
|
|
|
s.deleted--
|
|
|
|
} else {
|
|
|
|
s.files--
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
s.bytes -= f.FileSize()
|
2015-10-21 07:10:26 +00:00
|
|
|
if s.deleted < 0 || s.files < 0 {
|
|
|
|
panic("bug: removed more than added")
|
|
|
|
}
|
2015-10-20 13:58:18 +00:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sizeTracker) Size() (files, deleted int, bytes int64) {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
return s.files, s.deleted, s.bytes
|
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func NewFileSet(folder string, db *Instance) *FileSet {
|
2015-01-12 13:52:24 +00:00
|
|
|
var s = FileSet{
|
2016-07-29 19:54:24 +00:00
|
|
|
remoteSequence: make(map[protocol.DeviceID]int64),
|
|
|
|
folder: folder,
|
|
|
|
db: db,
|
|
|
|
blockmap: NewBlockMap(db, db.folderIdx.ID([]byte(folder))),
|
|
|
|
updateMutex: sync.NewMutex(),
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-07-15 11:04:37 +00:00
|
|
|
|
2015-10-28 20:01:46 +00:00
|
|
|
s.db.checkGlobals([]byte(folder), &s.globalSize)
|
2014-10-30 15:48:14 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
var deviceID protocol.DeviceID
|
2015-10-28 20:01:46 +00:00
|
|
|
s.db.withAllFolderTruncated([]byte(folder), func(device []byte, f FileInfoTruncated) bool {
|
2014-09-28 11:00:38 +00:00
|
|
|
copy(deviceID[:], device)
|
2015-10-20 13:58:18 +00:00
|
|
|
if deviceID == protocol.LocalDeviceID {
|
2016-07-29 19:54:24 +00:00
|
|
|
if f.Sequence > s.sequence {
|
|
|
|
s.sequence = f.Sequence
|
2016-07-23 12:46:31 +00:00
|
|
|
}
|
2015-10-21 07:10:26 +00:00
|
|
|
s.localSize.addFile(f)
|
2016-07-29 19:54:24 +00:00
|
|
|
} else if f.Sequence > s.remoteSequence[deviceID] {
|
|
|
|
s.remoteSequence[deviceID] = f.Sequence
|
2015-10-20 13:58:18 +00:00
|
|
|
}
|
2014-07-15 11:04:37 +00:00
|
|
|
return true
|
|
|
|
})
|
2016-07-29 19:54:24 +00:00
|
|
|
l.Debugf("loaded sequence for %q: %#v", folder, s.sequence)
|
2014-07-15 11:04:37 +00:00
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
return &s
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
|
2014-08-15 10:52:16 +00:00
|
|
|
normalizeFilenames(fs)
|
2016-07-23 18:32:10 +00:00
|
|
|
|
|
|
|
s.updateMutex.Lock()
|
|
|
|
defer s.updateMutex.Unlock()
|
|
|
|
|
2016-07-23 12:46:31 +00:00
|
|
|
if device == protocol.LocalDeviceID {
|
|
|
|
if len(fs) == 0 {
|
2016-07-29 19:54:24 +00:00
|
|
|
s.sequence = 0
|
2016-07-23 12:46:31 +00:00
|
|
|
} else {
|
2016-07-29 19:54:24 +00:00
|
|
|
// Always overwrite Sequence on updated files to ensure
|
2016-07-23 12:46:31 +00:00
|
|
|
// correct ordering. The caller is supposed to leave it set to
|
|
|
|
// zero anyhow.
|
|
|
|
for i := range fs {
|
2016-07-29 19:54:24 +00:00
|
|
|
fs[i].Sequence = atomic.AddInt64(&s.sequence, 1)
|
2016-07-23 12:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-07-29 19:54:24 +00:00
|
|
|
s.remoteSequence[device] = maxSequence(fs)
|
2014-09-28 05:56:05 +00:00
|
|
|
}
|
2016-07-23 12:46:31 +00:00
|
|
|
s.db.replace([]byte(s.folder), device[:], fs, &s.localSize, &s.globalSize)
|
2014-10-07 21:15:01 +00:00
|
|
|
if device == protocol.LocalDeviceID {
|
|
|
|
s.blockmap.Drop()
|
|
|
|
s.blockmap.Add(fs)
|
|
|
|
}
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
|
2014-08-15 10:52:16 +00:00
|
|
|
normalizeFilenames(fs)
|
2016-07-23 18:32:10 +00:00
|
|
|
|
|
|
|
s.updateMutex.Lock()
|
|
|
|
defer s.updateMutex.Unlock()
|
|
|
|
|
2014-10-22 14:24:11 +00:00
|
|
|
if device == protocol.LocalDeviceID {
|
|
|
|
discards := make([]protocol.FileInfo, 0, len(fs))
|
|
|
|
updates := make([]protocol.FileInfo, 0, len(fs))
|
2016-07-23 12:46:31 +00:00
|
|
|
for i, newFile := range fs {
|
2016-07-29 19:54:24 +00:00
|
|
|
fs[i].Sequence = atomic.AddInt64(&s.sequence, 1)
|
2015-10-28 20:25:02 +00:00
|
|
|
existingFile, ok := s.db.getFile([]byte(s.folder), device[:], []byte(newFile.Name))
|
2015-03-25 21:37:35 +00:00
|
|
|
if !ok || !existingFile.Version.Equal(newFile.Version) {
|
2014-10-22 14:24:11 +00:00
|
|
|
discards = append(discards, existingFile)
|
|
|
|
updates = append(updates, newFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.blockmap.Discard(discards)
|
|
|
|
s.blockmap.Update(updates)
|
2016-07-23 12:46:31 +00:00
|
|
|
} else {
|
2016-07-29 19:54:24 +00:00
|
|
|
s.remoteSequence[device] = maxSequence(fs)
|
2014-10-22 14:24:11 +00:00
|
|
|
}
|
2016-07-23 12:46:31 +00:00
|
|
|
s.db.updateFiles([]byte(s.folder), device[:], fs, &s.localSize, &s.globalSize)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) WithNeed(device protocol.DeviceID, fn Iterator) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s WithNeed(%v)", s.folder, device)
|
2015-10-28 20:01:46 +00:00
|
|
|
s.db.withNeed([]byte(s.folder), device[:], false, nativeFileIterator(fn))
|
2014-08-12 11:53:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) WithNeedTruncated(device protocol.DeviceID, fn Iterator) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
|
2015-10-28 20:01:46 +00:00
|
|
|
s.db.withNeed([]byte(s.folder), device[:], true, nativeFileIterator(fn))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) WithHave(device protocol.DeviceID, fn Iterator) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s WithHave(%v)", s.folder, device)
|
2016-03-18 12:16:33 +00:00
|
|
|
s.db.withHave([]byte(s.folder), device[:], nil, false, nativeFileIterator(fn))
|
2014-08-12 11:53:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
|
2016-03-18 12:16:33 +00:00
|
|
|
s.db.withHave([]byte(s.folder), device[:], nil, true, nativeFileIterator(fn))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 12:16:33 +00:00
|
|
|
func (s *FileSet) WithPrefixedHaveTruncated(device protocol.DeviceID, prefix string, fn Iterator) {
|
|
|
|
l.Debugf("%s WithPrefixedHaveTruncated(%v)", s.folder, device)
|
2016-05-28 04:18:31 +00:00
|
|
|
s.db.withHave([]byte(s.folder), device[:], []byte(osutil.NormalizedFilename(prefix)), true, nativeFileIterator(fn))
|
2016-03-18 12:16:33 +00:00
|
|
|
}
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) WithGlobal(fn Iterator) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s WithGlobal()", s.folder)
|
2015-10-28 20:01:46 +00:00
|
|
|
s.db.withGlobal([]byte(s.folder), nil, false, nativeFileIterator(fn))
|
2014-08-12 14:17:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) WithGlobalTruncated(fn Iterator) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s WithGlobalTruncated()", s.folder)
|
2015-10-28 20:01:46 +00:00
|
|
|
s.db.withGlobal([]byte(s.folder), nil, true, nativeFileIterator(fn))
|
2015-02-07 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSet) WithPrefixedGlobalTruncated(prefix string, fn Iterator) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("%s WithPrefixedGlobalTruncated()", s.folder, prefix)
|
2015-10-28 20:01:46 +00:00
|
|
|
s.db.withGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(prefix)), true, nativeFileIterator(fn))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) {
|
2015-10-28 20:25:02 +00:00
|
|
|
f, ok := s.db.getFile([]byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
|
2014-11-05 23:41:51 +00:00
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
2015-01-06 21:12:45 +00:00
|
|
|
return f, ok
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
|
2015-10-28 20:01:46 +00:00
|
|
|
fi, ok := s.db.getGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
|
2015-01-09 07:41:02 +00:00
|
|
|
if !ok {
|
|
|
|
return protocol.FileInfo{}, false
|
|
|
|
}
|
|
|
|
f := fi.(protocol.FileInfo)
|
2014-11-05 23:41:51 +00:00
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
2015-01-09 07:41:02 +00:00
|
|
|
return f, true
|
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
|
2015-10-28 20:01:46 +00:00
|
|
|
fi, ok := s.db.getGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
|
2015-01-09 07:41:02 +00:00
|
|
|
if !ok {
|
|
|
|
return FileInfoTruncated{}, false
|
|
|
|
}
|
|
|
|
f := fi.(FileInfoTruncated)
|
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
|
|
|
return f, true
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) Availability(file string) []protocol.DeviceID {
|
2015-10-28 20:01:46 +00:00
|
|
|
return s.db.availability([]byte(s.folder), []byte(osutil.NormalizedFilename(file)))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 19:54:24 +00:00
|
|
|
func (s *FileSet) Sequence(device protocol.DeviceID) int64 {
|
2016-07-23 12:46:31 +00:00
|
|
|
if device == protocol.LocalDeviceID {
|
2016-07-29 19:54:24 +00:00
|
|
|
return atomic.LoadInt64(&s.sequence)
|
2016-07-23 12:46:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 18:32:10 +00:00
|
|
|
s.updateMutex.Lock()
|
|
|
|
defer s.updateMutex.Unlock()
|
2016-07-29 19:54:24 +00:00
|
|
|
return s.remoteSequence[device]
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
|
2015-10-21 07:10:26 +00:00
|
|
|
func (s *FileSet) LocalSize() (files, deleted int, bytes int64) {
|
|
|
|
return s.localSize.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSet) GlobalSize() (files, deleted int, bytes int64) {
|
|
|
|
return s.globalSize.Size()
|
|
|
|
}
|
|
|
|
|
2016-07-23 12:46:31 +00:00
|
|
|
func (s *FileSet) IndexID(device protocol.DeviceID) protocol.IndexID {
|
|
|
|
id := s.db.getIndexID(device[:], []byte(s.folder))
|
|
|
|
if id == 0 && device == protocol.LocalDeviceID {
|
|
|
|
// No index ID set yet. We create one now.
|
|
|
|
id = protocol.NewIndexID()
|
|
|
|
s.db.setIndexID(device[:], []byte(s.folder), id)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSet) SetIndexID(device protocol.DeviceID, id protocol.IndexID) {
|
|
|
|
if device == protocol.LocalDeviceID {
|
|
|
|
panic("do not explicitly set index ID for local device")
|
|
|
|
}
|
|
|
|
s.db.setIndexID(device[:], []byte(s.folder), id)
|
|
|
|
}
|
|
|
|
|
2016-07-29 19:54:24 +00:00
|
|
|
// maxSequence returns the highest of the Sequence numbers found in
|
|
|
|
// the given slice of FileInfos. This should really be the Sequence of
|
2016-07-23 12:46:31 +00:00
|
|
|
// the last item, but Syncthing v0.14.0 and other implementations may not
|
|
|
|
// implement update sorting....
|
2016-07-29 19:54:24 +00:00
|
|
|
func maxSequence(fs []protocol.FileInfo) int64 {
|
2016-07-23 12:46:31 +00:00
|
|
|
var max int64
|
|
|
|
for _, f := range fs {
|
2016-07-29 19:54:24 +00:00
|
|
|
if f.Sequence > max {
|
|
|
|
max = f.Sequence
|
2016-07-23 12:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// DropFolder clears out all information related to the given folder from the
|
2014-08-31 11:34:17 +00:00
|
|
|
// database.
|
2015-10-31 11:31:25 +00:00
|
|
|
func DropFolder(db *Instance, folder string) {
|
|
|
|
db.dropFolder([]byte(folder))
|
2014-10-07 21:15:01 +00:00
|
|
|
bm := &BlockMap{
|
|
|
|
db: db,
|
2016-01-03 18:08:19 +00:00
|
|
|
folder: db.folderIdx.ID([]byte(folder)),
|
2014-10-07 21:15:01 +00:00
|
|
|
}
|
|
|
|
bm.Drop()
|
2015-05-13 14:57:29 +00:00
|
|
|
NewVirtualMtimeRepo(db, folder).Drop()
|
2014-08-31 11:34:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 10:52:16 +00:00
|
|
|
func normalizeFilenames(fs []protocol.FileInfo) {
|
|
|
|
for i := range fs {
|
2014-11-05 23:41:51 +00:00
|
|
|
fs[i].Name = osutil.NormalizedFilename(fs[i].Name)
|
2014-08-15 10:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 07:18:42 +00:00
|
|
|
func nativeFileIterator(fn Iterator) Iterator {
|
|
|
|
return func(fi FileIntf) bool {
|
2014-08-15 10:52:16 +00:00
|
|
|
switch f := fi.(type) {
|
|
|
|
case protocol.FileInfo:
|
2014-11-05 23:41:51 +00:00
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
2014-08-15 10:52:16 +00:00
|
|
|
return fn(f)
|
2015-01-09 07:19:32 +00:00
|
|
|
case FileInfoTruncated:
|
2014-11-05 23:41:51 +00:00
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
2014-08-15 10:52:16 +00:00
|
|
|
return fn(f)
|
|
|
|
default:
|
|
|
|
panic("unknown interface type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|