nativemodel_windows.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. Model
  19. }
  20. func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo) {
  21. fixupFiles(folder, files)
  22. m.Model.Index(deviceID, folder, files)
  23. }
  24. func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) {
  25. fixupFiles(folder, files)
  26. m.Model.IndexUpdate(deviceID, folder, files)
  27. }
  28. func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, hash []byte, fromTemporary bool, buf []byte) error {
  29. name = filepath.FromSlash(name)
  30. return m.Model.Request(deviceID, folder, name, offset, hash, fromTemporary, buf)
  31. }
  32. func fixupFiles(folder string, files []FileInfo) {
  33. for i, f := range files {
  34. if strings.ContainsAny(f.Name, disallowedCharacters) || strings.HasSuffix(f.Name, " ") {
  35. if f.IsDeleted() {
  36. // Don't complain if the file is marked as deleted, since it
  37. // can't possibly exist here anyway.
  38. continue
  39. }
  40. files[i].Invalid = true
  41. l.Warnf("File name %q (folder %q) contains invalid characters; marked as invalid.", f.Name, folder)
  42. }
  43. files[i].Name = filepath.FromSlash(files[i].Name)
  44. }
  45. }