2015-06-12 11:04:00 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// 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/.
|
2015-06-12 11:04:00 +00:00
|
|
|
|
|
|
|
package versioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2015-06-12 11:04:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Register the constructor for this type of versioner
|
|
|
|
Factories["trashcan"] = NewTrashcan
|
|
|
|
}
|
|
|
|
|
|
|
|
type Trashcan struct {
|
2017-08-19 14:36:56 +00:00
|
|
|
fs fs.Filesystem
|
2015-06-12 11:04:00 +00:00
|
|
|
cleanoutDays int
|
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
func NewTrashcan(folderID string, fs fs.Filesystem, params map[string]string) Versioner {
|
2015-06-12 11:04:00 +00:00
|
|
|
cleanoutDays, _ := strconv.Atoi(params["cleanoutDays"])
|
|
|
|
// On error we default to 0, "do not clean out the trash can"
|
|
|
|
|
|
|
|
s := &Trashcan{
|
2017-08-19 14:36:56 +00:00
|
|
|
fs: fs,
|
2015-06-12 11:04:00 +00:00
|
|
|
cleanoutDays: cleanoutDays,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("instantiated %#v", s)
|
2015-06-12 11:04:00 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
func (t *Trashcan) Archive(filePath string) error {
|
2017-08-19 14:36:56 +00:00
|
|
|
info, err := t.fs.Lstat(filePath)
|
|
|
|
if fs.IsNotExist(err) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("not archiving nonexistent file", filePath)
|
2015-06-12 11:04:00 +00:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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")
|
|
|
|
}
|
2015-06-12 11:04:00 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
versionsDir := ".stversions"
|
|
|
|
if _, err := t.fs.Stat(versionsDir); err != nil {
|
|
|
|
if !fs.IsNotExist(err) {
|
2015-06-12 11:04:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("creating versions dir", versionsDir)
|
2017-08-19 14:36:56 +00:00
|
|
|
if err := t.fs.MkdirAll(versionsDir, 0777); err != nil {
|
2015-06-12 11:04:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-08-19 14:36:56 +00:00
|
|
|
t.fs.Hide(versionsDir)
|
2015-06-12 11:04:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("archiving", filePath)
|
2015-06-12 11:04:00 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
archivedPath := filepath.Join(versionsDir, filePath)
|
|
|
|
if err := t.fs.MkdirAll(filepath.Dir(archivedPath), 0777); err != nil && !fs.IsExist(err) {
|
2015-06-12 11:04:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("moving to", archivedPath)
|
2015-06-12 11:04:00 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
if err := osutil.Rename(t.fs, filePath, archivedPath); err != nil {
|
2015-06-12 11:04:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the mtime to the time the file was deleted. This is used by the
|
|
|
|
// cleanout routine. If this fails things won't work optimally but there's
|
|
|
|
// not much we can do about it so we ignore the error.
|
2017-08-19 14:36:56 +00:00
|
|
|
t.fs.Chtimes(archivedPath, time.Now(), time.Now())
|
2015-06-12 11:04:00 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trashcan) Serve() {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(t, "starting")
|
|
|
|
defer l.Debugln(t, "stopping")
|
2015-06-12 11:04:00 +00:00
|
|
|
|
|
|
|
// Do the first cleanup one minute after startup.
|
|
|
|
timer := time.NewTimer(time.Minute)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.stop:
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-timer.C:
|
|
|
|
if t.cleanoutDays > 0 {
|
|
|
|
if err := t.cleanoutArchive(); err != nil {
|
|
|
|
l.Infoln("Cleaning trashcan:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanups once a day should be enough.
|
|
|
|
timer.Reset(24 * time.Hour)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trashcan) Stop() {
|
|
|
|
close(t.stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trashcan) String() string {
|
|
|
|
return fmt.Sprintf("trashcan@%p", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trashcan) cleanoutArchive() error {
|
2017-08-19 14:36:56 +00:00
|
|
|
versionsDir := ".stversions"
|
|
|
|
if _, err := t.fs.Lstat(versionsDir); fs.IsNotExist(err) {
|
2015-06-12 11:04:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cutoff := time.Now().Add(time.Duration(-24*t.cleanoutDays) * time.Hour)
|
2017-11-18 15:56:53 +00:00
|
|
|
dirTracker := make(emptyDirTracker)
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
walkFn := func(path string, info fs.FileInfo, err error) error {
|
2015-06-12 11:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-18 15:56:53 +00:00
|
|
|
if info.IsDir() && !info.IsSymlink() {
|
|
|
|
dirTracker.addDir(path)
|
2015-06-12 11:04:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.ModTime().Before(cutoff) {
|
|
|
|
// The file is too old; remove it.
|
2017-08-19 14:36:56 +00:00
|
|
|
t.fs.Remove(path)
|
2015-06-12 11:04:00 +00:00
|
|
|
} else {
|
|
|
|
// Keep this file, and remember it so we don't unnecessarily try
|
|
|
|
// to remove this directory.
|
2017-11-18 15:56:53 +00:00
|
|
|
dirTracker.addFile(path)
|
2015-06-12 11:04:00 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
if err := t.fs.Walk(versionsDir, walkFn); err != nil {
|
2015-06-12 11:04:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-18 15:56:53 +00:00
|
|
|
dirTracker.deleteEmptyDirs(t.fs)
|
|
|
|
|
2015-06-12 11:04:00 +00:00
|
|
|
return nil
|
|
|
|
}
|