Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

File::Path and mode with sgid

by Hena (Friar)
on Apr 27, 2012 at 08:58 UTC ( #967556=perlquestion: print w/ replies, xml ) Need Help??
Hena has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I have a question with regards to File::Path module. I wanted to do a directory path with 2750 or rwxr-s--- permissions. However instead of getting that, I got something completely different. Here's an example

perl -e 'BEGIN{use File::Path qw(make_path)} make_path("foo/bar", { mode => 2750 })'

Result of that is following:
d-w-rwxrwT+

This when converted to numerical mode with stat -c '%a %n' foo is 1274.

This seems like a bug in the module. And so is there a way to create a path like this without resorting to mkdir -pm2750 foo/bar and chgrp?

Comment on File::Path and mode with sgid
Select or Download Code
Re: File::Path and mode with sgid
by Eliya (Vicar) on Apr 27, 2012 at 09:12 UTC

    The mode is an octal number, so you need 02750 (note the leading zero).

      That won't produce proper permissions.
      $ perl -e 'BEGIN{use File::Path qw(make_path)} make_path("foo/bar", { +mode => 02750 })' $ ls -lh total 4.0K drwxr-x--- 3 hena hena 4.0K 2012-04-27 12:14 foo $ mkdir faa $ chmod 2750 faa $ ls -lh total 8.0K drwxr-s--- 2 hena hena 4.0K 2012-04-27 12:15 faa drwxr-x--- 3 hena hena 4.0K 2012-04-27 12:14 foo

        Well, it seems File::Path::make_path uses plain mkdir calls under the hood, which don't honor special bits.1  I.e., the following wouldn't work either

        $ perl -e 'mkdir "foo", 02750' $ ls -ld foo drwxr-x--- 2 eliya eliya 4096 Apr 27 11:40 foo/

        while chmod does

        $ perl -e 'mkdir "foo", 0750; chmod 02750, "foo"' $ ls -ld foo drwxr-s--- 2 eliya eliya 4096 Apr 27 11:41 foo/

        ___

        1 This is OS specific; it has nothing to do with Perl.  Perl just passes through the mode value to the mkdir system call (see man 2 mkdir).

        The command mkdir -pm2750 foo also has to apply a chmod after having used mkdir:

        $ strace mkdir -pm2750 foo ... mkdir("foo", 02750) = 0 open("foo", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = 3 fstat(3, {st_mode=S_IFDIR|0750, st_size=4096, ...}) = 0 fchmod(3, 02750) = 0 <--- ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967556]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2014-07-23 18:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (149 votes), past polls