diff --git a/pkging/file.go b/pkging/file.go index 167f46d..a09372e 100644 --- a/pkging/file.go +++ b/pkging/file.go @@ -8,15 +8,40 @@ import ( ) type File interface { + // Close closes the File, rendering it unusable for I/O. 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) + + // Info returns the here.Info of the file Info() here.Info + + // Name retuns the name of the file 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) + + // Path returns the here.Path of the file 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) + + // 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) + + // 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) + + // Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError. 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) } diff --git a/pkging/mem/file.go b/pkging/mem/file.go index 040e8a4..64c6791 100644 --- a/pkging/mem/file.go +++ b/pkging/mem/file.go @@ -62,6 +62,7 @@ func (f *File) UnmarshalJSON(b []byte) error { 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) { if sk, ok := f.reader.(io.Seeker); ok { return sk.Seek(ofpkginget, whence) @@ -69,6 +70,7 @@ func (f *File) Seek(ofpkginget int64, whence int) (int64, error) { return 0, nil } +// Close closes the File, rendering it unusable for I/O. func (f *File) Close() error { defer func() { f.reader = nil @@ -95,6 +97,7 @@ func (f *File) Close() error { 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) { if len(f.data) > 0 && f.reader == nil { 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()) } +// 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) { if f.writer == nil { f.writer = &bytes.Buffer{} @@ -115,10 +119,12 @@ func (f *File) Write(b []byte) (int, error) { return i, err } +// Info returns the here.Info of the file func (f File) Info() here.Info { 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) { if f.info == nil { return nil, os.ErrNotExist @@ -126,14 +132,17 @@ func (f File) Stat() (os.FileInfo, error) { return f.info, nil } +// Name retuns the name of the file func (f File) Name() string { 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) { return f.pkging.AbsPath(f.Path()) } +// Path returns the here.Path of the file func (f File) Path() here.Path { return f.path } @@ -142,6 +151,11 @@ func (f File) String() 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) { var infos []os.FileInfo 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) { pt, err := f.her.Parse(name) if err != nil { diff --git a/pkging/mem/mem.go b/pkging/mem/mem.go index 9ee240b..c2787ab 100644 --- a/pkging/mem/mem.go +++ b/pkging/mem/mem.go @@ -20,9 +20,9 @@ var _ pkging.Pkger = &Pkger{} // New returns *Pkger for the provided here.Info func New(info here.Info) (*Pkger, error) { f := &Pkger{ - infos: &maps.Infos{}, - files: &maps.Files{}, - current: info, + infos: &maps.Infos{}, + files: &maps.Files{}, + Here: info, } f.infos.Store(info.ImportPath, info) f.MkdirAll("/", 0755) @@ -30,9 +30,9 @@ func New(info here.Info) (*Pkger, error) { } type Pkger struct { - infos *maps.Infos - files *maps.Files - current here.Info + Here here.Info + infos *maps.Infos + files *maps.Files } type jay struct { @@ -57,7 +57,7 @@ func (p *Pkger) MarshalJSON() ([]byte, error) { return json.Marshal(jay{ Infos: p.infos, Files: files, - Current: p.current, + Current: p.Here, }) } @@ -69,7 +69,7 @@ func (p *Pkger) UnmarshalJSON(b []byte) error { return err } - p.current = y.Current + p.Here = y.Current 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. func (f *Pkger) Current() (here.Info, error) { - return f.current, nil + return f.Here, nil } // 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. 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. @@ -158,7 +158,7 @@ func (fx *Pkger) Add(f pkging.File) error { 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 { return err } diff --git a/pkging/stdos/file.go b/pkging/stdos/file.go index 068c292..a1f2e5e 100644 --- a/pkging/stdos/file.go +++ b/pkging/stdos/file.go @@ -19,22 +19,31 @@ type File struct { pkging pkging.Pkger } +// Close closes the File, rendering it unusable for I/O. func (f *File) Close() error { 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) { return f.pkging.AbsPath(f.path) } +// Info returns the here.Info of the file func (f *File) Info() here.Info { return f.her } +// Name retuns the name of the file func (f *File) Name() string { 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) { osinfos, err := f.File.Readdir(count) if err != nil { @@ -47,6 +56,8 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) { } 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) { fp := path.Join(f.Path().Name, name) f2, err := f.pkging.Open(fp) @@ -56,10 +67,12 @@ func (f *File) Open(name string) (http.File, error) { return f2, nil } +// Path returns the here.Path of the file func (f *File) Path() here.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) { if f.info != nil { return f.info, nil