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.

70 lines
1.7 KiB

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. // assign a volume location
  13. assignRequest := &operation.VolumeAssignRequest{
  14. Count: 1,
  15. }
  16. assignResult, err := operation.Assign(f.GetMaster(), f.GrpcDialOption, assignRequest)
  17. if err != nil {
  18. return fmt.Errorf("AssignVolume: %v", err)
  19. }
  20. if assignResult.Error != "" {
  21. return fmt.Errorf("AssignVolume error: %v", assignResult.Error)
  22. }
  23. // upload data
  24. targetUrl := "http://" + assignResult.Url + "/" + assignResult.Fid
  25. uploadResult, err := operation.UploadData(targetUrl, "", false, data, false, "", nil, assignResult.Auth)
  26. if err != nil {
  27. return fmt.Errorf("upload data %s: %v", targetUrl, err)
  28. }
  29. // println("uploaded to", targetUrl)
  30. // find out existing entry
  31. fullpath := util.FullPath(targetFile)
  32. entry, err := f.FindEntry(context.Background(), fullpath)
  33. var offset int64 = 0
  34. if err == filer_pb.ErrNotFound {
  35. entry = &Entry{
  36. FullPath: fullpath,
  37. Attr: Attr{
  38. Crtime: time.Now(),
  39. Mtime: time.Now(),
  40. Mode: os.FileMode(0644),
  41. Uid: OS_UID,
  42. Gid: OS_GID,
  43. },
  44. }
  45. } else {
  46. offset = int64(TotalSize(entry.Chunks))
  47. }
  48. // append to existing chunks
  49. chunk := &filer_pb.FileChunk{
  50. FileId: assignResult.Fid,
  51. Offset: offset,
  52. Size: uint64(uploadResult.Size),
  53. Mtime: time.Now().UnixNano(),
  54. ETag: uploadResult.ETag,
  55. IsGzipped: uploadResult.Gzip > 0,
  56. }
  57. entry.Chunks = append(entry.Chunks, chunk)
  58. // update the entry
  59. err = f.CreateEntry(context.Background(), entry, false)
  60. return err
  61. }