seaweedfs/weed/pb/filer_pb/filer_client.go

123 lines
2.6 KiB
Go
Raw Normal View History

2020-03-23 07:01:34 +00:00
package filer_pb
2019-05-03 07:24:35 +00:00
import (
"context"
"fmt"
"io"
"math"
2019-05-03 07:24:35 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
2020-03-23 07:01:34 +00:00
"github.com/chrislusf/seaweedfs/weed/util"
2019-05-03 07:24:35 +00:00
)
type FilerClient interface {
2020-03-23 07:01:34 +00:00
WithFilerClient(fn func(SeaweedFilerClient) error) error
AdjustedUrl(hostAndPort string) string
2019-05-03 07:24:35 +00:00
}
2020-03-23 07:01:34 +00:00
func GetEntry(filerClient FilerClient, fullFilePath util.FullPath) (entry *Entry, err error) {
2019-05-03 07:24:35 +00:00
2020-01-20 07:59:46 +00:00
dir, name := fullFilePath.DirAndName()
2019-05-03 07:24:35 +00:00
2020-03-23 07:01:34 +00:00
err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
2019-05-03 07:24:35 +00:00
2020-03-23 07:01:34 +00:00
request := &LookupDirectoryEntryRequest{
2019-05-03 07:24:35 +00:00
Directory: dir,
Name: name,
}
2020-01-25 08:31:53 +00:00
// glog.V(3).Infof("read %s request: %v", fullFilePath, request)
2020-03-23 07:01:34 +00:00
resp, err := LookupEntry(client, request)
2019-05-03 07:24:35 +00:00
if err != nil {
2020-03-23 07:01:34 +00:00
if err == ErrNotFound {
2019-05-03 21:12:51 +00:00
return nil
2019-05-03 07:24:35 +00:00
}
2020-01-25 08:31:53 +00:00
glog.V(3).Infof("read %s %v: %v", fullFilePath, resp, err)
2019-05-03 07:24:35 +00:00
return err
}
2019-12-11 07:13:14 +00:00
if resp.Entry == nil {
2020-01-25 08:31:53 +00:00
// glog.V(3).Infof("read %s entry: %v", fullFilePath, entry)
2019-12-11 07:13:14 +00:00
return nil
2019-05-03 07:24:35 +00:00
}
2019-12-11 07:13:14 +00:00
entry = resp.Entry
2019-05-03 07:24:35 +00:00
return nil
})
return
}
2020-03-23 07:01:34 +00:00
func ReadDirAllEntries(filerClient FilerClient, fullDirPath util.FullPath, prefix string, fn func(entry *Entry, isLast bool)) (err error) {
2019-05-03 07:24:35 +00:00
2020-03-23 07:01:34 +00:00
err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
2019-05-03 07:24:35 +00:00
lastEntryName := ""
2020-03-23 07:01:34 +00:00
request := &ListEntriesRequest{
2020-01-20 07:59:46 +00:00
Directory: string(fullDirPath),
Prefix: prefix,
StartFromFileName: lastEntryName,
Limit: math.MaxUint32,
}
2019-05-03 07:24:35 +00:00
glog.V(3).Infof("read directory: %v", request)
stream, err := client.ListEntries(context.Background(), request)
if err != nil {
return fmt.Errorf("list %s: %v", fullDirPath, err)
}
2019-05-03 07:24:35 +00:00
2020-03-23 07:01:34 +00:00
var prevEntry *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 07:24:35 +00:00
}
if prevEntry != nil {
fn(prevEntry, false)
2019-05-03 07:24:35 +00:00
}
prevEntry = resp.Entry
2019-05-03 07:24:35 +00:00
}
return nil
})
return
}
2020-03-23 07:06:24 +00:00
func Exists(filerClient FilerClient, parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
request := &LookupDirectoryEntryRequest{
Directory: parentDirectoryPath,
Name: entryName,
}
glog.V(4).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
resp, err := LookupEntry(client, request)
if err != nil {
if err == ErrNotFound {
exists = false
return nil
}
glog.V(0).Infof("exists entry %v: %v", request, err)
return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
}
exists = resp.Entry.IsDirectory == isDirectory
return nil
})
return
}