folderconfiguration.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (C) 2014 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 http://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "github.com/syncthing/syncthing/lib/osutil"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. type FolderConfiguration struct {
  17. ID string `xml:"id,attr" json:"id"`
  18. Label string `xml:"label,attr" json:"label"`
  19. RawPath string `xml:"path,attr" json:"path"`
  20. Type FolderType `xml:"type,attr" json:"type"`
  21. Devices []FolderDeviceConfiguration `xml:"device" json:"devices"`
  22. RescanIntervalS int `xml:"rescanIntervalS,attr" json:"rescanIntervalS"`
  23. IgnorePerms bool `xml:"ignorePerms,attr" json:"ignorePerms"`
  24. AutoNormalize bool `xml:"autoNormalize,attr" json:"autoNormalize"`
  25. MinDiskFreePct float64 `xml:"minDiskFreePct" json:"minDiskFreePct"`
  26. Versioning VersioningConfiguration `xml:"versioning" json:"versioning"`
  27. Copiers int `xml:"copiers" json:"copiers"` // This defines how many files are handled concurrently.
  28. Pullers int `xml:"pullers" json:"pullers"` // Defines how many blocks are fetched at the same time, possibly between separate copier routines.
  29. Hashers int `xml:"hashers" json:"hashers"` // Less than one sets the value to the number of cores. These are CPU bound due to hashing.
  30. Order PullOrder `xml:"order" json:"order"`
  31. IgnoreDelete bool `xml:"ignoreDelete" json:"ignoreDelete"`
  32. ScanProgressIntervalS int `xml:"scanProgressIntervalS" json:"scanProgressIntervalS"` // Set to a negative value to disable. Value of 0 will get replaced with value of 2 (default value)
  33. PullerSleepS int `xml:"pullerSleepS" json:"pullerSleepS"`
  34. PullerPauseS int `xml:"pullerPauseS" json:"pullerPauseS"`
  35. MaxConflicts int `xml:"maxConflicts" json:"maxConflicts"`
  36. DisableSparseFiles bool `xml:"disableSparseFiles" json:"disableSparseFiles"`
  37. DisableTempIndexes bool `xml:"disableTempIndexes" json:"disableTempIndexes"`
  38. Fsync bool `xml:"fsync" json:"fsync"`
  39. DisableWeakHash bool `xml:"disableWeakHash" json:"disableWeakHash"`
  40. cachedPath string
  41. DeprecatedReadOnly bool `xml:"ro,attr,omitempty" json:"-"`
  42. }
  43. type FolderDeviceConfiguration struct {
  44. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  45. IntroducedBy protocol.DeviceID `xml:"introducedBy,attr" json:"introducedBy"`
  46. }
  47. func NewFolderConfiguration(id, path string) FolderConfiguration {
  48. f := FolderConfiguration{
  49. ID: id,
  50. RawPath: path,
  51. }
  52. f.prepare()
  53. return f
  54. }
  55. func (f FolderConfiguration) Copy() FolderConfiguration {
  56. c := f
  57. c.Devices = make([]FolderDeviceConfiguration, len(f.Devices))
  58. copy(c.Devices, f.Devices)
  59. c.Versioning = f.Versioning.Copy()
  60. return c
  61. }
  62. func (f FolderConfiguration) Path() string {
  63. // This is intentionally not a pointer method, because things like
  64. // cfg.Folders["default"].Path() should be valid.
  65. if f.cachedPath == "" && f.RawPath != "" {
  66. l.Infoln("bug: uncached path call (should only happen in tests)")
  67. return f.cleanedPath()
  68. }
  69. return f.cachedPath
  70. }
  71. func (f *FolderConfiguration) CreateMarker() error {
  72. if !f.HasMarker() {
  73. marker := filepath.Join(f.Path(), ".stfolder")
  74. fd, err := os.Create(marker)
  75. if err != nil {
  76. return err
  77. }
  78. fd.Close()
  79. if err := osutil.SyncDir(filepath.Dir(marker)); err != nil {
  80. l.Infof("fsync %q failed: %v", filepath.Dir(marker), err)
  81. }
  82. osutil.HideFile(marker)
  83. }
  84. return nil
  85. }
  86. func (f *FolderConfiguration) HasMarker() bool {
  87. _, err := os.Stat(filepath.Join(f.Path(), ".stfolder"))
  88. if err != nil {
  89. return false
  90. }
  91. return true
  92. }
  93. func (f FolderConfiguration) Description() string {
  94. return fmt.Sprintf("%q (%s)", f.Label, f.ID)
  95. }
  96. func (f *FolderConfiguration) DeviceIDs() []protocol.DeviceID {
  97. deviceIDs := make([]protocol.DeviceID, len(f.Devices))
  98. for i, n := range f.Devices {
  99. deviceIDs[i] = n.DeviceID
  100. }
  101. return deviceIDs
  102. }
  103. func (f *FolderConfiguration) prepare() {
  104. if f.RawPath != "" {
  105. // The reason it's done like this:
  106. // C: -> C:\ -> C:\ (issue that this is trying to fix)
  107. // C:\somedir -> C:\somedir\ -> C:\somedir
  108. // C:\somedir\ -> C:\somedir\\ -> C:\somedir
  109. // This way in the tests, we get away without OS specific separators
  110. // in the test configs.
  111. f.RawPath = filepath.Dir(f.RawPath + string(filepath.Separator))
  112. // If we're not on Windows, we want the path to end with a slash to
  113. // penetrate symlinks. On Windows, paths must not end with a slash.
  114. if runtime.GOOS != "windows" && f.RawPath[len(f.RawPath)-1] != filepath.Separator {
  115. f.RawPath = f.RawPath + string(filepath.Separator)
  116. }
  117. }
  118. f.cachedPath = f.cleanedPath()
  119. if f.RescanIntervalS > MaxRescanIntervalS {
  120. f.RescanIntervalS = MaxRescanIntervalS
  121. } else if f.RescanIntervalS < 0 {
  122. f.RescanIntervalS = 0
  123. }
  124. if f.Versioning.Params == nil {
  125. f.Versioning.Params = make(map[string]string)
  126. }
  127. }
  128. func (f *FolderConfiguration) cleanedPath() string {
  129. if f.RawPath == "" {
  130. return ""
  131. }
  132. cleaned := f.RawPath
  133. // Attempt tilde expansion; leave unchanged in case of error
  134. if path, err := osutil.ExpandTilde(cleaned); err == nil {
  135. cleaned = path
  136. }
  137. // Attempt absolutification; leave unchanged in case of error
  138. if !filepath.IsAbs(cleaned) {
  139. // Abs() looks like a fairly expensive syscall on Windows, while
  140. // IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
  141. // somewhat faster in the general case, hence the outer if...
  142. if path, err := filepath.Abs(cleaned); err == nil {
  143. cleaned = path
  144. }
  145. }
  146. // Attempt to enable long filename support on Windows. We may still not
  147. // have an absolute path here if the previous steps failed.
  148. if runtime.GOOS == "windows" && filepath.IsAbs(cleaned) && !strings.HasPrefix(f.RawPath, `\\`) {
  149. return `\\?\` + cleaned
  150. }
  151. // If we're not on Windows, we want the path to end with a slash to
  152. // penetrate symlinks. On Windows, paths must not end with a slash.
  153. if runtime.GOOS != "windows" && cleaned[len(cleaned)-1] != filepath.Separator {
  154. cleaned = cleaned + string(filepath.Separator)
  155. }
  156. return cleaned
  157. }
  158. type FolderDeviceConfigurationList []FolderDeviceConfiguration
  159. func (l FolderDeviceConfigurationList) Less(a, b int) bool {
  160. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  161. }
  162. func (l FolderDeviceConfigurationList) Swap(a, b int) {
  163. l[a], l[b] = l[b], l[a]
  164. }
  165. func (l FolderDeviceConfigurationList) Len() int {
  166. return len(l)
  167. }