Use unique versions in staggered versioner (fixes #1063)

This commit is contained in:
Audrius Butkevicius 2014-12-02 18:53:38 +00:00
parent bc8907e90d
commit 3cbe92d797
3 changed files with 17 additions and 7 deletions

View File

@ -18,7 +18,6 @@ package versioner
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strconv" "strconv"
"github.com/syncthing/syncthing/internal/osutil" "github.com/syncthing/syncthing/internal/osutil"
@ -124,10 +123,9 @@ func (v Simple) Archive(filePath string) error {
// Use all the found filenames. "~" sorts after "." so all old pattern // Use all the found filenames. "~" sorts after "." so all old pattern
// files will be deleted before any new, which is as it should be. // files will be deleted before any new, which is as it should be.
versions := append(oldVersions, newVersions...) versions := uniqueSortedStrings(append(oldVersions, newVersions...))
if len(versions) > v.keep { if len(versions) > v.keep {
sort.Strings(versions)
for _, toRemove := range versions[:len(versions)-v.keep] { for _, toRemove := range versions[:len(versions)-v.keep] {
if debug { if debug {
l.Debugln("cleaning out", toRemove) l.Debugln("cleaning out", toRemove)

View File

@ -18,7 +18,6 @@ package versioner
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -357,9 +356,7 @@ func (v Staggered) Archive(filePath string) error {
// Use all the found filenames. // Use all the found filenames.
versions := append(oldVersions, newVersions...) versions := append(oldVersions, newVersions...)
v.expire(uniqueSortedStrings(versions))
sort.Strings(versions)
v.expire(versions)
return nil return nil
} }

View File

@ -18,6 +18,7 @@ package versioner
import ( import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort"
) )
// Inserts ~tag just before the extension of the filename. // Inserts ~tag just before the extension of the filename.
@ -40,3 +41,17 @@ func filenameTag(path string) string {
} }
return match[1] return match[1]
} }
func uniqueSortedStrings(strings []string) []string {
seen := make(map[string]struct{}, len(strings))
unique := make([]string, 0, len(strings))
for _, str := range strings {
_, ok := seen[str]
if !ok {
seen[str] = struct{}{}
unique = append(unique, str)
}
}
sort.Strings(unique)
return unique
}