2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-05-25 11:49:08 -07:00
|
|
|
package versioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2019-04-28 15:30:16 -07:00
|
|
|
"time"
|
2014-05-25 11:49:08 -07:00
|
|
|
|
2017-08-19 07:36:56 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2014-05-25 11:49:08 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Register the constructor for this type of versioner with the name "simple"
|
|
|
|
Factories["simple"] = NewSimple
|
|
|
|
}
|
|
|
|
|
|
|
|
type Simple struct {
|
2019-04-28 15:30:16 -07:00
|
|
|
keep int
|
|
|
|
folderFs fs.Filesystem
|
|
|
|
versionsFs fs.Filesystem
|
2014-05-25 11:49:08 -07:00
|
|
|
}
|
|
|
|
|
2019-04-28 15:30:16 -07:00
|
|
|
func NewSimple(folderID string, folderFs fs.Filesystem, params map[string]string) Versioner {
|
2014-05-25 11:49:08 -07:00
|
|
|
keep, err := strconv.Atoi(params["keep"])
|
|
|
|
if err != nil {
|
|
|
|
keep = 5 // A reasonable default
|
|
|
|
}
|
|
|
|
|
|
|
|
s := Simple{
|
2019-04-28 15:30:16 -07:00
|
|
|
keep: keep,
|
|
|
|
folderFs: folderFs,
|
|
|
|
versionsFs: fsFromParams(folderFs, params),
|
2014-05-25 11:49:08 -07:00
|
|
|
}
|
|
|
|
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("instantiated %#v", s)
|
2014-05-25 11:49:08 -07:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-04-28 13:32:10 -07: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-16 22:52:26 -07:00
|
|
|
func (v Simple) Archive(filePath string) error {
|
2019-04-28 15:30:16 -07:00
|
|
|
err := archiveFile(v.folderFs, v.versionsFs, filePath, TagFilename)
|
2014-07-11 02:44:00 -07:00
|
|
|
if err != nil {
|
2014-05-25 11:49:08 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 22:56:11 -07: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 08:25:21 -07:00
|
|
|
l.Debugln("cleaning out", toRemove)
|
2019-06-24 22:56:11 -07:00
|
|
|
err = v.versionsFs.Remove(toRemove)
|
2014-05-25 11:49:08 -07:00
|
|
|
if err != nil {
|
2014-08-17 01:28:36 -07:00
|
|
|
l.Warnln("removing old version:", err)
|
2014-05-25 11:49:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-28 15:30:16 -07: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)
|
|
|
|
}
|