Files
syncthing-arm/lib/versioner/simple.go
T

73 lines
1.8 KiB
Go
Raw Normal View History

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