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.

82 lines
2.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package filer2
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/operation"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func (f *Filer) appendToFile(targetFile string, data []byte) error {
  12. assignResult, uploadResult, err2 := f.assignAndUpload(data)
  13. if err2 != nil {
  14. return err2
  15. }
  16. // find out existing entry
  17. fullpath := util.FullPath(targetFile)
  18. entry, err := f.FindEntry(context.Background(), fullpath)
  19. var offset int64 = 0
  20. if err == filer_pb.ErrNotFound {
  21. entry = &Entry{
  22. FullPath: fullpath,
  23. Attr: Attr{
  24. Crtime: time.Now(),
  25. Mtime: time.Now(),
  26. Mode: os.FileMode(0644),
  27. Uid: OS_UID,
  28. Gid: OS_GID,
  29. },
  30. }
  31. } else {
  32. offset = int64(TotalSize(entry.Chunks))
  33. }
  34. // append to existing chunks
  35. chunk := &filer_pb.FileChunk{
  36. FileId: assignResult.Fid,
  37. Offset: offset,
  38. Size: uint64(uploadResult.Size),
  39. Mtime: time.Now().UnixNano(),
  40. ETag: uploadResult.ETag,
  41. CipherKey: uploadResult.CipherKey,
  42. IsGzipped: uploadResult.Gzip > 0,
  43. }
  44. entry.Chunks = append(entry.Chunks, chunk)
  45. // update the entry
  46. err = f.CreateEntry(context.Background(), entry, false)
  47. return err
  48. }
  49. func (f *Filer) assignAndUpload(data []byte) (*operation.AssignResult, *operation.UploadResult, error) {
  50. // assign a volume location
  51. assignRequest := &operation.VolumeAssignRequest{
  52. Count: 1,
  53. Collection: f.metaLogCollection,
  54. Replication: f.metaLogReplication,
  55. WritableVolumeCount: 1,
  56. }
  57. assignResult, err := operation.Assign(f.GetMaster(), f.GrpcDialOption, assignRequest)
  58. if err != nil {
  59. return nil, nil, fmt.Errorf("AssignVolume: %v", err)
  60. }
  61. if assignResult.Error != "" {
  62. return nil, nil, fmt.Errorf("AssignVolume error: %v", assignResult.Error)
  63. }
  64. // upload data
  65. targetUrl := "http://" + assignResult.Url + "/" + assignResult.Fid
  66. uploadResult, err := operation.UploadData(targetUrl, "", f.Cipher, data, false, "", nil, assignResult.Auth)
  67. if err != nil {
  68. return nil, nil, fmt.Errorf("upload data %s: %v", targetUrl, err)
  69. }
  70. // println("uploaded to", targetUrl)
  71. return assignResult, uploadResult, nil
  72. }