basicfs.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. }
  25. func newBasicFilesystem(root string) *BasicFilesystem {
  26. // The reason it's done like this:
  27. // C: -> C:\ -> C:\ (issue that this is trying to fix)
  28. // C:\somedir -> C:\somedir\ -> C:\somedir
  29. // C:\somedir\ -> C:\somedir\\ -> C:\somedir
  30. // This way in the tests, we get away without OS specific separators
  31. // in the test configs.
  32. sep := string(filepath.Separator)
  33. root = filepath.Dir(root + sep)
  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. // Attempt to enable long filename support on Windows. We may still not
  48. // have an absolute path here if the previous steps failed.
  49. if runtime.GOOS == "windows" {
  50. root = longFilenameSupport(root)
  51. }
  52. return &BasicFilesystem{root}
  53. }
  54. // rooted expands the relative path to the full path that is then used with os
  55. // package. If the relative path somehow causes the final path to escape the root
  56. // directory, this returns an error, to prevent accessing files that are not in the
  57. // shared directory.
  58. func (f *BasicFilesystem) rooted(rel string) (string, error) {
  59. return rooted(rel, f.root)
  60. }
  61. func rooted(rel, root string) (string, error) {
  62. // The root must not be empty.
  63. if root == "" {
  64. return "", ErrInvalidFilename
  65. }
  66. var err error
  67. // Takes care that rel does not try to escape
  68. rel, err = Canonicalize(rel)
  69. if err != nil {
  70. return "", err
  71. }
  72. return filepath.Join(root, rel), nil
  73. }
  74. func (f *BasicFilesystem) unrooted(path string) string {
  75. return rel(path, f.root)
  76. }
  77. func (f *BasicFilesystem) Chmod(name string, mode FileMode) error {
  78. name, err := f.rooted(name)
  79. if err != nil {
  80. return err
  81. }
  82. return os.Chmod(name, os.FileMode(mode))
  83. }
  84. func (f *BasicFilesystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
  85. name, err := f.rooted(name)
  86. if err != nil {
  87. return err
  88. }
  89. return os.Chtimes(name, atime, mtime)
  90. }
  91. func (f *BasicFilesystem) Mkdir(name string, perm FileMode) error {
  92. name, err := f.rooted(name)
  93. if err != nil {
  94. return err
  95. }
  96. return os.Mkdir(name, os.FileMode(perm))
  97. }
  98. // MkdirAll creates a directory named path, along with any necessary parents,
  99. // and returns nil, or else returns an error.
  100. // The permission bits perm are used for all directories that MkdirAll creates.
  101. // If path is already a directory, MkdirAll does nothing and returns nil.
  102. func (f *BasicFilesystem) MkdirAll(path string, perm FileMode) error {
  103. path, err := f.rooted(path)
  104. if err != nil {
  105. return err
  106. }
  107. return f.mkdirAll(path, os.FileMode(perm))
  108. }
  109. func (f *BasicFilesystem) Lstat(name string) (FileInfo, error) {
  110. name, err := f.rooted(name)
  111. if err != nil {
  112. return nil, err
  113. }
  114. fi, err := underlyingLstat(name)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return fsFileInfo{fi}, err
  119. }
  120. func (f *BasicFilesystem) Remove(name string) error {
  121. name, err := f.rooted(name)
  122. if err != nil {
  123. return err
  124. }
  125. return os.Remove(name)
  126. }
  127. func (f *BasicFilesystem) RemoveAll(name string) error {
  128. name, err := f.rooted(name)
  129. if err != nil {
  130. return err
  131. }
  132. return os.RemoveAll(name)
  133. }
  134. func (f *BasicFilesystem) Rename(oldpath, newpath string) error {
  135. oldpath, err := f.rooted(oldpath)
  136. if err != nil {
  137. return err
  138. }
  139. newpath, err = f.rooted(newpath)
  140. if err != nil {
  141. return err
  142. }
  143. return os.Rename(oldpath, newpath)
  144. }
  145. func (f *BasicFilesystem) Stat(name string) (FileInfo, error) {
  146. name, err := f.rooted(name)
  147. if err != nil {
  148. return nil, err
  149. }
  150. fi, err := os.Stat(name)
  151. if err != nil {
  152. return nil, err
  153. }
  154. return fsFileInfo{fi}, err
  155. }
  156. func (f *BasicFilesystem) DirNames(name string) ([]string, error) {
  157. name, err := f.rooted(name)
  158. if err != nil {
  159. return nil, err
  160. }
  161. fd, err := os.OpenFile(name, OptReadOnly, 0777)
  162. if err != nil {
  163. return nil, err
  164. }
  165. defer fd.Close()
  166. names, err := fd.Readdirnames(-1)
  167. if err != nil {
  168. return nil, err
  169. }
  170. return names, nil
  171. }
  172. func (f *BasicFilesystem) Open(name string) (File, error) {
  173. rootedName, err := f.rooted(name)
  174. if err != nil {
  175. return nil, err
  176. }
  177. fd, err := os.Open(rootedName)
  178. if err != nil {
  179. return nil, err
  180. }
  181. return fsFile{fd, name}, err
  182. }
  183. func (f *BasicFilesystem) OpenFile(name string, flags int, mode FileMode) (File, error) {
  184. rootedName, err := f.rooted(name)
  185. if err != nil {
  186. return nil, err
  187. }
  188. fd, err := os.OpenFile(rootedName, flags, os.FileMode(mode))
  189. if err != nil {
  190. return nil, err
  191. }
  192. return fsFile{fd, name}, err
  193. }
  194. func (f *BasicFilesystem) Create(name string) (File, error) {
  195. rootedName, err := f.rooted(name)
  196. if err != nil {
  197. return nil, err
  198. }
  199. fd, err := os.Create(rootedName)
  200. if err != nil {
  201. return nil, err
  202. }
  203. return fsFile{fd, name}, err
  204. }
  205. func (f *BasicFilesystem) Walk(root string, walkFn WalkFunc) error {
  206. // implemented in WalkFilesystem
  207. return errors.New("not implemented")
  208. }
  209. func (f *BasicFilesystem) Glob(pattern string) ([]string, error) {
  210. pattern, err := f.rooted(pattern)
  211. if err != nil {
  212. return nil, err
  213. }
  214. files, err := filepath.Glob(pattern)
  215. unrooted := make([]string, len(files))
  216. for i := range files {
  217. unrooted[i] = f.unrooted(files[i])
  218. }
  219. return unrooted, err
  220. }
  221. func (f *BasicFilesystem) Usage(name string) (Usage, error) {
  222. name, err := f.rooted(name)
  223. if err != nil {
  224. return Usage{}, err
  225. }
  226. u, err := du.Get(name)
  227. return Usage{
  228. Free: u.FreeBytes,
  229. Total: u.TotalBytes,
  230. }, err
  231. }
  232. func (f *BasicFilesystem) Type() FilesystemType {
  233. return FilesystemTypeBasic
  234. }
  235. func (f *BasicFilesystem) URI() string {
  236. return strings.TrimPrefix(f.root, `\\?\`)
  237. }
  238. func (f *BasicFilesystem) SameFile(fi1, fi2 FileInfo) bool {
  239. // Like os.SameFile, we always return false unless fi1 and fi2 were created
  240. // by this package's Stat/Lstat method.
  241. f1, ok1 := fi1.(fsFileInfo)
  242. f2, ok2 := fi2.(fsFileInfo)
  243. if !ok1 || !ok2 {
  244. return false
  245. }
  246. return os.SameFile(f1.FileInfo, f2.FileInfo)
  247. }
  248. // fsFile implements the fs.File interface on top of an os.File
  249. type fsFile struct {
  250. *os.File
  251. name string
  252. }
  253. func (f fsFile) Name() string {
  254. return f.name
  255. }
  256. func (f fsFile) Stat() (FileInfo, error) {
  257. info, err := f.File.Stat()
  258. if err != nil {
  259. return nil, err
  260. }
  261. return fsFileInfo{info}, nil
  262. }
  263. // fsFileInfo implements the fs.FileInfo interface on top of an os.FileInfo.
  264. type fsFileInfo struct {
  265. os.FileInfo
  266. }
  267. func (e fsFileInfo) IsSymlink() bool {
  268. // Must use fsFileInfo.Mode() because it may apply magic.
  269. return e.Mode()&ModeSymlink != 0
  270. }
  271. func (e fsFileInfo) IsRegular() bool {
  272. // Must use fsFileInfo.Mode() because it may apply magic.
  273. return e.Mode()&ModeType == 0
  274. }
  275. // longFilenameSupport adds the necessary prefix to the path to enable long
  276. // filename support on windows if necessary.
  277. // This does NOT check the current system, i.e. will also take effect on unix paths.
  278. func longFilenameSupport(path string) string {
  279. if filepath.IsAbs(path) && !strings.HasPrefix(path, `\\`) {
  280. return `\\?\` + path
  281. }
  282. return path
  283. }