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,
|
2017-02-09 07:52:18 +01:00
|
|
|
// 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 (
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
2017-08-19 16:36:56 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-08-06 11:29:25 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2016-06-26 12:47:23 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
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 {
|
2017-08-19 16:36:56 +02:00
|
|
|
keep int
|
|
|
|
fs fs.Filesystem
|
2014-05-25 20:49:08 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 16:36:56 +02:00
|
|
|
func NewSimple(folderID string, fs 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{
|
2017-08-19 16:36:56 +02:00
|
|
|
keep: keep,
|
|
|
|
fs: fs,
|
2014-05-25 20:49:08 +02:00
|
|
|
}
|
|
|
|
|
2015-10-03 17:25:21 +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).
|
2014-08-17 07:52:26 +02:00
|
|
|
func (v Simple) Archive(filePath string) error {
|
2017-08-19 16:36:56 +02:00
|
|
|
info, err := v.fs.Lstat(filePath)
|
|
|
|
if fs.IsNotExist(err) {
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln("not archiving nonexistent file", filePath)
|
2015-03-19 11:31:21 +01:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2014-12-08 16:36:15 +01:00
|
|
|
return err
|
2014-05-25 20:49:08 +02:00
|
|
|
}
|
2017-08-19 16:36:56 +02:00
|
|
|
if info.IsSymlink() {
|
2017-07-25 11:36:09 +02:00
|
|
|
panic("bug: attempting to version a symlink")
|
|
|
|
}
|
2014-05-25 20:49:08 +02:00
|
|
|
|
2017-08-19 16:36:56 +02:00
|
|
|
versionsDir := ".stversions"
|
|
|
|
_, err = v.fs.Stat(versionsDir)
|
2014-07-11 11:44:00 +02:00
|
|
|
if err != nil {
|
2017-08-19 16:36:56 +02:00
|
|
|
if fs.IsNotExist(err) {
|
|
|
|
l.Debugln("creating versions dir .stversions")
|
2019-02-02 12:16:27 +01:00
|
|
|
v.fs.Mkdir(versionsDir, 0755)
|
|
|
|
v.fs.Hide(versionsDir)
|
2014-07-11 11:44:00 +02:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln("archiving", filePath)
|
2014-05-25 20:49:08 +02:00
|
|
|
|
2014-07-11 11:44:00 +02:00
|
|
|
file := filepath.Base(filePath)
|
2017-08-19 16:36:56 +02:00
|
|
|
inFolderPath := filepath.Dir(filePath)
|
2014-07-11 11:44:00 +02:00
|
|
|
|
2014-09-28 13:00:38 +02:00
|
|
|
dir := filepath.Join(versionsDir, inFolderPath)
|
2017-08-19 16:36:56 +02:00
|
|
|
err = v.fs.MkdirAll(dir, 0755)
|
|
|
|
if err != nil && !fs.IsExist(err) {
|
2014-05-25 20:49:08 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-01 15:39:23 +01:00
|
|
|
ver := TagFilename(file, info.ModTime().Format(TimeFormat))
|
2014-07-11 11:44:00 +02:00
|
|
|
dst := filepath.Join(dir, ver)
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln("moving to", dst)
|
2017-08-19 16:36:56 +02:00
|
|
|
err = osutil.Rename(v.fs, filePath, dst)
|
2014-05-25 20:49:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-24 10:58:57 +01:00
|
|
|
// Glob according to the new file~timestamp.ext pattern.
|
2018-01-01 15:39:23 +01:00
|
|
|
pattern := filepath.Join(dir, TagFilename(file, TimeGlob))
|
2017-08-19 16:36:56 +02:00
|
|
|
newVersions, err := v.fs.Glob(pattern)
|
2014-05-25 20:49:08 +02:00
|
|
|
if err != nil {
|
2015-12-15 10:04:13 +01:00
|
|
|
l.Warnln("globbing:", err, "for", pattern)
|
2014-05-25 20:49:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-24 10:58:57 +01:00
|
|
|
// Also according to the old file.ext~timestamp pattern.
|
2015-12-15 10:04:13 +01:00
|
|
|
pattern = filepath.Join(dir, file+"~"+TimeGlob)
|
2017-08-19 16:36:56 +02:00
|
|
|
oldVersions, err := v.fs.Glob(pattern)
|
2014-11-24 10:58:57 +01:00
|
|
|
if err != nil {
|
2015-12-15 10:04:13 +01:00
|
|
|
l.Warnln("globbing:", err, "for", pattern)
|
2014-11-24 10:58:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use all the found filenames. "~" sorts after "." so all old pattern
|
|
|
|
// files will be deleted before any new, which is as it should be.
|
2016-06-26 12:47:23 +02:00
|
|
|
versions := util.UniqueStrings(append(oldVersions, newVersions...))
|
2014-11-24 10:58:57 +01:00
|
|
|
|
2014-05-25 20:49:08 +02:00
|
|
|
if len(versions) > v.keep {
|
|
|
|
for _, toRemove := range versions[:len(versions)-v.keep] {
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln("cleaning out", toRemove)
|
2017-08-19 16:36:56 +02:00
|
|
|
err = v.fs.Remove(toRemove)
|
2014-05-25 20:49:08 +02:00
|
|
|
if err != nil {
|
2014-08-17 10:28:36 +02:00
|
|
|
l.Warnln("removing old version:", err)
|
2014-05-25 20:49:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|