nativemodel_windows.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build windows
  16. package protocol
  17. // Windows uses backslashes as file separator and disallows a bunch of
  18. // characters in the filename
  19. import (
  20. "path/filepath"
  21. "strings"
  22. )
  23. var disallowedCharacters = string([]rune{
  24. '<', '>', ':', '"', '|', '?', '*',
  25. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  26. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  27. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  28. 31,
  29. })
  30. type nativeModel struct {
  31. next Model
  32. }
  33. func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo) {
  34. for i, f := range files {
  35. if strings.ContainsAny(f.Name, disallowedCharacters) {
  36. if f.IsDeleted() {
  37. // Don't complain if the file is marked as deleted, since it
  38. // can't possibly exist here anyway.
  39. continue
  40. }
  41. files[i].Flags |= FlagInvalid
  42. l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name)
  43. }
  44. files[i].Name = filepath.FromSlash(f.Name)
  45. }
  46. m.next.Index(deviceID, folder, files)
  47. }
  48. func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) {
  49. for i, f := range files {
  50. if strings.ContainsAny(f.Name, disallowedCharacters) {
  51. if f.IsDeleted() {
  52. // Don't complain if the file is marked as deleted, since it
  53. // can't possibly exist here anyway.
  54. continue
  55. }
  56. files[i].Flags |= FlagInvalid
  57. l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name)
  58. }
  59. files[i].Name = filepath.FromSlash(files[i].Name)
  60. }
  61. m.next.IndexUpdate(deviceID, folder, files)
  62. }
  63. func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int) ([]byte, error) {
  64. name = filepath.FromSlash(name)
  65. return m.next.Request(deviceID, folder, name, offset, size)
  66. }
  67. func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
  68. m.next.ClusterConfig(deviceID, config)
  69. }
  70. func (m nativeModel) Close(deviceID DeviceID, err error) {
  71. m.next.Close(deviceID, err)
  72. }