basicfs.go 7.9 KB

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