nativemodel_windows.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "fmt"
  12. "path/filepath"
  13. "strings"
  14. )
  15. func makeNative(m rawModel) rawModel { return nativeModel{m} }
  16. type nativeModel struct {
  17. rawModel
  18. }
  19. func (m nativeModel) Index(idx *Index) error {
  20. idx.Files = fixupFiles(idx.Files)
  21. return m.rawModel.Index(idx)
  22. }
  23. func (m nativeModel) IndexUpdate(idxUp *IndexUpdate) error {
  24. idxUp.Files = fixupFiles(idxUp.Files)
  25. return m.rawModel.IndexUpdate(idxUp)
  26. }
  27. func (m nativeModel) Request(req *Request) (RequestResponse, error) {
  28. if strings.Contains(req.Name, `\`) {
  29. l.Warnf("Dropping request for %s, contains invalid path separator", req.Name)
  30. return nil, ErrNoSuchFile
  31. }
  32. req.Name = filepath.FromSlash(req.Name)
  33. return m.rawModel.Request(req)
  34. }
  35. func fixupFiles(files []FileInfo) []FileInfo {
  36. var out []FileInfo
  37. for i := range files {
  38. if strings.Contains(files[i].Name, `\`) {
  39. msg := fmt.Sprintf("Dropping index entry for %s, contains invalid path separator", files[i].Name)
  40. if files[i].Deleted {
  41. // Dropping a deleted item doesn't have any consequences.
  42. l.Debugln(msg)
  43. } else {
  44. l.Warnln(msg)
  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. }