nativemodel_windows.go 1.9 KB

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