fixlinks 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/perl -w
  2. # vi: set ts=4:
  3. @LINKS=`find . -type l|LC_ALL=C xargs ls -l`;
  4. #print @LINKS;
  5. #$debug = 1;
  6. while($_ = shift @LINKS){
  7. chomp;
  8. my ($perm,$nlinks,$owner,$group,$size,$month,$day,$year,$file) =
  9. split(' ', $_, 9);
  10. my $link;
  11. if($perm =~ m/^l/){
  12. ($relfile, $link) = split(' -> ', $file);
  13. }
  14. # chop off leading . in $file
  15. $file = $relfile;
  16. $file =~ s/^\.//;
  17. if($perm =~ m/^l/){
  18. my @pathcomponents = split('/', $file);
  19. my @linkcomponents = split('/', $link);
  20. if($link =~ m/^\//){
  21. @newcomponents = @linkcomponents;
  22. }else{
  23. @newcomponents = @pathcomponents;
  24. # chop off filename
  25. pop(@newcomponents);
  26. while($comp = shift @linkcomponents){
  27. $debug && print "path: ",join(':',@newcomponents)," -- $comp -- ", join(':',@linkcomponents),"\n";
  28. if($comp eq ""){
  29. # ignore
  30. }elsif($comp eq ".."){
  31. pop(@newcomponents);
  32. }else{
  33. push @newcomponents,$comp;
  34. }
  35. }
  36. }
  37. if($newcomponents[0] eq ""){
  38. shift(@newcomponents);
  39. }
  40. if($pathcomponents[0] eq ""){
  41. shift(@pathcomponents);
  42. }
  43. #print "from ",join('/',@pathcomponents),"\n";
  44. #print "to ",join('/',@newcomponents),"\n";
  45. if($newcomponents[0] eq $pathcomponents[0]){
  46. $debug && print $newcomponents[0],", ",$pathcomponents[0];
  47. $debug && print "should be relative\n";
  48. while($newcomponents[0] eq $pathcomponents[0]){
  49. shift(@newcomponents);
  50. shift(@pathcomponents);
  51. }
  52. while(@pathcomponents > 1){
  53. shift(@pathcomponents);
  54. unshift(@newcomponents,"..");
  55. }
  56. }else{
  57. $debug && print "should be absolute\n";
  58. unshift(@newcomponents,"");
  59. }
  60. $newlink=join('/',@newcomponents);
  61. print "ln -sf $newlink $relfile\n";
  62. unlink($relfile);
  63. symlink($newlink,$relfile);
  64. }
  65. }