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.

463 lines
13 KiB

  1. package ExtUtils::Manifest;
  2. require Exporter;
  3. use Config;
  4. use File::Find;
  5. use File::Copy 'copy';
  6. use Carp;
  7. use strict;
  8. use vars qw($VERSION @ISA @EXPORT_OK
  9. $Is_MacOS $Is_VMS $Debug $Verbose $Quiet $MANIFEST $found);
  10. $VERSION = substr(q$Revision: 1.33 $, 10);
  11. @ISA=('Exporter');
  12. @EXPORT_OK = ('mkmanifest', 'manicheck', 'fullcheck', 'filecheck',
  13. 'skipcheck', 'maniread', 'manicopy');
  14. $Is_MacOS = $^O eq 'MacOS';
  15. $Is_VMS = $^O eq 'VMS';
  16. if ($Is_VMS) { require File::Basename }
  17. $Debug = 0;
  18. $Verbose = 1;
  19. $Quiet = 0;
  20. $MANIFEST = 'MANIFEST';
  21. # Really cool fix from Ilya :)
  22. unless (defined $Config{d_link}) {
  23. no warnings;
  24. *ln = \&cp;
  25. }
  26. sub mkmanifest {
  27. my $manimiss = 0;
  28. my $read = maniread() or $manimiss++;
  29. $read = {} if $manimiss;
  30. local *M;
  31. rename $MANIFEST, "$MANIFEST.bak" unless $manimiss;
  32. open M, ">$MANIFEST" or die "Could not open $MANIFEST: $!";
  33. my $matches = _maniskip();
  34. my $found = manifind();
  35. my($key,$val,$file,%all);
  36. %all = (%$found, %$read);
  37. $all{$MANIFEST} = ($Is_VMS ? "$MANIFEST\t\t" : '') . 'This list of files'
  38. if $manimiss; # add new MANIFEST to known file list
  39. foreach $file (sort keys %all) {
  40. next if &$matches($file);
  41. if ($Verbose){
  42. warn "Added to $MANIFEST: $file\n" unless exists $read->{$file};
  43. }
  44. my $text = $all{$file};
  45. ($file,$text) = split(/\s+/,$text,2) if $Is_VMS && $text;
  46. $file = _unmacify($file);
  47. my $tabs = (5 - (length($file)+1)/8);
  48. $tabs = 1 if $tabs < 1;
  49. $tabs = 0 unless $text;
  50. print M $file, "\t" x $tabs, $text, "\n";
  51. }
  52. close M;
  53. }
  54. sub manifind {
  55. local $found = {};
  56. find(sub {return if -d $_;
  57. (my $name = $File::Find::name) =~ s|^\./||;
  58. $name =~ s/^:([^:]+)$/$1/ if $Is_MacOS;
  59. warn "Debug: diskfile $name\n" if $Debug;
  60. $name =~ s#(.*)\.$#\L$1# if $Is_VMS;
  61. $found->{$name} = "";}, $Is_MacOS ? ":" : ".");
  62. $found;
  63. }
  64. sub fullcheck {
  65. _manicheck(3);
  66. }
  67. sub manicheck {
  68. return @{(_manicheck(1))[0]};
  69. }
  70. sub filecheck {
  71. return @{(_manicheck(2))[1]};
  72. }
  73. sub skipcheck {
  74. _manicheck(6);
  75. }
  76. sub _manicheck {
  77. my($arg) = @_;
  78. my $read = maniread();
  79. my $found = manifind();
  80. my $file;
  81. my $dosnames=(defined(&Dos::UseLFN) && Dos::UseLFN()==0);
  82. my(@missfile,@missentry);
  83. if ($arg & 1){
  84. foreach $file (sort keys %$read){
  85. warn "Debug: manicheck checking from $MANIFEST $file\n" if $Debug;
  86. if ($dosnames){
  87. $file = lc $file;
  88. $file =~ s=(\.(\w|-)+)=substr ($1,0,4)=ge;
  89. $file =~ s=((\w|-)+)=substr ($1,0,8)=ge;
  90. }
  91. unless ( exists $found->{$file} ) {
  92. warn "No such file: $file\n" unless $Quiet;
  93. push @missfile, $file;
  94. }
  95. }
  96. }
  97. if ($arg & 2){
  98. $read ||= {};
  99. my $matches = _maniskip();
  100. my $skipwarn = $arg & 4;
  101. foreach $file (sort keys %$found){
  102. if (&$matches($file)){
  103. warn "Skipping $file\n" if $skipwarn;
  104. next;
  105. }
  106. warn "Debug: manicheck checking from disk $file\n" if $Debug;
  107. unless ( exists $read->{$file} ) {
  108. my $canon = "\t" . _unmacify($file) if $Is_MacOS;
  109. warn "Not in $MANIFEST: $file$canon\n" unless $Quiet;
  110. push @missentry, $file;
  111. }
  112. }
  113. }
  114. (\@missfile,\@missentry);
  115. }
  116. sub maniread {
  117. my ($mfile) = @_;
  118. $mfile ||= $MANIFEST;
  119. my $read = {};
  120. local *M;
  121. unless (open M, $mfile){
  122. warn "$mfile: $!";
  123. return $read;
  124. }
  125. while (<M>){
  126. chomp;
  127. next if /^#/;
  128. if ($Is_MacOS) {
  129. my($item,$text) = /^(\S+)\s*(.*)/;
  130. $item = _macify($item);
  131. $item =~ s/\\([0-3][0-7][0-7])/sprintf("%c", oct($1))/ge;
  132. $read->{$item}=$text;
  133. }
  134. elsif ($Is_VMS) {
  135. my($file)= /^(\S+)/;
  136. next unless $file;
  137. my($base,$dir) = File::Basename::fileparse($file);
  138. # Resolve illegal file specifications in the same way as tar
  139. $dir =~ tr/./_/;
  140. my(@pieces) = split(/\./,$base);
  141. if (@pieces > 2) { $base = shift(@pieces) . '.' . join('_',@pieces); }
  142. my $okfile = "$dir$base";
  143. warn "Debug: Illegal name $file changed to $okfile\n" if $Debug;
  144. $read->{"\L$okfile"}=$_;
  145. }
  146. else { /^(\S+)\s*(.*)/ and $read->{$1}=$2; }
  147. }
  148. close M;
  149. $read;
  150. }
  151. # returns an anonymous sub that decides if an argument matches
  152. sub _maniskip {
  153. my ($mfile) = @_;
  154. my $matches = sub {0};
  155. my @skip ;
  156. $mfile ||= "$MANIFEST.SKIP";
  157. local *M;
  158. return $matches unless -f $mfile;
  159. open M, $mfile or return $matches;
  160. while (<M>){
  161. chomp;
  162. next if /^#/;
  163. next if /^\s*$/;
  164. push @skip, _macify($_);
  165. }
  166. close M;
  167. my $opts = $Is_VMS ? 'oi ' : 'o ';
  168. my $sub = "\$matches = "
  169. . "sub { my(\$arg)=\@_; return 1 if "
  170. . join (" || ", (map {s!/!\\/!g; "\$arg =~ m/$_/$opts"} @skip), 0)
  171. . " }";
  172. eval $sub;
  173. print "Debug: $sub\n" if $Debug;
  174. $matches;
  175. }
  176. sub manicopy {
  177. my($read,$target,$how)=@_;
  178. croak "manicopy() called without target argument" unless defined $target;
  179. $how ||= 'cp';
  180. require File::Path;
  181. require File::Basename;
  182. my(%dirs,$file);
  183. $target = VMS::Filespec::unixify($target) if $Is_VMS;
  184. File::Path::mkpath([ $target ],! $Quiet,$Is_VMS ? undef : 0755);
  185. foreach $file (keys %$read){
  186. if ($Is_MacOS) {
  187. if ($file =~ m!:!) {
  188. my $dir = _maccat($target, $file);
  189. $dir =~ s/[^:]+$//;
  190. File::Path::mkpath($dir,1,0755);
  191. }
  192. cp_if_diff($file, _maccat($target, $file), $how);
  193. } else {
  194. $file = VMS::Filespec::unixify($file) if $Is_VMS;
  195. if ($file =~ m!/!) { # Ilya, that hurts, I fear, or maybe not?
  196. my $dir = File::Basename::dirname($file);
  197. $dir = VMS::Filespec::unixify($dir) if $Is_VMS;
  198. File::Path::mkpath(["$target/$dir"],! $Quiet,$Is_VMS ? undef : 0755);
  199. }
  200. cp_if_diff($file, "$target/$file", $how);
  201. }
  202. }
  203. }
  204. sub cp_if_diff {
  205. my($from, $to, $how)=@_;
  206. -f $from or carp "$0: $from not found";
  207. my($diff) = 0;
  208. local(*F,*T);
  209. open(F,"< $from\0") or croak "Can't read $from: $!\n";
  210. if (open(T,"< $to\0")) {
  211. while (<F>) { $diff++,last if $_ ne <T>; }
  212. $diff++ unless eof(T);
  213. close T;
  214. }
  215. else { $diff++; }
  216. close F;
  217. if ($diff) {
  218. if (-e $to) {
  219. unlink($to) or confess "unlink $to: $!";
  220. }
  221. STRICT_SWITCH: {
  222. best($from,$to), last STRICT_SWITCH if $how eq 'best';
  223. cp($from,$to), last STRICT_SWITCH if $how eq 'cp';
  224. ln($from,$to), last STRICT_SWITCH if $how eq 'ln';
  225. croak("ExtUtils::Manifest::cp_if_diff " .
  226. "called with illegal how argument [$how]. " .
  227. "Legal values are 'best', 'cp', and 'ln'.");
  228. }
  229. }
  230. }
  231. sub cp {
  232. my ($srcFile, $dstFile) = @_;
  233. my ($perm,$access,$mod) = (stat $srcFile)[2,8,9];
  234. copy($srcFile,$dstFile);
  235. utime $access, $mod + ($Is_VMS ? 1 : 0), $dstFile;
  236. # chmod a+rX-w,go-w
  237. chmod( 0444 | ( $perm & 0111 ? 0111 : 0 ), $dstFile ) unless ($^O eq 'MacOS');
  238. }
  239. sub ln {
  240. my ($srcFile, $dstFile) = @_;
  241. return &cp if $Is_VMS or ($^O eq 'MSWin32' and Win32::IsWin95());
  242. link($srcFile, $dstFile);
  243. local($_) = $dstFile; # chmod a+r,go-w+X (except "X" only applies to u=x)
  244. my $mode= 0444 | (stat)[2] & 0700;
  245. if (! chmod( $mode | ( $mode & 0100 ? 0111 : 0 ), $_ )) {
  246. unlink $dstFile;
  247. return;
  248. }
  249. 1;
  250. }
  251. sub best {
  252. my ($srcFile, $dstFile) = @_;
  253. if (-l $srcFile) {
  254. cp($srcFile, $dstFile);
  255. } else {
  256. ln($srcFile, $dstFile) or cp($srcFile, $dstFile);
  257. }
  258. }
  259. sub _macify {
  260. my($file) = @_;
  261. return $file unless $Is_MacOS;
  262. $file =~ s|^\./||;
  263. if ($file =~ m|/|) {
  264. $file =~ s|/+|:|g;
  265. $file = ":$file";
  266. }
  267. $file;
  268. }
  269. sub _maccat {
  270. my($f1, $f2) = @_;
  271. return "$f1/$f2" unless $Is_MacOS;
  272. $f1 .= ":$f2";
  273. $f1 =~ s/([^:]:):/$1/g;
  274. return $f1;
  275. }
  276. sub _unmacify {
  277. my($file) = @_;
  278. return $file unless $Is_MacOS;
  279. $file =~ s|^:||;
  280. $file =~ s|([/ \n])|sprintf("\\%03o", unpack("c", $1))|ge;
  281. $file =~ y|:|/|;
  282. $file;
  283. }
  284. 1;
  285. __END__
  286. =head1 NAME
  287. ExtUtils::Manifest - utilities to write and check a MANIFEST file
  288. =head1 SYNOPSIS
  289. require ExtUtils::Manifest;
  290. ExtUtils::Manifest::mkmanifest;
  291. ExtUtils::Manifest::manicheck;
  292. ExtUtils::Manifest::filecheck;
  293. ExtUtils::Manifest::fullcheck;
  294. ExtUtils::Manifest::skipcheck;
  295. ExtUtils::Manifest::manifind();
  296. ExtUtils::Manifest::maniread($file);
  297. ExtUtils::Manifest::manicopy($read,$target,$how);
  298. =head1 DESCRIPTION
  299. mkmanifest() writes all files in and below the current directory to a
  300. file named in the global variable $ExtUtils::Manifest::MANIFEST (which
  301. defaults to C<MANIFEST>) in the current directory. It works similar to
  302. find . -print
  303. but in doing so checks each line in an existing C<MANIFEST> file and
  304. includes any comments that are found in the existing C<MANIFEST> file
  305. in the new one. Anything between white space and an end of line within
  306. a C<MANIFEST> file is considered to be a comment. Filenames and
  307. comments are separated by one or more TAB characters in the
  308. output. All files that match any regular expression in a file
  309. C<MANIFEST.SKIP> (if such a file exists) are ignored.
  310. manicheck() checks if all the files within a C<MANIFEST> in the
  311. current directory really do exist. It only reports discrepancies and
  312. exits silently if MANIFEST and the tree below the current directory
  313. are in sync.
  314. filecheck() finds files below the current directory that are not
  315. mentioned in the C<MANIFEST> file. An optional file C<MANIFEST.SKIP>
  316. will be consulted. Any file matching a regular expression in such a
  317. file will not be reported as missing in the C<MANIFEST> file.
  318. fullcheck() does both a manicheck() and a filecheck().
  319. skipcheck() lists all the files that are skipped due to your
  320. C<MANIFEST.SKIP> file.
  321. manifind() returns a hash reference. The keys of the hash are the
  322. files found below the current directory.
  323. maniread($file) reads a named C<MANIFEST> file (defaults to
  324. C<MANIFEST> in the current directory) and returns a HASH reference
  325. with files being the keys and comments being the values of the HASH.
  326. Blank lines and lines which start with C<#> in the C<MANIFEST> file
  327. are discarded.
  328. C<manicopy($read,$target,$how)> copies the files that are the keys in
  329. the HASH I<%$read> to the named target directory. The HASH reference
  330. $read is typically returned by the maniread() function. This
  331. function is useful for producing a directory tree identical to the
  332. intended distribution tree. The third parameter $how can be used to
  333. specify a different methods of "copying". Valid values are C<cp>,
  334. which actually copies the files, C<ln> which creates hard links, and
  335. C<best> which mostly links the files but copies any symbolic link to
  336. make a tree without any symbolic link. Best is the default.
  337. =head1 MANIFEST.SKIP
  338. The file MANIFEST.SKIP may contain regular expressions of files that
  339. should be ignored by mkmanifest() and filecheck(). The regular
  340. expressions should appear one on each line. Blank lines and lines
  341. which start with C<#> are skipped. Use C<\#> if you need a regular
  342. expression to start with a sharp character. A typical example:
  343. \bRCS\b
  344. ^MANIFEST\.
  345. ^Makefile$
  346. ~$
  347. \.html$
  348. \.old$
  349. ^blib/
  350. ^MakeMaker-\d
  351. =head1 EXPORT_OK
  352. C<&mkmanifest>, C<&manicheck>, C<&filecheck>, C<&fullcheck>,
  353. C<&maniread>, and C<&manicopy> are exportable.
  354. =head1 GLOBAL VARIABLES
  355. C<$ExtUtils::Manifest::MANIFEST> defaults to C<MANIFEST>. Changing it
  356. results in both a different C<MANIFEST> and a different
  357. C<MANIFEST.SKIP> file. This is useful if you want to maintain
  358. different distributions for different audiences (say a user version
  359. and a developer version including RCS).
  360. C<$ExtUtils::Manifest::Quiet> defaults to 0. If set to a true value,
  361. all functions act silently.
  362. =head1 DIAGNOSTICS
  363. All diagnostic output is sent to C<STDERR>.
  364. =over
  365. =item C<Not in MANIFEST:> I<file>
  366. is reported if a file is found, that is missing in the C<MANIFEST>
  367. file which is excluded by a regular expression in the file
  368. C<MANIFEST.SKIP>.
  369. =item C<No such file:> I<file>
  370. is reported if a file mentioned in a C<MANIFEST> file does not
  371. exist.
  372. =item C<MANIFEST:> I<$!>
  373. is reported if C<MANIFEST> could not be opened.
  374. =item C<Added to MANIFEST:> I<file>
  375. is reported by mkmanifest() if $Verbose is set and a file is added
  376. to MANIFEST. $Verbose is set to 1 by default.
  377. =back
  378. =head1 SEE ALSO
  379. L<ExtUtils::MakeMaker> which has handy targets for most of the functionality.
  380. =head1 AUTHOR
  381. Andreas Koenig <F<[email protected]>>
  382. =cut