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.

289 lines
7.0 KiB

  1. package ExtUtils::Packlist;
  2. use 5.005_64;
  3. use strict;
  4. use Carp qw();
  5. our $VERSION = '0.03';
  6. # Used for generating filehandle globs. IO::File might not be available!
  7. my $fhname = "FH1";
  8. sub mkfh()
  9. {
  10. no strict;
  11. my $fh = \*{$fhname++};
  12. use strict;
  13. return($fh);
  14. }
  15. sub new($$)
  16. {
  17. my ($class, $packfile) = @_;
  18. $class = ref($class) || $class;
  19. my %self;
  20. tie(%self, $class, $packfile);
  21. return(bless(\%self, $class));
  22. }
  23. sub TIEHASH
  24. {
  25. my ($class, $packfile) = @_;
  26. my $self = { packfile => $packfile };
  27. bless($self, $class);
  28. $self->read($packfile) if (defined($packfile) && -f $packfile);
  29. return($self);
  30. }
  31. sub STORE
  32. {
  33. $_[0]->{data}->{$_[1]} = $_[2];
  34. }
  35. sub FETCH
  36. {
  37. return($_[0]->{data}->{$_[1]});
  38. }
  39. sub FIRSTKEY
  40. {
  41. my $reset = scalar(keys(%{$_[0]->{data}}));
  42. return(each(%{$_[0]->{data}}));
  43. }
  44. sub NEXTKEY
  45. {
  46. return(each(%{$_[0]->{data}}));
  47. }
  48. sub EXISTS
  49. {
  50. return(exists($_[0]->{data}->{$_[1]}));
  51. }
  52. sub DELETE
  53. {
  54. return(delete($_[0]->{data}->{$_[1]}));
  55. }
  56. sub CLEAR
  57. {
  58. %{$_[0]->{data}} = ();
  59. }
  60. sub DESTROY
  61. {
  62. }
  63. sub read($;$)
  64. {
  65. my ($self, $packfile) = @_;
  66. $self = tied(%$self) || $self;
  67. if (defined($packfile)) { $self->{packfile} = $packfile; }
  68. else { $packfile = $self->{packfile}; }
  69. Carp::croak("No packlist filename specified") if (! defined($packfile));
  70. my $fh = mkfh();
  71. open($fh, "<$packfile") || Carp::croak("Can't open file $packfile: $!");
  72. $self->{data} = {};
  73. my ($line);
  74. while (defined($line = <$fh>))
  75. {
  76. chomp $line;
  77. my ($key, @kvs) = split(' ', $line);
  78. $key =~ s!/\./!/!g; # Some .packlists have spurious '/./' bits in the paths
  79. if (! @kvs)
  80. {
  81. $self->{data}->{$key} = undef;
  82. }
  83. else
  84. {
  85. my ($data) = {};
  86. foreach my $kv (@kvs)
  87. {
  88. my ($k, $v) = split('=', $kv);
  89. $data->{$k} = $v;
  90. }
  91. $self->{data}->{$key} = $data;
  92. }
  93. }
  94. close($fh);
  95. }
  96. sub write($;$)
  97. {
  98. my ($self, $packfile) = @_;
  99. $self = tied(%$self) || $self;
  100. if (defined($packfile)) { $self->{packfile} = $packfile; }
  101. else { $packfile = $self->{packfile}; }
  102. Carp::croak("No packlist filename specified") if (! defined($packfile));
  103. my $fh = mkfh();
  104. open($fh, ">$packfile") || Carp::croak("Can't open file $packfile: $!");
  105. foreach my $key (sort(keys(%{$self->{data}})))
  106. {
  107. print $fh ("$key");
  108. if (ref($self->{data}->{$key}))
  109. {
  110. my $data = $self->{data}->{$key};
  111. foreach my $k (sort(keys(%$data)))
  112. {
  113. print $fh (" $k=$data->{$k}");
  114. }
  115. }
  116. print $fh ("\n");
  117. }
  118. close($fh);
  119. }
  120. sub validate($;$)
  121. {
  122. my ($self, $remove) = @_;
  123. $self = tied(%$self) || $self;
  124. my @missing;
  125. foreach my $key (sort(keys(%{$self->{data}})))
  126. {
  127. if (! -e $key)
  128. {
  129. push(@missing, $key);
  130. delete($self->{data}{$key}) if ($remove);
  131. }
  132. }
  133. return(@missing);
  134. }
  135. sub packlist_file($)
  136. {
  137. my ($self) = @_;
  138. $self = tied(%$self) || $self;
  139. return($self->{packfile});
  140. }
  141. 1;
  142. __END__
  143. =head1 NAME
  144. ExtUtils::Packlist - manage .packlist files
  145. =head1 SYNOPSIS
  146. use ExtUtils::Packlist;
  147. my ($pl) = ExtUtils::Packlist->new('.packlist');
  148. $pl->read('/an/old/.packlist');
  149. my @missing_files = $pl->validate();
  150. $pl->write('/a/new/.packlist');
  151. $pl->{'/some/file/name'}++;
  152. or
  153. $pl->{'/some/other/file/name'} = { type => 'file',
  154. from => '/some/file' };
  155. =head1 DESCRIPTION
  156. ExtUtils::Packlist provides a standard way to manage .packlist files.
  157. Functions are provided to read and write .packlist files. The original
  158. .packlist format is a simple list of absolute pathnames, one per line. In
  159. addition, this package supports an extended format, where as well as a filename
  160. each line may contain a list of attributes in the form of a space separated
  161. list of key=value pairs. This is used by the installperl script to
  162. differentiate between files and links, for example.
  163. =head1 USAGE
  164. The hash reference returned by the new() function can be used to examine and
  165. modify the contents of the .packlist. Items may be added/deleted from the
  166. .packlist by modifying the hash. If the value associated with a hash key is a
  167. scalar, the entry written to the .packlist by any subsequent write() will be a
  168. simple filename. If the value is a hash, the entry written will be the
  169. filename followed by the key=value pairs from the hash. Reading back the
  170. .packlist will recreate the original entries.
  171. =head1 FUNCTIONS
  172. =over
  173. =item new()
  174. This takes an optional parameter, the name of a .packlist. If the file exists,
  175. it will be opened and the contents of the file will be read. The new() method
  176. returns a reference to a hash. This hash holds an entry for each line in the
  177. .packlist. In the case of old-style .packlists, the value associated with each
  178. key is undef. In the case of new-style .packlists, the value associated with
  179. each key is a hash containing the key=value pairs following the filename in the
  180. .packlist.
  181. =item read()
  182. This takes an optional parameter, the name of the .packlist to be read. If
  183. no file is specified, the .packlist specified to new() will be read. If the
  184. .packlist does not exist, Carp::croak will be called.
  185. =item write()
  186. This takes an optional parameter, the name of the .packlist to be written. If
  187. no file is specified, the .packlist specified to new() will be overwritten.
  188. =item validate()
  189. This checks that every file listed in the .packlist actually exists. If an
  190. argument which evaluates to true is given, any missing files will be removed
  191. from the internal hash. The return value is a list of the missing files, which
  192. will be empty if they all exist.
  193. =item packlist_file()
  194. This returns the name of the associated .packlist file
  195. =back
  196. =head1 EXAMPLE
  197. Here's C<modrm>, a little utility to cleanly remove an installed module.
  198. #!/usr/local/bin/perl -w
  199. use strict;
  200. use IO::Dir;
  201. use ExtUtils::Packlist;
  202. use ExtUtils::Installed;
  203. sub emptydir($) {
  204. my ($dir) = @_;
  205. my $dh = IO::Dir->new($dir) || return(0);
  206. my @count = $dh->read();
  207. $dh->close();
  208. return(@count == 2 ? 1 : 0);
  209. }
  210. # Find all the installed packages
  211. print("Finding all installed modules...\n");
  212. my $installed = ExtUtils::Installed->new();
  213. foreach my $module (grep(!/^Perl$/, $installed->modules())) {
  214. my $version = $installed->version($module) || "???";
  215. print("Found module $module Version $version\n");
  216. print("Do you want to delete $module? [n] ");
  217. my $r = <STDIN>; chomp($r);
  218. if ($r && $r =~ /^y/i) {
  219. # Remove all the files
  220. foreach my $file (sort($installed->files($module))) {
  221. print("rm $file\n");
  222. unlink($file);
  223. }
  224. my $pf = $installed->packlist($module)->packlist_file();
  225. print("rm $pf\n");
  226. unlink($pf);
  227. foreach my $dir (sort($installed->directory_tree($module))) {
  228. if (emptydir($dir)) {
  229. print("rmdir $dir\n");
  230. rmdir($dir);
  231. }
  232. }
  233. }
  234. }
  235. =head1 AUTHOR
  236. Alan Burlison <[email protected]>
  237. =cut