folderconfiguration.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "fmt"
  9. "runtime"
  10. "github.com/syncthing/syncthing/lib/fs"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. type FolderConfiguration struct {
  14. ID string `xml:"id,attr" json:"id"`
  15. Label string `xml:"label,attr" json:"label"`
  16. FilesystemType fs.FilesystemType `xml:"filesystemType" json:"filesystemType"`
  17. Path string `xml:"path,attr" json:"path"`
  18. Type FolderType `xml:"type,attr" json:"type"`
  19. Devices []FolderDeviceConfiguration `xml:"device" json:"devices"`
  20. RescanIntervalS int `xml:"rescanIntervalS,attr" json:"rescanIntervalS"`
  21. FSWatcherEnabled bool `xml:"fsWatcherEnabled,attr" json:"fsWatcherEnabled"`
  22. FSWatcherDelayS int `xml:"fsWatcherDelayS,attr" json:"fsWatcherDelayS"`
  23. IgnorePerms bool `xml:"ignorePerms,attr" json:"ignorePerms"`
  24. AutoNormalize bool `xml:"autoNormalize,attr" json:"autoNormalize"`
  25. MinDiskFree Size `xml:"minDiskFree" json:"minDiskFree"`
  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. Paused bool `xml:"paused" json:"paused"`
  39. WeakHashThresholdPct int `xml:"weakHashThresholdPct" json:"weakHashThresholdPct"` // Use weak hash if more than X percent of the file has changed. Set to -1 to always use weak hash.
  40. cachedFilesystem fs.Filesystem
  41. DeprecatedReadOnly bool `xml:"ro,attr,omitempty" json:"-"`
  42. DeprecatedMinDiskFreePct float64 `xml:"minDiskFreePct,omitempty" json:"-"`
  43. }
  44. type FolderDeviceConfiguration struct {
  45. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  46. IntroducedBy protocol.DeviceID `xml:"introducedBy,attr" json:"introducedBy"`
  47. }
  48. func NewFolderConfiguration(id string, fsType fs.FilesystemType, path string) FolderConfiguration {
  49. f := FolderConfiguration{
  50. ID: id,
  51. FilesystemType: fsType,
  52. Path: path,
  53. }
  54. f.prepare()
  55. return f
  56. }
  57. func (f FolderConfiguration) Copy() FolderConfiguration {
  58. c := f
  59. c.Devices = make([]FolderDeviceConfiguration, len(f.Devices))
  60. copy(c.Devices, f.Devices)
  61. c.Versioning = f.Versioning.Copy()
  62. return c
  63. }
  64. func (f FolderConfiguration) Filesystem() fs.Filesystem {
  65. // This is intentionally not a pointer method, because things like
  66. // cfg.Folders["default"].Filesystem() should be valid.
  67. if f.cachedFilesystem == nil && f.Path != "" {
  68. l.Infoln("bug: uncached filesystem call (should only happen in tests)")
  69. return fs.NewFilesystem(f.FilesystemType, f.Path)
  70. }
  71. return f.cachedFilesystem
  72. }
  73. func (f *FolderConfiguration) CreateMarker() error {
  74. if !f.HasMarker() {
  75. permBits := fs.FileMode(0777)
  76. if runtime.GOOS == "windows" {
  77. // Windows has no umask so we must chose a safer set of bits to
  78. // begin with.
  79. permBits = 0700
  80. }
  81. fs := f.Filesystem()
  82. err := fs.Mkdir(".stfolder", permBits)
  83. if err != nil {
  84. return err
  85. }
  86. if dir, err := fs.Open("."); err == nil {
  87. if serr := dir.Sync(); err != nil {
  88. l.Infof("fsync %q failed: %v", ".", serr)
  89. }
  90. } else {
  91. l.Infof("fsync %q failed: %v", ".", err)
  92. }
  93. fs.Hide(".stfolder")
  94. }
  95. return nil
  96. }
  97. func (f *FolderConfiguration) HasMarker() bool {
  98. _, err := f.Filesystem().Stat(".stfolder")
  99. return err == nil
  100. }
  101. func (f *FolderConfiguration) CreateRoot() (err error) {
  102. // Directory permission bits. Will be filtered down to something
  103. // sane by umask on Unixes.
  104. permBits := fs.FileMode(0777)
  105. if runtime.GOOS == "windows" {
  106. // Windows has no umask so we must chose a safer set of bits to
  107. // begin with.
  108. permBits = 0700
  109. }
  110. filesystem := f.Filesystem()
  111. if _, err = filesystem.Stat("."); fs.IsNotExist(err) {
  112. if err = filesystem.MkdirAll(".", permBits); err != nil {
  113. l.Warnf("Creating directory for %v: %v", f.Description(), err)
  114. }
  115. }
  116. return err
  117. }
  118. func (f FolderConfiguration) Description() string {
  119. if f.Label == "" {
  120. return f.ID
  121. }
  122. return fmt.Sprintf("%q (%s)", f.Label, f.ID)
  123. }
  124. func (f *FolderConfiguration) DeviceIDs() []protocol.DeviceID {
  125. deviceIDs := make([]protocol.DeviceID, len(f.Devices))
  126. for i, n := range f.Devices {
  127. deviceIDs[i] = n.DeviceID
  128. }
  129. return deviceIDs
  130. }
  131. func (f *FolderConfiguration) prepare() {
  132. if f.Path != "" {
  133. f.cachedFilesystem = fs.NewFilesystem(f.FilesystemType, f.Path)
  134. }
  135. if f.RescanIntervalS > MaxRescanIntervalS {
  136. f.RescanIntervalS = MaxRescanIntervalS
  137. } else if f.RescanIntervalS < 0 {
  138. f.RescanIntervalS = 0
  139. }
  140. if f.FSWatcherDelayS <= 0 {
  141. f.FSWatcherEnabled = false
  142. f.FSWatcherDelayS = 10
  143. }
  144. if f.Versioning.Params == nil {
  145. f.Versioning.Params = make(map[string]string)
  146. }
  147. if f.WeakHashThresholdPct == 0 {
  148. f.WeakHashThresholdPct = 25
  149. }
  150. }
  151. type FolderDeviceConfigurationList []FolderDeviceConfiguration
  152. func (l FolderDeviceConfigurationList) Less(a, b int) bool {
  153. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  154. }
  155. func (l FolderDeviceConfigurationList) Swap(a, b int) {
  156. l[a], l[b] = l[b], l[a]
  157. }
  158. func (l FolderDeviceConfigurationList) Len() int {
  159. return len(l)
  160. }