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