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.

87 lines
2.1 KiB

  1. #
  2. # Search for our Unix signature in text and binary files
  3. # and replace it with the real prefix ($Config{prefix} by default).
  4. #
  5. package PPM::RelocPerl;
  6. require Exporter;
  7. @ISA = qw(Exporter);
  8. @EXPORT = qw(RelocPerl);
  9. use File::Find;
  10. use Config;
  11. use strict;
  12. # We have to build up this variable, otherwise
  13. # PPM will mash it when it upgrades itself.
  14. my $frompath_default
  15. = '/tmp' . '/.ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZpErLZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZperl'
  16. ;
  17. my ($topath, $frompath);
  18. sub wanted {
  19. if (-l) {
  20. return; # do nothing for symlinks
  21. }
  22. elsif (-B) {
  23. check_for_frompath($_, 1); # binary file edit
  24. }
  25. elsif (-e && -s && -f) {
  26. check_for_frompath($_, 0); # text file edit
  27. }
  28. }
  29. sub check_for_frompath {
  30. my ($file, $binmode) = @_;
  31. local(*F, $_);
  32. open(F, "<$file") or die "Can't open `$file': $!";
  33. binmode F if $binmode;
  34. while (<F>) {
  35. if (/\Q$frompath\E/o) {
  36. close F;
  37. edit_it($file, $binmode);
  38. last;
  39. }
  40. }
  41. # implicit close of F;
  42. }
  43. sub edit_it
  44. {
  45. my ($file, $binmode) = @_;
  46. my $nullpad = length($frompath) - length($topath);
  47. $nullpad = "\0" x $nullpad;
  48. local $/;
  49. # Force the file to be writable
  50. my $mode = (stat($file))[2] & 07777;
  51. chmod $mode | 0222, $file;
  52. open(F, "<$file") or die "Couldn't open $file: $!";
  53. binmode(F) if $binmode;
  54. my $dat = <F>;
  55. if ($binmode) {
  56. $dat =~ s|\Q$frompath\E(.*?)\0|$topath$1$nullpad\0|gs;
  57. } else {
  58. $dat =~ s|\Q$frompath\E|$topath|gs;
  59. }
  60. close(F) or die "Couldn't close $file: $!";
  61. open(F, ">$file") or die "Couldn't open $file for writing: $!";
  62. binmode(F) if $binmode;
  63. print F $dat;
  64. close(F);
  65. # Restore the permissions
  66. chmod $mode, $file;
  67. }
  68. sub RelocPerl
  69. {
  70. my ($dir, $opt_topath, $opt_frompath) = @_;
  71. $topath = defined $opt_topath ? $opt_topath : $Config{'prefix'};
  72. $frompath = defined $opt_frompath ? $opt_frompath : $frompath_default;
  73. find(\&wanted, $dir);
  74. }
  75. 1;