Files
syncthing-arm/lib/versioner/external.go
T

117 lines
2.8 KiB
Go
Raw Normal View History

2015-03-19 11:31:21 +01: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,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2015-03-19 11:31:21 +01:00
package versioner
import (
"errors"
"os"
"os/exec"
"runtime"
2015-03-19 11:31:21 +01:00
"strings"
"time"
2015-04-14 19:31:25 +09:00
"github.com/syncthing/syncthing/lib/fs"
"github.com/kballard/go-shellquote"
2015-03-19 11:31:21 +01:00
)
func init() {
// Register the constructor for this type of versioner with the name "external"
Factories["external"] = NewExternal
}
type External struct {
command string
filesystem fs.Filesystem
2015-03-19 11:31:21 +01:00
}
func NewExternal(folderID string, filesystem fs.Filesystem, params map[string]string) Versioner {
2015-03-19 11:31:21 +01:00
command := params["command"]
if runtime.GOOS == "windows" {
command = strings.Replace(command, `\`, `\\`, -1)
}
2015-03-19 11:31:21 +01:00
s := External{
command: command,
filesystem: filesystem,
2015-03-19 11:31:21 +01:00
}
l.Debugf("instantiated %#v", s)
2015-03-19 11:31:21 +01:00
return s
}
2015-04-28 22:32:10 +02: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).
2015-03-19 11:31:21 +01:00
func (v External) Archive(filePath string) error {
info, err := v.filesystem.Lstat(filePath)
if fs.IsNotExist(err) {
l.Debugln("not archiving nonexistent file", filePath)
2015-03-19 11:31:21 +01:00
return nil
} else if err != nil {
return err
}
if info.IsSymlink() {
panic("bug: attempting to version a symlink")
}
2015-03-19 11:31:21 +01:00
l.Debugln("archiving", filePath)
2015-03-19 11:31:21 +01:00
if v.command == "" {
return errors.New("Versioner: command is empty, please enter a valid command")
}
words, err := shellquote.Split(v.command)
2015-03-19 11:31:21 +01:00
if err != nil {
return errors.New("Versioner: command is invalid: " + err.Error())
2015-03-19 11:31:21 +01:00
}
context := map[string]string{
"%FOLDER_FILESYSTEM%": v.filesystem.Type().String(),
"%FOLDER_PATH%": v.filesystem.URI(),
"%FILE_PATH%": filePath,
}
for i, word := range words {
for key, val := range context {
word = strings.Replace(word, key, val, -1)
}
words[i] = word
2015-03-19 11:31:21 +01:00
}
cmd := exec.Command(words[0], words[1:]...)
2015-03-19 11:31:21 +01: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
combinedOutput, err := cmd.CombinedOutput()
l.Debugln("external command output:", string(combinedOutput))
2015-03-19 11:31:21 +01:00
if err != nil {
return err
}
// return error if the file was not removed
if _, err = v.filesystem.Lstat(filePath); fs.IsNotExist(err) {
2015-03-19 11:31:21 +01:00
return nil
}
return errors.New("Versioner: file was not removed by external script")
}
func (v External) GetVersions() (map[string][]FileVersion, error) {
return nil, ErrRestorationNotSupported
}
func (v External) Restore(filePath string, versionTime time.Time) error {
return ErrRestorationNotSupported
}