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-08-01 14:35:37 +00:00
|
|
|
"github.com/syncthing/syncthing/lamport"
|
|
|
|
"github.com/syncthing/syncthing/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-07-15 11:04:37 +00:00
|
|
|
localVersion map[protocol.NodeID]uint64
|
|
|
|
mutex sync.Mutex
|
|
|
|
repo string
|
|
|
|
db *leveldb.DB
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
func NewSet(repo string, db *leveldb.DB) *Set {
|
|
|
|
var s = Set{
|
2014-07-15 11:04:37 +00:00
|
|
|
localVersion: make(map[protocol.NodeID]uint64),
|
|
|
|
repo: repo,
|
|
|
|
db: db,
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-07-15 11:04:37 +00:00
|
|
|
|
2014-07-17 09:00:54 +00:00
|
|
|
var nodeID protocol.NodeID
|
2014-08-12 11:53:31 +00:00
|
|
|
ldbWithAllRepoTruncated(db, []byte(repo), func(node []byte, f protocol.FileInfoTruncated) bool {
|
2014-07-17 09:00:54 +00:00
|
|
|
copy(nodeID[:], node)
|
|
|
|
if f.LocalVersion > s.localVersion[nodeID] {
|
|
|
|
s.localVersion[nodeID] = 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 {
|
|
|
|
l.Debugf("loaded localVersion for %q: %#v", repo, s.localVersion)
|
|
|
|
}
|
|
|
|
clock(s.localVersion[protocol.LocalNodeID])
|
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-07-12 21:06:48 +00:00
|
|
|
func (s *Set) Replace(node protocol.NodeID, fs []protocol.FileInfo) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-07-06 12:46:48 +00:00
|
|
|
l.Debugf("%s Replace(%v, [%d])", s.repo, node, 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-07-23 11:03:09 +00:00
|
|
|
s.localVersion[node] = ldbReplace(s.db, []byte(s.repo), node[:], fs)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
func (s *Set) ReplaceWithDelete(node protocol.NodeID, fs []protocol.FileInfo) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-07-06 12:46:48 +00:00
|
|
|
l.Debugf("%s ReplaceWithDelete(%v, [%d])", s.repo, node, 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-07-15 11:04:37 +00:00
|
|
|
if lv := ldbReplaceWithDelete(s.db, []byte(s.repo), node[:], fs); lv > s.localVersion[node] {
|
|
|
|
s.localVersion[node] = lv
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
func (s *Set) Update(node protocol.NodeID, fs []protocol.FileInfo) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-07-06 12:46:48 +00:00
|
|
|
l.Debugf("%s Update(%v, [%d])", s.repo, node, 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-07-15 11:04:37 +00:00
|
|
|
if lv := ldbUpdate(s.db, []byte(s.repo), node[:], fs); lv > s.localVersion[node] {
|
|
|
|
s.localVersion[node] = lv
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
func (s *Set) WithNeed(node protocol.NodeID, fn fileIterator) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-07-29 09:06:52 +00:00
|
|
|
l.Debugf("%s WithNeed(%v)", s.repo, node)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
ldbWithNeed(s.db, []byte(s.repo), node[:], false, nativeFileIterator(fn))
|
2014-08-12 11:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Set) WithNeedTruncated(node protocol.NodeID, fn fileIterator) {
|
|
|
|
if debug {
|
|
|
|
l.Debugf("%s WithNeedTruncated(%v)", s.repo, node)
|
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
ldbWithNeed(s.db, []byte(s.repo), node[:], true, nativeFileIterator(fn))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
func (s *Set) WithHave(node protocol.NodeID, fn fileIterator) {
|
2014-03-28 13:36:57 +00:00
|
|
|
if debug {
|
2014-07-06 12:46:48 +00:00
|
|
|
l.Debugf("%s WithHave(%v)", s.repo, node)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
ldbWithHave(s.db, []byte(s.repo), node[:], false, nativeFileIterator(fn))
|
2014-08-12 11:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Set) WithHaveTruncated(node protocol.NodeID, fn fileIterator) {
|
|
|
|
if debug {
|
|
|
|
l.Debugf("%s WithHaveTruncated(%v)", s.repo, node)
|
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
ldbWithHave(s.db, []byte(s.repo), node[:], 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 {
|
|
|
|
l.Debugf("%s WithGlobal()", s.repo)
|
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
ldbWithGlobal(s.db, []byte(s.repo), 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-08-12 14:17:28 +00:00
|
|
|
l.Debugf("%s WithGlobalTruncated()", s.repo)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
ldbWithGlobal(s.db, []byte(s.repo), true, nativeFileIterator(fn))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
func (s *Set) Get(node protocol.NodeID, file string) protocol.FileInfo {
|
2014-08-15 10:52:16 +00:00
|
|
|
f := ldbGet(s.db, []byte(s.repo), node[:], []byte(normalizedFilename(file)))
|
|
|
|
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-08-15 10:52:16 +00:00
|
|
|
f := ldbGetGlobal(s.db, []byte(s.repo), []byte(normalizedFilename(file)))
|
|
|
|
f.Name = nativeFilename(f.Name)
|
|
|
|
return f
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
func (s *Set) Availability(file string) []protocol.NodeID {
|
2014-08-15 10:52:16 +00:00
|
|
|
return ldbAvailability(s.db, []byte(s.repo), []byte(normalizedFilename(file)))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 11:04:37 +00:00
|
|
|
func (s *Set) LocalVersion(node protocol.NodeID) uint64 {
|
2014-07-06 17:21:58 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
2014-07-15 11:04:37 +00:00
|
|
|
return s.localVersion[node]
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-08-15 10:52:16 +00:00
|
|
|
|
2014-08-31 11:34:17 +00:00
|
|
|
// ListRepos returns the repository IDs seen in the database.
|
|
|
|
func ListRepos(db *leveldb.DB) []string {
|
|
|
|
return ldbListRepos(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DropRepo clears out all information related to the given repo from the
|
|
|
|
// database.
|
|
|
|
func DropRepo(db *leveldb.DB, repo string) {
|
|
|
|
ldbDropRepo(db, []byte(repo))
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|