syncthing/lib/versioner/simple.go

112 lines
3.0 KiB
Go
Raw Permalink Normal View History

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,
// 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 (
"context"
"sort"
2014-05-25 18:49:08 +00:00
"strconv"
"time"
2014-05-25 18:49:08 +00:00
"github.com/syncthing/syncthing/lib/config"
"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
2014-05-25 18:49:08 +00:00
}
type simple struct {
keep int
cleanoutDays int
folderFs fs.Filesystem
versionsFs fs.Filesystem
copyRangeMethod fs.CopyRangeMethod
2014-05-25 18:49:08 +00:00
}
func newSimple(cfg config.FolderConfiguration) Versioner {
var keep, err = strconv.Atoi(cfg.Versioning.Params["keep"])
cleanoutDays, _ := strconv.Atoi(cfg.Versioning.Params["cleanoutDays"])
// On error we default to 0, "do not clean out the versioned items"
2014-05-25 18:49:08 +00:00
if err != nil {
keep = 5 // A reasonable default
}
s := simple{
keep: keep,
cleanoutDays: cleanoutDays,
folderFs: cfg.Filesystem(nil),
versionsFs: versionerFsFromFolderCfg(cfg),
copyRangeMethod: cfg.CopyRangeMethod,
2014-05-25 18:49:08 +00:00
}
Implement facility based logger, debugging via REST API This implements a new debug/trace infrastructure based on a slightly hacked up logger. Instead of the traditional "if debug { ... }" I've rewritten the logger to have no-op Debugln and Debugf, unless debugging has been enabled for a given "facility". The "facility" is just a string, typically a package name. This will be slightly slower than before; but not that much as it's mostly a function call that returns immediately. For the cases where it matters (the Debugln takes a hex.Dump() of something for example, and it's not in a very occasional "if err != nil" branch) there is an l.ShouldDebug(facility) that is fast enough to be used like the old "if debug". The point of all this is that we can now toggle debugging for the various packages on and off at runtime. There's a new method /rest/system/debug that can be POSTed a set of facilities to enable and disable debug for, or GET from to get a list of facilities with descriptions and their current debug status. Similarly a /rest/system/log?since=... can grab the latest log entries, up to 250 of them (hardcoded constant in main.go) plus the initial few. Not implemented in this commit (but planned) is a simple debug GUI available on /debug that shows the current log in an easily pasteable format and has checkboxes to enable the various debug facilities. The debug instructions to a user then becomes "visit this URL, check these boxes, reproduce your problem, copy and paste the log". The actual log viewer on the hypothetical /debug URL can poll regularly for new log entries and this bypass the 250 line limit. The existing STTRACE=foo variable is still obeyed and just sets the start state of the system.
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).
func (v simple) Archive(filePath string) error {
err := archiveFile(v.copyRangeMethod, v.folderFs, v.versionsFs, filePath, TagFilename)
if err != nil {
2014-05-25 18:49:08 +00:00
return err
}
cleanVersions(v.versionsFs, findAllVersions(v.versionsFs, filePath), v.toRemove)
2014-05-25 18:49:08 +00: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.copyRangeMethod, v.versionsFs, v.folderFs, filepath, versionTime, TagFilename)
}
func (v simple) Clean(ctx context.Context) error {
return clean(ctx, v.versionsFs, v.toRemove)
}
func (v simple) toRemove(versions []string, now time.Time) []string {
var remove []string
// The list of versions may or may not be properly sorted.
sort.Strings(versions)
// If the amount of elements exceeds the limit: the oldest elements are to be removed.
if len(versions) > v.keep {
remove = versions[:len(versions)-v.keep]
versions = versions[len(versions)-v.keep:]
}
// If cleanoutDays is not a positive value then don't remove based on age.
if v.cleanoutDays <= 0 {
return remove
}
maxAge := time.Duration(v.cleanoutDays) * 24 * time.Hour
// For the rest of the versions, elements which are too old are to be removed
for _, version := range versions {
versionTime, err := time.ParseInLocation(TimeFormat, extractTag(version), time.Local)
if err != nil {
l.Debugf("Versioner: file name %q is invalid: %v", version, err)
continue
}
if now.Sub(versionTime) > maxAge {
remove = append(remove, version)
}
}
return remove
}