fixlinks 1.6 KB

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