stat.go 797 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2020 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package fs
  5. // A StatFS is a file system with a Stat method.
  6. type StatFS interface {
  7. FS
  8. // Stat returns a FileInfo describing the file.
  9. // If there is an error, it should be of type *PathError.
  10. Stat(name string) (FileInfo, error)
  11. }
  12. // Stat returns a FileInfo describing the named file from the file system.
  13. //
  14. // If fs implements StatFS, Stat calls fs.Stat.
  15. // Otherwise, Stat opens the file to stat it.
  16. func Stat(fsys FS, name string) (FileInfo, error) {
  17. if fsys, ok := fsys.(StatFS); ok {
  18. return fsys.Stat(name)
  19. }
  20. file, err := fsys.Open(name)
  21. if err != nil {
  22. return nil, err
  23. }
  24. defer file.Close()
  25. return file.Stat()
  26. }