cleanLinks 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #! /usr/local/bin/perl
  2. eval "exec /usr/local/bin/perl -S $0 $*"
  3. if $running_under_some_shell;
  4. ################################################################################
  5. #
  6. # File: cleanLinks <source tree>
  7. # RCS: $XConsortium: cleanLinks /main/3 1995/10/30 13:43:35 rswiston $
  8. # Author: Marc Ayotte Hewlett-Packard, OSSD-CV
  9. # Created: Sun Jul 4 17:57:13 PDT 1993
  10. # Language: perl
  11. # Package: N/A
  12. # Status: CDE distributed
  13. #
  14. # (c) Copyright 1993, Hewlett-Packard Company, all rights reserved.
  15. #
  16. # Usage: cleanLinks <directory>
  17. #
  18. # Description: This script removes symbolic links to nowhere in
  19. # <directory>. It does not remove anything with RCS
  20. # in its path.
  21. #
  22. ################################################################################
  23. if ($ARGV[0]) {
  24. $TREE = $ARGV[0];
  25. }
  26. else {
  27. die " USAGE CleanLinks <source directory>\n";
  28. }
  29. if (! chdir("$TREE")) {
  30. die " ERROR -> Couldn't change directory to $TREE.\n";
  31. }
  32. #######################################################
  33. # define local subroutines
  34. #######################################################
  35. sub dokill {
  36. die "\n left on INTR \n";
  37. exit 1;
  38. }
  39. ########################################################
  40. # Catch signals
  41. ########################################################
  42. $SIG{'INT'} = 'dokill';
  43. ##############################
  44. # get the symlinks in the tree
  45. ##############################
  46. if (! open(FIND,"find . -type d ! -type l -print|")) {
  47. print STDERR " ERROR failure in open used for find.\n";
  48. die "You may have to contact your build administrator\n";
  49. }
  50. #
  51. # don't buffer find output
  52. #
  53. $| = 1;
  54. print "************ List of symlinks to nowhere removed **********\n";
  55. ##################################################################
  56. # go through the directories and examine each symlink.
  57. # resolve the symlink and, if the file at the end doesn't exist,
  58. # remove the original symlink. Don't do anything that ends in RCS.
  59. ##################################################################
  60. FILE:
  61. while ($new = <FIND>) {
  62. chop $new;
  63. if (! opendir(THISDIR,"$new")) {
  64. print STDERR " WARNING -> could not process directory $new\n";
  65. next FILE;
  66. }
  67. else {
  68. if (! chdir("$new")) {
  69. print STDERR " WARNING -> could not change directory to $new\n";
  70. next FILE;
  71. }
  72. }
  73. # remove . from $new path for cuteness of output
  74. $new =~ s%^\.%%;
  75. @allfiles = grep(!/^\.\.?$/, readdir(THISDIR));
  76. foreach $file (@allfiles) {
  77. if (( -l $file) && ($file !~ m%RCS$%) && (! -d $file)) {
  78. #
  79. # resolve the link
  80. #
  81. $tmp1file = $file;
  82. $counter = 0;
  83. while (defined($tmp2file = readlink($tmp1file))) {
  84. $tmp1file = $tmp2file;
  85. #
  86. # watch for cyclic symlinks
  87. #
  88. if ($counter++ == 10) {
  89. last;
  90. }
  91. }
  92. #
  93. # if last piece in resolved chain is not a file
  94. # it is a symlink to nowhere -> remove
  95. #
  96. if ( ! -e $tmp1file) { # remove link to nowhere
  97. if (unlink("$file")) {
  98. print "removing ${TREE}${new}/${file}\n";
  99. }
  100. else {
  101. print STDERR " WARNING -> ${TREE}${new}/${file} -> could not remove\n";
  102. print "$!\n";
  103. }
  104. }
  105. }
  106. }
  107. closedir(THISDIR);
  108. if (! chdir("$TREE")) {
  109. die " ERROR -> Couldn't change directory to $TREE.\n";
  110. }
  111. }