nativemodel_windows.go 2.0 KB

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