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 (
|
2020-07-14 08:48:50 +00:00
|
|
|
"context"
|
2022-09-13 17:20:04 +00:00
|
|
|
"sort"
|
2014-05-25 18:49:08 +00:00
|
|
|
"strconv"
|
2019-04-28 22:30:16 +00:00
|
|
|
"time"
|
2014-05-25 18:49:08 +00:00
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
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"
|
2019-11-26 07:39:31 +00:00
|
|
|
factories["simple"] = newSimple
|
2014-05-25 18:49:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
type simple struct {
|
2020-06-18 06:15:47 +00:00
|
|
|
keep int
|
2020-08-24 11:14:30 +00:00
|
|
|
cleanoutDays int
|
2020-06-18 06:15:47 +00:00
|
|
|
folderFs fs.Filesystem
|
|
|
|
versionsFs fs.Filesystem
|
|
|
|
copyRangeMethod fs.CopyRangeMethod
|
2014-05-25 18:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
func newSimple(cfg config.FolderConfiguration) Versioner {
|
2024-11-19 10:32:56 +00:00
|
|
|
keep, err := strconv.Atoi(cfg.Versioning.Params["keep"])
|
2020-08-24 11:14:30 +00:00
|
|
|
cleanoutDays, _ := strconv.Atoi(cfg.Versioning.Params["cleanoutDays"])
|
2023-08-09 07:10:06 +00:00
|
|
|
// On error we default to 0, "do not clean out the versioned items"
|
2020-08-24 11:14:30 +00:00
|
|
|
|
2014-05-25 18:49:08 +00:00
|
|
|
if err != nil {
|
|
|
|
keep = 5 // A reasonable default
|
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
s := simple{
|
2020-06-18 06:15:47 +00:00
|
|
|
keep: keep,
|
2020-08-24 11:14:30 +00:00
|
|
|
cleanoutDays: cleanoutDays,
|
2022-04-10 18:55:05 +00:00
|
|
|
folderFs: cfg.Filesystem(nil),
|
2020-06-18 06:15:47 +00:00
|
|
|
versionsFs: versionerFsFromFolderCfg(cfg),
|
|
|
|
copyRangeMethod: cfg.CopyRangeMethod,
|
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).
|
2019-11-26 07:39:31 +00:00
|
|
|
func (v simple) Archive(filePath string) error {
|
2020-06-18 06:15:47 +00:00
|
|
|
err := archiveFile(v.copyRangeMethod, 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
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
cleanVersions(v.versionsFs, findAllVersions(v.versionsFs, filePath), v.toRemove)
|
2014-05-25 18:49:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-28 22:30:16 +00:00
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
func (v simple) GetVersions() (map[string][]FileVersion, error) {
|
2019-04-28 22:30:16 +00:00
|
|
|
return retrieveVersions(v.versionsFs)
|
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
func (v simple) Restore(filepath string, versionTime time.Time) error {
|
2020-06-18 06:15:47 +00:00
|
|
|
return restoreFile(v.copyRangeMethod, v.versionsFs, v.folderFs, filepath, versionTime, TagFilename)
|
2019-04-28 22:30:16 +00:00
|
|
|
}
|
2020-07-14 08:48:50 +00:00
|
|
|
|
2020-08-24 11:14:30 +00:00
|
|
|
func (v simple) Clean(ctx context.Context) error {
|
2022-09-13 17:20:04 +00:00
|
|
|
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
|
2020-07-14 08:48:50 +00:00
|
|
|
}
|