Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Permission problem in creating directory!

by madtoperl (Monk)
on Apr 13, 2006 at 13:04 UTC ( #543062=perlquestion: print w/ replies, xml ) Need Help??
madtoperl has asked for the wisdom of the Perl Monks concerning the following question:

I want to dump data from a mysql database using perl 5.8
I am trying to create a directory which is writable to everyone
so the user which the mysqld is running under has access permission to write the file
this is the piece of code for creating the directory
my $dump_dir = $output_dir."/".$dbname; if(! -e $dump_dir){ mkdir($dump_dir, 0777) or throw("Couldn't make ".$dump_dir." $! "); }
OUTPUT:
but this is the privileges i got after creating the directory
drwxr-xr-x 2 lec sieter 8192 Apr 13 15:15 boreg_new_2
Does anyone know let me know why the permission is still not writable for everyone?

2006-10-07 Unapproved by planetscape once evidence of habitual plagiarism uncovered.

Comment on Permission problem in creating directory!
Download Code
Re: Permission problem in creating directory!
by Corion (Pope) on Apr 13, 2006 at 13:08 UTC

    See mkdir and umask. In short, the mkdir function honors $ENV{UMASK}. If you really intend to make the directory world-writable, I guess the following code will work:

    my $old_umask = umask; umask 0777; mkdir $dump_dir or die "Couldn't create '$dump_dir': $!"; umask $old_umask;

      The umask is an attribute of the process (like its current working directory or nice level) and is manipulated by the system call of the same name. It's not related to the process' environment.</pedant>

      (Well, it is part of the process' environment, but it's not contained in %ENV or char *env or what have you . . . :)

      Update: And I just noticed, umask returns the previous value when you call it so you could just do my $old_umask = umask 077; in one swell foop.</nit>

      my $old_umask = umask; umask 0777; mkdir $dump_dir or die "Couldn't create '$dump_dir': $!"; umask $old_umask;

      This actually does the opposite of what the OP asked for. A permissive umask is a smaller number (IE: less bits turned on) than a non-permissive mask. Setting the umask to 0777 ensures that no permissions are assigned, regardless of what mode you set for mkdir (or if you leave it as the default, as above). The number in the umask is applied to the mode bits of a created file with the following logic:

      MODE & ~MASK

      So, for the mode bits 0777 and a umask of 0777:

      perl -e'printf "%o\n", 0777 & ~0777' => 0 (or 0000)

      For a umask of 0000:

      perl -e'printf "%o\n", 0777 & ~0000' => 777 (or 0777)

      By default (at least it is here), the umask is set to 022. If we use the logic above with this knowledge, we can see why the OP's mode argument to mkdir didn't take affect:

      $ perl -e'printf "umask: %04o\n", umask; printf "mode: %04o\n", 0777; +printf "masked mode : %04o\n", 0777 & ~umask' umask: 0022 mode: 0777 masked mode: 0755 $

      In short, the answer to the OP's question is to set the umask to 0. Note the following:

      $ perl -e'umask 0; printf "umask: %04o\n", umask; printf "mode: %04o\n +", 0777; printf "ma sked mode: %04o\n", 0777 & ~umask' umask: 0000 mode: 0777 masked mode: 0777 $ perl -e'umask 0; mkdir "FOO", 0777' $ ls -dl FOO drwxrwxrwx 2 matt matt 6 Apr 13 20:07 FOO $

      umask is slightly counter-intuitive, but I found that when I saw the boolean math it helped me to understand it much better. I hope this helps.

      Best Regards

      m.att

        Thank you, this really helped me!
Re: Permission problem in creating directory!
by ahmad (Hermit) on Apr 13, 2006 at 14:53 UTC

    try chmod the dir after creating it using :

    `chmod 777 $dump_dir`

    HTH

      chmod 0777, $dump_dir is preferable to `chmod 777 $dump_dir`.  No backticks needed; chmod is a Perl builtin.

      (This is just a side-note.  umask is the correct solution.)

Re: Permission problem in creating directory!
by liverpole (Monsignor) on Oct 06, 2006 at 23:47 UTC
    Why would you steal someone else's question??  It's apparent that the original post was plagiarized from this site, written June 4, 2004:
    I am currently writing a script to dump data from a mysql database I am trying to create a directory which is writable to by everyone so the user which the mysqld is running under has access to write the file this is the type of command I am creating the directory with my $dump_dir = $output_dir."/".$dbname; if(! -e $dump_dir){ mkdir($dump_dir, 0777) or throw("Couldn't make ".$dump_dir." $! "); } but this is the privileges i get for the directory drwxr-xr-x 2 lec ensembl 8192 Jun 4 15:15 briggsae_new_2 does anyone know what I am doing wrong

    Sure, you've changed the group name from "lec ensembl" to "lec sieter", but the question has almost the same wording, the code is identical, and you even kept time ("15:15") and size (8192) of the file the same.

    So I repeat, madtoperl, "Why are you plagiarizing other people's questions?!"


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-07-31 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (255 votes), past polls