For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > September 2004 > Optionally changing file perms?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Optionally changing file perms?
Doug O'Leary

2004-09-23, 3:58 pm

God, I hate bit math! I just can't get my mind around it..

I'm trying to identify new file modes for a variety of
files and directories on a given system. The first step is
to ensure directories have execute permissions if the appropriate
read permission is set. For instance, if a directory has
0660 permissions, I want to change the mode to 0770.

Here's what I have so far:

<<snip>>
open (In, "< $wws") || die "Can't read $wws - ($!)";
while (<In> )
{ chomp;
my $file = $_;
next if (! -d $file && ! -f $file); #ensure we're looking at dirs/files
my ($fmode, $fuid) = (stat($file))[2,4];
my $pmode = $fmode;
if ( -d $file)
{ # ;;
$pmode |= S_IXUSR ; # if ($pmode & S_IRUSR);
# $pmode |= S_IXGRP if ($pmode & S_IRGRP);
# $pmode |= S_IXOTH if ($pmode & S_IROTH);
}
# next if (! defined(${$uids}{$fuid}));
printf ("%04o %04o %s\n", $fmode & 07777, $pmode & 07777, $file);
}

When I run the script, the $pmode is all 0's:

# ./filter biteme
1777 0000 /tmp
0666 0000 ./testies

If I comment out the $pmode change, $pmode is the same as $fmode,
as I would expect...

if ( -d $file)
{ ;;
# $pmode |= S_IXUSR ; # if ($pmode & S_IRUSR);
# $pmode |= S_IXGRP if ($pmode & S_IRGRP);
# $pmode |= S_IXOTH if ($pmode & S_IROTH);
}

# ./filter biteme
1777 1777 /tmp
0666 0666 ./testies

I hate being such an idiot that I can't figure out a simple concept,
but can someone point out what I'm doing wrong?

Thanks.

Doug


--
--------
Senior UNIX Admin
O'Leary Computer Enterprises
dkoleary@olearycomputers.com (w) 630-904-6098 (c) 630-248-2749
resume: http://home.comcast.net/~dkoleary/resume.html

Greg Bacon

2004-09-23, 3:58 pm

In article <sdA4d.2105$o06.1757@news.flashnewsgroups.com>,
Doug O'Leary <dkoleary@olearycomputers.com> wrote:

: Here's what I have so far:
:
: <<snip>>
: open (In, "< $wws") || die "Can't read $wws - ($!)";
: while (<In> )
: { chomp;
: my $file = $_;
: next if (! -d $file && ! -f $file);
: my ($fmode, $fuid) = (stat($file))[2,4];
: my $pmode = $fmode;
: if ( -d $file)
: { # ;;
: $pmode |= S_IXUSR ; # if ($pmode & S_IRUSR);
: # $pmode |= S_IXGRP if ($pmode & S_IRGRP);
: # $pmode |= S_IXOTH if ($pmode & S_IROTH);
: }
: # next if (! defined(${$uids}{$fuid}));
: printf ("%04o %04o %s\n", $fmode & 07777, $pmode & 07777, $file);
: }
:
: When I run the script, the $pmode is all 0's:
:
: # ./filter biteme
: 1777 0000 /tmp
: 0666 0000 ./testies

I can't reproduce that result:

$ cat try
#! /usr/local/bin/perl

use warnings;
use strict;

use Fcntl ":mode";

while (<DATA> )
{ chomp;
my $file = $_;
next if (! -d $file && ! -f $file);
my ($fmode, $fuid) = (stat($file))[2,4];
my $pmode = $fmode;
if ( -d $file)
{ # ;;
$pmode |= S_IXUSR ; # if ($pmode & S_IRUSR);
# $pmode |= S_IXGRP if ($pmode & S_IRGRP);
# $pmode |= S_IXOTH if ($pmode & S_IROTH);
}
# next if (! defined(${$uids}{$fuid}));
printf ("%04o %04o %s\n", $fmode & 07777, $pmode & 07777, $file);
}

__DATA__
Doug O'Leary

2004-09-23, 8:58 pm

On 2004-09-23, Greg Bacon <gbacon@hiwaay.net> wrote:
>
> I can't reproduce that result:
>
> 0660 0760 ./foo
> 0770 0770 ./bar
>
> Hmm.. did you import the S_I* symbols using the Fcntl module? Is
> perl treating them as barewords in your program, in other words?


Well, it's good to know the logic is correct. I tried doing
the use Fcntl thing; however, I get a core dump on my HP 11.00 perl
v5.6. While browing through documentation, I found
use POSIX ":fcntl_h". My half-formed thought was that the POSIX
module was Fcntl's successor. POSIX *seemed* to work - at least it
compiled and ran. Apparently, those constants aren't defined...

I'll see about finding what the constants are defined as in the
fcntl module and use them directly.

Thanks for your help, I appreciate it.

Doug

--
--------
Senior UNIX Admin
O'Leary Computer Enterprises
dkoleary@olearycomputers.com (w) 630-904-6098 (c) 630-248-2749
resume: http://home.comcast.net/~dkoleary/resume.html

Anno Siegel

2004-09-29, 11:02 am

Doug O'Leary <dkoleary@olearycomputers.com> wrote in comp.lang.perl.misc:
> On 2004-09-23, Greg Bacon <gbacon@hiwaay.net> wrote:
>
> Well, it's good to know the logic is correct. I tried doing
> the use Fcntl thing; however, I get a core dump on my HP 11.00 perl
> v5.6. While browing through documentation, I found
> use POSIX ":fcntl_h". My half-formed thought was that the POSIX
> module was Fcntl's successor. POSIX *seemed* to work - at least it
> compiled and ran. Apparently, those constants aren't defined...


They are, but strangely not all of them under the ":fcntl_h" tag.
Some are in ":sys_stat_h". Request both, or just "use POSIX;"
without qualification to get them all.

Anno
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com