2014-11-24 09:58:57 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
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-11-24 09:58:57 +00:00
|
|
|
|
|
|
|
package versioner
|
|
|
|
|
|
|
|
import (
|
2020-08-24 11:14:30 +00:00
|
|
|
"context"
|
2022-08-16 08:01:49 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-11-24 09:58:57 +00:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2019-06-11 07:16:55 +00:00
|
|
|
"sort"
|
2018-01-01 14:39:23 +00:00
|
|
|
"strings"
|
2019-04-28 22:30:16 +00:00
|
|
|
"time"
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2019-04-28 22:30:16 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2023-08-21 17:44:33 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/stringutil"
|
2014-11-24 09:58:57 +00:00
|
|
|
)
|
|
|
|
|
2020-03-03 21:40:00 +00:00
|
|
|
var (
|
2021-02-12 19:30:51 +00:00
|
|
|
ErrDirectory = errors.New("cannot restore on top of a directory")
|
2020-03-03 21:40:00 +00:00
|
|
|
errNotFound = errors.New("version not found")
|
|
|
|
errFileAlreadyExists = errors.New("file already exists")
|
|
|
|
)
|
2019-04-28 22:30:16 +00:00
|
|
|
|
2023-08-09 07:10:06 +00:00
|
|
|
const (
|
|
|
|
DefaultPath = ".stversions"
|
|
|
|
)
|
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
// TagFilename inserts ~tag just before the extension of the filename.
|
2018-01-01 14:39:23 +00:00
|
|
|
func TagFilename(name, tag string) string {
|
2014-11-24 09:58:57 +00:00
|
|
|
dir, file := filepath.Dir(name), filepath.Base(name)
|
|
|
|
ext := filepath.Ext(file)
|
|
|
|
withoutExt := file[:len(file)-len(ext)]
|
|
|
|
return filepath.Join(dir, withoutExt+"~"+tag+ext)
|
|
|
|
}
|
|
|
|
|
2014-11-25 22:32:18 +00:00
|
|
|
var tagExp = regexp.MustCompile(`.*~([^~.]+)(?:\.[^.]+)?$`)
|
2014-11-24 09:58:57 +00:00
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
// extractTag returns the tag from a filename, whether at the end or middle.
|
|
|
|
func extractTag(path string) string {
|
2014-11-24 09:58:57 +00:00
|
|
|
match := tagExp.FindStringSubmatch(path)
|
2015-04-28 15:34:55 +00:00
|
|
|
// match is []string{"whole match", "submatch"} when successful
|
2014-11-24 09:58:57 +00:00
|
|
|
|
|
|
|
if len(match) != 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return match[1]
|
|
|
|
}
|
2018-01-01 14:39:23 +00:00
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
// UntagFilename returns the filename without tag, and the extracted tag
|
2018-01-01 14:39:23 +00:00
|
|
|
func UntagFilename(path string) (string, string) {
|
|
|
|
ext := filepath.Ext(path)
|
2019-11-26 07:39:31 +00:00
|
|
|
versionTag := extractTag(path)
|
2018-01-01 14:39:23 +00:00
|
|
|
|
|
|
|
// Files tagged with old style tags cannot be untagged.
|
2019-04-28 22:30:16 +00:00
|
|
|
if versionTag == "" {
|
2018-01-01 14:39:23 +00:00
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
// Old style tag
|
|
|
|
if strings.HasSuffix(ext, versionTag) {
|
|
|
|
return strings.TrimSuffix(path, "~"+versionTag), versionTag
|
|
|
|
}
|
|
|
|
|
2018-01-01 14:39:23 +00:00
|
|
|
withoutExt := path[:len(path)-len(ext)-len(versionTag)-1]
|
|
|
|
name := withoutExt + ext
|
|
|
|
return name, versionTag
|
|
|
|
}
|
2019-04-28 22:30:16 +00:00
|
|
|
|
|
|
|
func retrieveVersions(fileSystem fs.Filesystem) (map[string][]FileVersion, error) {
|
|
|
|
files := make(map[string][]FileVersion)
|
|
|
|
|
|
|
|
err := fileSystem.Walk(".", func(path string, f fs.FileInfo, err error) error {
|
|
|
|
// Skip root (which is ok to be a symlink)
|
|
|
|
if path == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip walking if we cannot walk...
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore symlinks
|
|
|
|
if f.IsSymlink() {
|
|
|
|
return fs.SkipDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// No records for directories
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
modTime := f.ModTime().Truncate(time.Second)
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
path = osutil.NormalizedFilename(path)
|
|
|
|
|
|
|
|
name, tag := UntagFilename(path)
|
2019-06-25 05:56:11 +00:00
|
|
|
// Something invalid, assume it's an untagged file (trashcan versioner stuff)
|
2019-04-28 22:30:16 +00:00
|
|
|
if name == "" || tag == "" {
|
|
|
|
files[path] = append(files[path], FileVersion{
|
2019-06-25 05:56:11 +00:00
|
|
|
VersionTime: modTime,
|
|
|
|
ModTime: modTime,
|
2019-04-28 22:30:16 +00:00
|
|
|
Size: f.Size(),
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-11 07:16:55 +00:00
|
|
|
versionTime, err := time.ParseInLocation(TimeFormat, tag, time.Local)
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
// Can't parse it, welp, continue
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
files[name] = append(files[name], FileVersion{
|
|
|
|
VersionTime: versionTime,
|
|
|
|
ModTime: modTime,
|
|
|
|
Size: f.Size(),
|
|
|
|
})
|
2019-04-28 22:30:16 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileTagger func(string, string) string
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
func archiveFile(method fs.CopyRangeMethod, srcFs, dstFs fs.Filesystem, filePath string, tagger fileTagger) error {
|
2019-04-28 22:30:16 +00:00
|
|
|
filePath = osutil.NativeFilename(filePath)
|
|
|
|
info, err := srcFs.Lstat(filePath)
|
|
|
|
if fs.IsNotExist(err) {
|
|
|
|
l.Debugln("not archiving nonexistent file", filePath)
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsSymlink() {
|
|
|
|
panic("bug: attempting to version a symlink")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dstFs.Stat(".")
|
|
|
|
if err != nil {
|
|
|
|
if fs.IsNotExist(err) {
|
|
|
|
l.Debugln("creating versions dir")
|
2023-08-21 17:44:33 +00:00
|
|
|
err := dstFs.MkdirAll(".", 0o755)
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = dstFs.Hide(".")
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file := filepath.Base(filePath)
|
|
|
|
inFolderPath := filepath.Dir(filePath)
|
|
|
|
|
2023-08-21 17:44:33 +00:00
|
|
|
err = dstFs.MkdirAll(inFolderPath, 0o755)
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil && !fs.IsExist(err) {
|
2019-06-25 05:56:11 +00:00
|
|
|
l.Debugln("archiving", filePath, err)
|
2019-04-28 22:30:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
ver := tagger(file, now.Format(TimeFormat))
|
2019-04-28 22:30:16 +00:00
|
|
|
dst := filepath.Join(inFolderPath, ver)
|
2019-06-25 05:56:11 +00:00
|
|
|
l.Debugln("archiving", filePath, "moving to", dst)
|
2020-06-18 06:15:47 +00:00
|
|
|
err = osutil.RenameOrCopy(method, srcFs, dstFs, filePath, dst)
|
2019-04-28 22:30:16 +00:00
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
mtime := info.ModTime()
|
|
|
|
// If it's a trashcan versioner type thing, then it does not have version time in the name
|
|
|
|
// so use mtime for that.
|
|
|
|
if ver == file {
|
|
|
|
mtime = now
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = dstFs.Chtimes(dst, mtime, mtime)
|
2019-04-28 22:30:16 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
func restoreFile(method fs.CopyRangeMethod, src, dst fs.Filesystem, filePath string, versionTime time.Time, tagger fileTagger) error {
|
2019-06-25 05:56:11 +00:00
|
|
|
tag := versionTime.In(time.Local).Truncate(time.Second).Format(TimeFormat)
|
|
|
|
taggedFilePath := tagger(filePath, tag)
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
// If the something already exists where we are restoring to, archive existing file for versioning
|
|
|
|
// remove if it's a symlink, or fail if it's a directory
|
|
|
|
if info, err := dst.Lstat(filePath); err == nil {
|
|
|
|
switch {
|
|
|
|
case info.IsDir():
|
2021-02-12 19:30:51 +00:00
|
|
|
return ErrDirectory
|
2019-04-28 22:30:16 +00:00
|
|
|
case info.IsSymlink():
|
|
|
|
// Remove existing symlinks (as we don't want to archive them)
|
|
|
|
if err := dst.Remove(filePath); err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("removing existing symlink: %w", err)
|
2019-04-28 22:30:16 +00:00
|
|
|
}
|
|
|
|
case info.IsRegular():
|
2020-06-18 06:15:47 +00:00
|
|
|
if err := archiveFile(method, dst, src, filePath, tagger); err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("archiving existing file: %w", err)
|
2019-04-28 22:30:16 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("bug: unknown item type")
|
|
|
|
}
|
|
|
|
} else if !fs.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath = osutil.NativeFilename(filePath)
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
// Try and find a file that has the correct mtime
|
2019-04-28 22:30:16 +00:00
|
|
|
sourceFile := ""
|
2019-06-25 05:56:11 +00:00
|
|
|
sourceMtime := time.Time{}
|
|
|
|
if info, err := src.Lstat(taggedFilePath); err == nil && info.IsRegular() {
|
|
|
|
sourceFile = taggedFilePath
|
|
|
|
sourceMtime = info.ModTime()
|
|
|
|
} else if err == nil {
|
|
|
|
l.Debugln("restore:", taggedFilePath, "not regular")
|
|
|
|
} else {
|
|
|
|
l.Debugln("restore:", taggedFilePath, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for untagged file
|
|
|
|
if sourceFile == "" {
|
|
|
|
info, err := src.Lstat(filePath)
|
|
|
|
if err == nil && info.IsRegular() && info.ModTime().Truncate(time.Second).Equal(versionTime) {
|
|
|
|
sourceFile = filePath
|
|
|
|
sourceMtime = info.ModTime()
|
2019-04-28 22:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if sourceFile == "" {
|
|
|
|
return errNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the target location of where we are supposed to restore does not exist.
|
|
|
|
// This should have been taken care of by the first few lines of this function.
|
|
|
|
if _, err := dst.Lstat(filePath); err == nil {
|
|
|
|
return errFileAlreadyExists
|
|
|
|
} else if !fs.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:44:33 +00:00
|
|
|
_ = dst.MkdirAll(filepath.Dir(filePath), 0o755)
|
2020-06-18 06:15:47 +00:00
|
|
|
err := osutil.RenameOrCopy(method, src, dst, sourceFile, filePath)
|
2019-06-25 05:56:11 +00:00
|
|
|
_ = dst.Chtimes(filePath, sourceMtime, sourceMtime)
|
|
|
|
return err
|
2019-04-28 22:30:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
func versionerFsFromFolderCfg(cfg config.FolderConfiguration) (versionsFs fs.Filesystem) {
|
2022-04-10 18:55:05 +00:00
|
|
|
folderFs := cfg.Filesystem(nil)
|
2021-02-26 11:04:05 +00:00
|
|
|
if cfg.Versioning.FSPath == "" {
|
2023-08-09 07:10:06 +00:00
|
|
|
versionsFs = fs.NewFilesystem(folderFs.Type(), filepath.Join(folderFs.URI(), DefaultPath))
|
refactor: use modern Protobuf encoder (#9817)
At a high level, this is what I've done and why:
- I'm moving the protobuf generation for the `protocol`, `discovery` and
`db` packages to the modern alternatives, and using `buf` to generate
because it's nice and simple.
- After trying various approaches on how to integrate the new types with
the existing code, I opted for splitting off our own data model types
from the on-the-wire generated types. This means we can have a
`FileInfo` type with nicer ergonomics and lots of methods, while the
protobuf generated type stays clean and close to the wire protocol. It
does mean copying between the two when required, which certainly adds a
small amount of inefficiency. If we want to walk this back in the future
and use the raw generated type throughout, that's possible, this however
makes the refactor smaller (!) as it doesn't change everything about the
type for everyone at the same time.
- I have simply removed in cold blood a significant number of old
database migrations. These depended on previous generations of generated
messages of various kinds and were annoying to support in the new
fashion. The oldest supported database version now is the one from
Syncthing 1.9.0 from Sep 7, 2020.
- I changed config structs to be regular manually defined structs.
For the sake of discussion, some things I tried that turned out not to
work...
### Embedding / wrapping
Embedding the protobuf generated structs in our existing types as a data
container and keeping our methods and stuff:
```
package protocol
type FileInfo struct {
*generated.FileInfo
}
```
This generates a lot of problems because the internal shape of the
generated struct is quite different (different names, different types,
more pointers), because initializing it doesn't work like you'd expect
(i.e., you end up with an embedded nil pointer and a panic), and because
the types of child types don't get wrapped. That is, even if we also
have a similar wrapper around a `Vector`, that's not the type you get
when accessing `someFileInfo.Version`, you get the `*generated.Vector`
that doesn't have methods, etc.
### Aliasing
```
package protocol
type FileInfo = generated.FileInfo
```
Doesn't help because you can't attach methods to it, plus all the above.
### Generating the types into the target package like we do now and
attaching methods
This fails because of the different shape of the generated type (as in
the embedding case above) plus the generated struct already has a bunch
of methods that we can't necessarily override properly (like `String()`
and a bunch of getters).
### Methods to functions
I considered just moving all the methods we attach to functions in a
specific package, so that for example
```
package protocol
func (f FileInfo) Equal(other FileInfo) bool
```
would become
```
package fileinfos
func Equal(a, b *generated.FileInfo) bool
```
and this would mostly work, but becomes quite verbose and cumbersome,
and somewhat limits discoverability (you can't see what methods are
available on the type in auto completions, etc). In the end I did this
in some cases, like in the database layer where a lot of things like
`func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv
*generated.FileVersion)` because they were anyway just internal methods.
Fixes #8247
2024-12-01 15:50:17 +00:00
|
|
|
} else if cfg.Versioning.FSType == config.FilesystemTypeBasic {
|
2024-01-12 09:46:18 +00:00
|
|
|
// Expand any leading tildes for basic filesystems,
|
|
|
|
// before checking for absolute paths.
|
|
|
|
path, err := fs.ExpandTilde(cfg.Versioning.FSPath)
|
|
|
|
if err != nil {
|
|
|
|
path = cfg.Versioning.FSPath
|
|
|
|
}
|
|
|
|
// We only know how to deal with relative folders for
|
|
|
|
// basic filesystems, as that's the only one we know
|
2019-04-28 22:30:16 +00:00
|
|
|
// how to check if it's absolute or relative.
|
2024-01-12 09:46:18 +00:00
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
path = filepath.Join(folderFs.URI(), path)
|
|
|
|
}
|
refactor: use modern Protobuf encoder (#9817)
At a high level, this is what I've done and why:
- I'm moving the protobuf generation for the `protocol`, `discovery` and
`db` packages to the modern alternatives, and using `buf` to generate
because it's nice and simple.
- After trying various approaches on how to integrate the new types with
the existing code, I opted for splitting off our own data model types
from the on-the-wire generated types. This means we can have a
`FileInfo` type with nicer ergonomics and lots of methods, while the
protobuf generated type stays clean and close to the wire protocol. It
does mean copying between the two when required, which certainly adds a
small amount of inefficiency. If we want to walk this back in the future
and use the raw generated type throughout, that's possible, this however
makes the refactor smaller (!) as it doesn't change everything about the
type for everyone at the same time.
- I have simply removed in cold blood a significant number of old
database migrations. These depended on previous generations of generated
messages of various kinds and were annoying to support in the new
fashion. The oldest supported database version now is the one from
Syncthing 1.9.0 from Sep 7, 2020.
- I changed config structs to be regular manually defined structs.
For the sake of discussion, some things I tried that turned out not to
work...
### Embedding / wrapping
Embedding the protobuf generated structs in our existing types as a data
container and keeping our methods and stuff:
```
package protocol
type FileInfo struct {
*generated.FileInfo
}
```
This generates a lot of problems because the internal shape of the
generated struct is quite different (different names, different types,
more pointers), because initializing it doesn't work like you'd expect
(i.e., you end up with an embedded nil pointer and a panic), and because
the types of child types don't get wrapped. That is, even if we also
have a similar wrapper around a `Vector`, that's not the type you get
when accessing `someFileInfo.Version`, you get the `*generated.Vector`
that doesn't have methods, etc.
### Aliasing
```
package protocol
type FileInfo = generated.FileInfo
```
Doesn't help because you can't attach methods to it, plus all the above.
### Generating the types into the target package like we do now and
attaching methods
This fails because of the different shape of the generated type (as in
the embedding case above) plus the generated struct already has a bunch
of methods that we can't necessarily override properly (like `String()`
and a bunch of getters).
### Methods to functions
I considered just moving all the methods we attach to functions in a
specific package, so that for example
```
package protocol
func (f FileInfo) Equal(other FileInfo) bool
```
would become
```
package fileinfos
func Equal(a, b *generated.FileInfo) bool
```
and this would mostly work, but becomes quite verbose and cumbersome,
and somewhat limits discoverability (you can't see what methods are
available on the type in auto completions, etc). In the end I did this
in some cases, like in the database layer where a lot of things like
`func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv
*generated.FileVersion)` because they were anyway just internal methods.
Fixes #8247
2024-12-01 15:50:17 +00:00
|
|
|
versionsFs = fs.NewFilesystem(cfg.Versioning.FSType.ToFS(), path)
|
2019-04-28 22:30:16 +00:00
|
|
|
} else {
|
refactor: use modern Protobuf encoder (#9817)
At a high level, this is what I've done and why:
- I'm moving the protobuf generation for the `protocol`, `discovery` and
`db` packages to the modern alternatives, and using `buf` to generate
because it's nice and simple.
- After trying various approaches on how to integrate the new types with
the existing code, I opted for splitting off our own data model types
from the on-the-wire generated types. This means we can have a
`FileInfo` type with nicer ergonomics and lots of methods, while the
protobuf generated type stays clean and close to the wire protocol. It
does mean copying between the two when required, which certainly adds a
small amount of inefficiency. If we want to walk this back in the future
and use the raw generated type throughout, that's possible, this however
makes the refactor smaller (!) as it doesn't change everything about the
type for everyone at the same time.
- I have simply removed in cold blood a significant number of old
database migrations. These depended on previous generations of generated
messages of various kinds and were annoying to support in the new
fashion. The oldest supported database version now is the one from
Syncthing 1.9.0 from Sep 7, 2020.
- I changed config structs to be regular manually defined structs.
For the sake of discussion, some things I tried that turned out not to
work...
### Embedding / wrapping
Embedding the protobuf generated structs in our existing types as a data
container and keeping our methods and stuff:
```
package protocol
type FileInfo struct {
*generated.FileInfo
}
```
This generates a lot of problems because the internal shape of the
generated struct is quite different (different names, different types,
more pointers), because initializing it doesn't work like you'd expect
(i.e., you end up with an embedded nil pointer and a panic), and because
the types of child types don't get wrapped. That is, even if we also
have a similar wrapper around a `Vector`, that's not the type you get
when accessing `someFileInfo.Version`, you get the `*generated.Vector`
that doesn't have methods, etc.
### Aliasing
```
package protocol
type FileInfo = generated.FileInfo
```
Doesn't help because you can't attach methods to it, plus all the above.
### Generating the types into the target package like we do now and
attaching methods
This fails because of the different shape of the generated type (as in
the embedding case above) plus the generated struct already has a bunch
of methods that we can't necessarily override properly (like `String()`
and a bunch of getters).
### Methods to functions
I considered just moving all the methods we attach to functions in a
specific package, so that for example
```
package protocol
func (f FileInfo) Equal(other FileInfo) bool
```
would become
```
package fileinfos
func Equal(a, b *generated.FileInfo) bool
```
and this would mostly work, but becomes quite verbose and cumbersome,
and somewhat limits discoverability (you can't see what methods are
available on the type in auto completions, etc). In the end I did this
in some cases, like in the database layer where a lot of things like
`func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv
*generated.FileVersion)` because they were anyway just internal methods.
Fixes #8247
2024-12-01 15:50:17 +00:00
|
|
|
versionsFs = fs.NewFilesystem(cfg.Versioning.FSType.ToFS(), cfg.Versioning.FSPath)
|
2019-04-28 22:30:16 +00:00
|
|
|
}
|
2019-06-25 05:56:11 +00:00
|
|
|
l.Debugf("%s (%s) folder using %s (%s) versioner dir", folderFs.URI(), folderFs.Type(), versionsFs.URI(), versionsFs.Type())
|
2019-04-28 22:30:16 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-11 07:16:55 +00:00
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
func findAllVersions(fs fs.Filesystem, filePath string) []string {
|
|
|
|
inFolderPath := filepath.Dir(filePath)
|
|
|
|
file := filepath.Base(filePath)
|
2019-06-11 07:16:55 +00:00
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
// Glob according to the new file~timestamp.ext pattern.
|
2019-11-26 07:39:31 +00:00
|
|
|
pattern := filepath.Join(inFolderPath, TagFilename(file, timeGlob))
|
2019-06-25 05:56:11 +00:00
|
|
|
versions, err := fs.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("globbing:", err, "for", pattern)
|
|
|
|
return nil
|
2019-06-11 07:16:55 +00:00
|
|
|
}
|
2023-08-21 17:44:33 +00:00
|
|
|
versions = stringutil.UniqueTrimmedStrings(versions)
|
2019-06-25 05:56:11 +00:00
|
|
|
sort.Strings(versions)
|
2019-06-11 07:16:55 +00:00
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
return versions
|
2019-06-11 07:16:55 +00:00
|
|
|
}
|
2020-08-24 11:14:30 +00:00
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
func clean(ctx context.Context, versionsFs fs.Filesystem, toRemove func([]string, time.Time) []string) error {
|
|
|
|
l.Debugln("Versioner clean: Cleaning", versionsFs)
|
2020-08-24 11:14:30 +00:00
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
if _, err := versionsFs.Stat("."); fs.IsNotExist(err) {
|
|
|
|
// There is no need to clean a nonexistent dir.
|
2020-08-24 11:14:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
versionsPerFile := make(map[string][]string)
|
2020-08-24 11:14:30 +00:00
|
|
|
dirTracker := make(emptyDirTracker)
|
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
walkFn := func(path string, f fs.FileInfo, err error) error {
|
2020-08-24 11:14:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
if f.IsDir() && !f.IsSymlink() {
|
2020-08-24 11:14:30 +00:00
|
|
|
dirTracker.addDir(path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
// Regular file, or possibly a symlink.
|
|
|
|
dirTracker.addFile(path)
|
|
|
|
|
|
|
|
name, _ := UntagFilename(path)
|
|
|
|
if name == "" {
|
|
|
|
return nil
|
2020-08-24 11:14:30 +00:00
|
|
|
}
|
2022-09-13 17:20:04 +00:00
|
|
|
|
|
|
|
versionsPerFile[name] = append(versionsPerFile[name], path)
|
|
|
|
|
|
|
|
return nil
|
2020-08-24 11:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := versionsFs.Walk(".", walkFn); err != nil {
|
2023-09-11 21:10:18 +00:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
l.Warnln("Versioner: scanning versions dir:", err)
|
|
|
|
}
|
2020-08-24 11:14:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
for _, versionList := range versionsPerFile {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
cleanVersions(versionsFs, versionList, toRemove)
|
|
|
|
}
|
|
|
|
|
2020-08-24 11:14:30 +00:00
|
|
|
dirTracker.deleteEmptyDirs(versionsFs)
|
|
|
|
|
2022-09-13 17:20:04 +00:00
|
|
|
l.Debugln("Cleaner: Finished cleaning", versionsFs)
|
2020-08-24 11:14:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-13 17:20:04 +00:00
|
|
|
|
|
|
|
func cleanVersions(versionsFs fs.Filesystem, versions []string, toRemove func([]string, time.Time) []string) {
|
|
|
|
l.Debugln("Versioner: Expiring versions", versions)
|
|
|
|
for _, file := range toRemove(versions, time.Now()) {
|
|
|
|
if err := versionsFs.Remove(file); err != nil {
|
|
|
|
l.Warnf("Versioner: can't remove %q: %v", file, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|