basicfs.go 7.9 KB

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