如何使用“正确”创建目录使用 Perl 的 mkdir 的权限?

发布于 2024-09-30 01:38:03 字数 657 浏览 0 评论 0原文

我需要这个程序的帮助。作为我项目的一部分,我需要 创建一个目录。我正在使用系统功能来执行此操作,但是 后来得知 Perl 有一个名为 mkdir 的内置函数。

我使用的是 Ubuntu 10.04。问题是 mkdir 似乎无法按需要工作。

它创建目录但权限不同。这里 是我创建目录的函数:

sub createDir {
       my ($dir,$perm) = @_;
       unless(-d $dir) {
               mkdir $dir,$perm or die "$!";
       }
}

我在程序的许多部分将其称为:

createDir('.today','0755');

创建了目录 .today 但问题在于权限, 它没有 0755 权限。

我做错了什么?

我的 Perl 详细信息是:

$perl -v

This is perl, v5.8.8 built for x86_64-linux-thread-multi

I need help with this program. As a part of my project I need to
create a directory. I was using the system function to do this, but
later was told that Perl has a builtin called mkdir.

I'm on Ubuntu 10.04. The problem is mkdir does not seem to work as needed.

It creates the directory but the permissions are different. Here
is my function that creates the directory:

sub createDir {
       my ($dir,$perm) = @_;
       unless(-d $dir) {
               mkdir $dir,$perm or die "$!";
       }
}

and I call it in many parts of my program as:

createDir('.today','0755');

the directory .today gets created but the problem is with permissions,
it does not have the 0755 permission.

What am I doing wrong?

My Perl details are:

$perl -v

This is perl, v5.8.8 built for x86_64-linux-thread-multi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落在眉间の轻吻 2024-10-07 01:38:03

您正在将权限作为字符串传递。 mkdir 期望它是数字。但字符串中的八进制数会被解释为十进制。因此,'0755' 被解释为十进制 755 并由 mkdir 使用。

要解决此问题,您可以调用子例程并向其传递数字权限:

createDir('.today',0755);

或者,您可以使用 oct 函数将八进制字符串转换为数字值。

子例程调用保持不变:

createDir('.today','0755');

但其定义更改为使用 oct 函数:

mkdir $dir,oct($perm) or die "$!";

You are passing the permission as a string. mkdir expects it to be numeric. But an octal number inside a string is interpreted as a decimal. So '0755' is being interpreted as decimal 755 and is being used by mkdir.

To fix this you can call the subroutine passing it numeric permission:

createDir('.today',0755);

Alternatively you can use make use of the oct function to convert the octal string into a numeric value.

Subroutine call remains the same:

createDir('.today','0755');

but its definition changes to use the oct function as:

mkdir $dir,oct($perm) or die "$!";
深居我梦 2024-10-07 01:38:03

修复了 codeaddict 的字符串与数字问题并注意 tchrist 的 umask 问题后,如果您需要特定权限,您应该在创建新目录后对新目录调用 chmod。

我通常不带掩码调用 mkdir,然后将目录 chmod 为我想要的权限。

从 shell 中检查这些内容:

$ perldoc -f mkdir
$ perldoc -f chmod
$ perldoc -f unmask

您还可以在调用 mkdir 之前将 umask 设置为零,如果您需要自动创建具有正确权限的目录,则需要这样做。您可能正在寻找这样的东西:

sub createDir {
        my ($dir, $perm) = @_;
        if(!-d $dir) {
                my $old = umask(0);
                mkdir($dir, $perm) or die "$!";
                umask($old);
        }
        else {
                chmod($dir, $perm);
        }
}

After you fix codeaddict's string versus number issue and note tchrist's umask issue you should call chmod on the new directory after creating it if you need specific permissions.

I usually call mkdir without the mask and then chmod the directory to the permissions I want.

Check these from the shell:

$ perldoc -f mkdir
$ perldoc -f chmod
$ perldoc -f unmask

You can also set the umask to zero before calling mkdir, you'd need to do it this way if you need to create the directory with the correct permissions atomically. Something like this is probably what you're looking for:

sub createDir {
        my ($dir, $perm) = @_;
        if(!-d $dir) {
                my $old = umask(0);
                mkdir($dir, $perm) or die "$!";
                umask($old);
        }
        else {
                chmod($dir, $perm);
        }
}
半暖夏伤 2024-10-07 01:38:03

mkdir 的第二个参数不是创建模式。它是一个掩码,将与 ~umask 进行 & 编辑以确定创建模式。如果您指定参数 0755 并且 umask 为 027,则 0755 &~ 0027 == 0750。确保所有内容均为八进制,而不是十进制。

通过 use POSIX qw[ :sys_stat_h ] 还可以使用这些常量,例如 S_IRWXUS_IWGRPS_ISVTX< /code>,但这些可能比它们的价值更麻烦。

The second argument to mkdir is not the creation mode. It is a mask which will be &ed with ~umask to determine the creation mode. If you specify an argument of 0755 and your umask is 027, then 0755 &~ 0027 == 0750. Make sure to keep everything in octal, not decimal.

There are also constants for these things available via use POSIX qw[ :sys_stat_h ], such as S_IRWXU, S_IWGRP, and S_ISVTX, but those may be more trouble than they're worth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文