basicfs.go 9.7 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. "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) 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. return fs
  31. }
  32. func (o *OptionJunctionsAsDirs) String() string {
  33. return "junctionsAsDirs"
  34. }
  35. // The BasicFilesystem implements all aspects by delegating to package os.
  36. // All paths are relative to the root and cannot (should not) escape the root directory.
  37. type BasicFilesystem struct {
  38. root string
  39. junctionsAsDirs bool
  40. options []Option
  41. }
  42. func newBasicFilesystem(root string, opts ...Option) *BasicFilesystem {
  43. if root == "" {
  44. root = "." // Otherwise "" becomes "/" below
  45. }
  46. // The reason it's done like this:
  47. // C: -> C:\ -> C:\ (issue that this is trying to fix)
  48. // C:\somedir -> C:\somedir\ -> C:\somedir
  49. // C:\somedir\ -> C:\somedir\\ -> C:\somedir
  50. // This way in the tests, we get away without OS specific separators
  51. // in the test configs.
  52. sep := string(filepath.Separator)
  53. root = filepath.Dir(root + sep)
  54. // Attempt tilde expansion; leave unchanged in case of error
  55. if path, err := ExpandTilde(root); err == nil {
  56. root = path
  57. }
  58. // Attempt absolutification; leave unchanged in case of error
  59. if !filepath.IsAbs(root) {
  60. // Abs() looks like a fairly expensive syscall on Windows, while
  61. // IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
  62. // somewhat faster in the general case, hence the outer if...
  63. if path, err := filepath.Abs(root); err == nil {
  64. root = path
  65. }
  66. }
  67. // Attempt to enable long filename support on Windows. We may still not
  68. // have an absolute path here if the previous steps failed.
  69. if runtime.GOOS == "windows" {
  70. root = longFilenameSupport(root)
  71. }
  72. fs := &BasicFilesystem{
  73. root: root,
  74. options: opts,
  75. }
  76. for _, opt := range opts {
  77. opt.apply(fs)
  78. }
  79. return fs
  80. }
  81. // rooted expands the relative path to the full path that is then used with os
  82. // package. If the relative path somehow causes the final path to escape the root
  83. // directory, this returns an error, to prevent accessing files that are not in the
  84. // shared directory.
  85. func (f *BasicFilesystem) rooted(rel string) (string, error) {
  86. return rooted(rel, f.root)
  87. }
  88. func rooted(rel, root string) (string, error) {
  89. // The root must not be empty.
  90. if root == "" {
  91. return "", errInvalidFilenameEmpty
  92. }
  93. var err error
  94. // Takes care that rel does not try to escape
  95. rel, err = Canonicalize(rel)
  96. if err != nil {
  97. return "", err
  98. }
  99. return filepath.Join(root, rel), nil
  100. }
  101. func (f *BasicFilesystem) unrooted(path string) string {
  102. return rel(path, f.root)
  103. }
  104. func (f *BasicFilesystem) Chmod(name string, mode FileMode) error {
  105. name, err := f.rooted(name)
  106. if err != nil {
  107. return err
  108. }
  109. return os.Chmod(name, os.FileMode(mode))
  110. }
  111. func (f *BasicFilesystem) Lchown(name string, uid, gid int) error {
  112. name, err := f.rooted(name)
  113. if err != nil {
  114. return err
  115. }
  116. return os.Lchown(name, uid, gid)
  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 (f *BasicFilesystem) Walk(root string, walkFn 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 (f *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 (f *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 (f *BasicFilesystem) underlying() (Filesystem, bool) {
  289. return nil, false
  290. }
  291. func (f *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. }