basicfs.go 8.7 KB

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