external.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package versioner
  7. import (
  8. "errors"
  9. "os"
  10. "os/exec"
  11. "strings"
  12. "github.com/syncthing/syncthing/lib/fs"
  13. "github.com/kballard/go-shellquote"
  14. )
  15. func init() {
  16. // Register the constructor for this type of versioner with the name "external"
  17. Factories["external"] = NewExternal
  18. }
  19. type External struct {
  20. command string
  21. filesystem fs.Filesystem
  22. }
  23. func NewExternal(folderID string, filesystem fs.Filesystem, params map[string]string) Versioner {
  24. command := params["command"]
  25. s := External{
  26. command: command,
  27. filesystem: filesystem,
  28. }
  29. l.Debugf("instantiated %#v", s)
  30. return s
  31. }
  32. // Archive moves the named file away to a version archive. If this function
  33. // returns nil, the named file does not exist any more (has been archived).
  34. func (v External) Archive(filePath string) error {
  35. info, err := v.filesystem.Lstat(filePath)
  36. if fs.IsNotExist(err) {
  37. l.Debugln("not archiving nonexistent file", filePath)
  38. return nil
  39. } else if err != nil {
  40. return err
  41. }
  42. if info.IsSymlink() {
  43. panic("bug: attempting to version a symlink")
  44. }
  45. l.Debugln("archiving", filePath)
  46. if v.command == "" {
  47. return errors.New("Versioner: command is empty, please enter a valid command")
  48. }
  49. words, err := shellquote.Split(v.command)
  50. if err != nil {
  51. return errors.New("Versioner: command is invalid: " + err.Error())
  52. }
  53. context := map[string]string{
  54. "%FOLDER_FILESYSTEM%": v.filesystem.Type().String(),
  55. "%FOLDER_PATH%": v.filesystem.URI(),
  56. "%FILE_PATH%": filePath,
  57. }
  58. for i, word := range words {
  59. if replacement, ok := context[word]; ok {
  60. words[i] = replacement
  61. }
  62. }
  63. cmd := exec.Command(words[0], words[1:]...)
  64. env := os.Environ()
  65. // filter STGUIAUTH and STGUIAPIKEY from environment variables
  66. filteredEnv := []string{}
  67. for _, x := range env {
  68. if !strings.HasPrefix(x, "STGUIAUTH=") && !strings.HasPrefix(x, "STGUIAPIKEY=") {
  69. filteredEnv = append(filteredEnv, x)
  70. }
  71. }
  72. cmd.Env = filteredEnv
  73. combinedOutput, err := cmd.CombinedOutput()
  74. l.Debugln("external command output:", string(combinedOutput))
  75. if err != nil {
  76. return err
  77. }
  78. // return error if the file was not removed
  79. if _, err = v.filesystem.Lstat(filePath); fs.IsNotExist(err) {
  80. return nil
  81. }
  82. return errors.New("Versioner: file was not removed by external script")
  83. }