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.

305 lines
8.3 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
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
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. 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(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(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. request := &ListEntriesRequest{
  85. Directory: string(fullDirPath),
  86. Prefix: prefix,
  87. StartFromFileName: startFrom,
  88. Limit: redLimit,
  89. InclusiveStartFrom: inclusive,
  90. }
  91. glog.V(4).Infof("read directory: %v", request)
  92. ctx, cancel := context.WithCancel(context.Background())
  93. defer cancel()
  94. stream, err := client.ListEntries(ctx, request)
  95. if err != nil {
  96. return fmt.Errorf("list %s: %v", fullDirPath, err)
  97. }
  98. var prevEntry *Entry
  99. count := 0
  100. for {
  101. resp, recvErr := stream.Recv()
  102. if recvErr != nil {
  103. if recvErr == io.EOF {
  104. if prevEntry != nil {
  105. if err := fn(prevEntry, true); err != nil {
  106. return err
  107. }
  108. }
  109. break
  110. } else {
  111. return recvErr
  112. }
  113. }
  114. if prevEntry != nil {
  115. if err := fn(prevEntry, false); err != nil {
  116. return err
  117. }
  118. }
  119. prevEntry = resp.Entry
  120. count++
  121. if count > int(limit) && limit != 0 {
  122. prevEntry = nil
  123. }
  124. }
  125. return nil
  126. }
  127. func Exists(filerClient FilerClient, parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  128. err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  129. request := &LookupDirectoryEntryRequest{
  130. Directory: parentDirectoryPath,
  131. Name: entryName,
  132. }
  133. glog.V(4).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
  134. resp, err := LookupEntry(client, request)
  135. if err != nil {
  136. if err == ErrNotFound {
  137. exists = false
  138. return nil
  139. }
  140. glog.V(0).Infof("exists entry %v: %v", request, err)
  141. return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  142. }
  143. exists = resp.Entry.IsDirectory == isDirectory
  144. return nil
  145. })
  146. return
  147. }
  148. func Touch(filerClient FilerClient, parentDirectoryPath string, entryName string, entry *Entry) (err error) {
  149. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  150. request := &UpdateEntryRequest{
  151. Directory: parentDirectoryPath,
  152. Entry: entry,
  153. }
  154. glog.V(4).Infof("touch entry %v/%v: %v", parentDirectoryPath, entryName, request)
  155. if err := UpdateEntry(client, request); err != nil {
  156. glog.V(0).Infof("touch exists entry %v: %v", request, err)
  157. return fmt.Errorf("touch exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  158. }
  159. return nil
  160. })
  161. }
  162. func Mkdir(filerClient FilerClient, parentDirectoryPath string, dirName string, fn func(entry *Entry)) error {
  163. return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  164. return DoMkdir(client, parentDirectoryPath, dirName, fn)
  165. })
  166. }
  167. func DoMkdir(client SeaweedFilerClient, parentDirectoryPath string, dirName string, fn func(entry *Entry)) error {
  168. entry := &Entry{
  169. Name: dirName,
  170. IsDirectory: true,
  171. Attributes: &FuseAttributes{
  172. Mtime: time.Now().Unix(),
  173. Crtime: time.Now().Unix(),
  174. FileMode: uint32(0777 | os.ModeDir),
  175. Uid: OS_UID,
  176. Gid: OS_GID,
  177. },
  178. }
  179. if fn != nil {
  180. fn(entry)
  181. }
  182. request := &CreateEntryRequest{
  183. Directory: parentDirectoryPath,
  184. Entry: entry,
  185. }
  186. glog.V(1).Infof("mkdir: %v", request)
  187. if err := CreateEntry(client, request); err != nil {
  188. glog.V(0).Infof("mkdir %v: %v", request, err)
  189. return fmt.Errorf("mkdir %s/%s: %v", parentDirectoryPath, dirName, err)
  190. }
  191. return nil
  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. return DoRemove(client, parentDirectoryPath, name, isDeleteData, isRecursive, ignoreRecursiveErr, isFromOtherCluster, signatures)
  225. })
  226. }
  227. func DoRemove(client SeaweedFilerClient, parentDirectoryPath string, name string, isDeleteData bool, isRecursive bool, ignoreRecursiveErr bool, isFromOtherCluster bool, signatures []int32) error {
  228. deleteEntryRequest := &DeleteEntryRequest{
  229. Directory: parentDirectoryPath,
  230. Name: name,
  231. IsDeleteData: isDeleteData,
  232. IsRecursive: isRecursive,
  233. IgnoreRecursiveError: ignoreRecursiveErr,
  234. IsFromOtherCluster: isFromOtherCluster,
  235. Signatures: signatures,
  236. }
  237. if resp, err := client.DeleteEntry(context.Background(), deleteEntryRequest); err != nil {
  238. if strings.Contains(err.Error(), ErrNotFound.Error()) {
  239. return nil
  240. }
  241. return err
  242. } else {
  243. if resp.Error != "" {
  244. if strings.Contains(resp.Error, ErrNotFound.Error()) {
  245. return nil
  246. }
  247. return errors.New(resp.Error)
  248. }
  249. }
  250. return nil
  251. }