mkdir() fails to apply correct permissions

R

Randall Perry

I've got a perl script, run by root, with the following command:
mkdir("cgi-bin", 0770) || die "Couldn't create cgi-bin dir.\n";

The directory is created ok, but the perms are 0750 . Is this a perl
bug? I'm using 5.8.4.


Randy
 
R

Randal L. Schwartz

Randall> I've got a perl script, run by root, with the following command:
Randall> mkdir("cgi-bin", 0770) || die "Couldn't create cgi-bin dir.\n";

Randall> The directory is created ok, but the perms are 0750 . Is this a perl
Randall> bug? I'm using 5.8.4.

The third parameter is not an absolute permission, but rather is
affected by the umask, as documented. I suspect your current umask
includes the 020 bit.

print "Just another Perl hacker,"; # the original
 
T

Tintin

Randall Perry said:
I've got a perl script, run by root, with the following command:
mkdir("cgi-bin", 0770) || die "Couldn't create cgi-bin dir.\n";

The directory is created ok, but the perms are 0750 . Is this a perl
bug? I'm using 5.8.4.

Not a bug, just a problem with you reading the documentation.

$ perldoc -f mkdir
mkdir FILENAME,MASK
mkdir FILENAME
Creates the directory specified by FILENAME, with permissions
specified by MASK (as modified by "umask"). If it succeeds it
returns true, otherwise it returns false and sets $! (errno). If
omitted, MASK defaults to 0777.

In general, it is better to create directories with permissive
MASK, and let the user modify that with their "umask", than it
is to supply a restrictive MASK and give the user no way to be
more permissive. The exceptions to this rule are when the file
or directory should be kept private (mail files, for instance).
The perlfunc(1) entry on "umask" discusses the choice of MASK in
more detail.
 
G

Gunnar Hjalmarsson

Randall said:
I've got a perl script, run by root, with the following command:
mkdir("cgi-bin", 0770) || die "Couldn't create cgi-bin dir.\n";

The directory is created ok, but the perms are 0750 .

As others have explained, your stated permissions are modified by
umask. To have the resulting permissions equal your stated
permissions, you can add

umask 0;

to your script somewhere before the mkdir statement. Another
possibility is to set the permissions with chmod() instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top