Skip to content

Commit

Permalink
add --max-depth option #27
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Jan 27, 2025
1 parent d1a221a commit 3e22295
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cmd/chkbit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type CLI struct {
NoRecurse bool `short:"R" help:"do not recurse into subdirectories" negatable:""`
NoDirInIndex bool `short:"D" help:"do not track directories in the index" negatable:""`
NoConfig bool `help:"ignore the config file"`
MaxDepth int `default:0 help:"process a directory only if it is N or fewer levels below the specified path(s); 0 for no limit"`
LogFile string `short:"l" help:"write to a logfile if specified"`
LogVerbose bool `help:"verbose logging" negatable:""`
Algo string `default:"blake3" help:"hash algorithm: md5, sha512, blake3"`
Expand Down Expand Up @@ -256,6 +257,7 @@ func (m *Main) process(cmd Command, cli CLI) (bool, error) {
m.context.SkipSymlinks = cli.SkipSymlinks
m.context.SkipSubdirectories = cli.NoRecurse
m.context.TrackDirectories = !cli.NoDirInIndex
m.context.MaxDepth = cli.MaxDepth

st, root, err := chkbit.LocateIndex(pathList[0], chkbit.IndexTypeAny, m.context.IndexFilename)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Context struct {
SkipSubdirectories bool
IndexFilename string
IgnoreFilename string
MaxDepth int

WorkQueue chan *WorkItem
LogQueue chan *LogEvent
Expand Down Expand Up @@ -141,7 +142,7 @@ func (context *Context) Process(pathList []string) {
}
go func() {
for _, path := range pathList {
context.scanDir(path, nil)
context.scanDir(path, nil, 1)
}
for i := 0; i < context.NumWorkers; i++ {
context.endWork()
Expand All @@ -155,7 +156,7 @@ func (context *Context) Process(pathList []string) {
context.LogQueue <- nil
}

func (context *Context) scanDir(root string, parentIgnore *Ignore) {
func (context *Context) scanDir(root string, parentIgnore *Ignore, depth int) {
files, err := os.ReadDir(root)
if err != nil {
context.logErr(root+"/", err)
Expand Down Expand Up @@ -200,9 +201,9 @@ func (context *Context) scanDir(root string, parentIgnore *Ignore) {

context.addWork(root, filesToIndex, dirList, ignore)

if !context.SkipSubdirectories {
if !context.SkipSubdirectories && (context.MaxDepth == 0 || depth < context.MaxDepth) {
for _, name := range dirList {
context.scanDir(filepath.Join(root, name), ignore)
context.scanDir(filepath.Join(root, name), ignore, depth+1)
}
}
}
Expand Down

0 comments on commit 3e22295

Please sign in to comment.