nativemodel_windows.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2014 The Protocol Authors.
  2. // +build windows
  3. package protocol
  4. // Windows uses backslashes as file separator and disallows a bunch of
  5. // characters in the filename
  6. import (
  7. "path/filepath"
  8. "strings"
  9. )
  10. var disallowedCharacters = string([]rune{
  11. '<', '>', ':', '"', '|', '?', '*',
  12. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  13. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  14. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  15. 31,
  16. })
  17. type nativeModel struct {
  18. next Model
  19. }
  20. func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
  21. for i, f := range files {
  22. if strings.ContainsAny(f.Name, disallowedCharacters) {
  23. if f.IsDeleted() {
  24. // Don't complain if the file is marked as deleted, since it
  25. // can't possibly exist here anyway.
  26. continue
  27. }
  28. files[i].Flags |= FlagInvalid
  29. l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name)
  30. }
  31. files[i].Name = filepath.FromSlash(f.Name)
  32. }
  33. m.next.Index(deviceID, folder, files, flags, options)
  34. }
  35. func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
  36. for i, f := range files {
  37. if strings.ContainsAny(f.Name, disallowedCharacters) {
  38. if f.IsDeleted() {
  39. // Don't complain if the file is marked as deleted, since it
  40. // can't possibly exist here anyway.
  41. continue
  42. }
  43. files[i].Flags |= FlagInvalid
  44. l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name)
  45. }
  46. files[i].Name = filepath.FromSlash(files[i].Name)
  47. }
  48. m.next.IndexUpdate(deviceID, folder, files, flags, options)
  49. }
  50. func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) {
  51. name = filepath.FromSlash(name)
  52. return m.next.Request(deviceID, folder, name, offset, size, hash, flags, options)
  53. }
  54. func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
  55. m.next.ClusterConfig(deviceID, config)
  56. }
  57. func (m nativeModel) Close(deviceID DeviceID, err error) {
  58. m.next.Close(deviceID, err)
  59. }