filesystem.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. "context"
  9. "errors"
  10. "io"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. )
  16. // The Filesystem interface abstracts access to the file system.
  17. type Filesystem interface {
  18. Chmod(name string, mode FileMode) error
  19. Lchown(name string, uid, gid int) error
  20. Chtimes(name string, atime time.Time, mtime time.Time) error
  21. Create(name string) (File, error)
  22. CreateSymlink(target, name string) error
  23. DirNames(name string) ([]string, error)
  24. Lstat(name string) (FileInfo, error)
  25. Mkdir(name string, perm FileMode) error
  26. MkdirAll(name string, perm FileMode) error
  27. Open(name string) (File, error)
  28. OpenFile(name string, flags int, mode FileMode) (File, error)
  29. ReadSymlink(name string) (string, error)
  30. Remove(name string) error
  31. RemoveAll(name string) error
  32. Rename(oldname, newname string) error
  33. Stat(name string) (FileInfo, error)
  34. SymlinksSupported() bool
  35. Walk(name string, walkFn WalkFunc) error
  36. // If setup fails, returns non-nil error, and if afterwards a fatal (!)
  37. // error occurs, sends that error on the channel. Afterwards this watch
  38. // can be considered stopped.
  39. Watch(path string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error)
  40. Hide(name string) error
  41. Unhide(name string) error
  42. Glob(pattern string) ([]string, error)
  43. Roots() ([]string, error)
  44. Usage(name string) (Usage, error)
  45. Type() FilesystemType
  46. URI() string
  47. SameFile(fi1, fi2 FileInfo) bool
  48. }
  49. // The File interface abstracts access to a regular file, being a somewhat
  50. // smaller interface than os.File
  51. type File interface {
  52. io.Closer
  53. io.Reader
  54. io.ReaderAt
  55. io.Seeker
  56. io.Writer
  57. io.WriterAt
  58. Name() string
  59. Truncate(size int64) error
  60. Stat() (FileInfo, error)
  61. Sync() error
  62. }
  63. // The FileInfo interface is almost the same as os.FileInfo, but with the
  64. // Sys method removed (as we don't want to expose whatever is underlying)
  65. // and with a couple of convenience methods added.
  66. type FileInfo interface {
  67. // Standard things present in os.FileInfo
  68. Name() string
  69. Mode() FileMode
  70. Size() int64
  71. ModTime() time.Time
  72. IsDir() bool
  73. // Extensions
  74. IsRegular() bool
  75. IsSymlink() bool
  76. Owner() int
  77. Group() int
  78. }
  79. // FileMode is similar to os.FileMode
  80. type FileMode uint32
  81. func (fm FileMode) String() string {
  82. return os.FileMode(fm).String()
  83. }
  84. // Usage represents filesystem space usage
  85. type Usage struct {
  86. Free uint64
  87. Total uint64
  88. }
  89. type Matcher interface {
  90. ShouldIgnore(name string) bool
  91. SkipIgnoredDirs() bool
  92. }
  93. type MatchResult interface {
  94. IsIgnored() bool
  95. }
  96. type Event struct {
  97. Name string
  98. Type EventType
  99. }
  100. type EventType int
  101. const (
  102. NonRemove EventType = 1 + iota
  103. Remove
  104. Mixed // Should probably not be necessary to be used in filesystem interface implementation
  105. )
  106. // Merge returns Mixed, except if evType and other are the same and not Mixed.
  107. func (evType EventType) Merge(other EventType) EventType {
  108. return evType | other
  109. }
  110. func (evType EventType) String() string {
  111. switch {
  112. case evType == NonRemove:
  113. return "non-remove"
  114. case evType == Remove:
  115. return "remove"
  116. case evType == Mixed:
  117. return "mixed"
  118. default:
  119. panic("bug: Unknown event type")
  120. }
  121. }
  122. var ErrWatchNotSupported = errors.New("watching is not supported")
  123. // Equivalents from os package.
  124. const ModePerm = FileMode(os.ModePerm)
  125. const ModeSetgid = FileMode(os.ModeSetgid)
  126. const ModeSetuid = FileMode(os.ModeSetuid)
  127. const ModeSticky = FileMode(os.ModeSticky)
  128. const ModeSymlink = FileMode(os.ModeSymlink)
  129. const ModeType = FileMode(os.ModeType)
  130. const PathSeparator = os.PathSeparator
  131. const OptAppend = os.O_APPEND
  132. const OptCreate = os.O_CREATE
  133. const OptExclusive = os.O_EXCL
  134. const OptReadOnly = os.O_RDONLY
  135. const OptReadWrite = os.O_RDWR
  136. const OptSync = os.O_SYNC
  137. const OptTruncate = os.O_TRUNC
  138. const OptWriteOnly = os.O_WRONLY
  139. // SkipDir is used as a return value from WalkFuncs to indicate that
  140. // the directory named in the call is to be skipped. It is not returned
  141. // as an error by any function.
  142. var SkipDir = filepath.SkipDir
  143. // IsExist is the equivalent of os.IsExist
  144. var IsExist = os.IsExist
  145. // IsExist is the equivalent of os.ErrExist
  146. var ErrExist = os.ErrExist
  147. // IsNotExist is the equivalent of os.IsNotExist
  148. var IsNotExist = os.IsNotExist
  149. // ErrNotExist is the equivalent of os.ErrNotExist
  150. var ErrNotExist = os.ErrNotExist
  151. // IsPermission is the equivalent of os.IsPermission
  152. var IsPermission = os.IsPermission
  153. // IsPathSeparator is the equivalent of os.IsPathSeparator
  154. var IsPathSeparator = os.IsPathSeparator
  155. type Option func(Filesystem)
  156. func NewFilesystem(fsType FilesystemType, uri string, opts ...Option) Filesystem {
  157. var fs Filesystem
  158. switch fsType {
  159. case FilesystemTypeBasic:
  160. fs = newBasicFilesystem(uri, opts...)
  161. case FilesystemTypeFake:
  162. fs = newFakeFilesystem(uri, opts...)
  163. default:
  164. l.Debugln("Unknown filesystem", fsType, uri)
  165. fs = &errorFilesystem{
  166. fsType: fsType,
  167. uri: uri,
  168. err: errors.New("filesystem with type " + fsType.String() + " does not exist."),
  169. }
  170. }
  171. if l.ShouldDebug("walkfs") {
  172. return NewWalkFilesystem(&logFilesystem{fs})
  173. }
  174. if l.ShouldDebug("fs") {
  175. return &logFilesystem{NewWalkFilesystem(fs)}
  176. }
  177. return NewWalkFilesystem(fs)
  178. }
  179. // IsInternal returns true if the file, as a path relative to the folder
  180. // root, represents an internal file that should always be ignored. The file
  181. // path must be clean (i.e., in canonical shortest form).
  182. func IsInternal(file string) bool {
  183. // fs cannot import config, so we hard code .stfolder here (config.DefaultMarkerName)
  184. internals := []string{".stfolder", ".stignore", ".stversions"}
  185. for _, internal := range internals {
  186. if file == internal {
  187. return true
  188. }
  189. if IsParent(file, internal) {
  190. return true
  191. }
  192. }
  193. return false
  194. }
  195. // Canonicalize checks that the file path is valid and returns it in the "canonical" form:
  196. // - /foo/bar -> foo/bar
  197. // - / -> "."
  198. func Canonicalize(file string) (string, error) {
  199. pathSep := string(PathSeparator)
  200. if strings.HasPrefix(file, pathSep+pathSep) {
  201. // The relative path may pretend to be an absolute path within
  202. // the root, but the double path separator on Windows implies
  203. // something else and is out of spec.
  204. return "", errNotRelative
  205. }
  206. // The relative path should be clean from internal dotdots and similar
  207. // funkyness.
  208. file = filepath.Clean(file)
  209. // It is not acceptable to attempt to traverse upwards.
  210. switch file {
  211. case "..":
  212. return "", errNotRelative
  213. }
  214. if strings.HasPrefix(file, ".."+pathSep) {
  215. return "", errNotRelative
  216. }
  217. if strings.HasPrefix(file, pathSep) {
  218. if file == pathSep {
  219. return ".", nil
  220. }
  221. return file[1:], nil
  222. }
  223. return file, nil
  224. }
  225. // unwrapFilesystem removes "wrapping" filesystems to expose the underlying filesystem.
  226. func unwrapFilesystem(fs Filesystem) Filesystem {
  227. for {
  228. switch sfs := fs.(type) {
  229. case *logFilesystem:
  230. fs = sfs.Filesystem
  231. case *walkFilesystem:
  232. fs = sfs.Filesystem
  233. case *MtimeFS:
  234. fs = sfs.Filesystem
  235. default:
  236. return sfs
  237. }
  238. }
  239. }