gpt4 book ai didi

用于在 Unix 上查找所有无主文件和目录的 Perl 脚本 - 如何进一步优化?

转载 作者:行者123 更新时间:2023-12-04 06:10:07 24 4
gpt4 key购买 nike

关闭。这个问题是off-topic .它目前不接受答案。












想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。

10年前关闭。




Improve this question




根据我在另一篇文章中的发现和建议 How to exclude a list of full directory paths in find command on Solaris ,我决定编写这个脚本的 Perl 版本,看看如何优化它以比原生 find 命令运行得更快。到目前为止,结果令人印象深刻!

此脚本的目的是报告 Unix 系统上所有未拥有的文件和目录,以确保审计合规性。该脚本必须接受要排除的目录和文件列表(通过完整路径或通配符名称),并且必须尽可能少地占用处理能力。它可以在我们(我工作的公司)支持的数百个 Unix 系统上运行,并且能够在所有这些 Unix 系统上运行(多操作系统、多平台:AIX、HP-UX、Solaris 和 Linux)无需我们先安装或升级任何东西。换句话说,它必须与我们可以在所有系统上期望的标准库和二进制文件一起运行。

我还没有让脚本参数感知,所以所有参数都硬编码在脚本中。我计划最后提出以下论点,并且可能会使用 getopts 来做到这一点:

-d = comma delimited list of directories to exclude by path name
-w = comma delimited list of directories to exclude by basename or wildcard
-f = comma delimited list of files to exclude by path name
-i = comma delimited list of files to exclude by basename or wildcard
-t:list|count = Defines the type of output I want to see (list of all findinds, or summary with count per directory)

这是我到目前为止所做的来源:
#! /usr/bin/perl
use strict;
use File::Find;

# Full paths of directories to prune
my @exclude_dirs = ('/dev','/proc','/home');

# Basenames or wildcard names of directories I want to prune
my $exclude_dirs_wildcard = '.svn';

# Full paths of files I want to ignore
my @exclude_files = ('/tmp/test/dir3/.svn/svn_file1.txt','/tmp/test/dir3/.svn/svn_file2.txt');

# Basenames of wildcard names of files I want to ignore
my $exclude_files_wildcard = '*.tmp';
my %dir_globs = ();
my %file_globs = ();

# Results will be sroted in this hash
my %found = ();

# Used for storing uid's and gid's present on system
my %uids = ();
my %gids = ();

# Callback function for find
sub wanted {
my $dir = $File::Find::dir;
my $name = $File::Find::name;
my $basename = $_;

# Ignore symbolic links
return if -l $name;

# Search for wildcards if dir was never searched before
if (!exists($dir_globs{$dir})) {
@{$dir_globs{$dir}} = glob($exclude_dirs_wildcard);
}
if (!exists($file_globs{$dir})) {
@{$file_globs{$dir}} = glob($exclude_files_wildcard);
}

# Prune directory if present in exclude list
if (-d $name && in_array(\@exclude_dirs, $name)) {
$File::Find::prune = 1;
return;
}

# Prune directory if present in dir_globs
if (-d $name && in_array(\@{$dir_globs{$dir}},$basename)) {
$File::Find::prune = 1;
return;
}

# Ignore excluded files
return if (-f $name && in_array(\@exclude_files, $name));
return if (-f $name && in_array(\@{$file_globs{$dir}},$basename));

# Check ownership and add to the hash if unowned (uid or gid does not exist on system)
my ($dev,$ino,$mode,$nlink,$uid,$gid) = stat($name);
if (!exists $uids{$uid} || !exists($gids{$gid})) {
push(@{$found{$dir}}, $basename);
} else {
return
}
}

# Standard in_array perl implementation
sub in_array {
my ($arr, $search_for) = @_;
my %items = map {$_ => 1} @$arr;
return (exists($items{$search_for}))?1:0;
}

# Get all uid's that exists on system and store in %uids
sub get_uids {
while (my ($name, $pw, $uid) = getpwent) {
$uids{$uid} = 1;
}
}

# Get all gid's that exists on system and store in %gids
sub get_gids {
while (my ($name, $pw, $gid) = getgrent) {
$gids{$gid} = 1;
}
}

# Print a list of unowned files in the format PARENT_DIR,BASENAME
sub print_list {
foreach my $dir (sort keys %found) {
foreach my $child (sort @{$found{$dir}}) {
print "$dir,$child\n";
}
}
}

# Prints a list of directories with the count of unowned childs in the format DIR,COUNT
sub print_count {
foreach my $dir (sort keys %found) {
print "$dir,".scalar(@{$found{$dir}})."\n";
}
}

# Call it all
&get_uids();
&get_gids();

find(\&wanted, '/');
print "List:\n";
&print_list();

print "\nCount:\n";
&print_count();

exit(0);

如果您想在您的系统上对其进行测试,只需使用通用文件创建一个测试目录结构,使用您为此目的创建的测试用户 chown 整个树,然后删除该用户。

我会接受你能给我的任何提示、提示或建议。

快乐阅读!

最佳答案

尝试从这些开始,然后看看是否还有什么可以做的。

  • 使用散列代替需要使用 in_array() 搜索的数组.这样您就可以在一个步骤中进行直接哈希查找,而不是在每次迭代时将整个数组转换为哈希。
  • 您不需要检查符号链接(symbolic link),因为您没有设置 follow,它们将被跳过。选项。
  • 最大限度地利用 _ ;避免重复 IO 操作。 _是一个特殊的文件句柄,无论何时调用 stat() 或任何文件测试,文件状态信息都会被缓存。这意味着您可以调用stat _-f _而不是 stat $name-f $name . (在我的机器上调用 -f _-f $name 快 1000 倍以上,因为它使用缓存而不是执行另一个 IO 操作。)
  • 使用 Benchmark 模块来测试不同的优化策略,看看你是否真的有所收获。例如。
    use Benchmark;
    stat 'myfile.txt';
    timethese(100_000, {
    a => sub {-f _},
    b => sub {-f 'myfile.txt'},
    });
  • 性能调优的一般原则是在尝试调优之前准确找出慢速部分的位置(因为慢速部分可能不在您期望的位置)。我的建议是使用 Devel::NYTProf ,它可以为您生成一个 html 配置文件报告。从概要中,关于如何使用它(从命令行):
    # profile code and write database to ./nytprof.out
    perl -d:NYTProf some_perl.pl

    # convert database into a set of html files, e.g., ./nytprof/index.html
    # and open a web browser on the nytprof/index.html file
    nytprofhtml --open
  • 关于用于在 Unix 上查找所有无主文件和目录的 Perl 脚本 - 如何进一步优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867686/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com