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