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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-05-25 18:49:08 +00:00
|
|
|
package versioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2019-04-28 22:30:16 +00:00
|
|
|
"time"
|
2014-05-25 18:49:08 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2016-06-26 10:47:23 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2014-05-25 18:49:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Register the constructor for this type of versioner with the name "simple"
|
|
|
|
Factories["simple"] = NewSimple
|
|
|
|
}
|
|
|
|
|
|
|
|
type Simple struct {
|
2019-04-28 22:30:16 +00:00
|
|
|
keep int
|
|
|
|
folderFs fs.Filesystem
|
|
|
|
versionsFs fs.Filesystem
|
2014-05-25 18:49:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
func NewSimple(folderID string, folderFs fs.Filesystem, params map[string]string) Versioner {
|
2014-05-25 18:49:08 +00:00
|
|
|
keep, err := strconv.Atoi(params["keep"])
|
|
|
|
if err != nil {
|
|
|
|
keep = 5 // A reasonable default
|
|
|
|
}
|
|
|
|
|
|
|
|
s := Simple{
|
2019-04-28 22:30:16 +00:00
|
|
|
keep: keep,
|
|
|
|
folderFs: folderFs,
|
|
|
|
versionsFs: fsFromParams(folderFs, params),
|
2014-05-25 18:49:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("instantiated %#v", s)
|
2014-05-25 18:49:08 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-04-28 20:32:10 +00:00
|
|
|
// Archive moves the named file away to a version archive. If this function
|
|
|
|
// returns nil, the named file does not exist any more (has been archived).
|
2014-08-17 05:52:26 +00:00
|
|
|
func (v Simple) Archive(filePath string) error {
|
2019-04-28 22:30:16 +00:00
|
|
|
err := archiveFile(v.folderFs, v.versionsFs, filePath, TagFilename)
|
2014-07-11 09:44:00 +00:00
|
|
|
if err != nil {
|
2014-05-25 18:49:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
file := filepath.Base(filePath)
|
|
|
|
dir := filepath.Dir(filePath)
|
2014-05-25 18:49:08 +00:00
|
|
|
|
2014-11-24 09:58:57 +00:00
|
|
|
// Glob according to the new file~timestamp.ext pattern.
|
2018-01-01 14:39:23 +00:00
|
|
|
pattern := filepath.Join(dir, TagFilename(file, TimeGlob))
|
2019-04-28 22:30:16 +00:00
|
|
|
newVersions, err := v.versionsFs.Glob(pattern)
|
2014-05-25 18:49:08 +00:00
|
|
|
if err != nil {
|
2015-12-15 09:04:13 +00:00
|
|
|
l.Warnln("globbing:", err, "for", pattern)
|
2014-05-25 18:49:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:58:57 +00:00
|
|
|
// Also according to the old file.ext~timestamp pattern.
|
2015-12-15 09:04:13 +00:00
|
|
|
pattern = filepath.Join(dir, file+"~"+TimeGlob)
|
2019-04-28 22:30:16 +00:00
|
|
|
oldVersions, err := v.versionsFs.Glob(pattern)
|
2014-11-24 09:58:57 +00:00
|
|
|
if err != nil {
|
2015-12-15 09:04:13 +00:00
|
|
|
l.Warnln("globbing:", err, "for", pattern)
|
2014-11-24 09:58:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use all the found filenames. "~" sorts after "." so all old pattern
|
|
|
|
// files will be deleted before any new, which is as it should be.
|
2016-06-26 10:47:23 +00:00
|
|
|
versions := util.UniqueStrings(append(oldVersions, newVersions...))
|
2014-11-24 09:58:57 +00:00
|
|
|
|
2014-05-25 18:49:08 +00:00
|
|
|
if len(versions) > v.keep {
|
|
|
|
for _, toRemove := range versions[:len(versions)-v.keep] {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("cleaning out", toRemove)
|
2019-04-28 22:30:16 +00:00
|
|
|
err = v.versionsFs.Remove(toRemove)
|
2014-05-25 18:49:08 +00:00
|
|
|
if err != nil {
|
2014-08-17 08:28:36 +00:00
|
|
|
l.Warnln("removing old version:", err)
|
2014-05-25 18:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-28 22:30:16 +00:00
|
|
|
|
|
|
|
func (v Simple) GetVersions() (map[string][]FileVersion, error) {
|
|
|
|
return retrieveVersions(v.versionsFs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Simple) Restore(filepath string, versionTime time.Time) error {
|
|
|
|
return restoreFile(v.versionsFs, v.folderFs, filepath, versionTime, TagFilename)
|
|
|
|
}
|