【问题标题】:How to check permission to directory and make new one如何检查目录权限并创建新目录
【发布时间】:2020-11-22 22:28:44
【问题描述】:

我在 Perl 中有脚本。我检查用户是否以-d dir_name 模式传递参数。

my $len = @ARGV;

    if($len == 2 ){
        if($ARGV[0] eq "-d"){
            $passed_name=$ARGV[1];
            return;
        }
    }

用户传递第二个参数,如/this/is/path/dir_name 现在我想检查用户是否有权访问此目录,然后检查 dir_name 是否存在(如果不存在 - 我必须这样做)。

【问题讨论】:

标签: linux perl


【解决方案1】:

现在我想检查用户是否有权访问此目录,然后检查 dir_name 是否存在(如果不存在 - 我必须创建它)。

“Check then do”邀请race condition,如果两件事同时尝试相同的事情,一个会失败。

     Process 1           Process 2
-----------------------------------
|    Check "dir"                
|                        Check "dir"
t    mkdir "dir"
i    success             
m                        mkdir "dir"
e                        error, it already exists
|
V

相反,做它并决定如果出现错误该怎么办。

Perl 的错误检查基于C's errno.h。要检查发生了什么类型的错误,请查看 %! 哈希中的错误是否为真。在这种情况下,我们正在寻找 EEXIST(文件存在)。

# It did not make the directory, and it wasn't because it already existed.
if( !mkdir $passed_name and !$!{EEXIST} ) {
    die "Could not create $passed_name: $!";
}
else {
    print "$passed_name was created, or it already existed.\n";
}

请注意,mkdir 不会创建必要的子目录。 /this/is/path/ 必须存在才能生成 /this/is/path/dir_name。如果您希望它创建子目录,请使用make_path from File::Path。或者更好的是,优秀的Path::Tiny

【讨论】:

    【解决方案2】:
    #!/usr/bin/env perl
    #
    # vim: ai ts=4 sw=4
    
    use strict;
    use warnings;
    use feature 'say';
    
    use Errno;
    use Getopt::Long qw(GetOptions);
    use Pod::Usage;
    
    my %opt;
    my @args = (
                'dir|d=s',
                'help|?',
                'man|m'
            );
    
    GetOptions( \%opt, @args ) or pod2usage(2);
    
    pod2usage(1) if $opt{help};
    pod2usage(-exitval => 0, -verbose => 2) if $opt{man};
    
    die 'Specify parameter -d [dirname]' unless $opt{dir};
    
    if( -e $opt{dir} and -d $opt{dir} ) {
        say 'Directory exist';
        say 'Readable   by effective uid/gid'   if -r $opt{dir};
        say 'Writable   by effective uid/gid'   if -w $opt{dir};
        say 'Executable by effective uid/gid'   if -x $opt{dir};
        say 'Readable   by real uid/gid'        if -R $opt{dir};
        say 'Writable   by real uid/gid'        if -W $opt{dir};
        say 'Executable by real uid/gid'        if -X $opt{dir};
        say 'Owned  by real uid'                if -O $opt{dir};
    } else {
        unless ( mkdir $opt{dir} ) {
            say 'Failed to create' if $!{EEXIST};
        }
    }
    
    __END__
    
    =head1 NAME
    
    dir_exist.pl - checks if directory exists
    
    =head1 SYNOPSIS
    
     dir_exist.pl [options] file(s)
    
     Options:
        -d,--dir    dirname to create
        -?,--help   brief help message
        -m,--man    full documentation
    
    =head1 OPTIONS
    
    =over 4
    
    =item B<-d,--dir>
    
    Specifies directory name as a parameter
    
    =item B<-?,--help>
    
    Print a brief help message and exits.
    
    =back
    
    =head1 DESCRIPTION
    
    B<This program> accepts B<input> and processes to B<output> with purpose of achiving some goal.
    
    =head1 EXIT STATUS
    
    The section describes B<EXIT STATUS> codes of the program
    
    =head1 ENVIRONMENT
    
    The section describes B<ENVIRONMENT VARIABLES> utilized in the program
    
    =head1 FILES
    
    The section describes B<FILES> which used for program's configuration
    
    =head1 EXAMPLES
    
    The section demonstrates some B<EXAMPLES> of the code
    
    =head1 REPORTING BUGS
    
    The section provides information how to report bugs
    
    =head1 AUTHOR
    
    The section describing author and his contanct information
    
    =head1 ACKNOWLEDGMENT
    
    The section to give credits people in some way related to the code
    
    =head1 SEE ALSO
    
    The section describing related information - reference to other programs, blogs, website, ...
    
    =head1 HISTORY
    
    The section gives historical information related to the code of the program
    
    =head1 COPYRIGHT
    
    Copyright information related to the code
    
    =cut
    

    文档:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      相关资源
      最近更新 更多