Professional Web Applications Themes

how to open a file with 666 permission - PERL Beginners

Hai! My requirement is to open file with 666 permissions.[If fine doesn't exists it should get created ].Iam doing as below,is this ok. =========================== sysopen(LOG,"$main::TRACELOGFILE",O_CREATE,0666) or die "Can't open trace file $main::TRACELOGFILE"; ============================ ami doing any thing wrong ,if yes correct me.If file is created once it is working fine.For the first time it is giving below error =========================== Can't open trace file /var/opt/XX/log/XX08013.log ========================== Arjun Confidentiality Notice The information contained in this electronic message and any attachments tothis message are intended for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If you are not the ...

  1. #1

    Default how to open a file with 666 permission


    Hai!
    My requirement is to open file with 666 permissions.[If fine doesn't
    exists it should get created ].Iam doing as below,is this ok.

    ===========================
    sysopen(LOG,"$main::TRACELOGFILE",O_CREATE,0666) or die "Can't open
    trace file $main::TRACELOGFILE";
    ============================

    ami doing any thing wrong ,if yes correct me.If file is created once it
    is working fine.For the first time it is giving below error
    ===========================
    Can't open trace file /var/opt/XX/log/XX08013.log
    ==========================


    Arjun






    Confidentiality Notice

    The information contained in this electronic message and any attachments tothis message are intended
    for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
    you are not the intended recipient, please notify the sender at Wipro com immediately
    and destroy all copies of this message and any attachments.
    Arjun Guest

  2. #2

    Default RE: how to open a file with 666 permission

    com wrote: 

    You need to set umask to 0 before creating the file.

    But don't do that. It's inadvisable to mess with the umask in a program,
    IMO. If the user wants to create files as 666, let him set the umask before
    running your program.
    Bob Guest

  3. #3

    Default Re: how to open a file with 666 permission

    com wrote: 

    Hello,
     

    You should include the $! variable in your error message so you know *why* it
    failed. The third argument to sysopen must include *one* of either O_RDONLY
    or O_WRONLY or O_RDWR (read only OR write only OR read and write.)

    Read the section "Open A la C" in perlopentut for more details on sysopen.

    perldoc perlopentut



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  4. #4

    Default Re: how to open a file with 666 permission

    Bob Showalter wrote: 
    >
    > You need to set umask to 0 before creating the file.
    >
    > But don't do that. It's inadvisable to mess with the umask in a program,
    > IMO. If the user wants to create files as 666, let him set the umask before
    > running your program.[/ref]

    Or use chmod() on the file to set whatever permissions you desire.


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  5. Moderated Post

    Default RE: how to open a file with 666 permission

    Removed by Administrator
    Arjun Guest
    Moderated Post

  6. Moderated Post

    Default Re: how to open a file with 666 permission

    Removed by Administrator
    John Guest
    Moderated Post

  7. #7

    Default Re: how to open a file with 666 permission

    Bob Showalter wrote: 

    Why would that be inadvisable?
     

    If the program, for some reason, requires that a file it creates has
    certain permissions, isn't it better to have the program set those
    permissions?

    One way may be to have umask and/or the permissions set as
    configuration variables.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  8. #8

    Default RE: how to open a file with 666 permission

    Gunnar Hjalmarsson wrote: 
    >
    > Why would that be inadvisable?[/ref]

    The spirit of umask is to allow the user/sysadmin to control the "policy"
    for permissions. It's desinged to be under the user's control. If you change
    the umask in the program, you force me to chmod the file after I run the
    program if I don't like the bits.
     
    >
    > If the program, for some reason, requires that a file it creates has
    > certain permissions, isn't it better to have the program set those
    > permissions?[/ref]

    Why would the program itself require this? Perhaps the way I'm _using_ the
    program requires this, but then let me control the environment.
    Bob Guest

  9. #9

    Default RE: how to open a file with 666 permission

    On Wed, 29 Sep 2004 com wrote:
     

    Still not right though -- it was suggested to you that you use the $!
    variable in your die statement so that you get the error message, hence:

    do_stuff( $arg) or die "Can't do stuff!"; # bad!
    do_stuff( $arg) or die "Can't do stuff!" $!; # good!

    Also, as another person noted, chown() and chmod() are built in to Perl,
    so there's no need to make a system call to do these operations.

    Also, why are you prefixing your variables with $main:: ? There can be
    good reasons to do this, but they're esoteric -- most of the time you
    can safely leave out the 'main::' part.



    --
    Chris Devers
    Chris Guest

  10. #10

    Default RE: how to open a file with 666 permission

    On Wed, 29 Sep 2004, Bob Showalter wrote:
     
    >
    > Why would the program itself require this? Perhaps the way I'm _using_
    > the program requires this, but then let me control the environment.[/ref]

    There's nothing wrong with enforcing file permissions programmatically.

    Maybe the program creates a log file that shouldn't be readable by
    anyone. Maybe the program is a code generator that produces other files
    which should be executable (I can't remember anyone doing this, but
    there's no reason why it couldn't be reasonably be done).

    Messing with the general umask isn't polite, but it's okay to manage the
    permissions of files a program governs, if the problem it was written to
    solve has such a requirement.


    --
    Chris Devers
    Chris Guest

  11. #11

    Default RE: how to open a file with 666 permission

    Chris Devers wrote: 
    > >
    > > Why would the program itself require this? Perhaps the way I'm
    > > _using_ the program requires this, but then let me control the
    > > environment.[/ref]
    >
    > There's nothing wrong with enforcing file permissions
    > programmatically.
    >
    > Maybe the program creates a log file that shouldn't be readable by
    > anyone.[/ref]

    Anyone? Maybe you mean owner only? Fine, use creation bits of 0600.
     

    Fine, use creation bits of 0777.

    Neither of these require fooling with umask.

    My gripe is with a program that decides a file _needs_ to be created as 666,
    for example.
     
    Bob Guest

  12. #12

    Default RE: how to open a file with 666 permission

    On Wed, 29 Sep 2004, Bob Showalter wrote:
     
    >
    > Fine, use creation bits of 0777.
    >
    > Neither of these require fooling with umask.[/ref]

    Did I say umask was required? If so, that wasn't what I meant.

    I'm not getting into the mechanism of how permissions get set; I'm
    saying that there are clear, justifiable cases where a program can have
    a legitmate need to set permissions in any way the programmer sees fit.
    The user of such a program may choose to override these settings, but
    that may or may not invalidate the reasons for originally setting them.
     

    Everything has a place. Sometimes it can make sense.


    --
    Chris Devers
    Chris Guest

  13. #13

    Default Re: how to open a file with 666 permission

    Bob Showalter wrote: 
    >
    > Fine, use creation bits of 0777.[/ref]

    Are you saying that

    open FH, "> $file";
    chmod 0777, $file;

    is fine, while

    umask 0;
    sysopen FH, $file, O_CREAT|O_RDWR, 0777;

    is not? In that case, I'm really confused by now. ;-)
     

    If you want to set permission 777 via e.g. sysopen() or mkdir(), you
    do need to set umask, unless it happens to be 0 to start with.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  14. #14

    Default RE: how to open a file with 666 permission

    Gunnar Hjalmarsson wrote: 
    > >
    > > Fine, use creation bits of 0777.[/ref]
    >
    > Are you saying that
    >
    > open FH, "> $file";
    > chmod 0777, $file;
    >
    > is fine, while
    >
    > umask 0;
    > sysopen FH, $file, O_CREAT|O_RDWR, 0777;
    >
    > is not? In that case, I'm really confused by now. ;-)[/ref]

    No, I'm saying that neither is fine. Just pass 0600 or 0777 to open(2) and
    let the umask determine the resulting permissions. I don't want the program
    to decide for me that the file needs to have world write priv, for example.

    I'm still struggling to think of a real-world situation where a file would
    _have_ to be created as rw-rw-rw- or rwxrwxrwx.
    Bob Guest

  15. #15

    Default Re: how to open a file with 666 permission

    Bob Showalter wrote: 

    There may be a need to grant more than one user write access. CGI
    scripts are running as the webserver user by default, and if you let
    e.g. sendmail start a process, another user may appear.

    Personally I prefer to handle these situations via GID, but you may
    not have permissions to create groups and change GID.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

Similar Threads

  1. Open file, make changes, save file, close, re-open, file contents not changed
    By brock@bergdesign.com in forum Adobe Illustrator Macintosh
    Replies: 7
    Last Post: March 7th, 07:29 PM
  2. Replies: 0
    Last Post: August 22nd, 11:46 AM
  3. Unable To Open File "..." Because It Is Already Open With Write Permission
    By PixelaseR webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 1
    Last Post: August 18th, 06:33 PM
  4. Replies: 3
    Last Post: August 15th, 06:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139