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 }