nativemodel_windows.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. fixupFiles(folder, files)
  22. m.next.Index(deviceID, folder, files, flags, options)
  23. }
  24. func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
  25. fixupFiles(folder, files)
  26. m.next.IndexUpdate(deviceID, folder, files, flags, options)
  27. }
  28. func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error {
  29. name = filepath.FromSlash(name)
  30. return m.next.Request(deviceID, folder, name, offset, hash, flags, options, buf)
  31. }
  32. func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
  33. m.next.ClusterConfig(deviceID, config)
  34. }
  35. func (m nativeModel) Close(deviceID DeviceID, err error) {
  36. m.next.Close(deviceID, err)
  37. }
  38. func fixupFiles(folder string, files []FileInfo) {
  39. for i, f := range files {
  40. if strings.ContainsAny(f.Name, disallowedCharacters) {
  41. if f.IsDeleted() {
  42. // Don't complain if the file is marked as deleted, since it
  43. // can't possibly exist here anyway.
  44. continue
  45. }
  46. files[i].Flags |= FlagInvalid
  47. l.Warnf("File name %q (folder %q) contains invalid characters; marked as invalid.", f.Name, folder)
  48. }
  49. files[i].Name = filepath.FromSlash(files[i].Name)
  50. }
  51. }