2019-05-17 17:03:23 +08:00
|
|
|
package filer_pb
|
|
|
|
|
|
|
|
import (
|
2020-01-26 01:17:19 +08:00
|
|
|
"context"
|
2020-03-08 09:01:39 +08:00
|
|
|
"errors"
|
2020-01-26 01:17:19 +08:00
|
|
|
"fmt"
|
2020-03-08 08:51:46 +08:00
|
|
|
"strings"
|
2020-01-26 01:17:19 +08:00
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2019-05-17 17:03:23 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
|
|
|
)
|
|
|
|
|
2020-08-16 10:55:28 +08:00
|
|
|
func ToFileIdObject(fileIdStr string) (*FileId, error) {
|
2019-05-17 17:03:23 +08:00
|
|
|
t, err := needle.ParseFileIdFromString(fileIdStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &FileId{
|
|
|
|
VolumeId: uint32(t.VolumeId),
|
|
|
|
Cookie: uint32(t.Cookie),
|
|
|
|
FileKey: uint64(t.Key),
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:30:08 +08:00
|
|
|
func (fid *FileId) toFileIdString() string {
|
2019-05-17 17:03:23 +08:00
|
|
|
return needle.NewFileId(needle.VolumeId(fid.VolumeId), fid.FileKey, fid.Cookie).String()
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:30:08 +08:00
|
|
|
func (c *FileChunk) GetFileIdString() string {
|
|
|
|
if c.FileId != "" {
|
|
|
|
return c.FileId
|
|
|
|
}
|
|
|
|
if c.Fid != nil {
|
|
|
|
c.FileId = c.Fid.toFileIdString()
|
|
|
|
return c.FileId
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-05-17 17:03:23 +08:00
|
|
|
func BeforeEntrySerialization(chunks []*FileChunk) {
|
|
|
|
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
|
|
|
|
if chunk.FileId != "" {
|
2020-08-16 10:55:28 +08:00
|
|
|
if fid, err := ToFileIdObject(chunk.FileId); err == nil {
|
2019-05-17 17:03:23 +08:00
|
|
|
chunk.Fid = fid
|
|
|
|
chunk.FileId = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if chunk.SourceFileId != "" {
|
2020-08-16 10:55:28 +08:00
|
|
|
if fid, err := ToFileIdObject(chunk.SourceFileId); err == nil {
|
2019-05-17 17:03:23 +08:00
|
|
|
chunk.SourceFid = fid
|
|
|
|
chunk.SourceFileId = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AfterEntryDeserialization(chunks []*FileChunk) {
|
|
|
|
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
|
|
|
|
if chunk.Fid != nil && chunk.FileId == "" {
|
2019-06-23 03:30:08 +08:00
|
|
|
chunk.FileId = chunk.Fid.toFileIdString()
|
2019-05-17 17:03:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if chunk.SourceFid != nil && chunk.SourceFileId == "" {
|
2019-06-23 03:30:08 +08:00
|
|
|
chunk.SourceFileId = chunk.SourceFid.toFileIdString()
|
2019-05-17 17:03:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 01:17:19 +08:00
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
func CreateEntry(client SeaweedFilerClient, request *CreateEntryRequest) error {
|
|
|
|
resp, err := client.CreateEntry(context.Background(), request)
|
2020-01-26 01:17:19 +08:00
|
|
|
if err != nil {
|
2020-02-26 13:50:12 +08:00
|
|
|
glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, err)
|
2020-01-26 01:17:19 +08:00
|
|
|
return fmt.Errorf("CreateEntry: %v", err)
|
|
|
|
}
|
2020-02-26 09:15:09 +08:00
|
|
|
if resp.Error != "" {
|
2020-08-19 03:52:54 +08:00
|
|
|
glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, resp.Error)
|
2020-02-26 09:24:08 +08:00
|
|
|
return fmt.Errorf("CreateEntry : %v", resp.Error)
|
2020-02-26 09:15:09 +08:00
|
|
|
}
|
2020-02-26 13:50:12 +08:00
|
|
|
return nil
|
2020-01-26 01:17:19 +08:00
|
|
|
}
|
2020-03-08 08:51:46 +08:00
|
|
|
|
|
|
|
func LookupEntry(client SeaweedFilerClient, request *LookupDirectoryEntryRequest) (*LookupDirectoryEntryResponse, error) {
|
2020-03-08 09:01:39 +08:00
|
|
|
resp, err := client.LookupDirectoryEntry(context.Background(), request)
|
2020-03-08 08:51:46 +08:00
|
|
|
if err != nil {
|
2020-03-08 09:01:39 +08:00
|
|
|
if err == ErrNotFound || strings.Contains(err.Error(), ErrNotFound.Error()) {
|
|
|
|
return nil, ErrNotFound
|
2020-03-08 08:51:46 +08:00
|
|
|
}
|
2020-03-08 09:01:39 +08:00
|
|
|
glog.V(3).Infof("read %s/%v: %v", request.Directory, request.Name, err)
|
2020-03-08 08:51:46 +08:00
|
|
|
return nil, fmt.Errorf("LookupEntry1: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Entry == nil {
|
2020-03-08 09:01:39 +08:00
|
|
|
return nil, ErrNotFound
|
2020-03-08 08:51:46 +08:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2020-03-08 09:01:39 +08:00
|
|
|
|
|
|
|
var ErrNotFound = errors.New("filer: no entry is found in filer store")
|