github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/ioctx/fsctx/stat.go (about)

     1  package fsctx
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  )
     7  
     8  // StatFS is io/fs.StatFS with context added.
     9  type StatFS interface {
    10  	FS
    11  	Stat(_ context.Context, name string) (os.FileInfo, error)
    12  }
    13  
    14  // Stat is io/fs.Stat with context added.
    15  func Stat(ctx context.Context, fsys FS, name string) (os.FileInfo, error) {
    16  	if fsys, ok := fsys.(StatFS); ok {
    17  		return fsys.Stat(ctx, name)
    18  	}
    19  
    20  	file, err := fsys.Open(ctx, name)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	defer func() { _ = file.Close(ctx) }()
    25  	return file.Stat(ctx)
    26  }