You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
7.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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 Touch(filerClient FilerClient, parentDirectoryPath string, entryName string, entry *Entry) (err error) {
  152. return filerClient.WithFilerClient(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(func(client SeaweedFilerClient) error {
  167. entry := &Entry{
  168. Name: dirName,
  169. IsDirectory: true,
  170. Attributes: &FuseAttributes{
  171. Mtime: time.Now().Unix(),
  172. Crtime: time.Now().Unix(),
  173. FileMode: uint32(0777 | os.ModeDir),
  174. Uid: OS_UID,
  175. Gid: OS_GID,
  176. },
  177. }
  178. if fn != nil {
  179. fn(entry)
  180. }
  181. request := &CreateEntryRequest{
  182. Directory: parentDirectoryPath,
  183. Entry: entry,
  184. }
  185. glog.V(1).Infof("mkdir: %v", request)
  186. if err := CreateEntry(client, request); err != nil {
  187. glog.V(0).Infof("mkdir %v: %v", request, err)
  188. return fmt.Errorf("mkdir %s/%s: %v", parentDirectoryPath, dirName, err)
  189. }
  190. return nil
  191. })
  192. }
  193. func MkFile(filerClient FilerClient, parentDirectoryPath string, fileName string, chunks []*FileChunk, fn func(entry *Entry)) error {
  194. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  195. entry := &Entry{
  196. Name: fileName,
  197. IsDirectory: false,
  198. Attributes: &FuseAttributes{
  199. Mtime: time.Now().Unix(),
  200. Crtime: time.Now().Unix(),
  201. FileMode: uint32(0770),
  202. Uid: OS_UID,
  203. Gid: OS_GID,
  204. },
  205. Chunks: chunks,
  206. }
  207. if fn != nil {
  208. fn(entry)
  209. }
  210. request := &CreateEntryRequest{
  211. Directory: parentDirectoryPath,
  212. Entry: entry,
  213. }
  214. glog.V(1).Infof("create file: %s/%s", parentDirectoryPath, fileName)
  215. if err := CreateEntry(client, request); err != nil {
  216. glog.V(0).Infof("create file %v:%v", request, err)
  217. return fmt.Errorf("create file %s/%s: %v", parentDirectoryPath, fileName, err)
  218. }
  219. return nil
  220. })
  221. }
  222. func Remove(filerClient FilerClient, parentDirectoryPath, name string, isDeleteData, isRecursive, ignoreRecursiveErr, isFromOtherCluster bool, signatures []int32) error {
  223. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  224. deleteEntryRequest := &DeleteEntryRequest{
  225. Directory: parentDirectoryPath,
  226. Name: name,
  227. IsDeleteData: isDeleteData,
  228. IsRecursive: isRecursive,
  229. IgnoreRecursiveError: ignoreRecursiveErr,
  230. IsFromOtherCluster: isFromOtherCluster,
  231. Signatures: signatures,
  232. }
  233. if resp, err := client.DeleteEntry(context.Background(), deleteEntryRequest); err != nil {
  234. if strings.Contains(err.Error(), ErrNotFound.Error()) {
  235. return nil
  236. }
  237. return err
  238. } else {
  239. if resp.Error != "" {
  240. if strings.Contains(resp.Error, ErrNotFound.Error()) {
  241. return nil
  242. }
  243. return errors.New(resp.Error)
  244. }
  245. }
  246. return nil
  247. })
  248. }