cmd/syncthing: UI for version restoration (fixes #2599) (#4602)

cmd/syncthing: Add UI for version restoration (fixes #2599)
This commit is contained in:
Audrius Butkevicius
2018-01-01 14:39:23 +00:00
committed by Jakob Borg
parent c7f136c2b8
commit b0e2050cdb
33 changed files with 20045 additions and 65 deletions

View File

@@ -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
}