folderdb_global.go 6.5 KB

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