nativemodel_windows.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (C) 2014 The Protocol Authors.
  2. //go:build windows
  3. // +build windows
  4. package protocol
  5. // Windows uses backslashes as file separator
  6. import (
  7. "fmt"
  8. "path/filepath"
  9. "strings"
  10. )
  11. func makeNative(m rawModel) rawModel { return nativeModel{m} }
  12. type nativeModel struct {
  13. rawModel
  14. }
  15. func (m nativeModel) Index(folder string, files []FileInfo) error {
  16. files = fixupFiles(files)
  17. return m.rawModel.Index(folder, files)
  18. }
  19. func (m nativeModel) IndexUpdate(folder string, files []FileInfo) error {
  20. files = fixupFiles(files)
  21. return m.rawModel.IndexUpdate(folder, files)
  22. }
  23. func (m nativeModel) Request(folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error) {
  24. if strings.Contains(name, `\`) {
  25. l.Warnf("Dropping request for %s, contains invalid path separator", name)
  26. return nil, ErrNoSuchFile
  27. }
  28. name = filepath.FromSlash(name)
  29. return m.rawModel.Request(folder, name, blockNo, size, offset, hash, weakHash, fromTemporary)
  30. }
  31. func fixupFiles(files []FileInfo) []FileInfo {
  32. var out []FileInfo
  33. for i := range files {
  34. if strings.Contains(files[i].Name, `\`) {
  35. msg := fmt.Sprintf("Dropping index entry for %s, contains invalid path separator", files[i].Name)
  36. if files[i].Deleted {
  37. // Dropping a deleted item doesn't have any consequences.
  38. l.Debugln(msg)
  39. } else {
  40. l.Warnln(msg)
  41. }
  42. if out == nil {
  43. // Most incoming updates won't contain anything invalid, so
  44. // we delay the allocation and copy to output slice until we
  45. // really need to do it, then copy all the so-far valid
  46. // files to it.
  47. out = make([]FileInfo, i, len(files)-1)
  48. copy(out, files)
  49. }
  50. continue
  51. }
  52. // Fixup the path separators
  53. files[i].Name = filepath.FromSlash(files[i].Name)
  54. if out != nil {
  55. out = append(out, files[i])
  56. }
  57. }
  58. if out != nil {
  59. // We did some filtering
  60. return out
  61. }
  62. // Unchanged
  63. return files
  64. }