filer_client.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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(streamingMode bool, 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(false, 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. glog.V(3).Infof("read %s %v: %v", fullFilePath, resp, err)
  33. return err
  34. }
  35. if resp.Entry == nil {
  36. // glog.V(3).Infof("read %s entry: %v", fullFilePath, entry)
  37. return nil
  38. }
  39. entry = resp.Entry
  40. return nil
  41. })
  42. return
  43. }
  44. type EachEntryFunciton func(entry *Entry, isLast bool) error
  45. func ReadDirAllEntries(filerClient FilerClient, fullDirPath util.FullPath, prefix string, fn EachEntryFunciton) (err error) {
  46. var counter uint32
  47. var startFrom string
  48. var counterFunc = func(entry *Entry, isLast bool) error {
  49. counter++
  50. startFrom = entry.Name
  51. return fn(entry, isLast)
  52. }
  53. var paginationLimit uint32 = 10000
  54. if err = doList(filerClient, fullDirPath, prefix, counterFunc, "", false, paginationLimit); err != nil {
  55. return err
  56. }
  57. for counter == paginationLimit {
  58. counter = 0
  59. if err = doList(filerClient, fullDirPath, prefix, counterFunc, startFrom, false, paginationLimit); err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. func List(filerClient FilerClient, parentDirectoryPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
  66. return filerClient.WithFilerClient(false, func(client SeaweedFilerClient) error {
  67. return doSeaweedList(client, util.FullPath(parentDirectoryPath), prefix, fn, startFrom, inclusive, limit)
  68. })
  69. }
  70. func doList(filerClient FilerClient, fullDirPath util.FullPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
  71. return filerClient.WithFilerClient(false, func(client SeaweedFilerClient) error {
  72. return doSeaweedList(client, fullDirPath, prefix, fn, startFrom, inclusive, limit)
  73. })
  74. }
  75. func SeaweedList(client SeaweedFilerClient, parentDirectoryPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
  76. return doSeaweedList(client, util.FullPath(parentDirectoryPath), prefix, fn, startFrom, inclusive, limit)
  77. }
  78. func doSeaweedList(client SeaweedFilerClient, fullDirPath util.FullPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
  79. // Redundancy limit to make it correctly judge whether it is the last file.
  80. redLimit := limit
  81. if limit < math.MaxInt32 && limit != 0 {
  82. redLimit = limit + 1
  83. }
  84. if redLimit > math.MaxInt32 {
  85. redLimit = math.MaxInt32
  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(false, 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 Touch(filerClient FilerClient, parentDirectoryPath string, entryName string, entry *Entry) (err error) {
  152. return filerClient.WithFilerClient(false, func(client SeaweedFilerClient) error {
  153. request := &UpdateEntryRequest{
  154. Directory: parentDirectoryPath,
  155. Entry: entry,
  156. }
  157. glog.V(4).Infof("touch entry %v/%v: %v", parentDirectoryPath, entryName, request)
  158. if err := UpdateEntry(client, request); err != nil {
  159. glog.V(0).Infof("touch exists entry %v: %v", request, err)
  160. return fmt.Errorf("touch exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  161. }
  162. return nil
  163. })
  164. }
  165. func Mkdir(filerClient FilerClient, parentDirectoryPath string, dirName string, fn func(entry *Entry)) error {
  166. return filerClient.WithFilerClient(false, func(client SeaweedFilerClient) error {
  167. return DoMkdir(client, parentDirectoryPath, dirName, fn)
  168. })
  169. }
  170. func DoMkdir(client SeaweedFilerClient, parentDirectoryPath string, dirName string, fn func(entry *Entry)) error {
  171. entry := &Entry{
  172. Name: dirName,
  173. IsDirectory: true,
  174. Attributes: &FuseAttributes{
  175. Mtime: time.Now().Unix(),
  176. Crtime: time.Now().Unix(),
  177. FileMode: uint32(0777 | os.ModeDir),
  178. Uid: OS_UID,
  179. Gid: OS_GID,
  180. },
  181. }
  182. if fn != nil {
  183. fn(entry)
  184. }
  185. request := &CreateEntryRequest{
  186. Directory: parentDirectoryPath,
  187. Entry: entry,
  188. }
  189. glog.V(1).Infof("mkdir: %v", request)
  190. if err := CreateEntry(client, request); err != nil {
  191. glog.V(0).Infof("mkdir %v: %v", request, err)
  192. return fmt.Errorf("mkdir %s/%s: %v", parentDirectoryPath, dirName, err)
  193. }
  194. return nil
  195. }
  196. func MkFile(filerClient FilerClient, parentDirectoryPath string, fileName string, chunks []*FileChunk, fn func(entry *Entry)) error {
  197. return filerClient.WithFilerClient(false, func(client SeaweedFilerClient) error {
  198. entry := &Entry{
  199. Name: fileName,
  200. IsDirectory: false,
  201. Attributes: &FuseAttributes{
  202. Mtime: time.Now().Unix(),
  203. Crtime: time.Now().Unix(),
  204. FileMode: uint32(0770),
  205. Uid: OS_UID,
  206. Gid: OS_GID,
  207. },
  208. Chunks: chunks,
  209. }
  210. if fn != nil {
  211. fn(entry)
  212. }
  213. request := &CreateEntryRequest{
  214. Directory: parentDirectoryPath,
  215. Entry: entry,
  216. }
  217. glog.V(1).Infof("create file: %s/%s", parentDirectoryPath, fileName)
  218. if err := CreateEntry(client, request); err != nil {
  219. glog.V(0).Infof("create file %v:%v", request, err)
  220. return fmt.Errorf("create file %s/%s: %v", parentDirectoryPath, fileName, err)
  221. }
  222. return nil
  223. })
  224. }
  225. func Remove(filerClient FilerClient, parentDirectoryPath, name string, isDeleteData, isRecursive, ignoreRecursiveErr, isFromOtherCluster bool, signatures []int32) error {
  226. return filerClient.WithFilerClient(false, func(client SeaweedFilerClient) error {
  227. return DoRemove(client, parentDirectoryPath, name, isDeleteData, isRecursive, ignoreRecursiveErr, isFromOtherCluster, signatures)
  228. })
  229. }
  230. func DoRemove(client SeaweedFilerClient, parentDirectoryPath string, name string, isDeleteData bool, isRecursive bool, ignoreRecursiveErr bool, isFromOtherCluster bool, signatures []int32) error {
  231. deleteEntryRequest := &DeleteEntryRequest{
  232. Directory: parentDirectoryPath,
  233. Name: name,
  234. IsDeleteData: isDeleteData,
  235. IsRecursive: isRecursive,
  236. IgnoreRecursiveError: ignoreRecursiveErr,
  237. IsFromOtherCluster: isFromOtherCluster,
  238. Signatures: signatures,
  239. }
  240. if resp, err := client.DeleteEntry(context.Background(), deleteEntryRequest); err != nil {
  241. if strings.Contains(err.Error(), ErrNotFound.Error()) {
  242. return nil
  243. }
  244. return err
  245. } else {
  246. if resp.Error != "" {
  247. if strings.Contains(resp.Error, ErrNotFound.Error()) {
  248. return nil
  249. }
  250. return errors.New(resp.Error)
  251. }
  252. }
  253. return nil
  254. }