如何检查目录权限并创建新目录
How to check permission to directory and make new one
我有 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
是否存在(如果不存在 - 我必须这样做)。
Now I want to check if user has permission to this directory, and then check if dir_name exists (if not - I have to make it).
“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
will not make the necessary subdirectories. /this/is/path/
must exist to make /this/is/path/dir_name
. If you want it to make the subdirectories, use make_path
from File::Path. Or better yet, the excellent Path::Tiny.
#!/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
文档:
我有 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
是否存在(如果不存在 - 我必须这样做)。
Now I want to check if user has permission to this directory, and then check if dir_name exists (if not - I have to make it).
“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
will not make the necessary subdirectories. /this/is/path/
must exist to make /this/is/path/dir_name
. If you want it to make the subdirectories, use make_path
from File::Path. Or better yet, the excellent Path::Tiny.
#!/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
文档: