123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- package bufffs
- /*
- bufffs.go
- This module provide a mockup template for buffered type file system to create
- a os.File compatible type of file for other purposes
- */
- import (
- "errors"
- "io"
- "os"
- "syscall"
- "time"
- )
- type file struct {
- name string
- buf []byte
- ptr int64
- IsDir bool
- CloseHandler func(interface{}) error
- ClosePayload interface{}
- FshRealPath string
- }
- // New creates new mock file, which can be used as os.File.
- //func New(name string, isDir bool, content []byte, fshRealpath string, closeHandler func(interface{}) error, closePayload interface{}) *file {
- func New(name string) *file {
- return &file{
- name: name,
- buf: []byte{},
- ptr: 0,
- IsDir: false,
- CloseHandler: nil,
- ClosePayload: nil,
- FshRealPath: "",
- }
- }
- func (f *file) SetContent(content []byte) {
- f.buf = content
- }
- func (f *file) Chdir() error {
- return errors.New("operation not supported")
- }
- func (f *file) Chmod(mode os.FileMode) error {
- return errors.New("operation not supported")
- }
- func (f *file) Chown(uid, gid int) error {
- return errors.New("operation not supported")
- }
- func (f *file) Fd() uintptr {
- return 0
- }
- func (f *file) Name() string {
- return ""
- }
- func (f *file) ReadDir(n int) ([]os.DirEntry, error) {
- return []os.DirEntry{}, errors.New("operation not supported")
- }
- func (f *file) ReadFrom(r io.Reader) (n int64, err error) {
- return 0, errors.New("operation not supported")
- }
- func (f *file) Readdir(n int) ([]os.FileInfo, error) {
- return []os.FileInfo{}, errors.New("operation not supported")
- }
- func (f *file) Readdirnames(n int) (names []string, err error) {
- return []string{}, errors.New("operation not supported")
- }
- func (f *file) SetDeadline(t time.Time) error {
- return errors.New("operation not supported")
- }
- func (f *file) SetReadDeadline(t time.Time) error {
- return errors.New("operation not supported")
- }
- func (f *file) SetWriteDeadline(t time.Time) error {
- return errors.New("operation not supported")
- }
- func (f *file) Sync() error {
- return errors.New("operation not supported")
- }
- func (f *file) SyscallConn() (syscall.RawConn, error) {
- return nil, errors.New("operation not supported")
- }
- func (f *file) WriteAt(b []byte, off int64) (n int, err error) {
- return 0, errors.New("operation not supported")
- }
- func (f *file) WriteString(s string) (n int, err error) {
- return 0, errors.New("operation not supported")
- }
- func (m *file) Close() error {
- if m.CloseHandler != nil {
- return m.CloseHandler(m.ClosePayload)
- }
- return nil
- }
- func (m *file) Read(p []byte) (n int, err error) {
- n, err = m.ReadAt(p, m.ptr)
- m.ptr += int64(n)
- return n, err
- }
- func (m *file) Seek(offset int64, whence int) (int64, error) {
- switch whence {
- case io.SeekStart:
- m.ptr = offset
- case io.SeekEnd:
- m.ptr = int64(len(m.buf)) + offset
- case io.SeekCurrent:
- m.ptr = m.ptr + offset
- }
- return m.ptr, nil
- }
- func (m *file) Stat() (os.FileInfo, error) {
- return fileInfo{
- size: int64(len(m.buf)),
- dir: m.IsDir,
- }, nil
- }
- func (m *file) ReadAt(p []byte, off int64) (n int, err error) {
- if n = copy(p, m.buf[off:]); n == 0 {
- return n, io.EOF
- } else {
- return n, nil
- }
- }
- func (m *file) Write(p []byte) (n int, err error) {
- m.buf = append(m.buf, p...)
- return len(p), nil
- }
- func (m *file) Truncate(size int64) error {
- if size > int64(len(m.buf)) {
- size = int64(len(m.buf))
- }
- m.buf = m.buf[:size-1]
- return nil
- }
- type fileInfo struct {
- size int64
- dir bool
- }
- func (m fileInfo) Name() string {
- return ""
- }
- func (m fileInfo) Size() int64 {
- return m.size
- }
- func (m fileInfo) Mode() os.FileMode {
- return os.FileMode(0)
- }
- func (m fileInfo) ModTime() time.Time {
- return time.Time{}
- }
- func (m fileInfo) IsDir() bool {
- return m.dir
- }
- func (m fileInfo) Sys() interface{} {
- return nil
- }
|