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.

81 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. IsGzipped: uploadResult.Gzip > 0,
  42. }
  43. entry.Chunks = append(entry.Chunks, chunk)
  44. // update the entry
  45. err = f.CreateEntry(context.Background(), entry, false)
  46. return err
  47. }
  48. func (f *Filer) assignAndUpload(data []byte) (*operation.AssignResult, *operation.UploadResult, error) {
  49. // assign a volume location
  50. assignRequest := &operation.VolumeAssignRequest{
  51. Count: 1,
  52. Collection: f.metaLogCollection,
  53. Replication: f.metaLogReplication,
  54. WritableVolumeCount: 1,
  55. }
  56. assignResult, err := operation.Assign(f.GetMaster(), f.GrpcDialOption, assignRequest)
  57. if err != nil {
  58. return nil, nil, fmt.Errorf("AssignVolume: %v", err)
  59. }
  60. if assignResult.Error != "" {
  61. return nil, nil, fmt.Errorf("AssignVolume error: %v", assignResult.Error)
  62. }
  63. // upload data
  64. targetUrl := "http://" + assignResult.Url + "/" + assignResult.Fid
  65. uploadResult, err := operation.UploadData(targetUrl, "", f.Cipher, data, false, "", nil, assignResult.Auth)
  66. if err != nil {
  67. return nil, nil, fmt.Errorf("upload data %s: %v", targetUrl, err)
  68. }
  69. // println("uploaded to", targetUrl)
  70. return assignResult, uploadResult, nil
  71. }