2019-04-16 08:37:11 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2021-04-15 18:41:34 +00:00
|
|
|
"strings"
|
2019-04-16 08:37:11 +00:00
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
2019-04-16 08:37:11 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 08:30:24 +00:00
|
|
|
Commands = append(Commands, &commandFsMetaLoad{})
|
2019-04-16 08:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandFsMetaLoad struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMetaLoad) Name() string {
|
|
|
|
return "fs.meta.load"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMetaLoad) Help() string {
|
|
|
|
return `load saved filer meta data to restore the directory and file structure
|
|
|
|
|
2019-04-16 16:40:27 +00:00
|
|
|
fs.meta.load <filer_host>-<port>-<time>.meta
|
2019-04-16 08:37:11 +00:00
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
func (c *commandFsMetaLoad) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-04-16 08:37:11 +00:00
|
|
|
|
2019-04-26 16:31:55 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Fprintf(writer, "missing a metadata file\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 08:37:11 +00:00
|
|
|
fileName := args[len(args)-1]
|
|
|
|
|
|
|
|
dst, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
var dirCount, fileCount uint64
|
|
|
|
|
2020-03-24 04:26:15 +00:00
|
|
|
err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2019-04-16 08:37:11 +00:00
|
|
|
|
|
|
|
sizeBuf := make([]byte, 4)
|
|
|
|
|
|
|
|
for {
|
|
|
|
if n, err := dst.Read(sizeBuf); n != 4 {
|
2019-04-18 07:19:18 +00:00
|
|
|
if err == io.EOF {
|
2019-04-16 08:37:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
size := util.BytesToUint32(sizeBuf)
|
|
|
|
|
|
|
|
data := make([]byte, int(size))
|
|
|
|
|
|
|
|
if n, err := dst.Read(data); n != len(data) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fullEntry := &filer_pb.FullEntry{}
|
|
|
|
if err = proto.Unmarshal(data, fullEntry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:41:34 +00:00
|
|
|
fullEntry.Entry.Name = strings.ReplaceAll(fullEntry.Entry.Name, "/", "x")
|
2020-02-26 05:50:12 +00:00
|
|
|
if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
|
2019-04-16 08:37:11 +00:00
|
|
|
Directory: fullEntry.Dir,
|
|
|
|
Entry: fullEntry.Entry,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
fmt.Fprintf(writer, "load %s\n", util.FullPath(fullEntry.Dir).Child(fullEntry.Entry.Name))
|
2019-04-16 08:37:11 +00:00
|
|
|
|
|
|
|
if fullEntry.Entry.IsDirectory {
|
|
|
|
dirCount++
|
|
|
|
} else {
|
|
|
|
fileCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
|
2020-03-24 04:26:15 +00:00
|
|
|
fmt.Fprintf(writer, "\n%s is loaded.\n", fileName)
|
2019-04-16 08:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2019-04-18 07:19:18 +00:00
|
|
|
}
|