nativemodel_windows.go 1.6 KB

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