set.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // Package db provides a set type to track local/remote files with newness
  7. // checks. We must do a certain amount of normalization in here. We will get
  8. // fed paths with either native or wire-format separators and encodings
  9. // depending on who calls us. We transform paths to wire-format (NFC and
  10. // slashes) on the way to the database, and transform to native format
  11. // (varying separator and encoding) on the way back out.
  12. package olddb
  13. import (
  14. "github.com/syncthing/syncthing/lib/osutil"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. )
  17. type deprecatedFileSet struct {
  18. folder string
  19. db *deprecatedLowlevel
  20. }
  21. // The Iterator is called with either a protocol.FileInfo or a
  22. // FileInfoTruncated (depending on the method) and returns true to
  23. // continue iteration, false to stop.
  24. type Iterator func(f protocol.FileInfo) bool
  25. func NewFileSet(folder string, db *deprecatedLowlevel) (*deprecatedFileSet, error) {
  26. s := &deprecatedFileSet{
  27. folder: folder,
  28. db: db,
  29. }
  30. return s, nil
  31. }
  32. type Snapshot struct {
  33. folder string
  34. t readOnlyTransaction
  35. }
  36. func (s *deprecatedFileSet) Snapshot() (*Snapshot, error) {
  37. t, err := s.db.newReadOnlyTransaction()
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &Snapshot{
  42. folder: s.folder,
  43. t: t,
  44. }, nil
  45. }
  46. func (s *Snapshot) Release() {
  47. s.t.close()
  48. }
  49. func (s *Snapshot) WithHaveSequence(startSeq int64, fn Iterator) error {
  50. return s.t.withHaveSequence([]byte(s.folder), startSeq, nativeFileIterator(fn))
  51. }
  52. func nativeFileIterator(fn Iterator) Iterator {
  53. return func(fi protocol.FileInfo) bool {
  54. fi.Name = osutil.NativeFilename(fi.Name)
  55. return fn(fi)
  56. }
  57. }