This commit is contained in:
Mark Bates 2019-10-16 16:09:17 -04:00
parent 0aab2574e7
commit 09e9684b65
4 changed files with 64 additions and 11 deletions

View File

@ -8,15 +8,40 @@ import (
) )
type File interface { type File interface {
// Close closes the File, rendering it unusable for I/O.
Close() error Close() error
// Abs returns an absolute representation of the file. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
Abs() (string, error) Abs() (string, error)
// Info returns the here.Info of the file
Info() here.Info Info() here.Info
// Name retuns the name of the file
Name() string Name() string
// 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.
Open(name string) (http.File, error) Open(name string) (http.File, error)
// Path returns the here.Path of the file
Path() here.Path Path() here.Path
// Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.
Read(p []byte) (int, error) Read(p []byte) (int, error)
// 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.
//
// 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.
//
// 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.
Readdir(count int) ([]os.FileInfo, error) Readdir(count int) ([]os.FileInfo, error)
// Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any.
Seek(offset int64, whence int) (int64, error) Seek(offset int64, whence int) (int64, error)
// Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError.
Stat() (os.FileInfo, error) Stat() (os.FileInfo, error)
// Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).
Write(b []byte) (int, error) Write(b []byte) (int, error)
} }

View File

@ -62,6 +62,7 @@ func (f *File) UnmarshalJSON(b []byte) error {
return nil return nil
} }
// Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any.
func (f *File) Seek(ofpkginget int64, whence int) (int64, error) { func (f *File) Seek(ofpkginget int64, whence int) (int64, error) {
if sk, ok := f.reader.(io.Seeker); ok { if sk, ok := f.reader.(io.Seeker); ok {
return sk.Seek(ofpkginget, whence) return sk.Seek(ofpkginget, whence)
@ -69,6 +70,7 @@ func (f *File) Seek(ofpkginget int64, whence int) (int64, error) {
return 0, nil return 0, nil
} }
// Close closes the File, rendering it unusable for I/O.
func (f *File) Close() error { func (f *File) Close() error {
defer func() { defer func() {
f.reader = nil f.reader = nil
@ -95,6 +97,7 @@ func (f *File) Close() error {
return nil return nil
} }
// Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.
func (f *File) Read(p []byte) (int, error) { func (f *File) Read(p []byte) (int, error) {
if len(f.data) > 0 && f.reader == nil { if len(f.data) > 0 && f.reader == nil {
f.reader = bytes.NewReader(f.data) f.reader = bytes.NewReader(f.data)
@ -107,6 +110,7 @@ func (f *File) Read(p []byte) (int, error) {
return 0, fmt.Errorf("unable to read %s", f.Name()) return 0, fmt.Errorf("unable to read %s", f.Name())
} }
// Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).
func (f *File) Write(b []byte) (int, error) { func (f *File) Write(b []byte) (int, error) {
if f.writer == nil { if f.writer == nil {
f.writer = &bytes.Buffer{} f.writer = &bytes.Buffer{}
@ -115,10 +119,12 @@ func (f *File) Write(b []byte) (int, error) {
return i, err return i, err
} }
// Info returns the here.Info of the file
func (f File) Info() here.Info { func (f File) Info() here.Info {
return f.her return f.her
} }
// Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError.
func (f File) Stat() (os.FileInfo, error) { func (f File) Stat() (os.FileInfo, error) {
if f.info == nil { if f.info == nil {
return nil, os.ErrNotExist return nil, os.ErrNotExist
@ -126,14 +132,17 @@ func (f File) Stat() (os.FileInfo, error) {
return f.info, nil return f.info, nil
} }
// Name retuns the name of the file
func (f File) Name() string { func (f File) Name() string {
return f.info.Name() return f.info.Name()
} }
// Abs returns an absolute representation of the file. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
func (f File) Abs() (string, error) { func (f File) Abs() (string, error) {
return f.pkging.AbsPath(f.Path()) return f.pkging.AbsPath(f.Path())
} }
// Path returns the here.Path of the file
func (f File) Path() here.Path { func (f File) Path() here.Path {
return f.path return f.path
} }
@ -142,6 +151,11 @@ func (f File) String() string {
return f.Path().String() return f.Path().String()
} }
// 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.
//
// 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.
//
// 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.
func (f *File) Readdir(count int) ([]os.FileInfo, error) { func (f *File) Readdir(count int) ([]os.FileInfo, error) {
var infos []os.FileInfo var infos []os.FileInfo
root := f.Path().String() root := f.Path().String()
@ -187,6 +201,7 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
} }
// 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.
func (f *File) Open(name string) (http.File, error) { func (f *File) Open(name string) (http.File, error) {
pt, err := f.her.Parse(name) pt, err := f.her.Parse(name)
if err != nil { if err != nil {

View File

@ -22,7 +22,7 @@ func New(info here.Info) (*Pkger, error) {
f := &Pkger{ f := &Pkger{
infos: &maps.Infos{}, infos: &maps.Infos{},
files: &maps.Files{}, files: &maps.Files{},
current: info, Here: info,
} }
f.infos.Store(info.ImportPath, info) f.infos.Store(info.ImportPath, info)
f.MkdirAll("/", 0755) f.MkdirAll("/", 0755)
@ -30,9 +30,9 @@ func New(info here.Info) (*Pkger, error) {
} }
type Pkger struct { type Pkger struct {
Here here.Info
infos *maps.Infos infos *maps.Infos
files *maps.Files files *maps.Files
current here.Info
} }
type jay struct { type jay struct {
@ -57,7 +57,7 @@ func (p *Pkger) MarshalJSON() ([]byte, error) {
return json.Marshal(jay{ return json.Marshal(jay{
Infos: p.infos, Infos: p.infos,
Files: files, Files: files,
Current: p.current, Current: p.Here,
}) })
} }
@ -69,7 +69,7 @@ func (p *Pkger) UnmarshalJSON(b []byte) error {
return err return err
} }
p.current = y.Current p.Here = y.Current
p.infos = y.Infos p.infos = y.Infos
@ -100,7 +100,7 @@ func (f *Pkger) AbsPath(pt here.Path) (string, error) {
// Current returns the here.Info representing the current Pkger implementation. // Current returns the here.Info representing the current Pkger implementation.
func (f *Pkger) Current() (here.Info, error) { func (f *Pkger) Current() (here.Info, error) {
return f.current, nil return f.Here, nil
} }
// Info returns the here.Info of the here.Path // Info returns the here.Info of the here.Path
@ -115,7 +115,7 @@ func (f *Pkger) Info(p string) (here.Info, error) {
// Parse the string in here.Path format. // Parse the string in here.Path format.
func (f *Pkger) Parse(p string) (here.Path, error) { func (f *Pkger) Parse(p string) (here.Path, error) {
return f.current.Parse(p) return f.Here.Parse(p)
} }
// Remove removes the named file or (empty) directory. // Remove removes the named file or (empty) directory.
@ -158,7 +158,7 @@ func (fx *Pkger) Add(f pkging.File) error {
return err return err
} }
if f.Path().Pkg == fx.current.ImportPath { if f.Path().Pkg == fx.Here.ImportPath {
if err := fx.MkdirAll(filepath.Dir(f.Name()), 0755); err != nil { if err := fx.MkdirAll(filepath.Dir(f.Name()), 0755); err != nil {
return err return err
} }

View File

@ -19,22 +19,31 @@ type File struct {
pkging pkging.Pkger pkging pkging.Pkger
} }
// Close closes the File, rendering it unusable for I/O.
func (f *File) Close() error { func (f *File) Close() error {
return f.File.Close() return f.File.Close()
} }
// Abs returns an absolute representation of the file. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
func (f *File) Abs() (string, error) { func (f *File) Abs() (string, error) {
return f.pkging.AbsPath(f.path) return f.pkging.AbsPath(f.path)
} }
// Info returns the here.Info of the file
func (f *File) Info() here.Info { func (f *File) Info() here.Info {
return f.her return f.her
} }
// Name retuns the name of the file
func (f *File) Name() string { func (f *File) Name() string {
return f.info.Name() return f.info.Name()
} }
// 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.
//
// 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.
//
// 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.
func (f *File) Readdir(count int) ([]os.FileInfo, error) { func (f *File) Readdir(count int) ([]os.FileInfo, error) {
osinfos, err := f.File.Readdir(count) osinfos, err := f.File.Readdir(count)
if err != nil { if err != nil {
@ -47,6 +56,8 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
} }
return infos, err return infos, err
} }
// 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.
func (f *File) Open(name string) (http.File, error) { func (f *File) Open(name string) (http.File, error) {
fp := path.Join(f.Path().Name, name) fp := path.Join(f.Path().Name, name)
f2, err := f.pkging.Open(fp) f2, err := f.pkging.Open(fp)
@ -56,10 +67,12 @@ func (f *File) Open(name string) (http.File, error) {
return f2, nil return f2, nil
} }
// Path returns the here.Path of the file
func (f *File) Path() here.Path { func (f *File) Path() here.Path {
return f.path return f.path
} }
// Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError.
func (f *File) Stat() (os.FileInfo, error) { func (f *File) Stat() (os.FileInfo, error) {
if f.info != nil { if f.info != nil {
return f.info, nil return f.info, nil