2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2015-01-12 06:50:30 -07:00
|
|
|
// Package db provides a set type to track local/remote files with newness
|
2014-08-15 03:52:16 -07: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 06:50:30 -07:00
|
|
|
package db
|
2014-03-28 06:36:57 -07:00
|
|
|
|
|
|
|
import (
|
2015-10-20 06:58:18 -07:00
|
|
|
stdsync "sync"
|
2016-07-23 05:46:31 -07:00
|
|
|
"sync/atomic"
|
2015-10-20 06:58:18 -07:00
|
|
|
|
2016-08-05 10:45:45 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 10:38:46 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-03-28 06:36:57 -07:00
|
|
|
)
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
type FileSet struct {
|
2016-07-29 12:54:24 -07:00
|
|
|
sequence int64 // Our local sequence number
|
|
|
|
folder string
|
2017-08-19 07:36:56 -07:00
|
|
|
fs fs.Filesystem
|
2016-07-29 12:54:24 -07:00
|
|
|
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 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-09 00:18:42 -07:00
|
|
|
// FileIntf is the set of methods implemented by both protocol.FileInfo and
|
2016-07-04 03:40:29 -07:00
|
|
|
// FileInfoTruncated.
|
2015-01-09 00:18:42 -07:00
|
|
|
type FileIntf interface {
|
2016-07-04 03:40:29 -07:00
|
|
|
FileSize() int64
|
|
|
|
FileName() string
|
2015-01-09 00:18:42 -07: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 03:40:29 -07:00
|
|
|
// FileInfoTruncated (depending on the method) and returns true to
|
2015-01-09 00:18:42 -07:00
|
|
|
// continue iteration, false to stop.
|
|
|
|
type Iterator func(f FileIntf) bool
|
|
|
|
|
2016-10-17 05:10:17 -07:00
|
|
|
type Counts struct {
|
|
|
|
Files int
|
|
|
|
Directories int
|
|
|
|
Symlinks int
|
|
|
|
Deleted int
|
|
|
|
Bytes int64
|
|
|
|
}
|
|
|
|
|
2015-10-20 06:58:18 -07:00
|
|
|
type sizeTracker struct {
|
2016-10-17 05:10:17 -07:00
|
|
|
Counts
|
|
|
|
mut stdsync.Mutex
|
2015-10-20 06:58:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sizeTracker) addFile(f FileIntf) {
|
|
|
|
if f.IsInvalid() {
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 00:10:26 -07:00
|
|
|
|
2015-10-20 06:58:18 -07:00
|
|
|
s.mut.Lock()
|
2016-10-17 05:10:17 -07:00
|
|
|
switch {
|
|
|
|
case f.IsDeleted():
|
|
|
|
s.Deleted++
|
2016-12-09 02:38:36 -07:00
|
|
|
case f.IsDirectory() && !f.IsSymlink():
|
2016-10-17 05:10:17 -07:00
|
|
|
s.Directories++
|
|
|
|
case f.IsSymlink():
|
|
|
|
s.Symlinks++
|
|
|
|
default:
|
|
|
|
s.Files++
|
2015-10-20 06:58:18 -07:00
|
|
|
}
|
2016-10-17 05:10:17 -07:00
|
|
|
s.Bytes += f.FileSize()
|
2015-10-20 06:58:18 -07:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sizeTracker) removeFile(f FileIntf) {
|
|
|
|
if f.IsInvalid() {
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 00:10:26 -07:00
|
|
|
|
2015-10-20 06:58:18 -07:00
|
|
|
s.mut.Lock()
|
2016-10-17 05:10:17 -07:00
|
|
|
switch {
|
|
|
|
case f.IsDeleted():
|
|
|
|
s.Deleted--
|
2016-12-09 02:38:36 -07:00
|
|
|
case f.IsDirectory() && !f.IsSymlink():
|
2016-10-17 05:10:17 -07:00
|
|
|
s.Directories--
|
|
|
|
case f.IsSymlink():
|
|
|
|
s.Symlinks--
|
|
|
|
default:
|
|
|
|
s.Files--
|
2015-10-20 06:58:18 -07:00
|
|
|
}
|
2016-10-17 05:10:17 -07:00
|
|
|
s.Bytes -= f.FileSize()
|
|
|
|
if s.Deleted < 0 || s.Files < 0 || s.Directories < 0 || s.Symlinks < 0 {
|
2015-10-21 00:10:26 -07:00
|
|
|
panic("bug: removed more than added")
|
|
|
|
}
|
2015-10-20 06:58:18 -07:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-11-12 13:20:34 -07:00
|
|
|
func (s *sizeTracker) reset() {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
s.Counts = Counts{}
|
|
|
|
}
|
|
|
|
|
2016-10-17 05:10:17 -07:00
|
|
|
func (s *sizeTracker) Size() Counts {
|
2015-10-20 06:58:18 -07:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
2016-10-17 05:10:17 -07:00
|
|
|
return s.Counts
|
2015-10-20 06:58:18 -07:00
|
|
|
}
|
|
|
|
|
2017-08-19 07:36:56 -07:00
|
|
|
func NewFileSet(folder string, fs fs.Filesystem, db *Instance) *FileSet {
|
2015-01-12 06:52:24 -07:00
|
|
|
var s = FileSet{
|
2016-07-29 12:54:24 -07:00
|
|
|
remoteSequence: make(map[protocol.DeviceID]int64),
|
|
|
|
folder: folder,
|
2017-08-19 07:36:56 -07:00
|
|
|
fs: fs,
|
2016-07-29 12:54:24 -07:00
|
|
|
db: db,
|
|
|
|
blockmap: NewBlockMap(db, db.folderIdx.ID([]byte(folder))),
|
|
|
|
updateMutex: sync.NewMutex(),
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-07-15 04:04:37 -07:00
|
|
|
|
2015-10-28 13:01:46 -07:00
|
|
|
s.db.checkGlobals([]byte(folder), &s.globalSize)
|
2014-10-30 08:48:14 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
var deviceID protocol.DeviceID
|
2015-10-28 13:01:46 -07:00
|
|
|
s.db.withAllFolderTruncated([]byte(folder), func(device []byte, f FileInfoTruncated) bool {
|
2014-09-28 04:00:38 -07:00
|
|
|
copy(deviceID[:], device)
|
2015-10-20 06:58:18 -07:00
|
|
|
if deviceID == protocol.LocalDeviceID {
|
2016-07-29 12:54:24 -07:00
|
|
|
if f.Sequence > s.sequence {
|
|
|
|
s.sequence = f.Sequence
|
2016-07-23 05:46:31 -07:00
|
|
|
}
|
2015-10-21 00:10:26 -07:00
|
|
|
s.localSize.addFile(f)
|
2016-07-29 12:54:24 -07:00
|
|
|
} else if f.Sequence > s.remoteSequence[deviceID] {
|
|
|
|
s.remoteSequence[deviceID] = f.Sequence
|
2015-10-20 06:58:18 -07:00
|
|
|
}
|
2014-07-15 04:04:37 -07:00
|
|
|
return true
|
|
|
|
})
|
2016-07-29 12:54:24 -07:00
|
|
|
l.Debugf("loaded sequence for %q: %#v", folder, s.sequence)
|
2014-07-15 04:04:37 -07:00
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
return &s
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2017-11-12 13:20:34 -07:00
|
|
|
func (s *FileSet) Drop(device protocol.DeviceID) {
|
|
|
|
l.Debugf("%s Drop(%v)", s.folder, device)
|
2016-07-23 11:32:10 -07:00
|
|
|
|
|
|
|
s.updateMutex.Lock()
|
|
|
|
defer s.updateMutex.Unlock()
|
|
|
|
|
2017-11-12 13:20:34 -07:00
|
|
|
s.db.dropDeviceFolder(device[:], []byte(s.folder), &s.globalSize)
|
|
|
|
|
2014-10-07 14:15:01 -07:00
|
|
|
if device == protocol.LocalDeviceID {
|
|
|
|
s.blockmap.Drop()
|
2017-11-12 13:20:34 -07:00
|
|
|
s.localSize.reset()
|
|
|
|
// We deliberately do not reset s.sequence here. Dropping all files
|
|
|
|
// for the local device ID only happens in testing - which expects
|
|
|
|
// the sequence to be retained, like an old Replace() of all files
|
|
|
|
// would do. However, if we ever did it "in production" we would
|
|
|
|
// anyway want to retain the sequence for delta indexes to be happy.
|
|
|
|
} else {
|
|
|
|
// Here, on the other hand, we want to make sure that any file
|
|
|
|
// announced from the remote is newer than our current sequence
|
|
|
|
// number.
|
|
|
|
s.remoteSequence[device] = 0
|
2014-10-07 14:15:01 -07:00
|
|
|
}
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
|
2014-08-15 03:52:16 -07:00
|
|
|
normalizeFilenames(fs)
|
2016-07-23 11:32:10 -07:00
|
|
|
|
|
|
|
s.updateMutex.Lock()
|
|
|
|
defer s.updateMutex.Unlock()
|
|
|
|
|
2017-11-12 13:20:34 -07:00
|
|
|
s.updateLocked(device, fs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSet) updateLocked(device protocol.DeviceID, fs []protocol.FileInfo) {
|
|
|
|
// names must be normalized and the lock held
|
|
|
|
|
2014-10-22 07:24:11 -07:00
|
|
|
if device == protocol.LocalDeviceID {
|
|
|
|
discards := make([]protocol.FileInfo, 0, len(fs))
|
|
|
|
updates := make([]protocol.FileInfo, 0, len(fs))
|
2017-10-24 13:05:29 -07:00
|
|
|
// db.UpdateFiles will sort unchanged files out -> save one db lookup
|
|
|
|
// filter slice according to https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
|
|
|
|
oldFs := fs
|
|
|
|
fs = fs[:0]
|
|
|
|
for _, nf := range oldFs {
|
|
|
|
ef, ok := s.db.getFile([]byte(s.folder), device[:], []byte(nf.Name))
|
|
|
|
if ok && ef.Version.Equal(nf.Version) && ef.Invalid == nf.Invalid {
|
|
|
|
continue
|
2014-10-22 07:24:11 -07:00
|
|
|
}
|
2017-11-12 13:20:34 -07:00
|
|
|
|
2017-10-24 13:05:29 -07:00
|
|
|
nf.Sequence = atomic.AddInt64(&s.sequence, 1)
|
|
|
|
fs = append(fs, nf)
|
2017-11-12 13:20:34 -07:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
discards = append(discards, ef)
|
|
|
|
}
|
2017-10-24 13:05:29 -07:00
|
|
|
updates = append(updates, nf)
|
2014-10-22 07:24:11 -07:00
|
|
|
}
|
|
|
|
s.blockmap.Discard(discards)
|
|
|
|
s.blockmap.Update(updates)
|
2016-07-23 05:46:31 -07:00
|
|
|
} else {
|
2016-07-29 12:54:24 -07:00
|
|
|
s.remoteSequence[device] = maxSequence(fs)
|
2014-10-22 07:24:11 -07:00
|
|
|
}
|
2016-07-23 05:46:31 -07:00
|
|
|
s.db.updateFiles([]byte(s.folder), device[:], fs, &s.localSize, &s.globalSize)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) WithNeed(device protocol.DeviceID, fn Iterator) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("%s WithNeed(%v)", s.folder, device)
|
2017-11-11 12:18:17 -07:00
|
|
|
s.db.withNeed([]byte(s.folder), device[:], false, false, nativeFileIterator(fn))
|
2014-08-12 04:53:31 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) WithNeedTruncated(device protocol.DeviceID, fn Iterator) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
|
2017-11-11 12:18:17 -07:00
|
|
|
s.db.withNeed([]byte(s.folder), device[:], true, false, nativeFileIterator(fn))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNeedOrInvalid considers all invalid files as needed, regardless of their version
|
|
|
|
// (e.g. for pulling when ignore patterns changed)
|
|
|
|
func (s *FileSet) WithNeedOrInvalid(device protocol.DeviceID, fn Iterator) {
|
|
|
|
l.Debugf("%s WithNeedExcludingInvalid(%v)", s.folder, device)
|
|
|
|
s.db.withNeed([]byte(s.folder), device[:], false, true, nativeFileIterator(fn))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSet) WithNeedOrInvalidTruncated(device protocol.DeviceID, fn Iterator) {
|
|
|
|
l.Debugf("%s WithNeedExcludingInvalidTruncated(%v)", s.folder, device)
|
|
|
|
s.db.withNeed([]byte(s.folder), device[:], true, true, nativeFileIterator(fn))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) WithHave(device protocol.DeviceID, fn Iterator) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("%s WithHave(%v)", s.folder, device)
|
2016-03-18 05:16:33 -07:00
|
|
|
s.db.withHave([]byte(s.folder), device[:], nil, false, nativeFileIterator(fn))
|
2014-08-12 04:53:31 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
|
2016-03-18 05:16:33 -07:00
|
|
|
s.db.withHave([]byte(s.folder), device[:], nil, true, nativeFileIterator(fn))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2016-03-18 05:16:33 -07:00
|
|
|
func (s *FileSet) WithPrefixedHaveTruncated(device protocol.DeviceID, prefix string, fn Iterator) {
|
|
|
|
l.Debugf("%s WithPrefixedHaveTruncated(%v)", s.folder, device)
|
2016-05-27 21:18:31 -07:00
|
|
|
s.db.withHave([]byte(s.folder), device[:], []byte(osutil.NormalizedFilename(prefix)), true, nativeFileIterator(fn))
|
2016-03-18 05:16:33 -07:00
|
|
|
}
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) WithGlobal(fn Iterator) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("%s WithGlobal()", s.folder)
|
2015-10-28 13:01:46 -07:00
|
|
|
s.db.withGlobal([]byte(s.folder), nil, false, nativeFileIterator(fn))
|
2014-08-12 07:17:28 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) WithGlobalTruncated(fn Iterator) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("%s WithGlobalTruncated()", s.folder)
|
2015-10-28 13:01:46 -07:00
|
|
|
s.db.withGlobal([]byte(s.folder), nil, true, nativeFileIterator(fn))
|
2015-02-07 03:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSet) WithPrefixedGlobalTruncated(prefix string, fn Iterator) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("%s WithPrefixedGlobalTruncated()", s.folder, prefix)
|
2015-10-28 13:01:46 -07:00
|
|
|
s.db.withGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(prefix)), true, nativeFileIterator(fn))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) {
|
2015-10-28 13:25:02 -07:00
|
|
|
f, ok := s.db.getFile([]byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
|
2014-11-05 16:41:51 -07:00
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
2015-01-06 14:12:45 -07:00
|
|
|
return f, ok
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
|
2015-10-28 13:01:46 -07:00
|
|
|
fi, ok := s.db.getGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
|
2015-01-09 00:41:02 -07:00
|
|
|
if !ok {
|
|
|
|
return protocol.FileInfo{}, false
|
|
|
|
}
|
|
|
|
f := fi.(protocol.FileInfo)
|
2014-11-05 16:41:51 -07:00
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
2015-01-09 00:41:02 -07:00
|
|
|
return f, true
|
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
|
2015-10-28 13:01:46 -07:00
|
|
|
fi, ok := s.db.getGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
|
2015-01-09 00:41:02 -07:00
|
|
|
if !ok {
|
|
|
|
return FileInfoTruncated{}, false
|
|
|
|
}
|
|
|
|
f := fi.(FileInfoTruncated)
|
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
|
|
|
return f, true
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-12 06:52:24 -07:00
|
|
|
func (s *FileSet) Availability(file string) []protocol.DeviceID {
|
2015-10-28 13:01:46 -07:00
|
|
|
return s.db.availability([]byte(s.folder), []byte(osutil.NormalizedFilename(file)))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2016-07-29 12:54:24 -07:00
|
|
|
func (s *FileSet) Sequence(device protocol.DeviceID) int64 {
|
2016-07-23 05:46:31 -07:00
|
|
|
if device == protocol.LocalDeviceID {
|
2016-07-29 12:54:24 -07:00
|
|
|
return atomic.LoadInt64(&s.sequence)
|
2016-07-23 05:46:31 -07:00
|
|
|
}
|
|
|
|
|
2016-07-23 11:32:10 -07:00
|
|
|
s.updateMutex.Lock()
|
|
|
|
defer s.updateMutex.Unlock()
|
2016-07-29 12:54:24 -07:00
|
|
|
return s.remoteSequence[device]
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-08-15 03:52:16 -07:00
|
|
|
|
2016-10-17 05:10:17 -07:00
|
|
|
func (s *FileSet) LocalSize() Counts {
|
2015-10-21 00:10:26 -07:00
|
|
|
return s.localSize.Size()
|
|
|
|
}
|
|
|
|
|
2016-10-17 05:10:17 -07:00
|
|
|
func (s *FileSet) GlobalSize() Counts {
|
2015-10-21 00:10:26 -07:00
|
|
|
return s.globalSize.Size()
|
|
|
|
}
|
|
|
|
|
2016-07-23 05:46:31 -07: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-08-05 10:45:45 -07:00
|
|
|
func (s *FileSet) MtimeFS() *fs.MtimeFS {
|
|
|
|
prefix := s.db.mtimesKey([]byte(s.folder))
|
|
|
|
kv := NewNamespacedKV(s.db, string(prefix))
|
2017-08-19 07:36:56 -07:00
|
|
|
return fs.NewMtimeFS(s.fs, kv)
|
2016-08-05 10:45:45 -07:00
|
|
|
}
|
|
|
|
|
2016-08-07 09:21:59 -07:00
|
|
|
func (s *FileSet) ListDevices() []protocol.DeviceID {
|
|
|
|
s.updateMutex.Lock()
|
|
|
|
devices := make([]protocol.DeviceID, 0, len(s.remoteSequence))
|
|
|
|
for id, seq := range s.remoteSequence {
|
|
|
|
if seq > 0 {
|
|
|
|
devices = append(devices, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.updateMutex.Unlock()
|
|
|
|
return devices
|
|
|
|
}
|
|
|
|
|
2016-07-29 12:54:24 -07: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 05:46:31 -07:00
|
|
|
// the last item, but Syncthing v0.14.0 and other implementations may not
|
|
|
|
// implement update sorting....
|
2016-07-29 12:54:24 -07:00
|
|
|
func maxSequence(fs []protocol.FileInfo) int64 {
|
2016-07-23 05:46:31 -07:00
|
|
|
var max int64
|
|
|
|
for _, f := range fs {
|
2016-07-29 12:54:24 -07:00
|
|
|
if f.Sequence > max {
|
|
|
|
max = f.Sequence
|
2016-07-23 05:46:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// DropFolder clears out all information related to the given folder from the
|
2014-08-31 04:34:17 -07:00
|
|
|
// database.
|
2015-10-31 04:31:25 -07:00
|
|
|
func DropFolder(db *Instance, folder string) {
|
|
|
|
db.dropFolder([]byte(folder))
|
2016-08-05 10:45:45 -07:00
|
|
|
db.dropMtimes([]byte(folder))
|
2014-10-07 14:15:01 -07:00
|
|
|
bm := &BlockMap{
|
|
|
|
db: db,
|
2016-01-03 11:08:19 -07:00
|
|
|
folder: db.folderIdx.ID([]byte(folder)),
|
2014-10-07 14:15:01 -07:00
|
|
|
}
|
|
|
|
bm.Drop()
|
2014-08-31 04:34:17 -07:00
|
|
|
}
|
|
|
|
|
2014-08-15 03:52:16 -07:00
|
|
|
func normalizeFilenames(fs []protocol.FileInfo) {
|
|
|
|
for i := range fs {
|
2014-11-05 16:41:51 -07:00
|
|
|
fs[i].Name = osutil.NormalizedFilename(fs[i].Name)
|
2014-08-15 03:52:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 00:18:42 -07:00
|
|
|
func nativeFileIterator(fn Iterator) Iterator {
|
|
|
|
return func(fi FileIntf) bool {
|
2014-08-15 03:52:16 -07:00
|
|
|
switch f := fi.(type) {
|
|
|
|
case protocol.FileInfo:
|
2014-11-05 16:41:51 -07:00
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
2014-08-15 03:52:16 -07:00
|
|
|
return fn(f)
|
2015-01-09 00:19:32 -07:00
|
|
|
case FileInfoTruncated:
|
2014-11-05 16:41:51 -07:00
|
|
|
f.Name = osutil.NativeFilename(f.Name)
|
2014-08-15 03:52:16 -07:00
|
|
|
return fn(f)
|
|
|
|
default:
|
|
|
|
panic("unknown interface type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|