basicfs.go 9.8 KB

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