basicfs.go 9.6 KB

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