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.

50 lines
2.0 KiB

3 years ago
3 years ago
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. )
  9. func (entry *Entry) IsInRemoteOnly() bool {
  10. return len(entry.Chunks) == 0 && entry.Remote != nil && entry.Remote.RemoteSize > 0
  11. }
  12. func (f *Filer) ReadRemote(entry *Entry, offset int64, size int64) (data []byte, err error) {
  13. client, _, found := f.RemoteStorage.GetRemoteStorageClient(entry.Remote.StorageName)
  14. if !found {
  15. return nil, fmt.Errorf("remote storage %v not found", entry.Remote.StorageName)
  16. }
  17. mountDir, remoteLoation := f.RemoteStorage.FindMountDirectory(entry.FullPath)
  18. sourceLoc := MapFullPathToRemoteStorageLocation(mountDir, remoteLoation, entry.FullPath)
  19. return client.ReadFile(sourceLoc, offset, size)
  20. }
  21. func MapFullPathToRemoteStorageLocation(localMountedDir util.FullPath, remoteMountedLocation *remote_pb.RemoteStorageLocation, fp util.FullPath) *remote_pb.RemoteStorageLocation {
  22. remoteLocation := &remote_pb.RemoteStorageLocation{
  23. Name: remoteMountedLocation.Name,
  24. Bucket: remoteMountedLocation.Bucket,
  25. Path: remoteMountedLocation.Path,
  26. }
  27. remoteLocation.Path = string(util.FullPath(remoteLocation.Path).Child(string(fp)[len(localMountedDir):]))
  28. return remoteLocation
  29. }
  30. func MapRemoteStorageLocationPathToFullPath(localMountedDir util.FullPath, remoteMountedLocation *remote_pb.RemoteStorageLocation, remoteLocationPath string)(fp util.FullPath) {
  31. return localMountedDir.Child(remoteLocationPath[len(remoteMountedLocation.Path):])
  32. }
  33. func DownloadToLocal(filerClient filer_pb.FilerClient, remoteConf *remote_pb.RemoteConf, remoteLocation *remote_pb.RemoteStorageLocation, parent util.FullPath, entry *filer_pb.Entry) error {
  34. return filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  35. _, err := client.DownloadToLocal(context.Background(), &filer_pb.DownloadToLocalRequest{
  36. Directory: string(parent),
  37. Name: entry.Name,
  38. })
  39. return err
  40. })
  41. }