bkembree has asked for the wisdom of the Perl Monks concerning the following question:

Background:

I am a developer working on a wxPerl application distributed to end users on multiple platforms. Part of the app includes a file transfer manager that allows the user to choose a download directory for file transfers from the server.


The Issue:

We've had issues with users not having write permissions to the download directory they choose.


The Intended Solution:

When the user sets their download directory, or adds a new download, confirm they have write access to the download directory with a '-w $path' check. If they do not have write access prompt for a different download directory.


The Problem:

'-w $path' is not fully cross platform compatible. It works great on Linux but has issues on Mac and Windows. Consider the following script:

use strict; use warnings; my $path = $ARGV[0]; exit unless $path; if ( -w $path ) { print "'$path' is writeable\n"; eval { open my $fh, '>', "$path/foo.txt" or die $!; print $fh "foo\n"; close $fh; }; if ( $@ ) { print "\twrite failed: '$@'\n"; } else { print "\twrite succeeded\n"; unlink "$path/foo.txt" or die $!; } } else { print "'$path' is not writeable\n"; eval { open my $fh, '>', "$path/foo.txt" or die $!; print $fh "foo\n"; close $fh; }; if ( $@ ) { print "\twrite failed: '$@'\n"; } else { print "\twrite succeeded\n"; unlink "$path/foo.txt" or die $!; } }
- On Mac OS X a user can lock a folder in the Finder Info dialog and '-w $path' will still return true:
perl is_path_writeable.pl folder\ with\ spaces 'folder with spaces/' is writeable write failed: 'Operation not permitted at is_path_writeable.pl + line 11. '
- On Windows '-w $path' will return false for a directory that the user can actually write to:
perl is_path_writeable.pl "C:\Documents and Settings\bke\My Documents" 'C:\Documents and Settings\bke\My Documents' is not writeable write succeeded


I've done some basic google searches and a search of perlmonks but haven't found anything useful regarding -w on windows and Mac.


We try to keep our non-core module prerequisites to a minimum, but am I going to need to resort to platform specific modules to check the path on Mac/Windows?

Or am I better off just attempting to write a file in the $path and check for errors?


Thanks,

Brad Embree

Replies are listed 'Best First'.
Re: -w $path Cross Platform Issues
by ahmad (Hermit) on Jul 24, 2010 at 18:40 UTC

    If it's being odd, then just make your own, Here's what I'm thinking of:

    sub WriteableDir { my $DIR = shift; if (open(F,">$DIR/tmp.file") ) { close(F); unlink("$DIR/tmp.file"); return 1; }else{ return 0; } }
      Technically, it looks feasible. But there might be some issues.

      Specifically, I suspect that if the "end users" are Joe Bagadonuts and Sally Soccermom that their (firewalls|A/V packages|observational skills) will complain rather vehemently, unless bkembree manages to convince them in advance that he's not passing malware.

Re: -w $path Cross Platform Issues
by Anonymous Monk on Jul 24, 2010 at 18:54 UTC
    Or am I better off just attempting to write a file in the $path and check for errors?

    You're better off just trying to write, because -w checks for permissions, not for locks