structs.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 ../../proto/scripts/protofmt.go structs.proto
  7. //go:generate protoc -I ../../ -I ../../proto -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. "time"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. )
  15. func (f FileInfoTruncated) String() string {
  16. switch f.Type {
  17. case protocol.FileInfoTypeDirectory:
  18. return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v}",
  19. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions)
  20. case protocol.FileInfoTypeFile:
  21. 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}",
  22. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.RawBlockSize)
  23. case protocol.FileInfoTypeSymlink, protocol.FileInfoTypeDeprecatedSymlinkDirectory, protocol.FileInfoTypeDeprecatedSymlinkFile:
  24. return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, SymlinkTarget:%q}",
  25. f.Name, f.Type, f.Sequence, f.Version, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.SymlinkTarget)
  26. default:
  27. panic("mystery file type detected")
  28. }
  29. }
  30. func (f FileInfoTruncated) IsDeleted() bool {
  31. return f.Deleted
  32. }
  33. func (f FileInfoTruncated) IsInvalid() bool {
  34. return f.RawInvalid || f.LocalFlags&protocol.LocalInvalidFlags != 0
  35. }
  36. func (f FileInfoTruncated) IsUnsupported() bool {
  37. return f.LocalFlags&protocol.FlagLocalUnsupported != 0
  38. }
  39. func (f FileInfoTruncated) IsIgnored() bool {
  40. return f.LocalFlags&protocol.FlagLocalIgnored != 0
  41. }
  42. func (f FileInfoTruncated) MustRescan() bool {
  43. return f.LocalFlags&protocol.FlagLocalMustRescan != 0
  44. }
  45. func (f FileInfoTruncated) IsReceiveOnlyChanged() bool {
  46. return f.LocalFlags&protocol.FlagLocalReceiveOnly != 0
  47. }
  48. func (f FileInfoTruncated) IsDirectory() bool {
  49. return f.Type == protocol.FileInfoTypeDirectory
  50. }
  51. func (f FileInfoTruncated) IsSymlink() bool {
  52. switch f.Type {
  53. case protocol.FileInfoTypeSymlink, protocol.FileInfoTypeDeprecatedSymlinkDirectory, protocol.FileInfoTypeDeprecatedSymlinkFile:
  54. return true
  55. default:
  56. return false
  57. }
  58. }
  59. func (f FileInfoTruncated) ShouldConflict() bool {
  60. return f.LocalFlags&protocol.LocalConflictFlags != 0
  61. }
  62. func (f FileInfoTruncated) HasPermissionBits() bool {
  63. return !f.NoPermissions
  64. }
  65. func (f FileInfoTruncated) FileSize() int64 {
  66. if f.Deleted {
  67. return 0
  68. }
  69. if f.IsDirectory() || f.IsSymlink() {
  70. return protocol.SyntheticDirectorySize
  71. }
  72. return f.Size
  73. }
  74. func (f FileInfoTruncated) BlockSize() int {
  75. if f.RawBlockSize == 0 {
  76. return protocol.MinBlockSize
  77. }
  78. return int(f.RawBlockSize)
  79. }
  80. func (f FileInfoTruncated) FileName() string {
  81. return f.Name
  82. }
  83. func (f FileInfoTruncated) FileLocalFlags() uint32 {
  84. return f.LocalFlags
  85. }
  86. func (f FileInfoTruncated) ModTime() time.Time {
  87. return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
  88. }
  89. func (f FileInfoTruncated) SequenceNo() int64 {
  90. return f.Sequence
  91. }
  92. func (f FileInfoTruncated) FileVersion() protocol.Vector {
  93. return f.Version
  94. }
  95. func (f FileInfoTruncated) FileType() protocol.FileInfoType {
  96. return f.Type
  97. }
  98. func (f FileInfoTruncated) FilePermissions() uint32 {
  99. return f.Permissions
  100. }
  101. func (f FileInfoTruncated) FileModifiedBy() protocol.ShortID {
  102. return f.ModifiedBy
  103. }
  104. func (f FileInfoTruncated) ConvertToIgnoredFileInfo(by protocol.ShortID) protocol.FileInfo {
  105. file := f.copyToFileInfo()
  106. file.SetIgnored(by)
  107. return file
  108. }
  109. func (f FileInfoTruncated) ConvertToDeletedFileInfo(by protocol.ShortID) protocol.FileInfo {
  110. file := f.copyToFileInfo()
  111. file.SetDeleted(by)
  112. return file
  113. }
  114. // ConvertDeletedToFileInfo converts a deleted truncated file info to a regular file info
  115. func (f FileInfoTruncated) ConvertDeletedToFileInfo() protocol.FileInfo {
  116. if !f.Deleted {
  117. panic("ConvertDeletedToFileInfo must only be called on deleted items")
  118. }
  119. return f.copyToFileInfo()
  120. }
  121. // copyToFileInfo just copies all members of FileInfoTruncated to protocol.FileInfo
  122. func (f FileInfoTruncated) copyToFileInfo() protocol.FileInfo {
  123. return protocol.FileInfo{
  124. Name: f.Name,
  125. Size: f.Size,
  126. ModifiedS: f.ModifiedS,
  127. ModifiedBy: f.ModifiedBy,
  128. Version: f.Version,
  129. Sequence: f.Sequence,
  130. SymlinkTarget: f.SymlinkTarget,
  131. BlocksHash: f.BlocksHash,
  132. Type: f.Type,
  133. Permissions: f.Permissions,
  134. ModifiedNs: f.ModifiedNs,
  135. RawBlockSize: f.RawBlockSize,
  136. LocalFlags: f.LocalFlags,
  137. Deleted: f.Deleted,
  138. RawInvalid: f.RawInvalid,
  139. NoPermissions: f.NoPermissions,
  140. }
  141. }
  142. func (c Counts) Add(other Counts) Counts {
  143. return Counts{
  144. Files: c.Files + other.Files,
  145. Directories: c.Directories + other.Directories,
  146. Symlinks: c.Symlinks + other.Symlinks,
  147. Deleted: c.Deleted + other.Deleted,
  148. Bytes: c.Bytes + other.Bytes,
  149. Sequence: c.Sequence + other.Sequence,
  150. DeviceID: protocol.EmptyDeviceID[:],
  151. LocalFlags: c.LocalFlags | other.LocalFlags,
  152. }
  153. }
  154. func (c Counts) TotalItems() int32 {
  155. return c.Files + c.Directories + c.Symlinks + c.Deleted
  156. }
  157. // Equal compares the numbers only, not sequence/dev/flags.
  158. func (c Counts) Equal(o Counts) bool {
  159. return c.Files == o.Files && c.Directories == o.Directories && c.Symlinks == o.Symlinks && c.Deleted == o.Deleted && c.Bytes == o.Bytes
  160. }
  161. func (vl VersionList) String() string {
  162. var b bytes.Buffer
  163. var id protocol.DeviceID
  164. b.WriteString("{")
  165. for i, v := range vl.RawVersions {
  166. if i > 0 {
  167. b.WriteString(", ")
  168. }
  169. fmt.Fprintf(&b, "{%v, {", v.Version)
  170. for j, dev := range v.Devices {
  171. if j > 0 {
  172. b.WriteString(", ")
  173. }
  174. copy(id[:], dev)
  175. fmt.Fprint(&b, id.Short())
  176. }
  177. b.WriteString("}, {")
  178. for j, dev := range v.InvalidDevices {
  179. if j > 0 {
  180. b.WriteString(", ")
  181. }
  182. copy(id[:], dev)
  183. fmt.Fprint(&b, id.Short())
  184. }
  185. fmt.Fprint(&b, "}}")
  186. }
  187. b.WriteString("}")
  188. return b.String()
  189. }
  190. // update brings the VersionList up to date with file. It returns the updated
  191. // VersionList, a device that has the global/newest version, a device that previously
  192. // had the global/newest version, a boolean indicating if the global version has
  193. // changed and if any error occurred (only possible in db interaction).
  194. func (vl *VersionList) update(folder, device []byte, file protocol.FileIntf, t readOnlyTransaction) (FileVersion, FileVersion, FileVersion, bool, bool, bool, error) {
  195. if len(vl.RawVersions) == 0 {
  196. nv := newFileVersion(device, file.FileVersion(), file.IsInvalid(), file.IsDeleted())
  197. vl.RawVersions = append(vl.RawVersions, nv)
  198. return nv, FileVersion{}, FileVersion{}, false, false, true, nil
  199. }
  200. // Get the current global (before updating)
  201. oldFV, haveOldGlobal := vl.GetGlobal()
  202. // Remove ourselves first
  203. removedFV, haveRemoved, _, err := vl.pop(folder, device, []byte(file.FileName()), t)
  204. if err == nil {
  205. // Find position and insert the file
  206. err = vl.insert(folder, device, file, t)
  207. }
  208. if err != nil {
  209. return FileVersion{}, FileVersion{}, FileVersion{}, false, false, false, err
  210. }
  211. newFV, _ := vl.GetGlobal() // We just inserted something above, can't be empty
  212. if !haveOldGlobal {
  213. return newFV, FileVersion{}, removedFV, false, haveRemoved, true, nil
  214. }
  215. globalChanged := true
  216. if oldFV.IsInvalid() == newFV.IsInvalid() && oldFV.Version.Equal(newFV.Version) {
  217. globalChanged = false
  218. }
  219. return newFV, oldFV, removedFV, true, haveRemoved, globalChanged, nil
  220. }
  221. func (vl *VersionList) insert(folder, device []byte, file protocol.FileIntf, t readOnlyTransaction) error {
  222. var added bool
  223. var err error
  224. i := 0
  225. for ; i < len(vl.RawVersions); i++ {
  226. // Insert our new version
  227. added, err = vl.checkInsertAt(i, folder, device, file, t)
  228. if err != nil {
  229. return err
  230. }
  231. if added {
  232. break
  233. }
  234. }
  235. if i == len(vl.RawVersions) {
  236. // Append to the end
  237. vl.RawVersions = append(vl.RawVersions, newFileVersion(device, file.FileVersion(), file.IsInvalid(), file.IsDeleted()))
  238. }
  239. return nil
  240. }
  241. func (vl *VersionList) insertAt(i int, v FileVersion) {
  242. vl.RawVersions = append(vl.RawVersions, FileVersion{})
  243. copy(vl.RawVersions[i+1:], vl.RawVersions[i:])
  244. vl.RawVersions[i] = v
  245. }
  246. // pop returns the VersionList without the entry for the given device, as well
  247. // as the removed FileVersion, whether it was found/removed at all and whether
  248. // the global changed in the process.
  249. func (vl *VersionList) pop(folder, device, name []byte, t readOnlyTransaction) (FileVersion, bool, bool, error) {
  250. invDevice, i, j, ok := vl.findDevice(device)
  251. if !ok {
  252. return FileVersion{}, false, false, nil
  253. }
  254. globalPos := vl.findGlobal()
  255. if vl.RawVersions[i].deviceCount() == 1 {
  256. fv := vl.RawVersions[i]
  257. vl.popVersionAt(i)
  258. return fv, true, globalPos == i, nil
  259. }
  260. if invDevice {
  261. vl.RawVersions[i].InvalidDevices = popDeviceAt(vl.RawVersions[i].InvalidDevices, j)
  262. } else {
  263. vl.RawVersions[i].Devices = popDeviceAt(vl.RawVersions[i].Devices, j)
  264. }
  265. // If the last valid device of the previous global was removed above,
  266. // the next entry is now the global entry (unless all entries are invalid).
  267. if len(vl.RawVersions[i].Devices) == 0 && globalPos == i {
  268. return vl.RawVersions[i], true, globalPos == vl.findGlobal(), nil
  269. }
  270. return vl.RawVersions[i], true, false, nil
  271. }
  272. // Get returns a FileVersion that contains the given device and whether it has
  273. // been found at all.
  274. func (vl *VersionList) Get(device []byte) (FileVersion, bool) {
  275. _, i, _, ok := vl.findDevice(device)
  276. if !ok {
  277. return FileVersion{}, false
  278. }
  279. return vl.RawVersions[i], true
  280. }
  281. // GetGlobal returns the current global FileVersion. The returned FileVersion
  282. // may be invalid, if all FileVersions are invalid. Returns false only if
  283. // VersionList is empty.
  284. func (vl *VersionList) GetGlobal() (FileVersion, bool) {
  285. i := vl.findGlobal()
  286. if i == -1 {
  287. return FileVersion{}, false
  288. }
  289. return vl.RawVersions[i], true
  290. }
  291. func (vl *VersionList) Empty() bool {
  292. return len(vl.RawVersions) == 0
  293. }
  294. // findGlobal returns the first version that isn't invalid, or if all versions are
  295. // invalid just the first version (i.e. 0) or -1, if there's no versions at all.
  296. func (vl *VersionList) findGlobal() int {
  297. for i, fv := range vl.RawVersions {
  298. if !fv.IsInvalid() {
  299. return i
  300. }
  301. }
  302. if len(vl.RawVersions) == 0 {
  303. return -1
  304. }
  305. return 0
  306. }
  307. // findDevices returns whether the device is in InvalidVersions or Versions and
  308. // in InvalidDevices or Devices (true for invalid), the positions in the version
  309. // and device slices and whether it has been found at all.
  310. func (vl *VersionList) findDevice(device []byte) (bool, int, int, bool) {
  311. for i, v := range vl.RawVersions {
  312. if j := deviceIndex(v.Devices, device); j != -1 {
  313. return false, i, j, true
  314. }
  315. if j := deviceIndex(v.InvalidDevices, device); j != -1 {
  316. return true, i, j, true
  317. }
  318. }
  319. return false, -1, -1, false
  320. }
  321. func (vl *VersionList) popVersion(version protocol.Vector) (FileVersion, bool) {
  322. i := vl.versionIndex(version)
  323. if i == -1 {
  324. return FileVersion{}, false
  325. }
  326. fv := vl.RawVersions[i]
  327. vl.popVersionAt(i)
  328. return fv, true
  329. }
  330. func (vl *VersionList) versionIndex(version protocol.Vector) int {
  331. for i, v := range vl.RawVersions {
  332. if version.Equal(v.Version) {
  333. return i
  334. }
  335. }
  336. return -1
  337. }
  338. func (vl *VersionList) popVersionAt(i int) {
  339. vl.RawVersions = append(vl.RawVersions[:i], vl.RawVersions[i+1:]...)
  340. }
  341. // checkInsertAt determines if the given device and associated file should be
  342. // inserted into the FileVersion at position i or into a new FileVersion at
  343. // position i.
  344. func (vl *VersionList) checkInsertAt(i int, folder, device []byte, file protocol.FileIntf, t readOnlyTransaction) (bool, error) {
  345. ordering := vl.RawVersions[i].Version.Compare(file.FileVersion())
  346. if ordering == protocol.Equal {
  347. if !file.IsInvalid() {
  348. vl.RawVersions[i].Devices = append(vl.RawVersions[i].Devices, device)
  349. } else {
  350. vl.RawVersions[i].InvalidDevices = append(vl.RawVersions[i].InvalidDevices, device)
  351. }
  352. return true, nil
  353. }
  354. existingDevice, _ := vl.RawVersions[i].FirstDevice()
  355. insert, err := shouldInsertBefore(ordering, folder, existingDevice, vl.RawVersions[i].IsInvalid(), file, t)
  356. if err != nil {
  357. return false, err
  358. }
  359. if insert {
  360. vl.insertAt(i, newFileVersion(device, file.FileVersion(), file.IsInvalid(), file.IsDeleted()))
  361. return true, nil
  362. }
  363. return false, nil
  364. }
  365. // shouldInsertBefore determines whether the file comes before an existing
  366. // entry, given the version ordering (existing compared to new one), existing
  367. // device and if the existing version is invalid.
  368. func shouldInsertBefore(ordering protocol.Ordering, folder, existingDevice []byte, existingInvalid bool, file protocol.FileIntf, t readOnlyTransaction) (bool, error) {
  369. switch ordering {
  370. case protocol.Lesser:
  371. // The version at this point in the list is lesser
  372. // ("older") than us. We insert ourselves in front of it.
  373. return true, nil
  374. case protocol.ConcurrentLesser, protocol.ConcurrentGreater:
  375. // The version in conflict with us.
  376. // Check if we can shortcut due to one being invalid.
  377. if existingInvalid != file.IsInvalid() {
  378. return existingInvalid, nil
  379. }
  380. // We must pull the actual file metadata to determine who wins.
  381. // If we win, we insert ourselves in front of the loser here.
  382. // (The "Lesser" and "Greater" in the condition above is just
  383. // based on the device IDs in the version vector, which is not
  384. // the only thing we use to determine the winner.)
  385. of, ok, err := t.getFile(folder, existingDevice, []byte(file.FileName()))
  386. if err != nil {
  387. return false, err
  388. }
  389. // A surprise missing file entry here is counted as a win for us.
  390. if !ok {
  391. return true, nil
  392. }
  393. if err != nil {
  394. return false, err
  395. }
  396. if protocol.WinsConflict(file, of) {
  397. return true, nil
  398. }
  399. }
  400. return false, nil
  401. }
  402. func deviceIndex(devices [][]byte, device []byte) int {
  403. for i, dev := range devices {
  404. if bytes.Equal(device, dev) {
  405. return i
  406. }
  407. }
  408. return -1
  409. }
  410. func popDeviceAt(devices [][]byte, i int) [][]byte {
  411. return append(devices[:i], devices[i+1:]...)
  412. }
  413. func popDevice(devices [][]byte, device []byte) ([][]byte, bool) {
  414. i := deviceIndex(devices, device)
  415. if i == -1 {
  416. return devices, false
  417. }
  418. return popDeviceAt(devices, i), true
  419. }
  420. func newFileVersion(device []byte, version protocol.Vector, invalid, deleted bool) FileVersion {
  421. fv := FileVersion{
  422. Version: version,
  423. Deleted: deleted,
  424. }
  425. if invalid {
  426. fv.InvalidDevices = [][]byte{device}
  427. } else {
  428. fv.Devices = [][]byte{device}
  429. }
  430. return fv
  431. }
  432. func (fv FileVersion) FirstDevice() ([]byte, bool) {
  433. if len(fv.Devices) != 0 {
  434. return fv.Devices[0], true
  435. }
  436. if len(fv.InvalidDevices) != 0 {
  437. return fv.InvalidDevices[0], true
  438. }
  439. return nil, false
  440. }
  441. func (fv FileVersion) IsInvalid() bool {
  442. return len(fv.Devices) == 0
  443. }
  444. func (fv FileVersion) deviceCount() int {
  445. return len(fv.Devices) + len(fv.InvalidDevices)
  446. }
  447. type fileList []protocol.FileInfo
  448. func (fl fileList) Len() int {
  449. return len(fl)
  450. }
  451. func (fl fileList) Swap(a, b int) {
  452. fl[a], fl[b] = fl[b], fl[a]
  453. }
  454. func (fl fileList) Less(a, b int) bool {
  455. return fl[a].Name < fl[b].Name
  456. }