structs.go 8.5 KB

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