folderdb_global.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (C) 2025 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. package sqlite
  7. import (
  8. "database/sql"
  9. "errors"
  10. "fmt"
  11. "iter"
  12. "github.com/syncthing/syncthing/internal/db"
  13. "github.com/syncthing/syncthing/internal/itererr"
  14. "github.com/syncthing/syncthing/lib/config"
  15. "github.com/syncthing/syncthing/lib/osutil"
  16. "github.com/syncthing/syncthing/lib/protocol"
  17. )
  18. func (s *folderDB) GetGlobalFile(file string) (protocol.FileInfo, bool, error) {
  19. file = osutil.NormalizedFilename(file)
  20. var ind indirectFI
  21. err := s.stmt(`
  22. SELECT fi.fiprotobuf, bl.blprotobuf FROM fileinfos fi
  23. INNER JOIN files f on fi.sequence = f.sequence
  24. LEFT JOIN blocklists bl ON bl.blocklist_hash = f.blocklist_hash
  25. WHERE f.name = ? AND f.local_flags & {{.FlagLocalGlobal}} != 0
  26. `).Get(&ind, file)
  27. if errors.Is(err, sql.ErrNoRows) {
  28. return protocol.FileInfo{}, false, nil
  29. }
  30. if err != nil {
  31. return protocol.FileInfo{}, false, wrap(err)
  32. }
  33. fi, err := ind.FileInfo()
  34. if err != nil {
  35. return protocol.FileInfo{}, false, wrap(err)
  36. }
  37. return fi, true, nil
  38. }
  39. func (s *folderDB) GetGlobalAvailability(file string) ([]protocol.DeviceID, error) {
  40. file = osutil.NormalizedFilename(file)
  41. var devStrs []string
  42. err := s.stmt(`
  43. SELECT d.device_id FROM files f
  44. INNER JOIN devices d ON d.idx = f.device_idx
  45. INNER JOIN files g ON g.version = f.version AND g.name = f.name
  46. WHERE g.name = ? AND g.local_flags & {{.FlagLocalGlobal}} != 0 AND f.device_idx != {{.LocalDeviceIdx}}
  47. ORDER BY d.device_id
  48. `).Select(&devStrs, file)
  49. if errors.Is(err, sql.ErrNoRows) {
  50. return nil, nil
  51. }
  52. if err != nil {
  53. return nil, wrap(err)
  54. }
  55. devs := make([]protocol.DeviceID, 0, len(devStrs))
  56. for _, s := range devStrs {
  57. d, err := protocol.DeviceIDFromString(s)
  58. if err != nil {
  59. return nil, wrap(err)
  60. }
  61. devs = append(devs, d)
  62. }
  63. return devs, nil
  64. }
  65. func (s *folderDB) AllGlobalFiles() (iter.Seq[db.FileMetadata], func() error) {
  66. it, errFn := iterStructs[db.FileMetadata](s.stmt(`
  67. SELECT f.sequence, f.name, f.type, f.modified as modnanos, f.size, f.deleted, f.invalid, f.local_flags as localflags FROM files f
  68. WHERE f.local_flags & {{.FlagLocalGlobal}} != 0
  69. ORDER BY f.name
  70. `).Queryx())
  71. return itererr.Map(it, errFn, func(m db.FileMetadata) (db.FileMetadata, error) {
  72. m.Name = osutil.NativeFilename(m.Name)
  73. return m, nil
  74. })
  75. }
  76. func (s *folderDB) AllGlobalFilesPrefix(prefix string) (iter.Seq[db.FileMetadata], func() error) {
  77. if prefix == "" {
  78. return s.AllGlobalFiles()
  79. }
  80. prefix = osutil.NormalizedFilename(prefix)
  81. end := prefixEnd(prefix)
  82. it, errFn := iterStructs[db.FileMetadata](s.stmt(`
  83. SELECT f.sequence, f.name, f.type, f.modified as modnanos, f.size, f.deleted, f.invalid, f.local_flags as localflags FROM files f
  84. WHERE f.name >= ? AND f.name < ? AND f.local_flags & {{.FlagLocalGlobal}} != 0
  85. ORDER BY f.name
  86. `).Queryx(prefix, end))
  87. return itererr.Map(it, errFn, func(m db.FileMetadata) (db.FileMetadata, error) {
  88. m.Name = osutil.NativeFilename(m.Name)
  89. return m, nil
  90. })
  91. }
  92. func (s *folderDB) AllNeededGlobalFiles(device protocol.DeviceID, order config.PullOrder, limit, offset int) (iter.Seq[protocol.FileInfo], func() error) {
  93. var selectOpts string
  94. switch order {
  95. case config.PullOrderRandom:
  96. selectOpts = "ORDER BY RANDOM()"
  97. case config.PullOrderAlphabetic:
  98. selectOpts = "ORDER BY g.name ASC"
  99. case config.PullOrderSmallestFirst:
  100. selectOpts = "ORDER BY g.size ASC"
  101. case config.PullOrderLargestFirst:
  102. selectOpts = "ORDER BY g.size DESC"
  103. case config.PullOrderOldestFirst:
  104. selectOpts = "ORDER BY g.modified ASC"
  105. case config.PullOrderNewestFirst:
  106. selectOpts = "ORDER BY g.modified DESC"
  107. }
  108. if limit > 0 {
  109. selectOpts += fmt.Sprintf(" LIMIT %d", limit)
  110. }
  111. if offset > 0 {
  112. selectOpts += fmt.Sprintf(" OFFSET %d", offset)
  113. }
  114. if device == protocol.LocalDeviceID {
  115. return s.neededGlobalFilesLocal(selectOpts)
  116. }
  117. return s.neededGlobalFilesRemote(device, selectOpts)
  118. }
  119. func (s *folderDB) neededGlobalFilesLocal(selectOpts string) (iter.Seq[protocol.FileInfo], func() error) {
  120. // Select all the non-ignored files with the need bit set.
  121. it, errFn := iterStructs[indirectFI](s.stmt(`
  122. SELECT fi.fiprotobuf, bl.blprotobuf, g.name, g.size, g.modified FROM fileinfos fi
  123. INNER JOIN files g on fi.sequence = g.sequence
  124. LEFT JOIN blocklists bl ON bl.blocklist_hash = g.blocklist_hash
  125. WHERE g.local_flags & {{.FlagLocalIgnored}} = 0 AND g.local_flags & {{.FlagLocalNeeded}} != 0
  126. ` + selectOpts).Queryx())
  127. return itererr.Map(it, errFn, indirectFI.FileInfo)
  128. }
  129. func (s *folderDB) neededGlobalFilesRemote(device protocol.DeviceID, selectOpts string) (iter.Seq[protocol.FileInfo], func() error) {
  130. // Select:
  131. //
  132. // - all the valid, non-deleted global files that don't have a
  133. // corresponding remote file with the same version.
  134. //
  135. // - all the valid, deleted global files that have a corresponding
  136. // non-deleted and valid remote file (of any version)
  137. it, errFn := iterStructs[indirectFI](s.stmt(`
  138. SELECT fi.fiprotobuf, bl.blprotobuf, g.name, g.size, g.modified FROM fileinfos fi
  139. INNER JOIN files g on fi.sequence = g.sequence
  140. LEFT JOIN blocklists bl ON bl.blocklist_hash = g.blocklist_hash
  141. WHERE g.local_flags & {{.FlagLocalGlobal}} != 0 AND NOT g.deleted AND NOT g.invalid AND NOT EXISTS (
  142. SELECT 1 FROM FILES f
  143. INNER JOIN devices d ON d.idx = f.device_idx
  144. WHERE f.name = g.name AND f.version = g.version AND d.device_id = ?
  145. )
  146. UNION ALL
  147. SELECT fi.fiprotobuf, bl.blprotobuf, g.name, g.size, g.modified FROM fileinfos fi
  148. INNER JOIN files g on fi.sequence = g.sequence
  149. LEFT JOIN blocklists bl ON bl.blocklist_hash = g.blocklist_hash
  150. WHERE g.local_flags & {{.FlagLocalGlobal}} != 0 AND g.deleted AND NOT g.invalid AND EXISTS (
  151. SELECT 1 FROM FILES f
  152. INNER JOIN devices d ON d.idx = f.device_idx
  153. WHERE f.name = g.name AND d.device_id = ? AND NOT f.deleted AND NOT f.invalid
  154. )
  155. `+selectOpts).Queryx(
  156. device.String(),
  157. device.String(),
  158. ))
  159. return itererr.Map(it, errFn, indirectFI.FileInfo)
  160. }