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
5package fs
6
7// A StatFS is a file system with a Stat method.
8type StatFS interface {
9	FS
10
11	// Stat returns a FileInfo describing the file.
12	// If there is an error, it should be of type *PathError.
13	Stat(name string) (FileInfo, error)
14}
15
16// Stat returns a [FileInfo] describing the named file from the file system.
17//
18// If fs implements [StatFS], Stat calls fs.Stat.
19// Otherwise, Stat opens the [File] to stat it.
20func Stat(fsys FS, name string) (FileInfo, error) {
21	if fsys, ok := fsys.(StatFS); ok {
22		return fsys.Stat(name)
23	}
24
25	file, err := fsys.Open(name)
26	if err != nil {
27		return nil, err
28	}
29	defer file.Close()
30	return file.Stat()
31}
32