perlquestion
osunderdog
<p>
I'm having a problems 'seeing' data located on an <b>automount directory</b>. I have tried <code>opendir/closedir</code> as well as <code>glob</code>. Both methods report that the directory is empty, however the os command <code>find</code> will report the data expected.
</p>
<p>
Of course the short term fix is to use <code>find</code>, however I've found that the options for it very between operating sytems.
</p>
<p>
Others have suggested that I try to open a file on the target operating system before using <code>opendir/closedir</code> or <code>glob</code> however I haven't had any luck getting that to work.
</p>
<p>
Here is my sample code, which will run on Linux. The three alternative implementations are located in functions <code>getDirs1</code>, <code>getDirs2</code> and <code>getDirs3</code>
</p>
<p>NOTE: you will need to select a directory that is automounted (<code>$mountpoint</code>) and a regex for a file (<code>$probepoint</code>) that is located on that mountpoint.
</p>
<p>
Of course, if you run this on a directory that isn't automounted, all three tests should succeed.
</p>
<p>
If anyone has any suggestions on how to get this to work, please let me know!
</p>
<readmore>
<code>
use strict;
use File::Find;
use Carp;
use Benchmark;
use Test::More tests => 3;
my $mountpoint = '/mnt/foofoo';
my $probepoint = qr/^3/;
#my $mountpoint = '/tmp';
#my $probepoint = qr/^G/;
ok(globMethod(), 'glob Method');
ok(opendirMethod(), 'opendir Method');
ok(osFindMethod(), 'OS Find Method');
sub globMethod
{
my $dirs = getDirs1($mountpoint);
#probe for known directory
return(scalar(grep(/$probepoint/, @$dirs)) == 1);
}
sub opendirMethod
{
my $dirs = getDirs2($mountpoint);
return(scalar(grep(/$probepoint/, @$dirs)) == 1);
}
sub osFindMethod
{
my $dirs = getDirs3($mountpoint);
return(scalar(grep(/$probepoint/, @$dirs)) == 1);
}
sub getDirs1
{
my $root = shift;
my $whackFile = "$root/Volume_Inventory.xml";
my @dirs = grep { $_ !~ /^\./ &&
-d "$_" }
glob("$root/*");
@dirs = map {s|^$root/||; $_;} @dirs;
return \@dirs;
}
sub getDirs2
{
my $root = shift;
my @dirs;
if(not opendir(DH, $root))
{
croak "Could not open dir: $root ($!)";
}
@dirs = grep { /^[^\.]/ &&
-d "$root/$_" }
readdir(DH);
closedir DH;
return \@dirs;
}
sub getDirs3
{
my $root = shift;
my $cmd = "find $root -maxdepth 1 -follow -type d";
my @dirs = grep {chomp &&
$_ !~ /^\./ &&
$_ !~ /^$root$/ &&
-d $_}
`$cmd`;
@dirs = map {s|^$root/||; $_;} @dirs;
return \@dirs;
}
</code>
</readmore>
<div class="pmsig"><div class="pmsig-338711">
<p>Hazah! I'm Employed!</p>
</div></div>