basicfs.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. return rooted(rel, f.root)
  78. }
  79. // rootedSymlinkEvaluated does the same as rooted, but the returned path will not
  80. // contain any symlinks. package. If the relative path somehow causes the final
  81. // path to escape the root directory, this returns an error, to prevent accessing
  82. // files that are not in the shared directory.
  83. func (f *BasicFilesystem) rootedSymlinkEvaluated(rel string) (string, error) {
  84. return rooted(rel, f.rootSymlinkEvaluated)
  85. }
  86. func rooted(rel, root string) (string, error) {
  87. // The root must not be empty.
  88. if root == "" {
  89. return "", ErrInvalidFilename
  90. }
  91. pathSep := string(PathSeparator)
  92. // The expected prefix for the resulting path is the root, with a path
  93. // separator at the end.
  94. expectedPrefix := filepath.FromSlash(root)
  95. if !strings.HasSuffix(expectedPrefix, pathSep) {
  96. expectedPrefix += pathSep
  97. }
  98. var err error
  99. rel, err = Canonicalize(rel)
  100. if err != nil {
  101. return "", err
  102. }
  103. // The supposedly correct path is the one filepath.Join will return, as
  104. // it does cleaning and so on. Check that one first to make sure no
  105. // obvious escape attempts have been made.
  106. joined := filepath.Join(root, rel)
  107. if rel == "." && !strings.HasSuffix(joined, pathSep) {
  108. joined += pathSep
  109. }
  110. if !strings.HasPrefix(joined, expectedPrefix) {
  111. return "", ErrNotRelative
  112. }
  113. return joined, nil
  114. }
  115. func (f *BasicFilesystem) unrooted(path string) string {
  116. return rel(path, f.root)
  117. }
  118. func (f *BasicFilesystem) unrootedSymlinkEvaluated(path string) string {
  119. return rel(path, f.rootSymlinkEvaluated)
  120. }
  121. func rel(path, prefix string) string {
  122. return strings.TrimPrefix(strings.TrimPrefix(path, prefix), string(PathSeparator))
  123. }
  124. func (f *BasicFilesystem) Chmod(name string, mode FileMode) error {
  125. name, err := f.rooted(name)
  126. if err != nil {
  127. return err
  128. }
  129. return os.Chmod(name, os.FileMode(mode))
  130. }
  131. func (f *BasicFilesystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
  132. name, err := f.rooted(name)
  133. if err != nil {
  134. return err
  135. }
  136. return os.Chtimes(name, atime, mtime)
  137. }
  138. func (f *BasicFilesystem) Mkdir(name string, perm FileMode) error {
  139. name, err := f.rooted(name)
  140. if err != nil {
  141. return err
  142. }
  143. return os.Mkdir(name, os.FileMode(perm))
  144. }
  145. // MkdirAll creates a directory named path, along with any necessary parents,
  146. // and returns nil, or else returns an error.
  147. // The permission bits perm are used for all directories that MkdirAll creates.
  148. // If path is already a directory, MkdirAll does nothing and returns nil.
  149. func (f *BasicFilesystem) MkdirAll(path string, perm FileMode) error {
  150. path, err := f.rooted(path)
  151. if err != nil {
  152. return err
  153. }
  154. return f.mkdirAll(path, os.FileMode(perm))
  155. }
  156. func (f *BasicFilesystem) Lstat(name string) (FileInfo, error) {
  157. name, err := f.rooted(name)
  158. if err != nil {
  159. return nil, err
  160. }
  161. fi, err := underlyingLstat(name)
  162. if err != nil {
  163. return nil, err
  164. }
  165. return fsFileInfo{fi}, err
  166. }
  167. func (f *BasicFilesystem) Remove(name string) error {
  168. name, err := f.rooted(name)
  169. if err != nil {
  170. return err
  171. }
  172. return os.Remove(name)
  173. }
  174. func (f *BasicFilesystem) RemoveAll(name string) error {
  175. name, err := f.rooted(name)
  176. if err != nil {
  177. return err
  178. }
  179. return os.RemoveAll(name)
  180. }
  181. func (f *BasicFilesystem) Rename(oldpath, newpath string) error {
  182. oldpath, err := f.rooted(oldpath)
  183. if err != nil {
  184. return err
  185. }
  186. newpath, err = f.rooted(newpath)
  187. if err != nil {
  188. return err
  189. }
  190. return os.Rename(oldpath, newpath)
  191. }
  192. func (f *BasicFilesystem) Stat(name string) (FileInfo, error) {
  193. name, err := f.rooted(name)
  194. if err != nil {
  195. return nil, err
  196. }
  197. fi, err := os.Stat(name)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return fsFileInfo{fi}, err
  202. }
  203. func (f *BasicFilesystem) DirNames(name string) ([]string, error) {
  204. name, err := f.rooted(name)
  205. if err != nil {
  206. return nil, err
  207. }
  208. fd, err := os.OpenFile(name, OptReadOnly, 0777)
  209. if err != nil {
  210. return nil, err
  211. }
  212. defer fd.Close()
  213. names, err := fd.Readdirnames(-1)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return names, nil
  218. }
  219. func (f *BasicFilesystem) Open(name string) (File, error) {
  220. rootedName, err := f.rooted(name)
  221. if err != nil {
  222. return nil, err
  223. }
  224. fd, err := os.Open(rootedName)
  225. if err != nil {
  226. return nil, err
  227. }
  228. return fsFile{fd, name}, err
  229. }
  230. func (f *BasicFilesystem) OpenFile(name string, flags int, mode FileMode) (File, error) {
  231. rootedName, err := f.rooted(name)
  232. if err != nil {
  233. return nil, err
  234. }
  235. fd, err := os.OpenFile(rootedName, flags, os.FileMode(mode))
  236. if err != nil {
  237. return nil, err
  238. }
  239. return fsFile{fd, name}, err
  240. }
  241. func (f *BasicFilesystem) Create(name string) (File, error) {
  242. rootedName, err := f.rooted(name)
  243. if err != nil {
  244. return nil, err
  245. }
  246. fd, err := os.Create(rootedName)
  247. if err != nil {
  248. return nil, err
  249. }
  250. return fsFile{fd, name}, err
  251. }
  252. func (f *BasicFilesystem) Walk(root string, walkFn WalkFunc) error {
  253. // implemented in WalkFilesystem
  254. return errors.New("not implemented")
  255. }
  256. func (f *BasicFilesystem) Glob(pattern string) ([]string, error) {
  257. pattern, err := f.rooted(pattern)
  258. if err != nil {
  259. return nil, err
  260. }
  261. files, err := filepath.Glob(pattern)
  262. unrooted := make([]string, len(files))
  263. for i := range files {
  264. unrooted[i] = f.unrooted(files[i])
  265. }
  266. return unrooted, err
  267. }
  268. func (f *BasicFilesystem) Usage(name string) (Usage, error) {
  269. name, err := f.rooted(name)
  270. if err != nil {
  271. return Usage{}, err
  272. }
  273. u, err := du.Get(name)
  274. return Usage{
  275. Free: u.FreeBytes,
  276. Total: u.TotalBytes,
  277. }, err
  278. }
  279. func (f *BasicFilesystem) Type() FilesystemType {
  280. return FilesystemTypeBasic
  281. }
  282. func (f *BasicFilesystem) URI() string {
  283. return strings.TrimPrefix(f.root, `\\?\`)
  284. }
  285. func (f *BasicFilesystem) SameFile(fi1, fi2 FileInfo) bool {
  286. // Like os.SameFile, we always return false unless fi1 and fi2 were created
  287. // by this package's Stat/Lstat method.
  288. f1, ok1 := fi1.(fsFileInfo)
  289. f2, ok2 := fi2.(fsFileInfo)
  290. if !ok1 || !ok2 {
  291. return false
  292. }
  293. return os.SameFile(f1.FileInfo, f2.FileInfo)
  294. }
  295. // fsFile implements the fs.File interface on top of an os.File
  296. type fsFile struct {
  297. *os.File
  298. name string
  299. }
  300. func (f fsFile) Name() string {
  301. return f.name
  302. }
  303. func (f fsFile) Stat() (FileInfo, error) {
  304. info, err := f.File.Stat()
  305. if err != nil {
  306. return nil, err
  307. }
  308. return fsFileInfo{info}, nil
  309. }
  310. // fsFileInfo implements the fs.FileInfo interface on top of an os.FileInfo.
  311. type fsFileInfo struct {
  312. os.FileInfo
  313. }
  314. func (e fsFileInfo) IsSymlink() bool {
  315. // Must use fsFileInfo.Mode() because it may apply magic.
  316. return e.Mode()&ModeSymlink != 0
  317. }
  318. func (e fsFileInfo) IsRegular() bool {
  319. // Must use fsFileInfo.Mode() because it may apply magic.
  320. return e.Mode()&ModeType == 0
  321. }