basicfs.go 9.8 KB

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