2015-03-19 10:31:21 +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-03-19 10:31:21 +00:00
|
|
|
|
|
|
|
package versioner
|
|
|
|
|
|
|
|
import (
|
2020-07-14 08:48:50 +00:00
|
|
|
"context"
|
2015-03-19 10:31:21 +00:00
|
|
|
"errors"
|
2021-02-12 19:30:51 +00:00
|
|
|
"fmt"
|
2015-03-19 10:31:21 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2019-04-28 22:30:16 +00:00
|
|
|
"time"
|
2015-04-14 10:31:25 +00:00
|
|
|
|
2022-07-28 17:36:39 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2020-06-18 06:15:47 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
|
|
|
|
|
|
"github.com/kballard/go-shellquote"
|
2015-03-19 10:31:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Register the constructor for this type of versioner with the name "external"
|
2019-11-26 07:39:31 +00:00
|
|
|
factories["external"] = newExternal
|
2015-03-19 10:31:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
type external struct {
|
2015-03-19 10:31:21 +00:00
|
|
|
command string
|
2017-08-19 14:36:56 +00:00
|
|
|
filesystem fs.Filesystem
|
2015-03-19 10:31:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
func newExternal(cfg config.FolderConfiguration) Versioner {
|
|
|
|
command := cfg.Versioning.Params["command"]
|
2015-03-19 10:31:21 +00:00
|
|
|
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsWindows {
|
2021-03-17 22:12:26 +00:00
|
|
|
command = strings.ReplaceAll(command, `\`, `\\`)
|
2018-02-07 14:12:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:31 +00:00
|
|
|
s := external{
|
2015-03-19 10:31:21 +00:00
|
|
|
command: command,
|
2022-04-10 18:55:05 +00:00
|
|
|
filesystem: cfg.Filesystem(nil),
|
2015-03-19 10:31:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("instantiated %#v", s)
|
2015-03-19 10:31:21 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-04-28 20:32:10 +00:00
|
|
|
// 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).
|
2019-11-26 07:39:31 +00:00
|
|
|
func (v external) Archive(filePath string) error {
|
2017-08-19 14:36:56 +00:00
|
|
|
info, err := v.filesystem.Lstat(filePath)
|
|
|
|
if fs.IsNotExist(err) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("not archiving nonexistent file", filePath)
|
2015-03-19 10:31:21 +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-03-19 10:31:21 +00:00
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("archiving", filePath)
|
2015-03-19 10:31:21 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
if v.command == "" {
|
2021-02-12 19:30:51 +00:00
|
|
|
return errors.New("command is empty, please enter a valid command")
|
2017-08-19 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
words, err := shellquote.Split(v.command)
|
2015-03-19 10:31:21 +00:00
|
|
|
if err != nil {
|
2021-02-12 19:30:51 +00:00
|
|
|
return fmt.Errorf("command is invalid: %w", err)
|
2015-03-19 10:31:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
context := map[string]string{
|
|
|
|
"%FOLDER_FILESYSTEM%": v.filesystem.Type().String(),
|
|
|
|
"%FOLDER_PATH%": v.filesystem.URI(),
|
|
|
|
"%FILE_PATH%": filePath,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, word := range words {
|
2019-07-12 07:45:39 +00:00
|
|
|
for key, val := range context {
|
2021-03-17 22:12:26 +00:00
|
|
|
word = strings.ReplaceAll(word, key, val)
|
2017-08-19 14:36:56 +00:00
|
|
|
}
|
2019-07-12 07:45:39 +00:00
|
|
|
|
|
|
|
words[i] = word
|
2015-03-19 10:31:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
cmd := exec.Command(words[0], words[1:]...)
|
2015-03-19 10:31:21 +00:00
|
|
|
env := os.Environ()
|
|
|
|
// filter STGUIAUTH and STGUIAPIKEY from environment variables
|
|
|
|
filteredEnv := []string{}
|
|
|
|
for _, x := range env {
|
|
|
|
if !strings.HasPrefix(x, "STGUIAUTH=") && !strings.HasPrefix(x, "STGUIAPIKEY=") {
|
|
|
|
filteredEnv = append(filteredEnv, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd.Env = filteredEnv
|
2017-08-19 14:36:56 +00:00
|
|
|
combinedOutput, err := cmd.CombinedOutput()
|
|
|
|
l.Debugln("external command output:", string(combinedOutput))
|
2015-03-19 10:31:21 +00:00
|
|
|
if err != nil {
|
2021-02-12 19:30:51 +00:00
|
|
|
if eerr, ok := err.(*exec.ExitError); ok && len(eerr.Stderr) > 0 {
|
|
|
|
return fmt.Errorf("%v: %v", err, string(eerr.Stderr))
|
|
|
|
}
|
2015-03-19 10:31:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// return error if the file was not removed
|
2017-08-19 14:36:56 +00:00
|
|
|
if _, err = v.filesystem.Lstat(filePath); fs.IsNotExist(err) {
|
2015-03-19 10:31:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-12 19:30:51 +00:00
|
|
|
return errors.New("file was not removed by external script")
|
2015-03-19 10:31:21 +00:00
|
|
|
}
|
2019-04-28 22:30:16 +00:00
|
|
|
|
2022-07-28 15:55:29 +00:00
|
|
|
func (external) GetVersions() (map[string][]FileVersion, error) {
|
2019-04-28 22:30:16 +00:00
|
|
|
return nil, ErrRestorationNotSupported
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:55:29 +00:00
|
|
|
func (external) Restore(_ string, _ time.Time) error {
|
2019-04-28 22:30:16 +00:00
|
|
|
return ErrRestorationNotSupported
|
|
|
|
}
|
2020-07-14 08:48:50 +00:00
|
|
|
|
2022-07-28 15:55:29 +00:00
|
|
|
func (external) Clean(_ context.Context) error {
|
2020-07-14 08:48:50 +00:00
|
|
|
return nil
|
|
|
|
}
|