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

info recursively support fast mode #3296

Merged
merged 4 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 12 additions & 3 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ $ juicefs info -i 100`,
Aliases: []string{"r"},
Usage: "get summary of directories recursively (NOTE: it may take a long time for huge trees)",
},
&cli.BoolFlag{
Name: "fast",
davies marked this conversation as resolved.
Show resolved Hide resolved
Aliases: []string{"f"},
Usage: "get summary of directories fastly (NOTE: it may be inaccurate)",
},
&cli.BoolFlag{
Name: "raw",
Usage: "show internal raw information",
Expand All @@ -73,10 +78,13 @@ func info(ctx *cli.Context) error {
logger.Infof("Windows is not supported")
return nil
}
var recursive, raw uint8
var recursive, fast, raw uint8
if ctx.Bool("recursive") {
recursive = 1
}
if ctx.Bool("fast") {
fast = 1
}
if ctx.Bool("raw") {
raw = 1
}
Expand Down Expand Up @@ -108,11 +116,12 @@ func info(ctx *cli.Context) error {
continue
}

wb := utils.NewBuffer(8 + 10)
wb := utils.NewBuffer(8 + 11)
wb.Put32(meta.InfoV2)
wb.Put32(10)
wb.Put32(11)
wb.Put64(inode)
wb.Put8(recursive)
wb.Put8(fast)
davies marked this conversation as resolved.
Show resolved Hide resolved
wb.Put8(raw)
_, err = f.Write(wb.Bytes())
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions pkg/meta/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,50 @@ func GetSummary(r Meta, ctx Context, inode Ino, summary *Summary, recursive bool
}
return 0
}

func FastGetSummary(r Meta, ctx Context, inode Ino, summary *Summary, recursive bool) syscall.Errno {
var attr Attr
if st := r.GetAttr(ctx, inode, &attr); st != 0 {
return st
}
if attr.Typ == TypeDirectory {
return fastGetSummary(r, ctx, inode, summary, recursive)
} else {
summary.Files++
summary.Length += attr.Length
summary.Size += uint64(align4K(attr.Length))
}
return 0
}

func fastGetSummary(r Meta, ctx Context, inode Ino, summary *Summary, recursive bool) syscall.Errno {
space, _, err := r.GetDirStat(ctx, inode)
davies marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return errno(err)
}
summary.Size += space
summary.Length += space
summary.Dirs++

var entries []*Entry
if st := r.Readdir(ctx, inode, 0, &entries); st != 0 {
return st
}
for _, e := range entries {
if e.Inode == inode || len(e.Name) == 2 && bytes.Equal(e.Name, []byte("..")) {
continue
}
if e.Attr.Typ == TypeDirectory {
if recursive {
if st := fastGetSummary(r, ctx, e.Inode, summary, recursive); st != 0 {
return st
}
} else {
summary.Dirs++
}
} else {
summary.Files++
}
}
return 0
}
11 changes: 10 additions & 1 deletion pkg/vfs/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,21 @@ func (v *VFS) handleInternalMsg(ctx meta.Context, cmd uint32, r *utils.Buffer, o
if r.HasMore() {
recursive = r.Get8()
}
var fast uint8
if r.HasMore() {
fast = r.Get8()
}
var raw bool
if r.HasMore() {
raw = r.Get8() != 0
}

r := meta.GetSummary(v.Meta, ctx, inode, &info.Summary, recursive != 0)
var r syscall.Errno
if fast == 0 {
r = meta.GetSummary(v.Meta, ctx, inode, &info.Summary, recursive != 0)
} else {
r = meta.FastGetSummary(v.Meta, ctx, inode, &info.Summary, recursive != 0)
}
if r != 0 {
info.Failed = true
info.Reason = r.Error()
Expand Down