bufffs.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package bufffs
  2. /*
  3. bufffs.go
  4. This module provide a mockup template for buffered type file system to create
  5. a os.File compatible type of file for other purposes
  6. */
  7. import (
  8. "errors"
  9. "io"
  10. "os"
  11. "syscall"
  12. "time"
  13. )
  14. type file struct {
  15. name string
  16. buf []byte
  17. ptr int64
  18. IsDir bool
  19. CloseHandler func(interface{}) error
  20. ClosePayload interface{}
  21. FshRealPath string
  22. }
  23. // New creates new mock file, which can be used as os.File.
  24. //func New(name string, isDir bool, content []byte, fshRealpath string, closeHandler func(interface{}) error, closePayload interface{}) *file {
  25. func New(name string) *file {
  26. return &file{
  27. name: name,
  28. buf: []byte{},
  29. ptr: 0,
  30. IsDir: false,
  31. CloseHandler: nil,
  32. ClosePayload: nil,
  33. FshRealPath: "",
  34. }
  35. }
  36. func (f *file) SetContent(content []byte) {
  37. f.buf = content
  38. }
  39. func (f *file) Chdir() error {
  40. return errors.New("operation not supported")
  41. }
  42. func (f *file) Chmod(mode os.FileMode) error {
  43. return errors.New("operation not supported")
  44. }
  45. func (f *file) Chown(uid, gid int) error {
  46. return errors.New("operation not supported")
  47. }
  48. func (f *file) Fd() uintptr {
  49. return 0
  50. }
  51. func (f *file) Name() string {
  52. return ""
  53. }
  54. func (f *file) ReadDir(n int) ([]os.DirEntry, error) {
  55. return []os.DirEntry{}, errors.New("operation not supported")
  56. }
  57. func (f *file) ReadFrom(r io.Reader) (n int64, err error) {
  58. return 0, errors.New("operation not supported")
  59. }
  60. func (f *file) Readdir(n int) ([]os.FileInfo, error) {
  61. return []os.FileInfo{}, errors.New("operation not supported")
  62. }
  63. func (f *file) Readdirnames(n int) (names []string, err error) {
  64. return []string{}, errors.New("operation not supported")
  65. }
  66. func (f *file) SetDeadline(t time.Time) error {
  67. return errors.New("operation not supported")
  68. }
  69. func (f *file) SetReadDeadline(t time.Time) error {
  70. return errors.New("operation not supported")
  71. }
  72. func (f *file) SetWriteDeadline(t time.Time) error {
  73. return errors.New("operation not supported")
  74. }
  75. func (f *file) Sync() error {
  76. return errors.New("operation not supported")
  77. }
  78. func (f *file) SyscallConn() (syscall.RawConn, error) {
  79. return nil, errors.New("operation not supported")
  80. }
  81. func (f *file) WriteAt(b []byte, off int64) (n int, err error) {
  82. return 0, errors.New("operation not supported")
  83. }
  84. func (f *file) WriteString(s string) (n int, err error) {
  85. return 0, errors.New("operation not supported")
  86. }
  87. func (m *file) Close() error {
  88. if m.CloseHandler != nil {
  89. return m.CloseHandler(m.ClosePayload)
  90. }
  91. return nil
  92. }
  93. func (m *file) Read(p []byte) (n int, err error) {
  94. n, err = m.ReadAt(p, m.ptr)
  95. m.ptr += int64(n)
  96. return n, err
  97. }
  98. func (m *file) Seek(offset int64, whence int) (int64, error) {
  99. switch whence {
  100. case io.SeekStart:
  101. m.ptr = offset
  102. case io.SeekEnd:
  103. m.ptr = int64(len(m.buf)) + offset
  104. case io.SeekCurrent:
  105. m.ptr = m.ptr + offset
  106. }
  107. return m.ptr, nil
  108. }
  109. func (m *file) Stat() (os.FileInfo, error) {
  110. return fileInfo{
  111. size: int64(len(m.buf)),
  112. dir: m.IsDir,
  113. }, nil
  114. }
  115. func (m *file) ReadAt(p []byte, off int64) (n int, err error) {
  116. if n = copy(p, m.buf[off:]); n == 0 {
  117. return n, io.EOF
  118. } else {
  119. return n, nil
  120. }
  121. }
  122. func (m *file) Write(p []byte) (n int, err error) {
  123. m.buf = append(m.buf, p...)
  124. return len(p), nil
  125. }
  126. func (m *file) Truncate(size int64) error {
  127. if size > int64(len(m.buf)) {
  128. size = int64(len(m.buf))
  129. }
  130. m.buf = m.buf[:size-1]
  131. return nil
  132. }
  133. type fileInfo struct {
  134. size int64
  135. dir bool
  136. }
  137. func (m fileInfo) Name() string {
  138. return ""
  139. }
  140. func (m fileInfo) Size() int64 {
  141. return m.size
  142. }
  143. func (m fileInfo) Mode() os.FileMode {
  144. return os.FileMode(0)
  145. }
  146. func (m fileInfo) ModTime() time.Time {
  147. return time.Time{}
  148. }
  149. func (m fileInfo) IsDir() bool {
  150. return m.dir
  151. }
  152. func (m fileInfo) Sys() interface{} {
  153. return nil
  154. }