2014-07-12 22:45:33 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-08-15 10:52:16 +00:00
|
|
|
// Package files provides a set type to track local/remote files with newness
|
|
|
|
// 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.
|
2014-03-28 13:36:57 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/lamport"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
type fileRecord struct {
|
2014-07-12 21:06:48 +00:00
|
|
|
File protocol.FileInfo
|
2014-04-14 09:44:29 +00:00
|
|
|
Usage int
|
|
|
|
Global bool
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type bitset uint64
|
|
|
|
|
|
|
|
type Set 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-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func NewSet(folder string, db *leveldb.DB) *Set {
|
2014-07-06 12:46:48 +00:00
|
|
|
var s = Set{
|
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-03-28 13:36:57 +00:00
|
|
|
}
|
2014-07-15 11:04:37 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
var deviceID protocol.DeviceID
|
|
|
|
ldbWithAllFolderTruncated(db, []byte(folder), func(device []byte, f protocol.FileInfoTruncated) bool {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) 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-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) 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-09-28 11:00:38 +00:00
|
|
|
func (s *Set) 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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) WithNeed(device protocol.DeviceID, fn fileIterator) {
|
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
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) WithNeedTruncated(device protocol.DeviceID, fn fileIterator) {
|
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
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) WithHave(device protocol.DeviceID, fn fileIterator) {
|
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
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) WithHaveTruncated(device protocol.DeviceID, fn fileIterator) {
|
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
|
|
|
}
|
|
|
|
|
2014-08-12 14:17:28 +00:00
|
|
|
func (s *Set) WithGlobal(fn fileIterator) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-12 11:53:31 +00:00
|
|
|
func (s *Set) WithGlobalTruncated(fn fileIterator) {
|
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
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) Get(device protocol.DeviceID, file string) protocol.FileInfo {
|
|
|
|
f := ldbGet(s.db, []byte(s.folder), device[:], []byte(normalizedFilename(file)))
|
2014-08-15 10:52:16 +00:00
|
|
|
f.Name = nativeFilename(f.Name)
|
|
|
|
return f
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
func (s *Set) GetGlobal(file string) protocol.FileInfo {
|
2014-09-28 11:00:38 +00:00
|
|
|
f := ldbGetGlobal(s.db, []byte(s.folder), []byte(normalizedFilename(file)))
|
2014-08-15 10:52:16 +00:00
|
|
|
f.Name = nativeFilename(f.Name)
|
|
|
|
return f
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) Availability(file string) []protocol.DeviceID {
|
|
|
|
return ldbAvailability(s.db, []byte(s.folder), []byte(normalizedFilename(file)))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (s *Set) 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-08-31 11:34:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 10:52:16 +00:00
|
|
|
func normalizeFilenames(fs []protocol.FileInfo) {
|
|
|
|
for i := range fs {
|
|
|
|
fs[i].Name = normalizedFilename(fs[i].Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nativeFileIterator(fn fileIterator) fileIterator {
|
|
|
|
return func(fi protocol.FileIntf) bool {
|
|
|
|
switch f := fi.(type) {
|
|
|
|
case protocol.FileInfo:
|
|
|
|
f.Name = nativeFilename(f.Name)
|
|
|
|
return fn(f)
|
|
|
|
case protocol.FileInfoTruncated:
|
|
|
|
f.Name = nativeFilename(f.Name)
|
|
|
|
return fn(f)
|
|
|
|
default:
|
|
|
|
panic("unknown interface type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|