2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
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 (
|
|
|
|
"sync"
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/lamport"
|
2014-11-05 23:41:51 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/osutil"
|
2014-09-22 19:42:11 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/protocol"
|
2014-07-06 12:46:48 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2014-03-28 13:36:57 +00:00
|
|
|
)
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
type FileSet struct {
|
2014-09-28 11:00:38 +00:00
|
|
|
localVersion map[protocol.DeviceID]uint64
|
2014-07-15 11:04:37 +00:00
|
|
|
mutex sync.Mutex
|
2014-09-28 11:05:25 +00:00
|
|
|
folder string
|
2014-07-15 11:04:37 +00:00
|
|
|
db *leveldb.DB
|
2014-10-07 21:15:01 +00:00
|
|
|
blockmap *BlockMap
|
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
|
|
|
|
// protocol.FileInfoTruncated.
|
|
|
|
type FileIntf interface {
|
|
|
|
Size() int64
|
|
|
|
IsDeleted() bool
|
|
|
|
IsInvalid() bool
|
|
|
|
IsDirectory() bool
|
|
|
|
IsSymlink() bool
|
|
|
|
HasPermissionBits() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Iterator is called with either a protocol.FileInfo or a
|
|
|
|
// protocol.FileInfoTruncated (depending on the method) and returns true to
|
|
|
|
// continue iteration, false to stop.
|
|
|
|
type Iterator func(f FileIntf) bool
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func NewFileSet(folder string, db *leveldb.DB) *FileSet {
|
|
|
|
var s = FileSet{
|
2014-09-28 11:00:38 +00:00
|
|
|
localVersion: make(map[protocol.DeviceID]uint64),
|
2014-09-28 11:05:25 +00:00
|
|
|
folder: folder,
|
2014-07-15 11:04:37 +00:00
|
|
|
db: db,
|
2014-10-07 21:15:01 +00:00
|
|
|
blockmap: NewBlockMap(db, folder),
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-07-15 11:04:37 +00:00
|
|
|
|
2014-10-30 15:48:14 +00:00
|
|
|
ldbCheckGlobals(db, []byte(folder))
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
var deviceID protocol.DeviceID
|
2015-01-09 07:19:32 +00:00
|
|
|
ldbWithAllFolderTruncated(db, []byte(folder), func(device []byte, f FileInfoTruncated) bool {
|
2014-09-28 11:00:38 +00:00
|
|
|
copy(deviceID[:], device)
|
|
|
|
if f.LocalVersion > s.localVersion[deviceID] {
|
|
|
|
s.localVersion[deviceID] = f.LocalVersion
|
2014-07-15 11:04:37 +00:00
|
|
|
}
|
2014-07-17 09:13:23 +00:00
|
|
|
lamport.Default.Tick(f.Version)
|
2014-07-15 11:04:37 +00:00
|
|
|
return true
|
|
|
|
})
|
2014-07-17 09:00:54 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("loaded localVersion for %q: %#v", folder, s.localVersion)
|
2014-07-17 09:00:54 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
clock(s.localVersion[protocol.LocalDeviceID])
|
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) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
normalizeFilenames(fs)
|
2014-07-06 12:46:48 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
2014-09-28 11:00:38 +00:00
|
|
|
s.localVersion[device] = ldbReplace(s.db, []byte(s.folder), device[:], fs)
|
2014-09-28 05:56:05 +00:00
|
|
|
if len(fs) == 0 {
|
|
|
|
// Reset the local version if all files were removed.
|
2014-09-28 11:00:38 +00:00
|
|
|
s.localVersion[device] = 0
|
2014-09-28 05:56:05 +00:00
|
|
|
}
|
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) ReplaceWithDelete(device protocol.DeviceID, fs []protocol.FileInfo) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s ReplaceWithDelete(%v, [%d])", s.folder, device, len(fs))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
normalizeFilenames(fs)
|
2014-07-06 12:46:48 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
2014-09-28 11:00:38 +00:00
|
|
|
if lv := ldbReplaceWithDelete(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
|
|
|
|
s.localVersion[device] = lv
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
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) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
normalizeFilenames(fs)
|
2014-07-06 12:46:48 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.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))
|
|
|
|
for _, newFile := range fs {
|
2015-01-06 21:12:45 +00:00
|
|
|
existingFile, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(newFile.Name))
|
|
|
|
if !ok || existingFile.Version <= 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)
|
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
if lv := ldbUpdate(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
|
|
|
|
s.localVersion[device] = lv
|
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) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s WithNeed(%v)", s.folder, device)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbWithNeed(s.db, []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) {
|
2014-08-12 11:53:31 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
|
2014-08-12 11:53:31 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbWithNeed(s.db, []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) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s WithHave(%v)", s.folder, device)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbWithHave(s.db, []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) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
|
2014-08-12 11:53:31 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
|
2014-08-12 11:53:31 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbWithHave(s.db, []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) WithGlobal(fn Iterator) {
|
2014-08-12 14:17:28 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s WithGlobal()", s.folder)
|
2014-08-12 14:17:28 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbWithGlobal(s.db, []byte(s.folder), 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) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("%s WithGlobalTruncated()", s.folder)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbWithGlobal(s.db, []byte(s.folder), 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-01-06 21:12:45 +00:00
|
|
|
f, ok := ldbGet(s.db, []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-01-09 07:41:02 +00:00
|
|
|
fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
|
|
|
|
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-01-09 07:41:02 +00:00
|
|
|
fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
|
|
|
|
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 {
|
2014-11-05 23:41:51 +00:00
|
|
|
return ldbAvailability(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 13:52:24 +00:00
|
|
|
func (s *FileSet) LocalVersion(device protocol.DeviceID) uint64 {
|
2014-07-06 17:21:58 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
2014-09-28 11:00:38 +00:00
|
|
|
return s.localVersion[device]
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// ListFolders returns the folder IDs seen in the database.
|
|
|
|
func ListFolders(db *leveldb.DB) []string {
|
|
|
|
return ldbListFolders(db)
|
2014-08-31 11:34:17 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2014-09-28 11:00:38 +00:00
|
|
|
func DropFolder(db *leveldb.DB, folder string) {
|
|
|
|
ldbDropFolder(db, []byte(folder))
|
2014-10-07 21:15:01 +00:00
|
|
|
bm := &BlockMap{
|
|
|
|
db: db,
|
|
|
|
folder: folder,
|
|
|
|
}
|
|
|
|
bm.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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|