2020-03-23 07:01:34 +00:00
|
|
|
package filer_pb
|
2019-05-03 07:24:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-26 09:21:30 +00:00
|
|
|
"errors"
|
2019-05-03 07:24:35 +00:00
|
|
|
"fmt"
|
2019-12-13 08:23:05 +00:00
|
|
|
"io"
|
|
|
|
"math"
|
2020-03-23 07:30:02 +00:00
|
|
|
"os"
|
2020-08-14 07:22:21 +00:00
|
|
|
"strings"
|
2020-03-23 07:30:02 +00:00
|
|
|
"time"
|
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
|
|
|
)
|
|
|
|
|
2020-03-23 07:30:02 +00:00
|
|
|
var (
|
|
|
|
OS_UID = uint32(os.Getuid())
|
|
|
|
OS_GID = uint32(os.Getgid())
|
|
|
|
)
|
|
|
|
|
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
|
2020-02-27 00:46:01 +00:00
|
|
|
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-04-30 00:40:08 +00:00
|
|
|
type EachEntryFunciton func(entry *Entry, isLast bool) error
|
|
|
|
|
|
|
|
func ReadDirAllEntries(filerClient FilerClient, fullDirPath util.FullPath, prefix string, fn EachEntryFunciton) (err error) {
|
2019-05-03 07:24:35 +00:00
|
|
|
|
2020-03-23 07:30:02 +00:00
|
|
|
return doList(filerClient, fullDirPath, prefix, fn, "", false, math.MaxUint32)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-30 00:40:08 +00:00
|
|
|
func List(filerClient FilerClient, parentDirectoryPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
|
2020-03-23 07:30:02 +00:00
|
|
|
|
|
|
|
return doList(filerClient, util.FullPath(parentDirectoryPath), prefix, fn, startFrom, inclusive, limit)
|
|
|
|
|
|
|
|
}
|
2019-05-03 07:24:35 +00:00
|
|
|
|
2020-04-30 00:40:08 +00:00
|
|
|
func doList(filerClient FilerClient, fullDirPath util.FullPath, prefix string, fn EachEntryFunciton, startFrom string, inclusive bool, limit uint32) (err error) {
|
2020-03-23 07:30:02 +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 := &ListEntriesRequest{
|
2020-03-23 07:30:02 +00:00
|
|
|
Directory: string(fullDirPath),
|
|
|
|
Prefix: prefix,
|
|
|
|
StartFromFileName: startFrom,
|
|
|
|
Limit: limit,
|
|
|
|
InclusiveStartFrom: inclusive,
|
2019-12-13 08:23:05 +00:00
|
|
|
}
|
2019-05-03 07:24:35 +00:00
|
|
|
|
2020-08-16 02:55:28 +00:00
|
|
|
glog.V(5).Infof("read directory: %v", request)
|
2020-05-17 15:56:33 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
stream, err := client.ListEntries(ctx, request)
|
2019-12-13 08:23:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("list %s: %v", fullDirPath, err)
|
|
|
|
}
|
2020-05-17 15:56:33 +00:00
|
|
|
defer cancel()
|
2019-05-03 07:24:35 +00:00
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
var prevEntry *Entry
|
2019-12-13 08:23:05 +00:00
|
|
|
for {
|
|
|
|
resp, recvErr := stream.Recv()
|
|
|
|
if recvErr != nil {
|
|
|
|
if recvErr == io.EOF {
|
|
|
|
if prevEntry != nil {
|
2020-04-30 00:40:08 +00:00
|
|
|
if err := fn(prevEntry, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 08:23:05 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
return recvErr
|
|
|
|
}
|
2019-05-03 07:24:35 +00:00
|
|
|
}
|
2019-12-13 08:23:05 +00:00
|
|
|
if prevEntry != nil {
|
2020-04-30 00:40:08 +00:00
|
|
|
if err := fn(prevEntry, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-03 07:24:35 +00:00
|
|
|
}
|
2019-12-13 08:23:05 +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
|
|
|
|
}
|
2020-03-23 07:30:02 +00:00
|
|
|
|
|
|
|
func Mkdir(filerClient FilerClient, parentDirectoryPath string, dirName string, fn func(entry *Entry)) error {
|
|
|
|
return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
entry := &Entry{
|
|
|
|
Name: dirName,
|
|
|
|
IsDirectory: true,
|
|
|
|
Attributes: &FuseAttributes{
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(0777 | os.ModeDir),
|
|
|
|
Uid: OS_UID,
|
|
|
|
Gid: OS_GID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn != nil {
|
|
|
|
fn(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &CreateEntryRequest{
|
|
|
|
Directory: parentDirectoryPath,
|
|
|
|
Entry: entry,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("mkdir: %v", request)
|
|
|
|
if err := CreateEntry(client, request); err != nil {
|
|
|
|
glog.V(0).Infof("mkdir %v: %v", request, err)
|
|
|
|
return fmt.Errorf("mkdir %s/%s: %v", parentDirectoryPath, dirName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func MkFile(filerClient FilerClient, parentDirectoryPath string, fileName string, chunks []*FileChunk) error {
|
|
|
|
return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
entry := &Entry{
|
|
|
|
Name: fileName,
|
|
|
|
IsDirectory: false,
|
|
|
|
Attributes: &FuseAttributes{
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(0770),
|
|
|
|
Uid: OS_UID,
|
|
|
|
Gid: OS_GID,
|
|
|
|
},
|
|
|
|
Chunks: chunks,
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &CreateEntryRequest{
|
|
|
|
Directory: parentDirectoryPath,
|
|
|
|
Entry: entry,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("create file: %s/%s", parentDirectoryPath, fileName)
|
|
|
|
if err := CreateEntry(client, request); err != nil {
|
|
|
|
glog.V(0).Infof("create file %v:%v", request, err)
|
|
|
|
return fmt.Errorf("create file %s/%s: %v", parentDirectoryPath, fileName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2020-03-23 08:14:21 +00:00
|
|
|
|
2020-07-01 05:53:53 +00:00
|
|
|
func Remove(filerClient FilerClient, parentDirectoryPath, name string, isDeleteData, isRecursive, ignoreRecursiveErr, isFromOtherCluster bool) error {
|
2020-03-23 08:14:21 +00:00
|
|
|
return filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
|
|
|
|
|
2020-03-26 09:21:30 +00:00
|
|
|
if resp, err := client.DeleteEntry(context.Background(), &DeleteEntryRequest{
|
2020-03-23 08:14:21 +00:00
|
|
|
Directory: parentDirectoryPath,
|
|
|
|
Name: name,
|
|
|
|
IsDeleteData: isDeleteData,
|
|
|
|
IsRecursive: isRecursive,
|
|
|
|
IgnoreRecursiveError: ignoreRecursiveErr,
|
2020-07-01 05:53:53 +00:00
|
|
|
IsFromOtherCluster: isFromOtherCluster,
|
2020-03-23 08:14:21 +00:00
|
|
|
}); err != nil {
|
2020-08-14 07:23:01 +00:00
|
|
|
if strings.Contains(err.Error(), ErrNotFound.Error()){
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-23 08:14:21 +00:00
|
|
|
return err
|
2020-03-26 09:21:30 +00:00
|
|
|
} else {
|
|
|
|
if resp.Error != "" {
|
2020-08-14 07:23:01 +00:00
|
|
|
if strings.Contains(resp.Error, ErrNotFound.Error()){
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-26 09:21:30 +00:00
|
|
|
return errors.New(resp.Error)
|
|
|
|
}
|
2020-03-23 08:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|