structs.go 9.0 KB

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