github.com/falafeljan/pkger@v0.18.0/pkging/stdos/file.go (about)

     1  package stdos
     2  
     3  import (
     4  	"net/http"
     5  	"os"
     6  	"path"
     7  
     8  	"github.com/markbates/pkger/here"
     9  	"github.com/markbates/pkger/pkging"
    10  )
    11  
    12  var _ pkging.File = &File{}
    13  
    14  type File struct {
    15  	*os.File
    16  	info   *pkging.FileInfo
    17  	her    here.Info
    18  	path   here.Path
    19  	pkging pkging.Pkger
    20  }
    21  
    22  // Close closes the File, rendering it unusable for I/O.
    23  func (f *File) Close() error {
    24  	return f.File.Close()
    25  }
    26  
    27  // Info returns the here.Info of the file
    28  func (f *File) Info() here.Info {
    29  	return f.her
    30  }
    31  
    32  // Name retuns the name of the file in pkger format
    33  func (f File) Name() string {
    34  	return f.path.String()
    35  }
    36  
    37  // Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order. Subsequent calls on the same file will yield further FileInfos.
    38  //
    39  // If n > 0, Readdir returns at most n FileInfo structures. In this case, if Readdir returns an empty slice, it will return a non-nil error explaining why. At the end of a directory, the error is io.EOF.
    40  //
    41  // If n <= 0, Readdir returns all the FileInfo from the directory in a single slice. In this case, if Readdir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error. If it encounters an error before the end of the directory, Readdir returns the FileInfo read until that point and a non-nil error.
    42  func (f *File) Readdir(count int) ([]os.FileInfo, error) {
    43  	return f.File.Readdir(count)
    44  }
    45  
    46  // Open implements the http.FileSystem interface. A FileSystem implements access to a collection of named files. The elements in a file path are separated by slash ('/', U+002F) characters, regardless of host operating system convention.
    47  func (f *File) Open(name string) (http.File, error) {
    48  	fp := path.Join(f.Path().Name, name)
    49  	f2, err := f.pkging.Open(fp)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return f2, nil
    54  }
    55  
    56  // Path returns the here.Path of the file
    57  func (f *File) Path() here.Path {
    58  	return f.path
    59  }
    60  
    61  // Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError.
    62  func (f *File) Stat() (os.FileInfo, error) {
    63  	if f.info != nil {
    64  		return f.info, nil
    65  	}
    66  
    67  	info, err := f.File.Stat()
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	f.info = pkging.NewFileInfo(info)
    72  	return f.info, nil
    73  }