structs.go 7.9 KB

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