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


In reply to -w $path Cross Platform Issues by bkembree

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.