summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2026-04-09 23:38:05 +0800
committerSadeep Madurange <sadeep@asciimx.com>2026-04-09 23:38:05 +0800
commit8a848fa2b1c67829c69001bbe5bff2cb182c3588 (patch)
treeeca2adc4aa372944a0011281657966bc8909c077
parent41a9e0db19abc7aa02c75a6ccc34d6ea5374ac43 (diff)
downloadcvn-8a848fa2b1c67829c69001bbe5bff2cb182c3588.tar.gz
wip: use sub in scan_tree().
-rw-r--r--vcx60
1 files 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;
+ }
}
}