nativemodel_windows.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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:build windows
  7. // +build windows
  8. package protocol
  9. // Windows uses backslashes as file separator
  10. import (
  11. "log/slog"
  12. "path/filepath"
  13. "strings"
  14. "github.com/syncthing/syncthing/internal/slogutil"
  15. )
  16. func makeNative(m rawModel) rawModel { return nativeModel{m} }
  17. type nativeModel struct {
  18. rawModel
  19. }
  20. func (m nativeModel) Index(idx *Index) error {
  21. idx.Files = fixupFiles(idx.Files)
  22. return m.rawModel.Index(idx)
  23. }
  24. func (m nativeModel) IndexUpdate(idxUp *IndexUpdate) error {
  25. idxUp.Files = fixupFiles(idxUp.Files)
  26. return m.rawModel.IndexUpdate(idxUp)
  27. }
  28. func (m nativeModel) Request(req *Request) (RequestResponse, error) {
  29. if strings.Contains(req.Name, `\`) {
  30. slog.Debug("Dropping request containing invalid path separator", slogutil.FilePath(req.Name))
  31. return nil, ErrNoSuchFile
  32. }
  33. req.Name = filepath.FromSlash(req.Name)
  34. return m.rawModel.Request(req)
  35. }
  36. func fixupFiles(files []FileInfo) []FileInfo {
  37. var out []FileInfo
  38. for i := range files {
  39. if strings.Contains(files[i].Name, `\`) {
  40. if files[i].Deleted {
  41. // Dropping a deleted item doesn't have any consequences.
  42. slog.Debug("Dropping index entry containing invalid path separator", slogutil.FilePath(files[i].Name))
  43. } else {
  44. slog.Error("Dropping index entry containing invalid path separator", slogutil.FilePath(files[i].Name))
  45. }
  46. if out == nil {
  47. // Most incoming updates won't contain anything invalid, so
  48. // we delay the allocation and copy to output slice until we
  49. // really need to do it, then copy all the so-far valid
  50. // files to it.
  51. out = make([]FileInfo, i, len(files)-1)
  52. copy(out, files)
  53. }
  54. continue
  55. }
  56. // Fixup the path separators
  57. files[i].Name = filepath.FromSlash(files[i].Name)
  58. if out != nil {
  59. out = append(out, files[i])
  60. }
  61. }
  62. if out != nil {
  63. // We did some filtering
  64. return out
  65. }
  66. // Unchanged
  67. return files
  68. }