Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/dump: use a temp file during dumping #4767

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,28 @@ Details: https://juicefs.com/docs/community/metadata_dump_load`,
}
}

func dump(ctx *cli.Context) (err error) {
setup(ctx, 1)
metaUri := ctx.Args().Get(0)
dst := ctx.Args().Get(1)
removePassword(metaUri)
func dumpMeta(m meta.Meta, dst string, threads int, keepSecret, fast, skipTrash bool) (err error) {
var w io.WriteCloser
if ctx.Args().Len() == 1 {
if dst == "" {
w = os.Stdout
dst = "STDOUT"
} else {
fp, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
tmp := dst + ".tmp"
fp, e := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if e != nil {
return e
}
defer func() {
e := fp.Close()
if err == nil {
err = e
}
if err == nil {
err = os.Rename(tmp, dst)
} else {
_ = os.Remove(tmp)
}
}()

if strings.HasSuffix(dst, ".gz") {
zw := gzip.NewWriter(fp)
defer func() {
Expand All @@ -105,6 +107,18 @@ func dump(ctx *cli.Context) (err error) {
w = fp
}
}
return m.DumpMeta(w, 1, threads, keepSecret, fast, skipTrash)
}

func dump(ctx *cli.Context) error {
setup(ctx, 1)
metaUri := ctx.Args().Get(0)
var dst string
if ctx.Args().Len() > 1 {
dst = ctx.Args().Get(1)
}
removePassword(metaUri)

metaConf := meta.DefaultConf()
metaConf.Subdir = ctx.String("subdir")
m := meta.NewClient(metaUri, metaConf)
Expand All @@ -114,14 +128,18 @@ func dump(ctx *cli.Context) (err error) {
if st := m.Chroot(meta.Background, metaConf.Subdir); st != 0 {
return st
}

threads := ctx.Int("threads")
if threads <= 0 {
logger.Warnf("Invalid threads number %d, reset to 1", threads)
threads = 1
}
if err := m.DumpMeta(w, 1, threads, ctx.Bool("keep-secret-key"), ctx.Bool("fast"), ctx.Bool("skip-trash")); err != nil {
return err
err := dumpMeta(m, dst, threads, ctx.Bool("keep-secret-key"), ctx.Bool("fast"), ctx.Bool("skip-trash"))
if err == nil {
if dst == "" {
dst = "STDOUT"
}
logger.Infof("Dump metadata into %s succeed", dst)
}
logger.Infof("Dump metadata into %s succeed", dst)
return nil
return err
}
Loading