English 中文(简体)
找到所有未掌握的“团结”档案和目录的文字——我如何进一步优化? [闭门]
原标题:Perl script to find all unowned files and directories on Unix - How can I optimize further? [closed]
Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it s on-topic for Stack Overflow.

Closed 11 years ago.

根据我在另一个职位上的调查结果和建议:。 如何排除索里斯·指挥中一条全局航道清单,我已决定撰写该书的每字版,并看到我如何能够优化其操作,使之比本土发现的指挥更快。 迄今为止,结果令人印象深刻!

该书的目的是报告所有未拥有的档案和目录,说明遵守审计准则的情况。 文字必须接受一份目录和档案清单,以排除(无论是通过完整途径还是用野心名称),而且必须尽可能少地处理。 其用意是,我们(我所工作的公司)支持数百个“不星”系统,并且能够运行所有这些“不星”系统(多功能职业顾问、多个平台:AIX、HP-UX、Solari和Ltlin),而没有我们首先安装或升级任何东西。 换言之,它必须拥有标准图书馆和我们期望所有系统的双筒。

我尚未对书面论点作出说明,因此,所有论点都难以在文字中加以表述。 我计划最终提出以下论点,并可能利用机会这样做:

-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
";
        }
    }
}

# 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}})."
";
    }
}

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

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

print "
Count:
";
&print_count();

exit(0);

如果你想在你的系统上测试它,就简单地建立一个带有通用文档的测试目录结构,把整个树木与测试用户相混,然后删除用户。

I ll take any hints, tips or recommendations you could give me.

快乐!

最佳回答

然后看看看你能做些什么。

  1. 使用斜体而不是需要使用<条码>的阵列。 因此,你可以在一个步骤中进行直接的盘点,而不是把整个阵列变成一个 every。

  2. 由于你没有制定<条码>(<<><>>>/代码>选择,因此你无需核对这些链接。

  3. 尽量使用<代码>_;避免重复国际交易日志的运作。 。 这意味着,请打电话<代码>stat_或-f_,而不是<代码> 姓名或-f$。 (Calling -f__ is more than1,000x supra than -f$ on my organ, which is used the cache rather than do another IO operation.)

  4. 利用Benchmark 模块,测试不同的优化战略,看看你是否实际获得任何东西。 E.g.

    use Benchmark;
    stat  myfile.txt ;
    timethese(100_000, {
        a => sub {-f _},
        b => sub {-f  myfile.txt },
    });
    
  5. 业绩考核的一般原则恰如其分,在你试图调整之前,情况迟缓(因为预计不会有缓慢部分)。 我的建议是使用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
    
问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签