2020-12-07 07:16:20 +00:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-07-11 01:44:48 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
2020-12-07 07:16:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ReadEntry(masterClient *wdclient.MasterClient, filerClient filer_pb.SeaweedFilerClient, dir, name string, byteBuffer *bytes.Buffer) error {
|
|
|
|
|
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
respLookupEntry, err := filer_pb.LookupEntry(filerClient, request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(respLookupEntry.Entry.Content) > 0 {
|
|
|
|
_, err = byteBuffer.Write(respLookupEntry.Entry.Content)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:33:36 +00:00
|
|
|
return StreamContent(masterClient, byteBuffer, respLookupEntry.Entry.GetChunks(), 0, int64(FileSize(respLookupEntry.Entry)))
|
2020-12-07 07:16:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-09 09:48:03 +00:00
|
|
|
func ReadInsideFiler(filerClient filer_pb.SeaweedFilerClient, dir, name string) (content []byte, err error) {
|
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
2020-12-07 07:16:20 +00:00
|
|
|
}
|
2021-07-09 09:48:03 +00:00
|
|
|
respLookupEntry, err := filer_pb.LookupEntry(filerClient, request)
|
2020-12-09 09:34:24 +00:00
|
|
|
if err != nil {
|
2021-07-09 09:48:03 +00:00
|
|
|
return
|
2020-12-09 09:34:24 +00:00
|
|
|
}
|
2021-07-09 09:48:03 +00:00
|
|
|
content = respLookupEntry.Entry.Content
|
|
|
|
return
|
2020-12-07 07:16:20 +00:00
|
|
|
}
|
2020-12-10 08:15:22 +00:00
|
|
|
|
|
|
|
func SaveInsideFiler(client filer_pb.SeaweedFilerClient, dir, name string, content []byte) error {
|
|
|
|
|
|
|
|
resp, err := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == filer_pb.ErrNotFound {
|
|
|
|
err = filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
|
2020-12-22 10:34:08 +00:00
|
|
|
Directory: dir,
|
|
|
|
Entry: &filer_pb.Entry{
|
2020-12-10 08:15:22 +00:00
|
|
|
Name: name,
|
|
|
|
IsDirectory: false,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
2022-06-06 07:39:35 +00:00
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(0644),
|
|
|
|
FileSize: uint64(len(content)),
|
2020-12-10 08:15:22 +00:00
|
|
|
},
|
|
|
|
Content: content,
|
|
|
|
},
|
2022-07-11 01:44:48 +00:00
|
|
|
SkipCheckParentDirectory: false,
|
2020-12-10 08:15:22 +00:00
|
|
|
})
|
|
|
|
} else if err == nil {
|
|
|
|
entry := resp.Entry
|
|
|
|
entry.Content = content
|
|
|
|
entry.Attributes.Mtime = time.Now().Unix()
|
|
|
|
entry.Attributes.FileSize = uint64(len(content))
|
|
|
|
err = filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
|
2020-12-22 10:34:08 +00:00
|
|
|
Directory: dir,
|
|
|
|
Entry: entry,
|
2020-12-10 08:15:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2020-12-22 10:34:08 +00:00
|
|
|
}
|