cmd/syncthing: Add UI for version restoration (fixes #2599)
This commit is contained in:
committed by
Jakob Borg
parent
c7f136c2b8
commit
b0e2050cdb
@@ -77,7 +77,7 @@ func (v Simple) Archive(filePath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
ver := taggedFilename(file, info.ModTime().Format(TimeFormat))
|
||||
ver := TagFilename(file, info.ModTime().Format(TimeFormat))
|
||||
dst := filepath.Join(dir, ver)
|
||||
l.Debugln("moving to", dst)
|
||||
err = osutil.Rename(v.fs, filePath, dst)
|
||||
@@ -86,7 +86,7 @@ func (v Simple) Archive(filePath string) error {
|
||||
}
|
||||
|
||||
// Glob according to the new file~timestamp.ext pattern.
|
||||
pattern := filepath.Join(dir, taggedFilename(file, TimeGlob))
|
||||
pattern := filepath.Join(dir, TagFilename(file, TimeGlob))
|
||||
newVersions, err := v.fs.Glob(pattern)
|
||||
if err != nil {
|
||||
l.Warnln("globbing:", err, "for", pattern)
|
||||
|
||||
@@ -34,14 +34,14 @@ func TestTaggedFilename(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
if tc[0] != "" {
|
||||
// Test tagger
|
||||
tf := taggedFilename(tc[0], tc[1])
|
||||
tf := TagFilename(tc[0], tc[1])
|
||||
if tf != tc[2] {
|
||||
t.Errorf("%s != %s", tf, tc[2])
|
||||
}
|
||||
}
|
||||
|
||||
// Test parser
|
||||
tag := filenameTag(tc[2])
|
||||
tag := ExtractTag(tc[2])
|
||||
if tag != tc[1] {
|
||||
t.Errorf("%s != %s", tag, tc[1])
|
||||
}
|
||||
|
||||
@@ -124,12 +124,13 @@ func (v *Staggered) clean() {
|
||||
}
|
||||
|
||||
// Regular file, or possibly a symlink.
|
||||
ext := filepath.Ext(path)
|
||||
versionTag := filenameTag(path)
|
||||
withoutExt := path[:len(path)-len(ext)-len(versionTag)-1]
|
||||
name := withoutExt + ext
|
||||
|
||||
dirTracker.addFile(path)
|
||||
|
||||
name, _ := UntagFilename(path)
|
||||
if name == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
versionsPerFile[name] = append(versionsPerFile[name], path)
|
||||
|
||||
return nil
|
||||
@@ -173,7 +174,7 @@ func (v *Staggered) toRemove(versions []string, now time.Time) []string {
|
||||
var remove []string
|
||||
for _, file := range versions {
|
||||
loc, _ := time.LoadLocation("Local")
|
||||
versionTime, err := time.ParseInLocation(TimeFormat, filenameTag(file), loc)
|
||||
versionTime, err := time.ParseInLocation(TimeFormat, ExtractTag(file), loc)
|
||||
if err != nil {
|
||||
l.Debugf("Versioner: file name %q is invalid: %v", file, err)
|
||||
continue
|
||||
@@ -258,7 +259,7 @@ func (v *Staggered) Archive(filePath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
ver := taggedFilename(file, time.Now().Format(TimeFormat))
|
||||
ver := TagFilename(file, time.Now().Format(TimeFormat))
|
||||
dst := filepath.Join(inFolderPath, ver)
|
||||
l.Debugln("moving to", dst)
|
||||
|
||||
@@ -273,7 +274,7 @@ func (v *Staggered) Archive(filePath string) error {
|
||||
}
|
||||
|
||||
// Glob according to the new file~timestamp.ext pattern.
|
||||
pattern := filepath.Join(inFolderPath, taggedFilename(file, TimeGlob))
|
||||
pattern := filepath.Join(inFolderPath, TagFilename(file, TimeGlob))
|
||||
newVersions, err := v.versionsFs.Glob(pattern)
|
||||
if err != nil {
|
||||
l.Warnln("globbing:", err, "for", pattern)
|
||||
|
||||
@@ -9,10 +9,11 @@ package versioner
|
||||
import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Inserts ~tag just before the extension of the filename.
|
||||
func taggedFilename(name, tag string) string {
|
||||
func TagFilename(name, tag string) string {
|
||||
dir, file := filepath.Dir(name), filepath.Base(name)
|
||||
ext := filepath.Ext(file)
|
||||
withoutExt := file[:len(file)-len(ext)]
|
||||
@@ -22,7 +23,7 @@ func taggedFilename(name, tag string) string {
|
||||
var tagExp = regexp.MustCompile(`.*~([^~.]+)(?:\.[^.]+)?$`)
|
||||
|
||||
// Returns the tag from a filename, whether at the end or middle.
|
||||
func filenameTag(path string) string {
|
||||
func ExtractTag(path string) string {
|
||||
match := tagExp.FindStringSubmatch(path)
|
||||
// match is []string{"whole match", "submatch"} when successful
|
||||
|
||||
@@ -31,3 +32,17 @@ func filenameTag(path string) string {
|
||||
}
|
||||
return match[1]
|
||||
}
|
||||
|
||||
func UntagFilename(path string) (string, string) {
|
||||
ext := filepath.Ext(path)
|
||||
versionTag := ExtractTag(path)
|
||||
|
||||
// Files tagged with old style tags cannot be untagged.
|
||||
if versionTag == "" || strings.HasSuffix(ext, versionTag) {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
withoutExt := path[:len(path)-len(ext)-len(versionTag)-1]
|
||||
name := withoutExt + ext
|
||||
return name, versionTag
|
||||
}
|
||||
|
||||
@@ -8,12 +8,22 @@
|
||||
// simple default versioning scheme.
|
||||
package versioner
|
||||
|
||||
import "github.com/syncthing/syncthing/lib/fs"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/fs"
|
||||
)
|
||||
|
||||
type Versioner interface {
|
||||
Archive(filePath string) error
|
||||
}
|
||||
|
||||
type FileVersion struct {
|
||||
VersionTime time.Time `json:"versionTime"`
|
||||
ModTime time.Time `json:"modTime"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
var Factories = map[string]func(folderID string, filesystem fs.Filesystem, params map[string]string) Versioner{}
|
||||
|
||||
const (
|
||||
|
||||
Reference in New Issue
Block a user