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.

309 lines
8.4 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
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
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/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/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. }