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 (
|
|
|
|
"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"
|
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-06-25 05:56:11 +00:00
|
|
|
// Versions are sorted by timestamp in the file name, oldest first.
|
|
|
|
versions := findAllVersions(v.versionsFs, filePath)
|
|
|
|
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-06-25 05:56:11 +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)
|
|
|
|
}
|