2019-05-17 09:03:23 +00:00
|
|
|
package filer_pb
|
|
|
|
|
|
|
|
import (
|
2020-01-25 17:17:19 +00:00
|
|
|
"context"
|
2020-03-08 01:01:39 +00:00
|
|
|
"errors"
|
2020-01-25 17:17:19 +00:00
|
|
|
"fmt"
|
2022-01-12 19:51:13 +00:00
|
|
|
"os"
|
2020-03-08 00:51:46 +00:00
|
|
|
"strings"
|
2023-04-25 15:31:14 +00:00
|
|
|
"time"
|
2020-01-25 17:17:19 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
2020-11-15 08:26:05 +00:00
|
|
|
"github.com/viant/ptrie"
|
2022-08-17 19:05:07 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-05-17 09:03:23 +00:00
|
|
|
)
|
|
|
|
|
2023-04-25 15:31:14 +00:00
|
|
|
const cutoffTimeNewEmptyDir = 3
|
|
|
|
|
2021-08-10 05:11:57 +00:00
|
|
|
func (entry *Entry) IsInRemoteOnly() bool {
|
2022-11-15 14:33:36 +00:00
|
|
|
return len(entry.GetChunks()) == 0 && entry.RemoteEntry != nil && entry.RemoteEntry.RemoteSize > 0
|
2021-08-10 05:11:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 09:43:10 +00:00
|
|
|
func (entry *Entry) IsDirectoryKeyObject() bool {
|
|
|
|
return entry.IsDirectory && entry.Attributes != nil && entry.Attributes.Mime != ""
|
|
|
|
}
|
|
|
|
|
2022-01-12 19:51:13 +00:00
|
|
|
func (entry *Entry) FileMode() (fileMode os.FileMode) {
|
|
|
|
if entry != nil && entry.Attributes != nil {
|
|
|
|
fileMode = os.FileMode(entry.Attributes.FileMode)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:31:14 +00:00
|
|
|
func (entry *Entry) IsOlderDir() bool {
|
|
|
|
return entry.IsDirectory && entry.Attributes != nil && entry.Attributes.Mime == "" && entry.Attributes.GetCrtime() <= time.Now().Unix()-cutoffTimeNewEmptyDir
|
|
|
|
}
|
|
|
|
|
2020-08-16 02:55:28 +00:00
|
|
|
func ToFileIdObject(fileIdStr string) (*FileId, error) {
|
2019-05-17 09:03:23 +00: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-22 19:30:08 +00:00
|
|
|
func (fid *FileId) toFileIdString() string {
|
2019-05-17 09:03:23 +00:00
|
|
|
return needle.NewFileId(needle.VolumeId(fid.VolumeId), fid.FileKey, fid.Cookie).String()
|
|
|
|
}
|
|
|
|
|
2019-06-22 19:30:08 +00: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 09:03:23 +00:00
|
|
|
func BeforeEntrySerialization(chunks []*FileChunk) {
|
|
|
|
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
|
|
|
|
if chunk.FileId != "" {
|
2020-08-16 02:55:28 +00:00
|
|
|
if fid, err := ToFileIdObject(chunk.FileId); err == nil {
|
2019-05-17 09:03:23 +00:00
|
|
|
chunk.Fid = fid
|
|
|
|
chunk.FileId = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if chunk.SourceFileId != "" {
|
2020-08-16 02:55:28 +00:00
|
|
|
if fid, err := ToFileIdObject(chunk.SourceFileId); err == nil {
|
2019-05-17 09:03:23 +00:00
|
|
|
chunk.SourceFid = fid
|
|
|
|
chunk.SourceFileId = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:21:23 +00:00
|
|
|
func EnsureFid(chunk *FileChunk) {
|
|
|
|
if chunk.Fid != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fid, err := ToFileIdObject(chunk.FileId); err == nil {
|
|
|
|
chunk.Fid = fid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:03:23 +00:00
|
|
|
func AfterEntryDeserialization(chunks []*FileChunk) {
|
|
|
|
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
|
|
|
|
if chunk.Fid != nil && chunk.FileId == "" {
|
2019-06-22 19:30:08 +00:00
|
|
|
chunk.FileId = chunk.Fid.toFileIdString()
|
2019-05-17 09:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if chunk.SourceFid != nil && chunk.SourceFileId == "" {
|
2019-06-22 19:30:08 +00:00
|
|
|
chunk.SourceFileId = chunk.SourceFid.toFileIdString()
|
2019-05-17 09:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 17:17:19 +00:00
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func CreateEntry(client SeaweedFilerClient, request *CreateEntryRequest) error {
|
|
|
|
resp, err := client.CreateEntry(context.Background(), request)
|
2020-01-25 17:17:19 +00:00
|
|
|
if err != nil {
|
2020-02-26 05:50:12 +00:00
|
|
|
glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, err)
|
2020-01-25 17:17:19 +00:00
|
|
|
return fmt.Errorf("CreateEntry: %v", err)
|
|
|
|
}
|
2020-02-26 01:15:09 +00:00
|
|
|
if resp.Error != "" {
|
2020-08-18 19:52:54 +00:00
|
|
|
glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, resp.Error)
|
2020-02-26 01:24:08 +00:00
|
|
|
return fmt.Errorf("CreateEntry : %v", resp.Error)
|
2020-02-26 01:15:09 +00:00
|
|
|
}
|
2020-02-26 05:50:12 +00:00
|
|
|
return nil
|
2020-01-25 17:17:19 +00:00
|
|
|
}
|
2020-03-08 00:51:46 +00:00
|
|
|
|
2020-09-24 10:06:44 +00:00
|
|
|
func UpdateEntry(client SeaweedFilerClient, request *UpdateEntryRequest) error {
|
|
|
|
_, err := client.UpdateEntry(context.Background(), request)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("update entry %s/%s :%v", request.Directory, request.Entry.Name, err)
|
|
|
|
return fmt.Errorf("UpdateEntry: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-08 00:51:46 +00:00
|
|
|
func LookupEntry(client SeaweedFilerClient, request *LookupDirectoryEntryRequest) (*LookupDirectoryEntryResponse, error) {
|
2020-03-08 01:01:39 +00:00
|
|
|
resp, err := client.LookupDirectoryEntry(context.Background(), request)
|
2020-03-08 00:51:46 +00:00
|
|
|
if err != nil {
|
2020-03-08 01:01:39 +00:00
|
|
|
if err == ErrNotFound || strings.Contains(err.Error(), ErrNotFound.Error()) {
|
|
|
|
return nil, ErrNotFound
|
2020-03-08 00:51:46 +00:00
|
|
|
}
|
2020-03-08 01:01:39 +00:00
|
|
|
glog.V(3).Infof("read %s/%v: %v", request.Directory, request.Name, err)
|
2020-03-08 00:51:46 +00:00
|
|
|
return nil, fmt.Errorf("LookupEntry1: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Entry == nil {
|
2020-03-08 01:01:39 +00:00
|
|
|
return nil, ErrNotFound
|
2020-03-08 00:51:46 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2020-03-08 01:01:39 +00:00
|
|
|
|
|
|
|
var ErrNotFound = errors.New("filer: no entry is found in filer store")
|
2020-11-15 05:21:58 +00:00
|
|
|
|
2022-02-25 09:17:26 +00:00
|
|
|
func IsEmpty(event *SubscribeMetadataResponse) bool {
|
|
|
|
return event.EventNotification.NewEntry == nil && event.EventNotification.OldEntry == nil
|
|
|
|
}
|
2023-04-25 15:31:14 +00:00
|
|
|
|
2020-11-15 05:21:58 +00:00
|
|
|
func IsCreate(event *SubscribeMetadataResponse) bool {
|
|
|
|
return event.EventNotification.NewEntry != nil && event.EventNotification.OldEntry == nil
|
|
|
|
}
|
2023-04-25 15:31:14 +00:00
|
|
|
|
2020-11-15 05:21:58 +00:00
|
|
|
func IsUpdate(event *SubscribeMetadataResponse) bool {
|
|
|
|
return event.EventNotification.NewEntry != nil &&
|
|
|
|
event.EventNotification.OldEntry != nil &&
|
2022-02-25 08:54:16 +00:00
|
|
|
event.Directory == event.EventNotification.NewParentPath &&
|
|
|
|
event.EventNotification.NewEntry.Name == event.EventNotification.OldEntry.Name
|
2020-11-15 05:21:58 +00:00
|
|
|
}
|
2023-04-25 15:31:14 +00:00
|
|
|
|
2020-11-15 05:21:58 +00:00
|
|
|
func IsDelete(event *SubscribeMetadataResponse) bool {
|
|
|
|
return event.EventNotification.NewEntry == nil && event.EventNotification.OldEntry != nil
|
|
|
|
}
|
2023-04-25 15:31:14 +00:00
|
|
|
|
2020-11-15 05:21:58 +00:00
|
|
|
func IsRename(event *SubscribeMetadataResponse) bool {
|
|
|
|
return event.EventNotification.NewEntry != nil &&
|
|
|
|
event.EventNotification.OldEntry != nil &&
|
2022-02-25 08:54:16 +00:00
|
|
|
(event.Directory != event.EventNotification.NewParentPath ||
|
|
|
|
event.EventNotification.NewEntry.Name != event.EventNotification.OldEntry.Name)
|
2020-11-15 05:21:58 +00:00
|
|
|
}
|
2020-11-15 08:26:05 +00:00
|
|
|
|
|
|
|
var _ = ptrie.KeyProvider(&FilerConf_PathConf{})
|
2020-11-16 00:59:28 +00:00
|
|
|
|
2020-11-15 08:26:05 +00:00
|
|
|
func (fp *FilerConf_PathConf) Key() interface{} {
|
2020-11-16 04:15:47 +00:00
|
|
|
key, _ := proto.Marshal(fp)
|
|
|
|
return string(key)
|
2020-11-15 08:26:05 +00:00
|
|
|
}
|