basicfs.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. "fmt"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "strings"
  14. "time"
  15. "github.com/shirou/gopsutil/v3/disk"
  16. )
  17. var (
  18. errInvalidFilenameEmpty = errors.New("name is invalid, must not be empty")
  19. errInvalidFilenameWindowsSpacePeriod = errors.New("name is invalid, must not end in space or period on Windows")
  20. errInvalidFilenameWindowsReservedName = errors.New("name is invalid, contains Windows reserved name (NUL, COM1, etc.)")
  21. errInvalidFilenameWindowsReservedChar = errors.New("name is invalid, contains Windows reserved character (?, *, etc.)")
  22. )
  23. type OptionJunctionsAsDirs struct{}
  24. func (o *OptionJunctionsAsDirs) apply(fs Filesystem) {
  25. if basic, ok := fs.(*BasicFilesystem); !ok {
  26. l.Warnln("WithJunctionsAsDirs must only be used with FilesystemTypeBasic")
  27. } else {
  28. basic.junctionsAsDirs = true
  29. }
  30. }
  31. func (o *OptionJunctionsAsDirs) String() string {
  32. return "junctionsAsDirs"
  33. }
  34. // The BasicFilesystem implements all aspects by delegating to package os.
  35. // All paths are relative to the root and cannot (should not) escape the root directory.
  36. type BasicFilesystem struct {
  37. root string
  38. junctionsAsDirs bool
  39. options []Option
  40. }
  41. func newBasicFilesystem(root string, opts ...Option) *BasicFilesystem {
  42. if root == "" {
  43. root = "." // Otherwise "" becomes "/" below
  44. }
  45. // The reason it's done like this:
  46. // C: -> C:\ -> C:\ (issue that this is trying to fix)
  47. // C:\somedir -> C:\somedir\ -> C:\somedir
  48. // C:\somedir\ -> C:\somedir\\ -> C:\somedir
  49. // This way in the tests, we get away without OS specific separators
  50. // in the test configs.
  51. sep := string(filepath.Separator)
  52. root = filepath.Dir(root + sep)
  53. // Attempt tilde expansion; leave unchanged in case of error
  54. if path, err := ExpandTilde(root); err == nil {
  55. root = path
  56. }
  57. // Attempt absolutification; leave unchanged in case of error
  58. if !filepath.IsAbs(root) {
  59. // Abs() looks like a fairly expensive syscall on Windows, while
  60. // IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
  61. // somewhat faster in the general case, hence the outer if...
  62. if path, err := filepath.Abs(root); err == nil {
  63. root = path
  64. }
  65. }
  66. // Attempt to enable long filename support on Windows. We may still not
  67. // have an absolute path here if the previous steps failed.
  68. if runtime.GOOS == "windows" {
  69. root = longFilenameSupport(root)
  70. }
  71. fs := &BasicFilesystem{
  72. root: root,
  73. options: opts,
  74. }
  75. for _, opt := range opts {
  76. opt.apply(fs)
  77. }
  78. return fs
  79. }
  80. // rooted expands the relative path to the full path that is then used with os
  81. // package. If the relative path somehow causes the final path to escape the root
  82. // directory, this returns an error, to prevent accessing files that are not in the
  83. // shared directory.
  84. func (f *BasicFilesystem) rooted(rel string) (string, error) {
  85. return rooted(rel, f.root)
  86. }
  87. func rooted(rel, root string) (string, error) {
  88. // The root must not be empty.
  89. if root == "" {
  90. return "", errInvalidFilenameEmpty
  91. }
  92. var err error
  93. // Takes care that rel does not try to escape
  94. rel, err = Canonicalize(rel)
  95. if err != nil {
  96. return "", err
  97. }
  98. return filepath.Join(root, rel), nil
  99. }
  100. func (f *BasicFilesystem) unrooted(path string) string {
  101. return rel(path, f.root)
  102. }
  103. func (f *BasicFilesystem) Chmod(name string, mode FileMode) error {
  104. name, err := f.rooted(name)
  105. if err != nil {
  106. return err
  107. }
  108. return os.Chmod(name, os.FileMode(mode))
  109. }
  110. func (f *BasicFilesystem) Lchown(name string, uid, gid int) error {
  111. name, err := f.rooted(name)
  112. if err != nil {
  113. return err
  114. }
  115. return os.Lchown(name, uid, gid)
  116. }
  117. func (f *BasicFilesystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
  118. name, err := f.rooted(name)
  119. if err != nil {
  120. return err
  121. }
  122. return os.Chtimes(name, atime, mtime)
  123. }
  124. func (f *BasicFilesystem) Mkdir(name string, perm FileMode) error {
  125. name, err := f.rooted(name)
  126. if err != nil {
  127. return err
  128. }
  129. return os.Mkdir(name, os.FileMode(perm))
  130. }
  131. // MkdirAll creates a directory named path, along with any necessary parents,
  132. // and returns nil, or else returns an error.
  133. // The permission bits perm are used for all directories that MkdirAll creates.
  134. // If path is already a directory, MkdirAll does nothing and returns nil.
  135. func (f *BasicFilesystem) MkdirAll(path string, perm FileMode) error {
  136. path, err := f.rooted(path)
  137. if err != nil {
  138. return err
  139. }
  140. return f.mkdirAll(path, os.FileMode(perm))
  141. }
  142. func (f *BasicFilesystem) Lstat(name string) (FileInfo, error) {
  143. name, err := f.rooted(name)
  144. if err != nil {
  145. return nil, err
  146. }
  147. fi, err := f.underlyingLstat(name)
  148. if err != nil {
  149. return nil, err
  150. }
  151. return basicFileInfo{fi}, err
  152. }
  153. func (f *BasicFilesystem) Remove(name string) error {
  154. name, err := f.rooted(name)
  155. if err != nil {
  156. return err
  157. }
  158. return os.Remove(name)
  159. }
  160. func (f *BasicFilesystem) RemoveAll(name string) error {
  161. name, err := f.rooted(name)
  162. if err != nil {
  163. return err
  164. }
  165. return os.RemoveAll(name)
  166. }
  167. func (f *BasicFilesystem) Rename(oldpath, newpath string) error {
  168. oldpath, err := f.rooted(oldpath)
  169. if err != nil {
  170. return err
  171. }
  172. newpath, err = f.rooted(newpath)
  173. if err != nil {
  174. return err
  175. }
  176. return os.Rename(oldpath, newpath)
  177. }
  178. func (f *BasicFilesystem) Stat(name string) (FileInfo, error) {
  179. name, err := f.rooted(name)
  180. if err != nil {
  181. return nil, err
  182. }
  183. fi, err := os.Stat(name)
  184. if err != nil {
  185. return nil, err
  186. }
  187. return basicFileInfo{fi}, err
  188. }
  189. func (f *BasicFilesystem) DirNames(name string) ([]string, error) {
  190. name, err := f.rooted(name)
  191. if err != nil {
  192. return nil, err
  193. }
  194. fd, err := os.OpenFile(name, OptReadOnly, 0777)
  195. if err != nil {
  196. return nil, err
  197. }
  198. defer fd.Close()
  199. names, err := fd.Readdirnames(-1)
  200. if err != nil {
  201. return nil, err
  202. }
  203. return names, nil
  204. }
  205. func (f *BasicFilesystem) Open(name string) (File, error) {
  206. rootedName, err := f.rooted(name)
  207. if err != nil {
  208. return nil, err
  209. }
  210. fd, err := os.Open(rootedName)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return basicFile{fd, name}, err
  215. }
  216. func (f *BasicFilesystem) OpenFile(name string, flags int, mode FileMode) (File, error) {
  217. rootedName, err := f.rooted(name)
  218. if err != nil {
  219. return nil, err
  220. }
  221. fd, err := os.OpenFile(rootedName, flags, os.FileMode(mode))
  222. if err != nil {
  223. return nil, err
  224. }
  225. return basicFile{fd, name}, err
  226. }
  227. func (f *BasicFilesystem) Create(name string) (File, error) {
  228. rootedName, err := f.rooted(name)
  229. if err != nil {
  230. return nil, err
  231. }
  232. fd, err := os.Create(rootedName)
  233. if err != nil {
  234. return nil, err
  235. }
  236. return basicFile{fd, name}, err
  237. }
  238. func (f *BasicFilesystem) Walk(root string, walkFn WalkFunc) error {
  239. // implemented in WalkFilesystem
  240. return errors.New("not implemented")
  241. }
  242. func (f *BasicFilesystem) Glob(pattern string) ([]string, error) {
  243. pattern, err := f.rooted(pattern)
  244. if err != nil {
  245. return nil, err
  246. }
  247. files, err := filepath.Glob(pattern)
  248. unrooted := make([]string, len(files))
  249. for i := range files {
  250. unrooted[i] = f.unrooted(files[i])
  251. }
  252. return unrooted, err
  253. }
  254. func (f *BasicFilesystem) Usage(name string) (Usage, error) {
  255. name, err := f.rooted(name)
  256. if err != nil {
  257. return Usage{}, err
  258. }
  259. u, err := disk.Usage(name)
  260. if err != nil {
  261. return Usage{}, err
  262. }
  263. return Usage{
  264. Free: u.Free,
  265. Total: u.Total,
  266. }, nil
  267. }
  268. func (f *BasicFilesystem) Type() FilesystemType {
  269. return FilesystemTypeBasic
  270. }
  271. func (f *BasicFilesystem) URI() string {
  272. return strings.TrimPrefix(f.root, `\\?\`)
  273. }
  274. func (f *BasicFilesystem) Options() []Option {
  275. return f.options
  276. }
  277. func (f *BasicFilesystem) SameFile(fi1, fi2 FileInfo) bool {
  278. // Like os.SameFile, we always return false unless fi1 and fi2 were created
  279. // by this package's Stat/Lstat method.
  280. f1, ok1 := fi1.(basicFileInfo)
  281. f2, ok2 := fi2.(basicFileInfo)
  282. if !ok1 || !ok2 {
  283. return false
  284. }
  285. return os.SameFile(f1.osFileInfo(), f2.osFileInfo())
  286. }
  287. func (f *BasicFilesystem) underlying() (Filesystem, bool) {
  288. return nil, false
  289. }
  290. func (f *BasicFilesystem) wrapperType() filesystemWrapperType {
  291. return filesystemWrapperTypeNone
  292. }
  293. // basicFile implements the fs.File interface on top of an os.File
  294. type basicFile struct {
  295. *os.File
  296. name string
  297. }
  298. func (f basicFile) Name() string {
  299. return f.name
  300. }
  301. func (f basicFile) Stat() (FileInfo, error) {
  302. info, err := f.File.Stat()
  303. if err != nil {
  304. return nil, err
  305. }
  306. return basicFileInfo{info}, nil
  307. }
  308. // basicFileInfo implements the fs.FileInfo interface on top of an os.FileInfo.
  309. type basicFileInfo struct {
  310. os.FileInfo
  311. }
  312. func (e basicFileInfo) IsSymlink() bool {
  313. // Must use basicFileInfo.Mode() because it may apply magic.
  314. return e.Mode()&ModeSymlink != 0
  315. }
  316. func (e basicFileInfo) IsRegular() bool {
  317. // Must use basicFileInfo.Mode() because it may apply magic.
  318. return e.Mode()&ModeType == 0
  319. }
  320. // longFilenameSupport adds the necessary prefix to the path to enable long
  321. // filename support on windows if necessary.
  322. // This does NOT check the current system, i.e. will also take effect on unix paths.
  323. func longFilenameSupport(path string) string {
  324. if filepath.IsAbs(path) && !strings.HasPrefix(path, `\\`) {
  325. return `\\?\` + path
  326. }
  327. return path
  328. }
  329. type ErrWatchEventOutsideRoot struct{ msg string }
  330. func (e *ErrWatchEventOutsideRoot) Error() string {
  331. return e.msg
  332. }
  333. func (f *BasicFilesystem) newErrWatchEventOutsideRoot(absPath string, roots []string) *ErrWatchEventOutsideRoot {
  334. return &ErrWatchEventOutsideRoot{fmt.Sprintf("Watching for changes encountered an event outside of the filesystem root: f.root==%v, roots==%v, path==%v. This should never happen, please report this message to forum.syncthing.net.", f.root, roots, absPath)}
  335. }