basicfs_fileinfo_windows.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (C) 2017 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. var execExts map[string]bool
  13. func init() {
  14. // PATHEXT contains a list of executable file extensions, on Windows
  15. pathext := filepath.SplitList(os.Getenv("PATHEXT"))
  16. // We want the extensions in execExts to be lower case
  17. execExts = make(map[string]bool, len(pathext))
  18. for _, ext := range pathext {
  19. execExts[strings.ToLower(ext)] = true
  20. }
  21. }
  22. // isWindowsExecutable returns true if the given path has an extension that is
  23. // in the list of executable extensions.
  24. func isWindowsExecutable(path string) bool {
  25. return execExts[strings.ToLower(filepath.Ext(path))]
  26. }
  27. func (e basicFileInfo) Mode() FileMode {
  28. m := e.FileInfo.Mode()
  29. if m&os.ModeSymlink != 0 && e.Size() > 0 {
  30. // "Symlinks" with nonzero size are in fact "hard" links, such as
  31. // NTFS deduped files. Remove the symlink bit.
  32. m &^= os.ModeSymlink
  33. }
  34. // Set executable bits on files with executable extenions (.exe, .bat, etc).
  35. if isWindowsExecutable(e.Name()) {
  36. m |= 0111
  37. }
  38. // There is no user/group/others in Windows' read-only attribute, and
  39. // all "w" bits are set if the file is not read-only. Do not send these
  40. // group/others-writable bits to other devices in order to avoid
  41. // unexpected world-writable files on other platforms.
  42. m &^= 0022
  43. return FileMode(m)
  44. }
  45. func (e basicFileInfo) Owner() int {
  46. return -1
  47. }
  48. func (e basicFileInfo) Group() int {
  49. return -1
  50. }
  51. // osFileInfo converts e to os.FileInfo that is suitable
  52. // to be passed to os.SameFile.
  53. func (e *basicFileInfo) osFileInfo() os.FileInfo {
  54. fi := e.FileInfo
  55. if fi, ok := fi.(*dirJunctFileInfo); ok {
  56. return fi.FileInfo
  57. }
  58. return fi
  59. }