structs.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 . --gogofast_out=Mlib/protocol/bep.proto=github.com/syncthing/syncthing/lib/protocol:. 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 (c Counts) TotalItems() int32 {
  121. return c.Files + c.Directories + c.Symlinks + c.Deleted
  122. }
  123. func (vl VersionList) String() string {
  124. var b bytes.Buffer
  125. var id protocol.DeviceID
  126. b.WriteString("{")
  127. for i, v := range vl.Versions {
  128. if i > 0 {
  129. b.WriteString(", ")
  130. }
  131. copy(id[:], v.Device)
  132. fmt.Fprintf(&b, "{%v, %v}", v.Version, id)
  133. }
  134. b.WriteString("}")
  135. return b.String()
  136. }
  137. // update brings the VersionList up to date with file. It returns the updated
  138. // VersionList, a potentially removed old FileVersion and its index, as well as
  139. // the index where the new FileVersion was inserted.
  140. func (vl VersionList) update(folder, device []byte, file protocol.FileInfo, t readOnlyTransaction) (_ VersionList, removedFV FileVersion, removedAt int, insertedAt int) {
  141. vl, removedFV, removedAt = vl.pop(device)
  142. nv := FileVersion{
  143. Device: device,
  144. Version: file.Version,
  145. Invalid: file.IsInvalid(),
  146. }
  147. i := 0
  148. if nv.Invalid {
  149. i = sort.Search(len(vl.Versions), func(j int) bool {
  150. return vl.Versions[j].Invalid
  151. })
  152. }
  153. for ; i < len(vl.Versions); i++ {
  154. switch vl.Versions[i].Version.Compare(file.Version) {
  155. case protocol.Equal:
  156. fallthrough
  157. case protocol.Lesser:
  158. // The version at this point in the list is equal to or lesser
  159. // ("older") than us. We insert ourselves in front of it.
  160. vl = vl.insertAt(i, nv)
  161. return vl, removedFV, removedAt, i
  162. case protocol.ConcurrentLesser, protocol.ConcurrentGreater:
  163. // The version at this point is in conflict with us. We must pull
  164. // the actual file metadata to determine who wins. If we win, we
  165. // insert ourselves in front of the loser here. (The "Lesser" and
  166. // "Greater" in the condition above is just based on the device
  167. // IDs in the version vector, which is not the only thing we use
  168. // to determine the winner.)
  169. //
  170. // A surprise missing file entry here is counted as a win for us.
  171. if of, ok := t.getFile(folder, vl.Versions[i].Device, []byte(file.Name)); !ok || file.WinsConflict(of) {
  172. vl = vl.insertAt(i, nv)
  173. return vl, removedFV, removedAt, i
  174. }
  175. }
  176. }
  177. // We didn't find a position for an insert above, so append to the end.
  178. vl.Versions = append(vl.Versions, nv)
  179. return vl, removedFV, removedAt, len(vl.Versions) - 1
  180. }
  181. func (vl VersionList) insertAt(i int, v FileVersion) VersionList {
  182. vl.Versions = append(vl.Versions, FileVersion{})
  183. copy(vl.Versions[i+1:], vl.Versions[i:])
  184. vl.Versions[i] = v
  185. return vl
  186. }
  187. // pop returns the VersionList without the entry for the given device, as well
  188. // as the removed FileVersion and the position, where that FileVersion was.
  189. // If there is no FileVersion for the given device, the position is -1.
  190. func (vl VersionList) pop(device []byte) (VersionList, FileVersion, int) {
  191. removedAt := -1
  192. for i, v := range vl.Versions {
  193. if bytes.Equal(v.Device, device) {
  194. vl.Versions = append(vl.Versions[:i], vl.Versions[i+1:]...)
  195. return vl, v, i
  196. }
  197. }
  198. return vl, FileVersion{}, removedAt
  199. }
  200. func (vl VersionList) Get(device []byte) (FileVersion, bool) {
  201. for _, v := range vl.Versions {
  202. if bytes.Equal(v.Device, device) {
  203. return v, true
  204. }
  205. }
  206. return FileVersion{}, false
  207. }
  208. type fileList []protocol.FileInfo
  209. func (fl fileList) Len() int {
  210. return len(fl)
  211. }
  212. func (fl fileList) Swap(a, b int) {
  213. fl[a], fl[b] = fl[b], fl[a]
  214. }
  215. func (fl fileList) Less(a, b int) bool {
  216. return fl[a].Name < fl[b].Name
  217. }