home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / File / HomeDir.pm next >
Encoding:
Perl POD Document  |  2009-03-27  |  18.3 KB  |  619 lines

  1. package File::HomeDir;
  2.  
  3. # See POD at end for documentation
  4.  
  5. use 5.00503;
  6. use strict;
  7. use Carp       ();
  8. use Config     ();
  9. use File::Spec ();
  10.  
  11. # Globals
  12. use vars qw{$VERSION @ISA @EXPORT @EXPORT_OK $IMPLEMENTED_BY};
  13. BEGIN {
  14.     $VERSION = '0.86';
  15.  
  16.     # Inherit manually
  17.     require Exporter;
  18.     @ISA       = qw{ Exporter };
  19.     @EXPORT    = qw{ home     };
  20.     @EXPORT_OK = qw{
  21.         home
  22.         my_home
  23.         my_desktop
  24.         my_documents
  25.         my_music
  26.         my_pictures
  27.         my_videos
  28.         my_data
  29.         users_home
  30.         users_desktop
  31.         users_documents
  32.         users_music
  33.         users_pictures
  34.         users_videos
  35.         users_data
  36.     };
  37.  
  38.     # %~ doesn't need (and won't take) exporting, as it's a magic
  39.     # symbol name that's always looked for in package 'main'.
  40. }
  41.  
  42. # Inlined Params::Util functions
  43. sub _CLASS ($) {
  44.     (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s) ? $_[0] : undef;
  45. }
  46. sub _DRIVER ($$) {
  47.     (defined _CLASS($_[0]) and eval "require $_[0];" and ! $@ and $_[0]->isa($_[1]) and $_[0] ne $_[1]) ? $_[0] : undef;
  48. }
  49.  
  50. # Platform detection
  51. if ( $IMPLEMENTED_BY ) {
  52.     # Allow for custom HomeDir classes
  53.     # Leave it as the existing value
  54. } elsif ( $^O eq 'MSWin32' ) {
  55.     # All versions of Windows
  56.     $IMPLEMENTED_BY = 'File::HomeDir::Windows';
  57. } elsif ( $^O eq 'darwin' && $Config::Config{ptrsize} != 8 ) {
  58.     # Modern Max OS X, but fallback to unix on 64 bit
  59.     $IMPLEMENTED_BY = 'File::HomeDir::Darwin';
  60. } elsif ( $^O eq 'MacOS' ) {
  61.     # Legacy Mac OS
  62.     $IMPLEMENTED_BY = 'File::HomeDir::MacOS9';
  63. } else {
  64.     # Default to Unix semantics
  65.     $IMPLEMENTED_BY = 'File::HomeDir::Unix';
  66. }
  67. unless ( _DRIVER($IMPLEMENTED_BY, 'File::HomeDir::Driver') ) {
  68.     Carp::croak("Missing or invalid File::HomeDir driver $IMPLEMENTED_BY");
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75. #####################################################################
  76. # Current User Methods
  77.  
  78. sub my_home {
  79.     $IMPLEMENTED_BY->my_home;
  80. }
  81.  
  82. sub my_desktop {
  83.     $IMPLEMENTED_BY->can('my_desktop')
  84.         ? $IMPLEMENTED_BY->my_desktop
  85.         : Carp::croak("The my_desktop method is not implemented on this platform");
  86. }
  87.  
  88. sub my_documents {
  89.     $IMPLEMENTED_BY->can('my_documents')
  90.         ? $IMPLEMENTED_BY->my_documents
  91.         : Carp::croak("The my_documents method is not implemented on this platform");
  92. }
  93.  
  94. sub my_music {
  95.     $IMPLEMENTED_BY->can('my_music')
  96.         ? $IMPLEMENTED_BY->my_music
  97.         : Carp::croak("The my_music method is not implemented on this platform");
  98. }
  99.  
  100. sub my_pictures {
  101.     $IMPLEMENTED_BY->can('my_pictures')
  102.         ? $IMPLEMENTED_BY->my_pictures
  103.         : Carp::croak("The my_pictures method is not implemented on this platform");
  104. }
  105.  
  106. sub my_videos {
  107.     $IMPLEMENTED_BY->can('my_videos')
  108.         ? $IMPLEMENTED_BY->my_videos
  109.         : Carp::croak("The my_videos method is not implemented on this platform");
  110. }
  111.  
  112. sub my_data {
  113.     $IMPLEMENTED_BY->can('my_data')
  114.         ? $IMPLEMENTED_BY->my_data
  115.         : Carp::croak("The my_data method is not implemented on this platform");
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. #####################################################################
  123. # General User Methods
  124.  
  125. sub users_home {
  126.     $IMPLEMENTED_BY->can('users_home')
  127.         ? $IMPLEMENTED_BY->users_home( $_[-1] )
  128.         : Carp::croak("The users_home method is not implemented on this platform");
  129. }
  130.  
  131. sub users_desktop {
  132.     $IMPLEMENTED_BY->can('users_desktop')
  133.         ? $IMPLEMENTED_BY->users_desktop( $_[-1] )
  134.         : Carp::croak("The users_desktop method is not implemented on this platform");
  135. }
  136.  
  137. sub users_documents {
  138.     $IMPLEMENTED_BY->can('users_documents')
  139.         ? $IMPLEMENTED_BY->users_documents( $_[-1] )
  140.         : Carp::croak("The users_documents method is not implemented on this platform");
  141. }
  142.  
  143. sub users_music {
  144.     $IMPLEMENTED_BY->can('users_music')
  145.         ? $IMPLEMENTED_BY->users_music( $_[-1] )
  146.         : Carp::croak("The users_music method is not implemented on this platform");
  147. }
  148.  
  149. sub users_pictures {
  150.     $IMPLEMENTED_BY->can('users_pictures')
  151.         ? $IMPLEMENTED_BY->users_pictures( $_[-1] )
  152.         : Carp::croak("The users_pictures method is not implemented on this platform");
  153. }
  154.  
  155. sub users_videos {
  156.     $IMPLEMENTED_BY->can('users_videos')
  157.         ? $IMPLEMENTED_BY->users_videos( $_[-1] )
  158.         : Carp::croak("The users_videos method is not implemented on this platform");
  159. }
  160.  
  161. sub users_data {
  162.     $IMPLEMENTED_BY->can('users_data')
  163.         ? $IMPLEMENTED_BY->users_data( $_[-1] )
  164.         : Carp::croak("The users_data method is not implemented on this platform");
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. #####################################################################
  173. # Legacy Methods
  174.  
  175. # Find the home directory of an arbitrary user
  176. sub home (;$) {
  177.     # Allow to be called as a method
  178.     if ( $_[0] and $_[0] eq 'File::HomeDir' ) {
  179.         shift();
  180.     }
  181.  
  182.     # No params means my home
  183.     return my_home() unless @_;
  184.  
  185.     # Check the param
  186.     my $name = shift;
  187.     if ( ! defined $name ) {
  188.         Carp::croak("Can't use undef as a username");
  189.     }
  190.     if ( ! length $name ) {
  191.         Carp::croak("Can't use empty-string (\"\") as a username");
  192.     }
  193.  
  194.     # A dot also means my home
  195.     ### Is this meant to mean File::Spec->curdir?
  196.     if ( $name eq '.' ) {
  197.         return my_home();
  198.     }
  199.  
  200.     # Now hand off to the implementor
  201.     $IMPLEMENTED_BY->users_home($name);
  202. }
  203.  
  204.  
  205.  
  206.  
  207.  
  208. #####################################################################
  209. # Tie-Based Interface
  210.  
  211. # Okay, things below this point get scary
  212.  
  213. CLASS: {
  214.     # Make the class for the %~ tied hash:
  215.     package File::HomeDir::TIE;
  216.  
  217.     # Make the singleton object.
  218.     # (We don't use the hash for anything, though)
  219.     ### THEN WHY MAKE IT???
  220.     my $SINGLETON = bless {};
  221.  
  222.     sub TIEHASH { $SINGLETON }
  223.  
  224.     sub FETCH {
  225.         # Catch a bad username
  226.         unless ( defined $_[1] ) {
  227.             Carp::croak("Can't use undef as a username");
  228.         }
  229.  
  230.         # Get our homedir
  231.         unless ( length $_[1] ) {
  232.             return File::HomeDir::my_home();
  233.         }
  234.  
  235.         # Get a named user's homedir
  236.         return File::HomeDir::home($_[1]);
  237.     }
  238.  
  239.     sub STORE    { _bad('STORE')    }
  240.     sub EXISTS   { _bad('EXISTS')   }
  241.     sub DELETE   { _bad('DELETE')   }
  242.     sub CLEAR    { _bad('CLEAR')    }
  243.     sub FIRSTKEY { _bad('FIRSTKEY') }
  244.     sub NEXTKEY  { _bad('NEXTKEY')  }
  245.  
  246.     sub _bad ($) {
  247.         Carp::croak("You can't $_[0] with the %~ hash")
  248.     }
  249. }
  250.  
  251. # Do the actual tie of the global %~ variable
  252. tie %~, 'File::HomeDir::TIE';
  253.  
  254. 1;
  255.  
  256. __END__
  257.  
  258. =pod
  259.  
  260. =head1 NAME
  261.  
  262. File::HomeDir - Find your home and other directories, on any platform
  263.  
  264. =head1 SYNOPSIS
  265.  
  266.   use File::HomeDir;
  267.   
  268.   # Modern Interface (Current User)
  269.   $home    = File::HomeDir->my_home;
  270.   $desktop = File::HomeDir->my_desktop;
  271.   $docs    = File::HomeDir->my_documents;
  272.   $music   = File::HomeDir->my_music;
  273.   $pics    = File::HomeDir->my_pictures;
  274.   $videos  = File::HomeDir->my_videos;
  275.   $data    = File::HomeDir->my_data;
  276.   
  277.   # Modern Interface (Other Users)
  278.   $home    = File::HomeDir->users_home('foo');
  279.   $desktop = File::HomeDir->users_desktop('foo');
  280.   $docs    = File::HomeDir->users_documents('foo');
  281.   $music   = File::HomeDir->users_music('foo');
  282.   $pics    = File::HomeDir->users_pictures('foo');
  283.   $video   = File::HomeDir->users_videos('foo');
  284.   $data    = File::HomeDir->users_data('foo');
  285.   
  286.   # Legacy Interfaces
  287.   print "My dir is ", home(), " and root's is ", home('root'), "\n";
  288.   print "My dir is $~{''} and root's is $~{root}\n";
  289.   # These both print the same thing, something like:
  290.   #  "My dir is /home/user/mojo and root's is /"
  291.  
  292. =head1 DESCRIPTION
  293.  
  294. B<File::HomeDir> is a module for dealing with issues relating to the
  295. location of directories that are "owned" by a user, primarily your user,
  296. and to solve these issues consistently across a wide variety of
  297. platforms.
  298.  
  299. Thus, a single API is presented that can find your resources on any
  300. platform.
  301.  
  302. This module provides two main interfaces.
  303.  
  304. The first is a modern L<File::Spec>-style interface with a consistent
  305. OO API and different implementation modules to support various
  306. platforms. You are B<strongly> recommended to use this interface.
  307.  
  308. The second interface is for legacy support of the original 0.07 interface
  309. that exported a C<home()> function by default and tied the C<%~> variable.
  310.  
  311. It is generally not recommended that you use this interface, but due to
  312. back-compatibility reasons they will remain supported until at least 2010.
  313.  
  314. After this date, the home() function will remain, but we will consider
  315. deprecating the (namespace-polluting) C<%~> tied hash, to be removed by
  316. 2015 (maintaining the general Perl convention of a 10 year support period
  317. for legacy APIs potentially or actually in common use).
  318.  
  319. =head2 Platform Neutrality
  320.  
  321. In the Unix world, many different types of data can be mixed together
  322. in your home directory (although on some Unix platforms this is no longer
  323. the case, particularly for "desktop"-oriented platforms).
  324.  
  325. On some non-Unix platforms, seperate directories are allocated for
  326. different types of data and have been for a long time.
  327.  
  328. When writing applications on top of B<File::HomeDir>, you should thus
  329. always try to use the most specific method you can. User documents should
  330. be saved in C<my_documents>, data that supports an application but isn't
  331. normally editing by the user directory should go into C<my_data>.
  332.  
  333. On platforms that do not make any distinction, all these different
  334. methods will harmlessly degrade to the main home directory, but on
  335. platforms that care B<File::HomeDir> will always try to Do The Right
  336. Thing(tm).
  337.  
  338. =head1 METHODS
  339.  
  340. Two types of methods are provided. The C<my_method> series of methods for
  341. finding resources for the current user, and the C<users_method> (read as
  342. "user's method") series for finding resources for arbitrary users.
  343.  
  344. This split is necesary, as on most platforms it is B<much> easier to find
  345. information about the current user compared to other users, and indeed
  346. on a number you cannot find out information such as C<users_desktop> at
  347. all, due to security restrictions.
  348.  
  349. All methods will double check (using a C<-d> test) that a directory
  350. actually exists before returning it, so you may trust in the values
  351. that are returned (subject to the usual caveats of race conditions of
  352. directories being deleted at the moment between a directory being returned
  353. and you using it).
  354.  
  355. However, because in some cases platforms may not support the concept of home
  356. directories at all, any method may return C<undef> (both in scalar and list
  357. context) to indicate that there is no matching directory on the system.
  358.  
  359. For example, most untrusted 'nobody'-type users do not have a home
  360. directory. So any modules that are used in a CGI application that
  361. at some level of recursion use your code, will result in calls to
  362. File::HomeDir returning undef, even for a basic home() call.
  363.  
  364. =head2 my_home
  365.  
  366. The C<my_home> method takes no arguments and returns the main home/profile
  367. directory for the current user.
  368.  
  369. If the distinction is important to you, the term "current" refers to the
  370. real user, and not the effective user.
  371.  
  372. This is also the case for all of the other "my" methods.
  373.  
  374. Returns the directory path as a string, C<undef> if the current user
  375. does not have a home directory, or dies on error.
  376.  
  377. =head2 my_desktop
  378.  
  379. The C<my_desktop> method takes no arguments and returns the "desktop"
  380. directory for the current user.
  381.  
  382. Due to the diversity and complexity of implementions required to deal with
  383. implementing the required functionality fully and completely, for the moment
  384. C<my_desktop> is B<not> going to be implemented.
  385.  
  386. That said, I am extremely interested in code to implement C<my_desktop> on
  387. Unix, as long as it is capable of dealing (as the Windows implementation
  388. does) with internationalisation. It should also avoid false positive
  389. results by making sure it only returns the appropriate directories for the
  390. appropriate platforms.
  391.  
  392. Returns the directory path as a string, C<undef> if the current user
  393. does not have a desktop directory, or dies on error.
  394.  
  395. =head2 my_documents
  396.  
  397. The C<my_documents> method takes no arguments and returns the directory (for
  398. the current user) where the user's documents are stored.
  399.  
  400. Returns the directory path as a string, C<undef> if the current user
  401. does not have a documents directory, or dies on error.
  402.  
  403. =head2 my_music
  404.  
  405. The C<my_music> method takes no arguments and returns the directory
  406. where the current user's music is stored.
  407.  
  408. No bias is made to any particular music type or music program, rather the
  409. concept of a directory to hold the user's music is made at the level of the
  410. underlying operating system or (at least) desktop environment.
  411.  
  412. Returns the directory path as a string, C<undef> if the current user
  413. does not have a suitable directory, or dies on error.
  414.  
  415. =head2 my_pictures
  416.  
  417. The C<my_pictures> method takes no arguments and returns the directory
  418. where the current user's pictures are stored.
  419.  
  420. No bias is made to any particular picture type or picture program, rather the
  421. concept of a directory to hold the user's pictures is made at the level of the
  422. underlying operating system or (at least) desktop environment.
  423.  
  424. Returns the directory path as a string, C<undef> if the current user
  425. does not have a suitable directory, or dies on error.
  426.  
  427. =head2 my_videos
  428.  
  429. The C<my_videos> method takes no arguments and returns the directory
  430. where the current user's videos are stored.
  431.  
  432. No bias is made to any particular video type or video program, rather the
  433. concept of a directory to hold the user's videos is made at the level of the
  434. underlying operating system or (at least) desktop environment.
  435.  
  436. Returns the directory path as a string, C<undef> if the current user
  437. does not have a suitable directory, or dies on error.
  438.  
  439. =head2 my_data
  440.  
  441. The C<my_data> method takes no arguments and returns the directory where
  442. local applications should stored their internal data for the current
  443. user.
  444.  
  445. Generally an application would create a subdirectory such as C<.foo>,
  446. beneath this directory, and store its data there. By creating your
  447. directory this way, you get an accurate result on the maximum number
  448. of platforms.
  449.  
  450. For example, on Unix you get C<~/.foo> and on Win32 you get
  451. C<~/Local Settings/Application Data/.foo>
  452.  
  453. Returns the directory path as a string, C<undef> if the current user
  454. does not have a data directory, or dies on error.
  455.  
  456. =head2 users_home
  457.  
  458.   $home = File::HomeDir->users_home('foo');
  459.  
  460. The C<users_home> method takes a single param and is used to locate the
  461. parent home/profile directory for an identified user on the system.
  462.  
  463. While most of the time this identifier would be some form of user name,
  464. it is permitted to vary per-platform to support user ids or UUIDs as
  465. applicable for that platform.
  466.  
  467. Returns the directory path as a string, C<undef> if that user
  468. does not have a home directory, or dies on error.
  469.  
  470. =head2 users_documents
  471.  
  472.   $docs = File::HomeDir->users_documents('foo');
  473.  
  474. Returns the directory path as a string, C<undef> if that user
  475. does not have a documents directory, or dies on error.
  476.  
  477. =head2 users_data
  478.  
  479.   $data = File::HomeDir->users_data('foo');
  480.  
  481. Returns the directory path as a string, C<undef> if that user
  482. does not have a data directory, or dies on error.
  483.  
  484. =head1 FUNCTIONS
  485.  
  486. =head2 home
  487.  
  488.   use File::HomeDir;
  489.   $home = home();
  490.   $home = home('foo');
  491.   $home = File::HomeDir::home();
  492.   $home = File::HomeDir::home('foo');
  493.  
  494. The C<home> function is exported by default and is provided for
  495. compatibility with legacy applications. In new applications, you should
  496. use the newer method-based interface above.
  497.  
  498. Returns the directory path to a named user's home/profile directory.
  499.  
  500. If provided no param, returns the directory path to the current user's
  501. home/profile directory.
  502.  
  503. =head1 TIED INTERFACE
  504.  
  505. =head2 %~
  506.  
  507.   $home = $~{""};
  508.   $home = $~{undef};
  509.   $home = $~{$user};
  510.   $home = $~{username};
  511.   print "... $~{''} ...";
  512.   print "... $~{$user} ...";
  513.   print "... $~{username} ...";
  514.  
  515. This calls C<home($user)> or C<home('username')> -- except that if you
  516. ask for C<$~{some_user}> and there is no such user, it will die.
  517.  
  518. Note that this is especially useful in double-quotish strings, like:
  519.  
  520.      print "Jojo's .newsrc is ", -s "$~{jojo}/.newsrc", "b long!\n";
  521.       # (helpfully dies if there is no user 'jojo')
  522.  
  523. If you want to avoid the fatal errors, first test the value of
  524. C<home('jojo')>, which will return undef (instead of dying) in case of
  525. there being no such user.
  526.  
  527. Note, however, that if the hash key is "" or undef (whether thru being
  528. a literal "", or a scalar whose value is empty-string or undef), then
  529. this returns zero-argument C<home()>, i.e., your home directory:
  530.  
  531. Further, please note that because the C<%~> hash compulsorily modifies
  532. a hash outside of it's namespace, and presents an overly simplistic
  533. approach to home directories, it is likely to ultimately be removed.
  534.  
  535. The interface is currently expected to be formally deprecated from 2010
  536. (but no earlier) and removed from 2015 (but no earlier). If very heavy
  537. use is found in the wild, these plans may be pushed back.
  538.  
  539. =head1 TO DO
  540.  
  541. =over 4
  542.  
  543. =item * Become generally clearer on situations in which a user might not
  544. have a particular resource.
  545.  
  546. =item * Add more granularity to Unix, and add support to VMS and other
  547. esoteric platforms, so we can consider going core.
  548.  
  549. =item * Add consistent support for users_* methods 
  550.  
  551. =back
  552.  
  553. =head1 SUPPORT
  554.  
  555. This module is stored in an Open Repository at the following address.
  556.  
  557. L<http://svn.ali.as/cpan/trunk/File-HomeDir>
  558.  
  559. Write access to the repository is made available automatically to any
  560. published CPAN author, and to most other volunteers on request.
  561.  
  562. If you are able to submit your bug report in the form of new (failing)
  563. unit tests, or can apply your fix directly instead of submitting a patch,
  564. you are B<strongly> encouraged to do so as the author currently maintains
  565. over 100 modules and it can take some time to deal with non-Critical bug
  566. reports or patches.
  567.  
  568. This will guarantee that your issue will be addressed in the next
  569. release of the module.
  570.  
  571. If you cannot provide a direct test or fix, or don't have time to do so,
  572. then regular bug reports are still accepted and appreciated via the CPAN
  573. bug tracker.
  574.  
  575. L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-HomeDir>
  576.  
  577. For other issues, for commercial enhancement or support, or to have your
  578. write access enabled for the repository, contact the author at the email
  579. address above.
  580.  
  581. =head1 ACKNOWLEDGEMENTS
  582.  
  583. The biggest acknowledgement must go to Chris Nandor, who wielded his
  584. legendary Mac-fu and turned my initial fairly ordinary Darwin
  585. implementation into something that actually worked properly everywhere,
  586. and then donated a Mac OS X license to allow it to be maintained properly.
  587.  
  588. =head1 AUTHORS
  589.  
  590. Adam Kennedy E<lt>adamk@cpan.orgE<gt>
  591.  
  592. Sean M. Burke E<lt>sburke@cpan.orgE<gt>
  593.  
  594. Chris Nandor E<lt>cnandor@cpan.orgE<gt>
  595.  
  596. Stephen Steneker E<lt>stennie@cpan.orgE<gt>
  597.  
  598. =head1 SEE ALSO
  599.  
  600. L<File::ShareDir>, L<File::HomeDir::Win32> (legacy)
  601.  
  602. =head1 COPYRIGHT
  603.  
  604. Copyright 2005 - 2009 Adam Kennedy.
  605.  
  606. Some parts copyright 2000 Sean M. Burke.
  607.  
  608. Some parts copyright 2006 Chris Nandor.
  609.  
  610. Some parts copyright 2006 Stephen Steneker.
  611.  
  612. This program is free software; you can redistribute
  613. it and/or modify it under the same terms as Perl itself.
  614.  
  615. The full text of the license can be found in the
  616. LICENSE file included with this module.
  617.  
  618. =cut
  619.