basicfs.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright (C) 2016 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. "errors"
  9. "fmt"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "strings"
  14. "time"
  15. "github.com/shirou/gopsutil/disk"
  16. )
  17. var (
  18. ErrInvalidFilename = errors.New("filename is invalid")
  19. ErrNotRelative = errors.New("not a relative path")
  20. )
  21. // The BasicFilesystem implements all aspects by delegating to package os.
  22. // All paths are relative to the root and cannot (should not) escape the root directory.
  23. type BasicFilesystem struct {
  24. root string
  25. }
  26. func newBasicFilesystem(root string) *BasicFilesystem {
  27. if root == "" {
  28. root = "." // Otherwise "" becomes "/" below
  29. }
  30. // The reason it's done like this:
  31. // C: -> C:\ -> C:\ (issue that this is trying to fix)
  32. // C:\somedir -> C:\somedir\ -> C:\somedir
  33. // C:\somedir\ -> C:\somedir\\ -> C:\somedir
  34. // This way in the tests, we get away without OS specific separators
  35. // in the test configs.
  36. sep := string(filepath.Separator)
  37. root = filepath.Dir(root + sep)
  38. // Attempt tilde expansion; leave unchanged in case of error
  39. if path, err := ExpandTilde(root); err == nil {
  40. root = path
  41. }
  42. // Attempt absolutification; leave unchanged in case of error
  43. if !filepath.IsAbs(root) {
  44. // Abs() looks like a fairly expensive syscall on Windows, while
  45. // IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
  46. // somewhat faster in the general case, hence the outer if...
  47. if path, err := filepath.Abs(root); err == nil {
  48. root = path
  49. }
  50. }
  51. // Attempt to enable long filename support on Windows. We may still not
  52. // have an absolute path here if the previous steps failed.
  53. if runtime.GOOS == "windows" {
  54. root = longFilenameSupport(root)
  55. }
  56. return &BasicFilesystem{root}
  57. }
  58. // rooted expands the relative path to the full path that is then used with os
  59. // package. If the relative path somehow causes the final path to escape the root
  60. // directory, this returns an error, to prevent accessing files that are not in the
  61. // shared directory.
  62. func (f *BasicFilesystem) rooted(rel string) (string, error) {
  63. return rooted(rel, f.root)
  64. }
  65. func rooted(rel, root string) (string, error) {
  66. // The root must not be empty.
  67. if root == "" {
  68. return "", ErrInvalidFilename
  69. }
  70. var err error
  71. // Takes care that rel does not try to escape
  72. rel, err = Canonicalize(rel)
  73. if err != nil {
  74. return "", err
  75. }
  76. return filepath.Join(root, rel), nil
  77. }
  78. func (f *BasicFilesystem) unrooted(path string) string {
  79. return rel(path, f.root)
  80. }
  81. func (f *BasicFilesystem) Chmod(name string, mode FileMode) error {
  82. name, err := f.rooted(name)
  83. if err != nil {
  84. return err
  85. }
  86. return os.Chmod(name, os.FileMode(mode))
  87. }
  88. func (f *BasicFilesystem) Lchown(name string, uid, gid int) error {
  89. name, err := f.rooted(name)
  90. if err != nil {
  91. return err
  92. }
  93. return os.Lchown(name, uid, gid)
  94. }
  95. func (f *BasicFilesystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
  96. name, err := f.rooted(name)
  97. if err != nil {
  98. return err
  99. }
  100. return os.Chtimes(name, atime, mtime)
  101. }
  102. func (f *BasicFilesystem) Mkdir(name string, perm FileMode) error {
  103. name, err := f.rooted(name)
  104. if err != nil {
  105. return err
  106. }
  107. return os.Mkdir(name, os.FileMode(perm))
  108. }
  109. // MkdirAll creates a directory named path, along with any necessary parents,
  110. // and returns nil, or else returns an error.
  111. // The permission bits perm are used for all directories that MkdirAll creates.
  112. // If path is already a directory, MkdirAll does nothing and returns nil.
  113. func (f *BasicFilesystem) MkdirAll(path string, perm FileMode) error {
  114. path, err := f.rooted(path)
  115. if err != nil {
  116. return err
  117. }
  118. return f.mkdirAll(path, os.FileMode(perm))
  119. }
  120. func (f *BasicFilesystem) Lstat(name string) (FileInfo, error) {
  121. name, err := f.rooted(name)
  122. if err != nil {
  123. return nil, err
  124. }
  125. fi, err := underlyingLstat(name)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return basicFileInfo{fi}, err
  130. }
  131. func (f *BasicFilesystem) Remove(name string) error {
  132. name, err := f.rooted(name)
  133. if err != nil {
  134. return err
  135. }
  136. return os.Remove(name)
  137. }
  138. func (f *BasicFilesystem) RemoveAll(name string) error {
  139. name, err := f.rooted(name)
  140. if err != nil {
  141. return err
  142. }
  143. return os.RemoveAll(name)
  144. }
  145. func (f *BasicFilesystem) Rename(oldpath, newpath string) error {
  146. oldpath, err := f.rooted(oldpath)
  147. if err != nil {
  148. return err
  149. }
  150. newpath, err = f.rooted(newpath)
  151. if err != nil {
  152. return err
  153. }
  154. return os.Rename(oldpath, newpath)
  155. }
  156. func (f *BasicFilesystem) Stat(name string) (FileInfo, error) {
  157. name, err := f.rooted(name)
  158. if err != nil {
  159. return nil, err
  160. }
  161. fi, err := os.Stat(name)
  162. if err != nil {
  163. return nil, err
  164. }
  165. return basicFileInfo{fi}, err
  166. }
  167. func (f *BasicFilesystem) DirNames(name string) ([]string, error) {
  168. name, err := f.rooted(name)
  169. if err != nil {
  170. return nil, err
  171. }
  172. fd, err := os.OpenFile(name, OptReadOnly, 0777)
  173. if err != nil {
  174. return nil, err
  175. }
  176. defer fd.Close()
  177. names, err := fd.Readdirnames(-1)
  178. if err != nil {
  179. return nil, err
  180. }
  181. return names, nil
  182. }
  183. func (f *BasicFilesystem) Open(name string) (File, error) {
  184. rootedName, err := f.rooted(name)
  185. if err != nil {
  186. return nil, err
  187. }
  188. fd, err := os.Open(rootedName)
  189. if err != nil {
  190. return nil, err
  191. }
  192. return basicFile{fd, name}, err
  193. }
  194. func (f *BasicFilesystem) OpenFile(name string, flags int, mode FileMode) (File, error) {
  195. rootedName, err := f.rooted(name)
  196. if err != nil {
  197. return nil, err
  198. }
  199. fd, err := os.OpenFile(rootedName, flags, os.FileMode(mode))
  200. if err != nil {
  201. return nil, err
  202. }
  203. return basicFile{fd, name}, err
  204. }
  205. func (f *BasicFilesystem) Create(name string) (File, error) {
  206. rootedName, err := f.rooted(name)
  207. if err != nil {
  208. return nil, err
  209. }
  210. fd, err := os.Create(rootedName)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return basicFile{fd, name}, err
  215. }
  216. func (f *BasicFilesystem) Walk(root string, walkFn WalkFunc) error {
  217. // implemented in WalkFilesystem
  218. return errors.New("not implemented")
  219. }
  220. func (f *BasicFilesystem) Glob(pattern string) ([]string, error) {
  221. pattern, err := f.rooted(pattern)
  222. if err != nil {
  223. return nil, err
  224. }
  225. files, err := filepath.Glob(pattern)
  226. unrooted := make([]string, len(files))
  227. for i := range files {
  228. unrooted[i] = f.unrooted(files[i])
  229. }
  230. return unrooted, err
  231. }
  232. func (f *BasicFilesystem) Usage(name string) (Usage, error) {
  233. name, err := f.rooted(name)
  234. if err != nil {
  235. return Usage{}, err
  236. }
  237. u, err := disk.Usage(name)
  238. if err != nil {
  239. return Usage{}, err
  240. }
  241. return Usage{
  242. Free: int64(u.Free),
  243. Total: int64(u.Total),
  244. }, nil
  245. }
  246. func (f *BasicFilesystem) Type() FilesystemType {
  247. return FilesystemTypeBasic
  248. }
  249. func (f *BasicFilesystem) URI() string {
  250. return strings.TrimPrefix(f.root, `\\?\`)
  251. }
  252. func (f *BasicFilesystem) SameFile(fi1, fi2 FileInfo) bool {
  253. // Like os.SameFile, we always return false unless fi1 and fi2 were created
  254. // by this package's Stat/Lstat method.
  255. f1, ok1 := fi1.(basicFileInfo)
  256. f2, ok2 := fi2.(basicFileInfo)
  257. if !ok1 || !ok2 {
  258. return false
  259. }
  260. return os.SameFile(f1.FileInfo, f2.FileInfo)
  261. }
  262. // basicFile implements the fs.File interface on top of an os.File
  263. type basicFile struct {
  264. *os.File
  265. name string
  266. }
  267. func (f basicFile) Name() string {
  268. return f.name
  269. }
  270. func (f basicFile) Stat() (FileInfo, error) {
  271. info, err := f.File.Stat()
  272. if err != nil {
  273. return nil, err
  274. }
  275. return basicFileInfo{info}, nil
  276. }
  277. // basicFileInfo implements the fs.FileInfo interface on top of an os.FileInfo.
  278. type basicFileInfo struct {
  279. os.FileInfo
  280. }
  281. func (e basicFileInfo) IsSymlink() bool {
  282. // Must use basicFileInfo.Mode() because it may apply magic.
  283. return e.Mode()&ModeSymlink != 0
  284. }
  285. func (e basicFileInfo) IsRegular() bool {
  286. // Must use basicFileInfo.Mode() because it may apply magic.
  287. return e.Mode()&ModeType == 0
  288. }
  289. // longFilenameSupport adds the necessary prefix to the path to enable long
  290. // filename support on windows if necessary.
  291. // This does NOT check the current system, i.e. will also take effect on unix paths.
  292. func longFilenameSupport(path string) string {
  293. if filepath.IsAbs(path) && !strings.HasPrefix(path, `\\`) {
  294. return `\\?\` + path
  295. }
  296. return path
  297. }
  298. type ErrWatchEventOutsideRoot struct{ msg string }
  299. func (e *ErrWatchEventOutsideRoot) Error() string {
  300. return e.msg
  301. }
  302. func (f *BasicFilesystem) newErrWatchEventOutsideRoot(absPath string, roots []string) *ErrWatchEventOutsideRoot {
  303. return &ErrWatchEventOutsideRoot{fmt.Sprintf("Watching for changes encountered an event outside of the filesystem root: f.root==%v, roots==%v, path==%v. This should never happen, please report this message to forum.syncthing.net.", f.root, roots, absPath)}
  304. }