aboutsummaryrefslogtreecommitdiff
path: root/weed/filer/read_remote.go
blob: 77ca81f15b5a00f27f63880b24ec51fc058898db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package filer

import (
	"context"
	"fmt"
	"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
	"github.com/chrislusf/seaweedfs/weed/util"
)

func (entry *Entry) IsInRemoteOnly() bool {
	return len(entry.Chunks) == 0 && entry.Remote != nil && entry.Remote.RemoteSize > 0
}

func (f *Filer) ReadRemote(entry *Entry, offset int64, size int64) (data []byte, err error) {
	client, _, found := f.RemoteStorage.GetRemoteStorageClient(entry.Remote.StorageName)
	if !found {
		return nil, fmt.Errorf("remote storage %v not found", entry.Remote.StorageName)
	}

	mountDir, remoteLoation := f.RemoteStorage.FindMountDirectory(entry.FullPath)

	sourceLoc := MapFullPathToRemoteStorageLocation(mountDir, remoteLoation, entry.FullPath)

	return client.ReadFile(sourceLoc, offset, size)
}

func MapFullPathToRemoteStorageLocation(localMountedDir util.FullPath, remoteMountedLocation *filer_pb.RemoteStorageLocation, fp util.FullPath) *filer_pb.RemoteStorageLocation {
	remoteLocation := &filer_pb.RemoteStorageLocation{
		Name:   remoteMountedLocation.Name,
		Bucket: remoteMountedLocation.Bucket,
		Path:   remoteMountedLocation.Path,
	}
	remoteLocation.Path += string(fp)[len(localMountedDir):]
	return remoteLocation
}

func DownloadToLocal(filerClient filer_pb.FilerClient, remoteConf *filer_pb.RemoteConf, remoteLocation *filer_pb.RemoteStorageLocation, parent util.FullPath, entry *filer_pb.Entry) error {
	return filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
		_, err := client.DownloadToLocal(context.Background(), &filer_pb.DownloadToLocalRequest{
			Directory: string(parent),
			Name:      entry.Name,
		})
		return err
	})
}