Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How do I get all the permission flags on a file?

by arturo (Vicar)
on Sep 25, 2000 at 23:04 UTC ( [id://33944]=perlquestion: print w/replies, xml ) Need Help??

arturo has asked for the wisdom of the Perl Monks concerning the following question: (files)

I want to take $file1 and give it the same permissions as $file2, which may be owned by another user. Perl's stat() function returns the mode, but it's not obvious how to get from the 5-digit decimal number returned to octal values (executing oct($mode) on it doesn't give a sensible value). So, how do you do the conversion?

Originally posted as a Categorized Question.

  • Comment on How do I get all the permission flags on a file?

Replies are listed 'Best First'.
Re: How do I get all the permission flags on a file?
by merlyn (Sage) on Sep 25, 2000 at 23:20 UTC
    You don't need octal, ever. You need a number. That number from stat can be handed over directly. For safety, you may want to bit-and it with 07777, but I don't think there'll be any nonsense anyway.
Re: How do I get all the permission flags on a file?
by BastardOperator (Monk) on Sep 26, 2000 at 00:32 UTC
    If merlyn's answer confuses you at all, try this:
    sub return_mode { my $mode = shift; my @modelist = (($mode & 7000)>>9, ($mode & 0700)>>6, ($mode & 007 +0)>>3, ($mode &0007)); return join('', @modelist); }
    Also, I don't know if you would be concerned with ACL's at all, I'm not sure how you would handle those if you were, just wanted to give you a heads up depending on how detailed you're being.
      sub return_mode { my $mode = shift; my @modelist = (($mode & 7000)>>9, ($mode & 0700)>>6, ($mode & 007 +0)>>3, ($mode &0007)); return join('', @modelist); }
      Hmm. Is that just a difficult (and incorrect, see "7000") way of doing this...?
      sub return_mode { sprintf "%04o", (shift) & 07777; }

      -- Randal L. Schwartz, Perl hacker

        Hmm. Is that just a difficult (and incorrect) way of being a human being...?

        Ya know Randall, the more I watch you, the more I dislike you. I guess it would have been way too difficult for you to simply say something like
        "A slightly shorter method would be ..., and you need to use 07777 for reason blah".
        Yeah, I could see how much that would strain you. Frankly Randall, your Perl skills are fabulous, but your people skills need work.

        What I'd like to know is why you didn't just offer that answer in the first place? You certainly gave a previous person his homework answer without thought, why wouldn't you have offered this up rather than what might have been an answer that this individual didn't get?

        I don't claim to be some perl guru, I'm completely self-taught and haven't been doing it but sparsely for about a year, I think that's probably the case for a lot of folks here. We can't all have the experience that you have. I pick up bits and pieces here and there, and this is the only way I've ever seen to do this, besides the fact that it's always worked fine for me. The funny thing is, I probably take criticism better than anyone I've ever met, but constructive criticism is different than sarcastic criticism.

        Please try to realize that we're not all guru's and some of us won't stand to be degraded.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found