filesystem.go 6.0 KB

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