theillien1 has asked for the wisdom of the Perl Monks concerning the following question:
I wrote a script which will scan local, mounted file systems for world-writable files excluding any which are in a hash generated from a list in a file. It utilizes Fcntl in order to make use of the file mode constants.
The current version runs stat three times against a file:
return unless (((stat)[2] & S_IWUSR) && ((stat)[2] & S_IWGRP) && ((s +tat)[2] & S_IWOTH));
This is, of course, less than ideal since it is running one operation three times against every file. That said, it works and the output contains only the files I'm looking for minus those I've explicitly excluded.
It was suggested that I instead set a variable which contains these constants and run a bitwise AND with the $mode field from stat(). I tried this. The result was that the script returned every file, even those not world-writable.
Additionally, I get a "Possible precedence problem" error. I'm inexperienced with bitwise operations so I don't know what I'm encountering with that particular error.
# Some code my $mask = S_IWUSR | S_IWGRP | S_IWOTH; # Some more code return unless $dirStats[2] & $mask == $mask; # Even more code
Possible precedence problem on bitwise & operator at ./ww_files-v4-2 +.pl line 106.
Undestandably, the context is extremely vague here. I'm not sure if etiquette allows for the posting of entire scripts so I've pastebin'ed it: http://pastebin.com/A8d3K2mR
EDIT: All answers pointed to using parens around the bitwise AND before comparing to $mask. This solved both the "Possible precedence" error as well as the results being unexpected.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: bitwise AND against a variable containing Fnctl constants not returning desired results
by no_slogan (Deacon) on Jun 05, 2014 at 22:29 UTC | |
|
Re: bitwise AND against a variable containing Fnctl constants not returning desired results
by RichardK (Parson) on Jun 05, 2014 at 22:32 UTC | |
|
Re: bitwise AND against a variable containing Fnctl constants not returning desired results
by Old_Gray_Bear (Bishop) on Jun 05, 2014 at 22:35 UTC |