diff options
| author | Feng Shao <88640691+shaofeng66@users.noreply.github.com> | 2025-11-18 23:45:12 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-18 07:45:12 -0800 |
| commit | 0299e78de7c7f7e19d5b206b384d0331609e15aa (patch) | |
| tree | 1c71b1399e5ec88e11e399bee826fd7c9d9d5855 | |
| parent | 156ce42f2671f653f87aecedb6bb716f736913af (diff) | |
| download | seaweedfs-0299e78de7c7f7e19d5b206b384d0331609e15aa.tar.xz seaweedfs-0299e78de7c7f7e19d5b206b384d0331609e15aa.zip | |
de/compress the fs meta file if filename ends with gz/gzip (#7500)
* de/compress the fs meta file if filename ends with gz/gzip
* gemini code review
* update help msg
| -rw-r--r-- | weed/shell/command_fs_meta_load.go | 27 | ||||
| -rw-r--r-- | weed/shell/command_fs_meta_save.go | 30 |
2 files changed, 48 insertions, 9 deletions
diff --git a/weed/shell/command_fs_meta_load.go b/weed/shell/command_fs_meta_load.go index f43574f49..c2e01dfc2 100644 --- a/weed/shell/command_fs_meta_load.go +++ b/weed/shell/command_fs_meta_load.go @@ -1,6 +1,7 @@ package shell import ( + "compress/gzip" "context" "flag" "fmt" @@ -60,11 +61,31 @@ func (c *commandFsMetaLoad) Do(args []string, commandEnv *CommandEnv, writer io. return nil } - dst, err := os.OpenFile(fileName, os.O_RDONLY, 0644) + var dst io.Reader + + f, err := os.OpenFile(fileName, os.O_RDONLY, 0644) if err != nil { - return nil + return fmt.Errorf("failed to open file %s: %v", fileName, err) + } + defer f.Close() + + dst = f + + if strings.HasSuffix(fileName, ".gz") || strings.HasSuffix(fileName, ".gzip") { + var gr *gzip.Reader + gr, err = gzip.NewReader(dst) + if err != nil { + return err + } + defer func() { + err1 := gr.Close() + if err == nil { + err = err1 + } + }() + + dst = gr } - defer dst.Close() var dirCount, fileCount uint64 lastLogTime := time.Now() diff --git a/weed/shell/command_fs_meta_save.go b/weed/shell/command_fs_meta_save.go index a8be9fe2c..ce982820d 100644 --- a/weed/shell/command_fs_meta_save.go +++ b/weed/shell/command_fs_meta_save.go @@ -1,9 +1,9 @@ package shell import ( + "compress/gzip" "flag" "fmt" - "github.com/seaweedfs/seaweedfs/weed/filer" "io" "os" "path/filepath" @@ -12,6 +12,8 @@ import ( "sync/atomic" "time" + "github.com/seaweedfs/seaweedfs/weed/filer" + "google.golang.org/protobuf/proto" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" @@ -38,7 +40,7 @@ func (c *commandFsMetaSave) Help() string { fs.meta.save . # save from current directory fs.meta.save # save from current directory - The meta data will be saved into a local <filer_host>-<port>-<time>.meta file. + The meta data will be saved into a local <filer_host>-<port>-<time>.meta.gz file. These meta data can be later loaded by fs.meta.load command ` @@ -52,7 +54,7 @@ func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io. fsMetaSaveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) verbose := fsMetaSaveCommand.Bool("v", false, "print out each processed files") - outputFileName := fsMetaSaveCommand.String("o", "", "output the meta data to this file") + outputFileName := fsMetaSaveCommand.String("o", "", "output the meta data to this file. If file name ends with .gz or .gzip, it will be gzip compressed") isObfuscate := fsMetaSaveCommand.Bool("obfuscate", false, "obfuscate the file names") // chunksFileName := fsMetaSaveCommand.String("chunks", "", "output all the chunks to this file") if err = fsMetaSaveCommand.Parse(args); err != nil { @@ -67,15 +69,31 @@ func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io. fileName := *outputFileName if fileName == "" { t := time.Now() - fileName = fmt.Sprintf("%s-%4d%02d%02d-%02d%02d%02d.meta", + fileName = fmt.Sprintf("%s-%4d%02d%02d-%02d%02d%02d.meta.gz", commandEnv.option.FilerAddress.ToHttpAddress(), t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) } - dst, openErr := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + var dst io.Writer + + f, openErr := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if openErr != nil { return fmt.Errorf("failed to create file %s: %v", fileName, openErr) } - defer dst.Close() + defer f.Close() + + dst = f + + if strings.HasSuffix(fileName, ".gz") || strings.HasSuffix(fileName, ".gzip") { + gw := gzip.NewWriter(dst) + defer func() { + err1 := gw.Close() + if err == nil { + err = err1 + } + }() + + dst = gw + } var cipherKey util.CipherKey if *isObfuscate { |
