I am writing some prototype code for a larger application that needs to search all directories from a particular starting point and return all directories that contain certain types of files. The function works fine excepts for producing warnings related to not being able to chdir to directories for which the user who runs the application does not have sufficient permissions to read.
Perhaps I am just being pickier than I should, but the application needs to traverse directories that belong to the user and also ones that do not belong to the user, but for which the user has sufficient permissions to read. When I run the test code, everything works as expected, but produces warnings. They are expected, but I would like to suppress them or "tell" File::Find to skip any directories that the user does not have permissions to access. On the other hand, maybe there is a way to tell File::Find to first test if it can read the directory and then move on if it cannot?
Here is my current code. Any suggestions would be greatly appreciated.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use Carp qw(cluck carp croak);
use JSON qw( encode_json );
use File::Find;
use Data::Dumper;
my %seen;
my @files;
find ({ wanted => \&wanted }, '/mnt/lustre');
say Dumper(\@files);
sub wanted {
if($File::Find::name =~ /\.nt$|^dbQuads$|^graph.info$|^string_table_
+chars.index$|^string_table_chars$/ && !exists $seen{$File::Find::dir}
+) {
my $user = (getpwuid ((stat $File::Find::dir)[4]))[0];
my $name = $1 if $File::Find::dir =~ /\/([^\/]+)$/;
#=+ Would like to know how big the directory contents are
my $raw_size = 0;
my $db_size = 0;
my $built = 0;
find(sub {
if(-f $_ && $_ =~ /\.nt$/) {
$raw_size += -s $_;
}
elsif(-f $_ && $_ =~ /^dbQuads$|^string_table_chars.index$|^stri
+ng_table_chars$/) {
$db_size += -s $_;
$built = 1;
}
},$File::Find::dir);
my %temp = (
owner => $user,
raw_size => $raw_size,
db_size => $db_size,
name => $name,
path => $File::Find::dir,
built => $built
);
push @files, \%temp;
$seen{$File::Find::dir} = 1;
}
}