tree.go.tmp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package smb
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. )
  9. var _ TreeI = (*Tree)(nil)
  10. type Tree struct {
  11. name string
  12. id uint32
  13. baseDirPath string
  14. openedFiles map[FileId]*os.File
  15. lastFileId FileId
  16. }
  17. func (tree *Tree) Name() string {
  18. return tree.name
  19. }
  20. func (tree *Tree) Id() uint32 {
  21. return tree.id
  22. }
  23. func (tree *Tree) Close() error {
  24. return nil
  25. }
  26. func CreateTree(id uint32, name string, dirpath string) *Tree {
  27. return &Tree{
  28. name: name,
  29. id: id,
  30. baseDirPath: dirpath,
  31. openedFiles: make(map[FileId]*os.File),
  32. lastFileId: 10,
  33. }
  34. }
  35. func (tree *Tree) OpenFile(path string, flags int) (FileId, error) {
  36. fullPath := filepath.Join(tree.baseDirPath, path)
  37. f, err := os.OpenFile(fullPath, flags, 0666)
  38. // f, err := os.Open(fullPath)
  39. if err != nil {
  40. return 0, err
  41. }
  42. tree.lastFileId++
  43. tree.openedFiles[tree.lastFileId] = f
  44. return tree.lastFileId, nil
  45. }
  46. func (tree *Tree) CloseFile(fp FileId) error {
  47. if f, ok := tree.openedFiles[fp]; ok {
  48. delete(tree.openedFiles, fp)
  49. f.Close()
  50. return nil
  51. }
  52. return fmt.Errorf("Cannot close unknown file with id %d", fp)
  53. }
  54. func (tree *Tree) NameOfFile(fp FileId) string {
  55. if f, ok := tree.openedFiles[fp]; ok {
  56. return f.Name()
  57. }
  58. return "[unknown]"
  59. }
  60. var unknownFile = errors.New("Unknown file")
  61. func (tree *Tree) StatById(fp FileId, followLinks bool) (StatI, error) {
  62. if f, ok := tree.openedFiles[fp]; ok {
  63. st, err := f.Stat()
  64. if err != nil {
  65. return nil, err
  66. }
  67. return &Stat{st}, err
  68. }
  69. return nil, unknownFile
  70. }
  71. func (tree *Tree) Read(id FileId, p []byte) (n int, err error) {
  72. if f, ok := tree.openedFiles[id]; ok {
  73. return f.Read(p)
  74. }
  75. return 0, unknownFile
  76. }
  77. func (tree *Tree) Write(id FileId, p []byte) (n int, err error) {
  78. if f, ok := tree.openedFiles[id]; ok {
  79. return f.Write(p)
  80. }
  81. return 0, unknownFile
  82. }
  83. func (tree *Tree) Seek(id FileId, offset int64) error {
  84. if f, ok := tree.openedFiles[id]; ok {
  85. _, err := f.Seek(offset, os.SEEK_SET)
  86. return err
  87. }
  88. return unknownFile
  89. }
  90. func (tree *Tree) CurrentPosition(id FileId) int64 {
  91. if f, ok := tree.openedFiles[id]; ok {
  92. off, _ := f.Seek(0, os.SEEK_CUR)
  93. return off
  94. }
  95. return 0
  96. }
  97. func (tree *Tree) ReadDir(id FileId, n int) ([]Stat, error) {
  98. if f, ok := tree.openedFiles[id]; ok {
  99. r, err := f.Readdir(n)
  100. if err != nil {
  101. return nil, err
  102. }
  103. r2 := make([]Stat, len(r))
  104. for i := 0; i < len(r); i++ {
  105. r2[i] = Stat{r[i]}
  106. }
  107. return r2, nil
  108. }
  109. return nil, unknownFile
  110. }
  111. func (tree *Tree) Mkdir(path string) error {
  112. return os.Mkdir(fmt.Sprintf("%s/%s", tree.baseDirPath, path), 0777)
  113. }
  114. func (tree *Tree) Symlink(oldname, newname string) error {
  115. return os.Symlink(fmt.Sprintf("%s/%s", tree.baseDirPath, oldname), fmt.Sprintf("%s/%s", tree.baseDirPath, newname))
  116. }
  117. func (tree *Tree) Remove(path string, recursive bool) error {
  118. if recursive {
  119. return os.RemoveAll(fmt.Sprintf("%s/%s", tree.baseDirPath, path))
  120. }
  121. return os.Remove(fmt.Sprintf("%s/%s", tree.baseDirPath, path))
  122. }
  123. func (tree *Tree) Truncate(id FileId, size int64) error {
  124. if f, ok := tree.openedFiles[id]; ok {
  125. return f.Truncate(size)
  126. }
  127. return unknownFile
  128. }
  129. func (tree *Tree) ChangeMode(id FileId, allowWrite bool, allowExecute bool) error {
  130. return nil
  131. }
  132. func (tree *Tree) Sync(fp FileId) error {
  133. if f, ok := tree.openedFiles[fp]; ok {
  134. return f.Sync()
  135. }
  136. return unknownFile
  137. }
  138. func (tree *Tree) SyncAll() error {
  139. return nil
  140. }
  141. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  142. type Stat struct {
  143. osStat os.FileInfo
  144. }
  145. func (st Stat) Name() string {
  146. return st.osStat.Name()
  147. }
  148. func (st Stat) Size() int64 {
  149. return st.osStat.Size()
  150. }
  151. func (st Stat) ModTime() time.Time {
  152. return st.osStat.ModTime()
  153. }
  154. func (st Stat) IsReadOnly() bool {
  155. if (st.osStat.Mode().Perm() & 0333) == 0 {
  156. return true
  157. }
  158. return false
  159. }
  160. func (st Stat) IsExecutable() bool {
  161. if (st.osStat.Mode().Perm() & 0111) == 0 {
  162. return true
  163. }
  164. return false
  165. }
  166. func (st Stat) IsDir() bool {
  167. return st.osStat.Mode().IsDir()
  168. }
  169. func (st Stat) IsLink() bool {
  170. if st.osStat.Mode()&os.ModeSymlink != 0 {
  171. return true
  172. }
  173. return false
  174. }
  175. func (st Stat) Link() string {
  176. return "noit_implemented"
  177. }