filesystem.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "io"
  9. "os"
  10. "path/filepath"
  11. "time"
  12. )
  13. // The Filesystem interface abstracts access to the file system.
  14. type Filesystem interface {
  15. Chmod(name string, mode FileMode) error
  16. Chtimes(name string, atime time.Time, mtime time.Time) error
  17. Create(name string) (File, error)
  18. CreateSymlink(name, target string) error
  19. DirNames(name string) ([]string, error)
  20. Lstat(name string) (FileInfo, error)
  21. Mkdir(name string, perm FileMode) error
  22. Open(name string) (File, error)
  23. ReadSymlink(name string) (string, error)
  24. Remove(name string) error
  25. Rename(oldname, newname string) error
  26. Stat(name string) (FileInfo, error)
  27. SymlinksSupported() bool
  28. Walk(root string, walkFn WalkFunc) error
  29. }
  30. // The File interface abstracts access to a regular file, being a somewhat
  31. // smaller interface than os.File
  32. type File interface {
  33. io.Reader
  34. io.WriterAt
  35. io.Closer
  36. Truncate(size int64) error
  37. Stat() (FileInfo, error)
  38. }
  39. // The FileInfo interface is almost the same as os.FileInfo, but with the
  40. // Sys method removed (as we don't want to expose whatever is underlying)
  41. // and with a couple of convenience methods added.
  42. type FileInfo interface {
  43. // Standard things present in os.FileInfo
  44. Name() string
  45. Mode() FileMode
  46. Size() int64
  47. ModTime() time.Time
  48. IsDir() bool
  49. // Extensions
  50. IsRegular() bool
  51. IsSymlink() bool
  52. }
  53. // FileMode is similar to os.FileMode
  54. type FileMode uint32
  55. // ModePerm is the equivalent of os.ModePerm
  56. const ModePerm = FileMode(os.ModePerm)
  57. // DefaultFilesystem is the fallback to use when nothing explicitly has
  58. // been passed.
  59. var DefaultFilesystem Filesystem = NewBasicFilesystem()
  60. // SkipDir is used as a return value from WalkFuncs to indicate that
  61. // the directory named in the call is to be skipped. It is not returned
  62. // as an error by any function.
  63. var SkipDir = filepath.SkipDir
  64. // IsExist is the equivalent of os.IsExist
  65. var IsExist = os.IsExist
  66. // IsNotExist is the equivalent of os.IsNotExist
  67. var IsNotExist = os.IsNotExist