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 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
16 changes: 12 additions & 4 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ $ juicefs info -i 100`,
&cli.BoolFlag{
Name: "recursive",
Aliases: []string{"r"},
Usage: "get summary of directories recursively (NOTE: it may take a long time for huge trees)",
Usage: "get summary of directories recursively (NOTE: it may be inaccurate, use --strict to get accurate result)",
},
&cli.BoolFlag{
Name: "strict",
Usage: "get accurate summary of directories (NOTE: it may take a long time for huge trees)",
},
&cli.BoolFlag{
Name: "raw",
Expand All @@ -73,10 +77,13 @@ func info(ctx *cli.Context) error {
logger.Infof("Windows is not supported")
return nil
}
var recursive, raw uint8
var recursive, strict, raw uint8
if ctx.Bool("recursive") {
recursive = 1
}
if ctx.Bool("strict") {
strict = 1
}
if ctx.Bool("raw") {
raw = 1
}
Expand Down Expand Up @@ -108,12 +115,13 @@ 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(raw)
wb.Put8(strict)
_, err = f.Write(wb.Bytes())
if err != nil {
logger.Fatalf("write message: %s", err)
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 @@ -398,8 +398,17 @@ func (v *VFS) handleInternalMsg(ctx meta.Context, cmd uint32, r *utils.Buffer, o
if r.HasMore() {
raw = r.Get8() != 0
}
var strict bool
if r.HasMore() {
strict = r.Get8() != 0
}

r := meta.GetSummary(v.Meta, ctx, inode, &info.Summary, recursive != 0)
var r syscall.Errno
if strict {
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