From 8a848fa2b1c67829c69001bbe5bff2cb182c3588 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Thu, 9 Apr 2026 23:38:05 +0800 Subject: wip: use sub in scan_tree(). --- vcx | 60 ++++++++++++++++++++++-------------------------------------- 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/vcx b/vcx index dbcd117..8682be8 100644 --- a/vcx +++ b/vcx @@ -329,68 +329,52 @@ sub hash_file_content { sub scan_tree { my $cb = pop @_; - my @paths = @_; my @stack; - my @input_files; - # If input contains files, process them first - foreach my $p (@paths) { - my @p_stats = lstat($p); - unless (@p_stats) { - warn "Can't lstat '$p': $!\n"; - next; - } - if (-f _ || -l _) { - push @input_files, { - path => $p =~ s|^\./||r, - size => $p_stats[7], - mtime => $p_stats[9], + my $collect = sub { + my ($path, $files) = @_; + my @stat = lstat($path) or (warn("lstat '$path': $!\n") and return); + if (-d _) { + push @stack, $path; + } elsif (-f _ || -l _) { + push @$files, { + path => $path =~ s|^\./||r, + size => $stat[7], + mtime => $stat[9], }; - } elsif (-d _) { - push @stack, $p; } - } + }; + my @input_files; + $collect->($_, \@input_files) for @_; if (@input_files) { - @input_files = sort { $a->{path} cmp $b->{path} } @input_files; + @input_files = sort { $a->{path} cmp $b->{path} } @input_files; $cb->('.', \@input_files); } while (@stack) { my $dir = pop @stack; my $dh; - unless (opendir($dh, $dir)) { warn "Can't open $dir\n"; next; } my @files; - my @subdirs; + my $subdir_idx = @stack; # Track where this level's subdirs start while (my $ent = readdir($dh)) { next if $ent eq '.' or $ent eq '..' or $ent eq REPO; - my $path = File::Spec->catfile($dir, $ent) =~ s|^\./||r; - my @stats = lstat($path); - unless (@stats) { - warn "Can't lstat $dir\n"; - next; - } - - if (-f _ || -l _) { - push @files, { - path => $path, - size => $stats[7], - mtime => $stats[9], - }; - } elsif (-d $path) { - push @subdirs, $path; - } + $collect->(File::Spec->catfile($dir, $ent), \@files); } - closedir($dh); + closedir($dh); @files = sort { $a->{path} cmp $b->{path} } @files; $cb->($dir, \@files) if @files; - push @stack, sort { $b cmp $a } @subdirs; + + if (@stack > $subdir_idx) { + my @subdirs = splice(@stack, $subdir_idx); + push @stack, sort { $b cmp $a } @subdirs; + } } } -- cgit v1.2.3