最近在工作中使用到了Perl 脚本,需要在给定目录下对各类编译过的文件进行 Valgrind 安全检查。 谢了一个脚本自动将检查结果写入到文本文件当中。
程序名为 process_files:
转载请注明出处: http://blog.csdn.net/elfprincexu
原理: 从根目录(给定参数)开始查找, 如果找到的文件仍然为子文件夹,递归调用。
如果找到的是文件,则判断是否是我们感兴趣的文件。
查找过程中,排除linux下 . .. 文件夹
最后将结果输出到文本文件,便于查看和纠错。
主要函数: process_files
#!/usr/bin/perl
use warnings;
use strict;
use IO::Handle;
use File::stat;
use Term::ANSIColor;
STDOUT->autoflush(1);
my $binStr = "bin.x86_64";
my $dir = '/rapid/rapid_workspaces/jxu/jxu_RTC_New/Fusion/SW';
print ("fusion dir is :$dir\n");
my $wantedfiles = 0;
my $processedfs =0;
my $target = 'valgrind.log';
my $processedfiles = 'valgrindProcessFiles.log';
system ("rm -f $processedfiles"); #remove it
system ("touch $processedfiles"); #create it
print ("processedfiles file is :$processedfiles\n");
system ("rm -f $target"); #remove it
system ("touch $target"); #create it
print ("target file is :$target\n");
sub process_files {
my $path =shift; #get the argument
opendir (DIR, $path) or die "unable to open $path: $!";
my @files = grep{!/^\.{1,2}$/} readdir(DIR); #exclude . or .. directory
closedir (DIR);
@files = map{$path . '/' . $_} @files;
for(@files)
{
if (-d $_)
{
process_files ($_);
}
else # this a file we found
{
my $mode = (stat($_)->mode) or (warn "$_ coun't get its mode\n"; next); #if not succcess, warn and go to next element;
$mode = $mode & 0777;
$processedfs++;
print "$processedfs: $_ : $mode\n";
print Processedfiles "$processedfs: $_ : $mode \n"; #out put processed files
if ((index($_,$binStr)!=-1) #find bin.x86_64 str
&& ($mode & 111 == 111 ) #execution permission file
) #bin.x86_64 files and has execution permission
{
print color 'red';
$wantedfiles++;
print ($wantedfiles,": $_ : $mode\n");
# only these executable files
print FILE "wanted files found: $wantedfiles : $_ : $mode\n";
print color 'reset';
system("valgrind --tool=memcheck --leak-check=full $_ >>$target 2>&1 &");
}
}
}
}
print "\nValgrind Unit Tests Start...\n";
for (my $num =0; $num <3; $num++)
{
print ("counting .... " ,3-$num , "\n");
sleep (1);
}
print "\nValgrind Unit Tests now Start...\n";
#really starts recursively start#
open (FILE,'>>',"$target") or die "could not open $target : $!"; #append , if not exist, create a new file
open (Processedfiles,'>>',"$processedfiles") or die "couldn't open the file : $!"; # open a file with open command
process_files($dir);
close Processedfiles;
close FILE;
my $editor ="vi "; # finally we open the result file
my $pidEditor = fork();
if ($pidEditor ==0) {
my $editcmd = $editor . $target . " &";
system ($editcmd);
exit (0);
}
my $pidEditor2 = fork();
if ($pidEditor2 ==0)
{
my $editcmd2 = $editor . $processedfiles . " &";
system ($editcmd2);
exit (0);
}
其中,使用到了sub routine : process_files, 具体代码如下:
<pre name="code" class="plain">sub process_files {
my $path =shift; #get the argument
opendir (DIR, $path) or die "unable to open $path: $!";
my @files = grep{!/^\.{1,2}$/} readdir(DIR); #exclude . or .. directory
closedir (DIR);
@files = map{$path . '/' . $_} @files;
for(@files)
{
if (-d $_)
{
process_files ($_);
}
else # this a file we found
{
$processedfs++;
print "$processedfs: $_\n";
print Processdfiles "$processedfs: $_\n";
if ((index($_,$binStr)!=-1) && (index($_,'.')==-1)) #bin.x86_64 files and not contains .
{
print color 'red';
$wantedfiles++;
print ($wantedfiles,": $_");
print color 'reset';
# only these executable files
system("valgrind --tool=memcheck --leak-check=full $_ >> $target &");
}
}
}
}