filer_client.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package filer_pb
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "math"
  8. "os"
  9. "strings"
  10. "time"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. var (
  15. OS_UID = uint32(os.Getuid())
  16. OS_GID = uint32(os.Getgid())
  17. )
  18. type FilerClient interface {
  19. WithFilerClient(fn func(SeaweedFilerClient) error) error
  20. AdjustedUrl(location *Location) string
  21. }
  22. func GetEntry(filerClient FilerClient, fullFilePath util.FullPath) (entry *Entry, err error) {
  23. dir, name := fullFilePath.DirAndName()
  24. err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  25. request := &LookupDirectoryEntryRequest{
  26. Directory: dir,
  27. Name: name,
  28. }
  29. // glog.V(3).Infof("read %s request: %v", fullFilePath, request)
  30. resp, err := LookupEntry(client, request)
  31. if err != nil {
  32. if err == ErrNotFound {
  33. return nil
  34. }
  35. glog.V(3).Infof("read %s %v: %v", fullFilePath, resp, err)
  36. return err
  37. }
  38. if resp.Entry == nil {
  39. // glog.V(3).Infof("read %s entry: %v", fullFilePath, entry)
  40. return nil
  41. }
  42. entry = resp.Entry
  43. return nil
  44. })
  45. return
  46. }
  47. type EachEntryFunciton func(entry *Entry, isLast bool) error
  48. func ReadDirAllEntries(filerClient FilerClient, fullDirPath util.FullPath, prefix string, fn EachEntryFunciton) (err error) {
  49. var counter uint32
  50. var startFrom string
  51. var counterFunc = func(entry *Entry, isLast bool) error {
  52. counter++
  53. startFrom = entry.Name
  54. return fn(entry, isLast)
  55. }
  56. var paginationLimit uint32 = 10000
  57. if err = doList(filerClient, fullDirPath, prefix, counterFunc, "", false, paginationLimit); err != nil {
  58. return err
  59. }
  60. for counter == paginationLimit {
  61. counter = 0
  62. if err = doList(filerClient, fullDirPath, prefix, counterFunc, startFrom, false, paginationLimit); err != nil {
  63. return err
  64. }
  65. }
  66. return nil
  67. }
  68. func List(filerClient FilerClient, parentDirectoryPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
  69. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  70. return doSeaweedList(client, util.FullPath(parentDirectoryPath), prefix, fn, startFrom, inclusive, limit)
  71. })
  72. }
  73. func doList(filerClient FilerClient, fullDirPath util.FullPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
  74. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  75. return doSeaweedList(client, fullDirPath, prefix, fn, startFrom, inclusive, limit)
  76. })
  77. }
  78. func SeaweedList(client SeaweedFilerClient, parentDirectoryPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
  79. return doSeaweedList(client, util.FullPath(parentDirectoryPath), prefix, fn, startFrom, inclusive, limit)
  80. }
  81. func doSeaweedList(client SeaweedFilerClient, fullDirPath util.FullPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
  82. // Redundancy limit to make it correctly judge whether it is the last file.
  83. redLimit := limit
  84. if limit != math.MaxInt32 && limit != 0{
  85. redLimit = limit + 1
  86. }
  87. request := &ListEntriesRequest{
  88. Directory: string(fullDirPath),
  89. Prefix: prefix,
  90. StartFromFileName: startFrom,
  91. Limit: redLimit,
  92. InclusiveStartFrom: inclusive,
  93. }
  94. glog.V(4).Infof("read directory: %v", request)
  95. ctx, cancel := context.WithCancel(context.Background())
  96. defer cancel()
  97. stream, err := client.ListEntries(ctx, request)
  98. if err != nil {
  99. return fmt.Errorf("list %s: %v", fullDirPath, err)
  100. }
  101. var prevEntry *Entry
  102. count := 0
  103. for {
  104. resp, recvErr := stream.Recv()
  105. if recvErr != nil {
  106. if recvErr == io.EOF {
  107. if prevEntry != nil {
  108. if err := fn(prevEntry, true); err != nil {
  109. return err
  110. }
  111. }
  112. break
  113. } else {
  114. return recvErr
  115. }
  116. }
  117. if prevEntry != nil {
  118. if err := fn(prevEntry, false); err != nil {
  119. return err
  120. }
  121. }
  122. prevEntry = resp.Entry
  123. count++
  124. if count > int(limit) && limit != 0 {
  125. prevEntry = nil
  126. }
  127. }
  128. return nil
  129. }
  130. func Exists(filerClient FilerClient, parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  131. err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  132. request := &LookupDirectoryEntryRequest{
  133. Directory: parentDirectoryPath,
  134. Name: entryName,
  135. }
  136. glog.V(4).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
  137. resp, err := LookupEntry(client, request)
  138. if err != nil {
  139. if err == ErrNotFound {
  140. exists = false
  141. return nil
  142. }
  143. glog.V(0).Infof("exists entry %v: %v", request, err)
  144. return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  145. }
  146. exists = resp.Entry.IsDirectory == isDirectory
  147. return nil
  148. })
  149. return
  150. }
  151. func Mkdir(filerClient FilerClient, parentDirectoryPath string, dirName string, fn func(entry *Entry)) error {
  152. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  153. entry := &Entry{
  154. Name: dirName,
  155. IsDirectory: true,
  156. Attributes: &FuseAttributes{
  157. Mtime: time.Now().Unix(),
  158. Crtime: time.Now().Unix(),
  159. FileMode: uint32(0777 | os.ModeDir),
  160. Uid: OS_UID,
  161. Gid: OS_GID,
  162. },
  163. }
  164. if fn != nil {
  165. fn(entry)
  166. }
  167. request := &CreateEntryRequest{
  168. Directory: parentDirectoryPath,
  169. Entry: entry,
  170. }
  171. glog.V(1).Infof("mkdir: %v", request)
  172. if err := CreateEntry(client, request); err != nil {
  173. glog.V(0).Infof("mkdir %v: %v", request, err)
  174. return fmt.Errorf("mkdir %s/%s: %v", parentDirectoryPath, dirName, err)
  175. }
  176. return nil
  177. })
  178. }
  179. func MkFile(filerClient FilerClient, parentDirectoryPath string, fileName string, chunks []*FileChunk) error {
  180. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  181. entry := &Entry{
  182. Name: fileName,
  183. IsDirectory: false,
  184. Attributes: &FuseAttributes{
  185. Mtime: time.Now().Unix(),
  186. Crtime: time.Now().Unix(),
  187. FileMode: uint32(0770),
  188. Uid: OS_UID,
  189. Gid: OS_GID,
  190. },
  191. Chunks: chunks,
  192. }
  193. request := &CreateEntryRequest{
  194. Directory: parentDirectoryPath,
  195. Entry: entry,
  196. }
  197. glog.V(1).Infof("create file: %s/%s", parentDirectoryPath, fileName)
  198. if err := CreateEntry(client, request); err != nil {
  199. glog.V(0).Infof("create file %v:%v", request, err)
  200. return fmt.Errorf("create file %s/%s: %v", parentDirectoryPath, fileName, err)
  201. }
  202. return nil
  203. })
  204. }
  205. func Remove(filerClient FilerClient, parentDirectoryPath, name string, isDeleteData, isRecursive, ignoreRecursiveErr, isFromOtherCluster bool, signatures []int32) error {
  206. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  207. deleteEntryRequest := &DeleteEntryRequest{
  208. Directory: parentDirectoryPath,
  209. Name: name,
  210. IsDeleteData: isDeleteData,
  211. IsRecursive: isRecursive,
  212. IgnoreRecursiveError: ignoreRecursiveErr,
  213. IsFromOtherCluster: isFromOtherCluster,
  214. Signatures: signatures,
  215. }
  216. if resp, err := client.DeleteEntry(context.Background(), deleteEntryRequest); err != nil {
  217. if strings.Contains(err.Error(), ErrNotFound.Error()) {
  218. return nil
  219. }
  220. return err
  221. } else {
  222. if resp.Error != "" {
  223. if strings.Contains(resp.Error, ErrNotFound.Error()) {
  224. return nil
  225. }
  226. return errors.New(resp.Error)
  227. }
  228. }
  229. return nil
  230. })
  231. }