structs.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. //go:generate go run ../../script/protofmt.go structs.proto
  7. //go:generate protoc -I ../../../../../ -I ../../vendor/ -I ../../vendor/github.com/gogo/protobuf/protobuf -I . --gogofast_out=. structs.proto
  8. package db
  9. import (
  10. "bytes"
  11. "fmt"
  12. "sort"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. func (f FileInfoTruncated) String() string {
  17. switch f.Type {
  18. case protocol.FileInfoTypeDirectory:
  19. return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v}",
  20. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions)
  21. case protocol.FileInfoTypeFile:
  22. return fmt.Sprintf("File{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Length:%d, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, BlockSize:%d}",
  23. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.RawBlockSize)
  24. case protocol.FileInfoTypeSymlink, protocol.FileInfoTypeDeprecatedSymlinkDirectory, protocol.FileInfoTypeDeprecatedSymlinkFile:
  25. return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, SymlinkTarget:%q}",
  26. f.Name, f.Type, f.Sequence, f.Version, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.SymlinkTarget)
  27. default:
  28. panic("mystery file type detected")
  29. }
  30. }
  31. func (f FileInfoTruncated) IsDeleted() bool {
  32. return f.Deleted
  33. }
  34. func (f FileInfoTruncated) IsInvalid() bool {
  35. return f.RawInvalid || f.LocalFlags&protocol.LocalInvalidFlags != 0
  36. }
  37. func (f FileInfoTruncated) IsUnsupported() bool {
  38. return f.LocalFlags&protocol.FlagLocalUnsupported != 0
  39. }
  40. func (f FileInfoTruncated) IsIgnored() bool {
  41. return f.LocalFlags&protocol.FlagLocalIgnored != 0
  42. }
  43. func (f FileInfoTruncated) MustRescan() bool {
  44. return f.LocalFlags&protocol.FlagLocalMustRescan != 0
  45. }
  46. func (f FileInfoTruncated) IsReceiveOnlyChanged() bool {
  47. return f.LocalFlags&protocol.FlagLocalReceiveOnly != 0
  48. }
  49. func (f FileInfoTruncated) IsDirectory() bool {
  50. return f.Type == protocol.FileInfoTypeDirectory
  51. }
  52. func (f FileInfoTruncated) IsSymlink() bool {
  53. switch f.Type {
  54. case protocol.FileInfoTypeSymlink, protocol.FileInfoTypeDeprecatedSymlinkDirectory, protocol.FileInfoTypeDeprecatedSymlinkFile:
  55. return true
  56. default:
  57. return false
  58. }
  59. }
  60. func (f FileInfoTruncated) ShouldConflict() bool {
  61. return f.LocalFlags&protocol.LocalConflictFlags != 0
  62. }
  63. func (f FileInfoTruncated) HasPermissionBits() bool {
  64. return !f.NoPermissions
  65. }
  66. func (f FileInfoTruncated) FileSize() int64 {
  67. if f.Deleted {
  68. return 0
  69. }
  70. if f.IsDirectory() || f.IsSymlink() {
  71. return protocol.SyntheticDirectorySize
  72. }
  73. return f.Size
  74. }
  75. func (f FileInfoTruncated) BlockSize() int {
  76. if f.RawBlockSize == 0 {
  77. return protocol.MinBlockSize
  78. }
  79. return int(f.RawBlockSize)
  80. }
  81. func (f FileInfoTruncated) FileName() string {
  82. return f.Name
  83. }
  84. func (f FileInfoTruncated) FileLocalFlags() uint32 {
  85. return f.LocalFlags
  86. }
  87. func (f FileInfoTruncated) ModTime() time.Time {
  88. return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
  89. }
  90. func (f FileInfoTruncated) SequenceNo() int64 {
  91. return f.Sequence
  92. }
  93. func (f FileInfoTruncated) FileVersion() protocol.Vector {
  94. return f.Version
  95. }
  96. func (f FileInfoTruncated) ConvertToIgnoredFileInfo(by protocol.ShortID) protocol.FileInfo {
  97. return protocol.FileInfo{
  98. Name: f.Name,
  99. Type: f.Type,
  100. ModifiedS: f.ModifiedS,
  101. ModifiedNs: f.ModifiedNs,
  102. ModifiedBy: by,
  103. Version: f.Version,
  104. RawBlockSize: f.RawBlockSize,
  105. LocalFlags: protocol.FlagLocalIgnored,
  106. }
  107. }
  108. func (c Counts) Add(other Counts) Counts {
  109. return Counts{
  110. Files: c.Files + other.Files,
  111. Directories: c.Directories + other.Directories,
  112. Symlinks: c.Symlinks + other.Symlinks,
  113. Deleted: c.Deleted + other.Deleted,
  114. Bytes: c.Bytes + other.Bytes,
  115. Sequence: c.Sequence + other.Sequence,
  116. DeviceID: protocol.EmptyDeviceID[:],
  117. LocalFlags: c.LocalFlags | other.LocalFlags,
  118. }
  119. }
  120. func (vl VersionList) String() string {
  121. var b bytes.Buffer
  122. var id protocol.DeviceID
  123. b.WriteString("{")
  124. for i, v := range vl.Versions {
  125. if i > 0 {
  126. b.WriteString(", ")
  127. }
  128. copy(id[:], v.Device)
  129. fmt.Fprintf(&b, "{%v, %v}", v.Version, id)
  130. }
  131. b.WriteString("}")
  132. return b.String()
  133. }
  134. // update brings the VersionList up to date with file. It returns the updated
  135. // VersionList, a potentially removed old FileVersion and its index, as well as
  136. // the index where the new FileVersion was inserted.
  137. func (vl VersionList) update(folder, device []byte, file protocol.FileInfo, db *instance) (_ VersionList, removedFV FileVersion, removedAt int, insertedAt int) {
  138. removedAt, insertedAt = -1, -1
  139. for i, v := range vl.Versions {
  140. if bytes.Equal(v.Device, device) {
  141. removedAt = i
  142. removedFV = v
  143. vl.Versions = append(vl.Versions[:i], vl.Versions[i+1:]...)
  144. break
  145. }
  146. }
  147. nv := FileVersion{
  148. Device: device,
  149. Version: file.Version,
  150. Invalid: file.IsInvalid(),
  151. }
  152. i := 0
  153. if nv.Invalid {
  154. i = sort.Search(len(vl.Versions), func(j int) bool {
  155. return vl.Versions[j].Invalid
  156. })
  157. }
  158. for ; i < len(vl.Versions); i++ {
  159. switch vl.Versions[i].Version.Compare(file.Version) {
  160. case protocol.Equal:
  161. fallthrough
  162. case protocol.Lesser:
  163. // The version at this point in the list is equal to or lesser
  164. // ("older") than us. We insert ourselves in front of it.
  165. vl = vl.insertAt(i, nv)
  166. return vl, removedFV, removedAt, i
  167. case protocol.ConcurrentLesser, protocol.ConcurrentGreater:
  168. // The version at this point is in conflict with us. We must pull
  169. // the actual file metadata to determine who wins. If we win, we
  170. // insert ourselves in front of the loser here. (The "Lesser" and
  171. // "Greater" in the condition above is just based on the device
  172. // IDs in the version vector, which is not the only thing we use
  173. // to determine the winner.)
  174. //
  175. // A surprise missing file entry here is counted as a win for us.
  176. if of, ok := db.getFile(db.keyer.GenerateDeviceFileKey(nil, folder, vl.Versions[i].Device, []byte(file.Name))); !ok || file.WinsConflict(of) {
  177. vl = vl.insertAt(i, nv)
  178. return vl, removedFV, removedAt, i
  179. }
  180. }
  181. }
  182. // We didn't find a position for an insert above, so append to the end.
  183. vl.Versions = append(vl.Versions, nv)
  184. return vl, removedFV, removedAt, len(vl.Versions) - 1
  185. }
  186. func (vl VersionList) insertAt(i int, v FileVersion) VersionList {
  187. vl.Versions = append(vl.Versions, FileVersion{})
  188. copy(vl.Versions[i+1:], vl.Versions[i:])
  189. vl.Versions[i] = v
  190. return vl
  191. }
  192. func (vl VersionList) Get(device []byte) (FileVersion, bool) {
  193. for _, v := range vl.Versions {
  194. if bytes.Equal(v.Device, device) {
  195. return v, true
  196. }
  197. }
  198. return FileVersion{}, false
  199. }
  200. type fileList []protocol.FileInfo
  201. func (fl fileList) Len() int {
  202. return len(fl)
  203. }
  204. func (fl fileList) Swap(a, b int) {
  205. fl[a], fl[b] = fl[b], fl[a]
  206. }
  207. func (fl fileList) Less(a, b int) bool {
  208. return fl[a].Name < fl[b].Name
  209. }