2019-05-03 15:24:35 +08:00
|
|
|
package filer2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-12-13 16:23:05 +08:00
|
|
|
"io"
|
|
|
|
"math"
|
2019-05-03 15:24:35 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func VolumeId(fileId string) string {
|
|
|
|
lastCommaIndex := strings.LastIndex(fileId, ",")
|
|
|
|
if lastCommaIndex > 0 {
|
|
|
|
return fileId[:lastCommaIndex]
|
|
|
|
}
|
|
|
|
return fileId
|
|
|
|
}
|
|
|
|
|
|
|
|
type FilerClient interface {
|
2020-02-26 13:50:12 +08:00
|
|
|
WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error
|
2020-02-27 08:46:01 +08:00
|
|
|
AdjustedUrl(hostAndPort string) string
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
func GetEntry(filerClient FilerClient, fullFilePath FullPath) (entry *filer_pb.Entry, err error) {
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2020-01-20 15:59:46 +08:00
|
|
|
dir, name := fullFilePath.DirAndName()
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2019-05-03 15:24:35 +08:00
|
|
|
|
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
2020-01-25 16:31:53 +08:00
|
|
|
// glog.V(3).Infof("read %s request: %v", fullFilePath, request)
|
2020-03-08 08:51:46 +08:00
|
|
|
resp, err := filer_pb.LookupEntry(client, request)
|
2019-05-03 15:24:35 +08:00
|
|
|
if err != nil {
|
2020-03-08 09:01:39 +08:00
|
|
|
if err == filer_pb.ErrNotFound {
|
2019-05-04 05:12:51 +08:00
|
|
|
return nil
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
2020-01-25 16:31:53 +08:00
|
|
|
glog.V(3).Infof("read %s %v: %v", fullFilePath, resp, err)
|
2019-05-03 15:24:35 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-11 15:13:14 +08:00
|
|
|
if resp.Entry == nil {
|
2020-01-25 16:31:53 +08:00
|
|
|
// glog.V(3).Infof("read %s entry: %v", fullFilePath, entry)
|
2019-12-11 15:13:14 +08:00
|
|
|
return nil
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
|
|
|
|
2019-12-11 15:13:14 +08:00
|
|
|
entry = resp.Entry
|
2019-05-03 15:24:35 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
func ReadDirAllEntries(filerClient FilerClient, fullDirPath FullPath, prefix string, fn func(entry *filer_pb.Entry, isLast bool)) (err error) {
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2019-05-03 15:24:35 +08:00
|
|
|
|
|
|
|
lastEntryName := ""
|
|
|
|
|
2019-12-13 16:23:05 +08:00
|
|
|
request := &filer_pb.ListEntriesRequest{
|
2020-01-20 15:59:46 +08:00
|
|
|
Directory: string(fullDirPath),
|
2019-12-13 16:23:05 +08:00
|
|
|
Prefix: prefix,
|
|
|
|
StartFromFileName: lastEntryName,
|
|
|
|
Limit: math.MaxUint32,
|
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2019-12-13 16:23:05 +08:00
|
|
|
glog.V(3).Infof("read directory: %v", request)
|
2020-02-26 13:50:12 +08:00
|
|
|
stream, err := client.ListEntries(context.Background(), request)
|
2019-12-13 16:23:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("list %s: %v", fullDirPath, err)
|
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
|
2019-12-13 16:23:05 +08:00
|
|
|
var prevEntry *filer_pb.Entry
|
|
|
|
for {
|
|
|
|
resp, recvErr := stream.Recv()
|
|
|
|
if recvErr != nil {
|
|
|
|
if recvErr == io.EOF {
|
|
|
|
if prevEntry != nil {
|
|
|
|
fn(prevEntry, true)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
return recvErr
|
|
|
|
}
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
2019-12-13 16:23:05 +08:00
|
|
|
if prevEntry != nil {
|
|
|
|
fn(prevEntry, false)
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
2019-12-13 16:23:05 +08:00
|
|
|
prevEntry = resp.Entry
|
2019-05-03 15:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|