Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.1 KiB

  1. # IO::Dir.pm
  2. #
  3. # Copyright (c) 1997-8 Graham Barr <[email protected]>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. package IO::Dir;
  7. use 5.003_26;
  8. use strict;
  9. use Carp;
  10. use Symbol;
  11. use Exporter;
  12. use IO::File;
  13. our(@ISA, $VERSION, @EXPORT_OK);
  14. use Tie::Hash;
  15. use File::stat;
  16. @ISA = qw(Tie::Hash Exporter);
  17. $VERSION = "1.03";
  18. @EXPORT_OK = qw(DIR_UNLINK);
  19. sub DIR_UNLINK () { 1 }
  20. sub new {
  21. @_ >= 1 && @_ <= 2 or croak 'usage: new IO::Dir [DIRNAME]';
  22. my $class = shift;
  23. my $dh = gensym;
  24. if (@_) {
  25. IO::Dir::open($dh, $_[0])
  26. or return undef;
  27. }
  28. bless $dh, $class;
  29. }
  30. sub DESTROY {
  31. my ($dh) = @_;
  32. closedir($dh);
  33. }
  34. sub open {
  35. @_ == 2 or croak 'usage: $dh->open(DIRNAME)';
  36. my ($dh, $dirname) = @_;
  37. return undef
  38. unless opendir($dh, $dirname);
  39. ${*$dh}{io_dir_path} = $dirname;
  40. 1;
  41. }
  42. sub close {
  43. @_ == 1 or croak 'usage: $dh->close()';
  44. my ($dh) = @_;
  45. closedir($dh);
  46. }
  47. sub read {
  48. @_ == 1 or croak 'usage: $dh->read()';
  49. my ($dh) = @_;
  50. readdir($dh);
  51. }
  52. sub seek {
  53. @_ == 2 or croak 'usage: $dh->seek(POS)';
  54. my ($dh,$pos) = @_;
  55. seekdir($dh,$pos);
  56. }
  57. sub tell {
  58. @_ == 1 or croak 'usage: $dh->tell()';
  59. my ($dh) = @_;
  60. telldir($dh);
  61. }
  62. sub rewind {
  63. @_ == 1 or croak 'usage: $dh->rewind()';
  64. my ($dh) = @_;
  65. rewinddir($dh);
  66. }
  67. sub TIEHASH {
  68. my($class,$dir,$options) = @_;
  69. my $dh = $class->new($dir)
  70. or return undef;
  71. $options ||= 0;
  72. ${*$dh}{io_dir_unlink} = $options & DIR_UNLINK;
  73. $dh;
  74. }
  75. sub FIRSTKEY {
  76. my($dh) = @_;
  77. $dh->rewind;
  78. scalar $dh->read;
  79. }
  80. sub NEXTKEY {
  81. my($dh) = @_;
  82. scalar $dh->read;
  83. }
  84. sub EXISTS {
  85. my($dh,$key) = @_;
  86. -e ${*$dh}{io_dir_path} . "/" . $key;
  87. }
  88. sub FETCH {
  89. my($dh,$key) = @_;
  90. &lstat(${*$dh}{io_dir_path} . "/" . $key);
  91. }
  92. sub STORE {
  93. my($dh,$key,$data) = @_;
  94. my($atime,$mtime) = ref($data) ? @$data : ($data,$data);
  95. my $file = ${*$dh}{io_dir_path} . "/" . $key;
  96. unless(-e $file) {
  97. my $io = IO::File->new($file,O_CREAT | O_RDWR);
  98. $io->close if $io;
  99. }
  100. utime($atime,$mtime, $file);
  101. }
  102. sub DELETE {
  103. my($dh,$key) = @_;
  104. # Only unlink if unlink-ing is enabled
  105. my $file = ${*$dh}{io_dir_path} . "/" . $key;
  106. return 0
  107. unless ${*$dh}{io_dir_unlink};
  108. -d $file
  109. ? rmdir($file)
  110. : unlink($file);
  111. }
  112. 1;
  113. __END__
  114. =head1 NAME
  115. IO::Dir - supply object methods for directory handles
  116. =head1 SYNOPSIS
  117. use IO::Dir;
  118. $d = new IO::Dir ".";
  119. if (defined $d) {
  120. while (defined($_ = $d->read)) { something($_); }
  121. $d->rewind;
  122. while (defined($_ = $d->read)) { something_else($_); }
  123. undef $d;
  124. }
  125. tie %dir, IO::Dir, ".";
  126. foreach (keys %dir) {
  127. print $_, " " , $dir{$_}->size,"\n";
  128. }
  129. =head1 DESCRIPTION
  130. The C<IO::Dir> package provides two interfaces to perl's directory reading
  131. routines.
  132. The first interface is an object approach. C<IO::Dir> provides an object
  133. constructor and methods, which are just wrappers around perl's built in
  134. directory reading routines.
  135. =over 4
  136. =item new ( [ DIRNAME ] )
  137. C<new> is the constuctor for C<IO::Dir> objects. It accepts one optional
  138. argument which, if given, C<new> will pass to C<open>
  139. =back
  140. The following methods are wrappers for the directory related functions built
  141. into perl (the trailing `dir' has been removed from the names). See L<perlfunc>
  142. for details of these functions.
  143. =over 4
  144. =item open ( DIRNAME )
  145. =item read ()
  146. =item seek ( POS )
  147. =item tell ()
  148. =item rewind ()
  149. =item close ()
  150. =back
  151. C<IO::Dir> also provides a interface to reading directories via a tied
  152. HASH. The tied HASH extends the interface beyond just the directory
  153. reading routines by the use of C<lstat>, from the C<File::stat> package,
  154. C<unlink>, C<rmdir> and C<utime>.
  155. =over 4
  156. =item tie %hash, IO::Dir, DIRNAME [, OPTIONS ]
  157. =back
  158. The keys of the HASH will be the names of the entries in the directory.
  159. Reading a value from the hash will be the result of calling
  160. C<File::stat::lstat>. Deleting an element from the hash will call C<unlink>
  161. providing that C<DIR_UNLINK> is passed in the C<OPTIONS>.
  162. Assigning to an entry in the HASH will cause the time stamps of the file
  163. to be modified. If the file does not exist then it will be created. Assigning
  164. a single integer to a HASH element will cause both the access and
  165. modification times to be changed to that value. Alternatively a reference to
  166. an array of two values can be passed. The first array element will be used to
  167. set the access time and the second element will be used to set the modification
  168. time.
  169. =head1 SEE ALSO
  170. L<File::stat>
  171. =head1 AUTHOR
  172. Graham Barr. Currently maintained by the Perl Porters. Please report all
  173. bugs to <[email protected]>.
  174. =head1 COPYRIGHT
  175. Copyright (c) 1997-8 Graham Barr <[email protected]>. All rights reserved.
  176. This program is free software; you can redistribute it and/or
  177. modify it under the same terms as Perl itself.
  178. =cut