0

I have a script written using ActivePerl that creates files where the user specifies. If the directory doesn't already exist, it uses mkpath to attempt to create it and trap any error conditions (such as not having permission to create the directory there). This seems fine. What I'm running into trouble with is determine the permissions of a directory that already exists. I don't want a user to be able to specify a protected folder that they can read from (c:\windows\system32 comes to mind) and the script silently fail attempting to create its files there.

From other perl examples on the web I've tried using the following, but I'm always given 0777 as the result for any existing directory:

use File::stat;
#
#...
#
my $info = stat($candiDirectory);
my $retMode = $info->mode;
my $mymode = sprintf("0%o, $retMode & 07777);
print "retMode for $candiDirectory is $mymode \n";

While this seems reasonable for unix/Linux, I'd be surprised if it didn't require something different under Win32 or 64.

  • I think you need to use [`Win32::File`](http://search.cpan.org/~jdb/Win32-File-0.06/File.pm) instead. – admdrew Dec 05 '13 at 21:32
  • Thanks admdrew. I've contacted that author via his link on cpan for example code using it. None were included there. Can you point me to any? – consultmac Dec 06 '13 at 14:34

1 Answers1

0

from perldoc perlfunc:

-w $filename

unless (-w $filename) {
  say "i can't write this file";
}
Len Jaffe
  • 3,442
  • 1
  • 21
  • 28
  • Thank you for your input. Unfortunately, in my Activestate ActivePerl environment, using that technique always results in the say (or print, in my case) statement being reached regardless of my user's actual ability to write to the file. – consultmac Dec 06 '13 at 14:31
  • Well that's hardly cross-platform :-( – Len Jaffe Dec 06 '13 at 16:16