vobject.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package bokofs
  2. /*
  3. vObjects
  4. The vObjects accept and forward the request of the WebDAV server to the
  5. underlying workers that might be serving a file system or other
  6. system management functions
  7. vObjects and its definations shall only be used on the root layer of each
  8. of the bokoFS instance.
  9. */
  10. import (
  11. "fmt"
  12. "os"
  13. "time"
  14. "golang.org/x/net/webdav"
  15. )
  16. type vObjectProperties struct {
  17. name string
  18. size int64
  19. mode os.FileMode
  20. modTime time.Time
  21. isDir bool
  22. }
  23. type vObjectFileInfo struct {
  24. properties *vObjectProperties
  25. sys interface{}
  26. }
  27. type vObject struct {
  28. properties *vObjectProperties
  29. parent *RootRouter
  30. }
  31. // newVirtualObject creates a new virtual object
  32. func (p *RootRouter) newVirtualObject(properties *vObjectProperties) *vObject {
  33. return &vObject{
  34. properties: properties,
  35. parent: p,
  36. }
  37. }
  38. func (r *vObject) GetFileInfo() os.FileInfo {
  39. return &vObjectFileInfo{
  40. properties: r.properties,
  41. sys: nil,
  42. }
  43. }
  44. /* File Info Interface */
  45. func (r *vObjectFileInfo) IsDir() bool {
  46. return r.properties.isDir
  47. }
  48. func (r *vObjectFileInfo) ModTime() time.Time {
  49. return r.properties.modTime
  50. }
  51. func (r *vObjectFileInfo) Mode() os.FileMode {
  52. return r.properties.mode
  53. }
  54. func (r *vObjectFileInfo) Name() string {
  55. return r.properties.name
  56. }
  57. func (r *vObjectFileInfo) Size() int64 {
  58. return r.properties.size
  59. }
  60. func (r *vObjectFileInfo) Sys() interface{} {
  61. return r.sys
  62. }
  63. /* File Interface */
  64. func (r *vObject) Close() error {
  65. // Implement the Close method
  66. fmt.Println("Close called")
  67. return nil
  68. }
  69. func (r *vObject) Read(p []byte) (n int, err error) {
  70. // Implement the Read method
  71. fmt.Println("Read called")
  72. return 0, nil
  73. }
  74. func (r *vObject) Readdir(count int) ([]os.FileInfo, error) {
  75. // Generate a emulated folder structure from worker registered paths
  76. fmt.Println("Readdir called")
  77. rootFolders, err := r.parent.parent.GetRegisteredRootFolders()
  78. if err != nil {
  79. return nil, err
  80. }
  81. // Generate the folder structure
  82. var folderList []os.FileInfo
  83. for _, folder := range rootFolders {
  84. thisVirtualObject := r.parent.newVirtualObject(&vObjectProperties{
  85. name: folder,
  86. size: 0,
  87. mode: os.ModeDir,
  88. modTime: time.Now(),
  89. isDir: true,
  90. })
  91. folderList = append(folderList, thisVirtualObject.GetFileInfo())
  92. }
  93. return folderList, nil
  94. }
  95. func (r *vObject) Seek(offset int64, whence int) (int64, error) {
  96. // Implement the Seek method
  97. fmt.Println("Seek called")
  98. return 0, nil
  99. }
  100. func (r *vObject) Write(p []byte) (n int, err error) {
  101. // Implement the Write method
  102. fmt.Println("Write called")
  103. return 0, nil
  104. }
  105. func (r *vObject) Stat() (os.FileInfo, error) {
  106. // Implement the Stat method
  107. fmt.Println("Stat called")
  108. return r.GetFileInfo(), nil
  109. }
  110. // Ensure vObject implements the File interface
  111. var _ webdav.File = (*vObject)(nil)