home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5787 < prev    next >
Encoding:
Internet Message Format  |  1992-09-08  |  5.3 KB

  1. Xref: sparky comp.lang.perl:5787 alt.sources:2025
  2. Newsgroups: comp.lang.perl,alt.sources
  3. Path: sparky!uunet!mcsun!sun4nl!mhres!pronto!jv
  4. From: jv@mh.nl (Johan Vromans)
  5. Subject: SOURCE: Relink symbolic links (enhanced version)
  6. Message-ID: <BuAx0L.DDu@pronto.mh.nl>
  7. X-Md4-Signature: 05bc6d0845ef480fb37e64a2880ac57a
  8. Sender: jv@pronto.mh.nl (Johan Vromans)
  9. Organization: Multihouse Automation, the Netherlands
  10. Date: Wed, 9 Sep 1992 08:02:44 GMT
  11. Lines: 185
  12.  
  13. Archive-name: relink
  14. Submitted-by: jv@mh.nl (Johan Vromans)
  15. Version: 1.2
  16.  
  17. This little goodie is a slightly enhanced version of the 'relink'
  18. program as included in the perl 'eg' library and Camel book. It has
  19. better error checking and a few interesting options. And it doesn't
  20. push the input from STDIN on ARGV.
  21.  
  22. One of our system administration scripts maintains a list of all
  23. symlinks on the system in the format 'filename -> where_it_links_to',
  24. a format that is also understood by this version of relink. So it is
  25. usually faster to execute
  26.  
  27.   % grep 'epoch-4.0p2' /usr/tmp/symlinks | relink -verbose 's/4.0p2/4.2/'
  28.  
  29. than to use a 'find'.
  30.  
  31. Happy hacking!
  32.  
  33. ---- Cut Here and feed the following to sh ----
  34. #!/bin/sh
  35. # This is a shell archive (produced by shar 3.49)
  36. # To extract the files from this archive, save it to a file, remove
  37. # everything above the "!/bin/sh" line above, and type "sh file_name".
  38. #
  39. # made 09/09/1992 07:53 UTC by jv@largo
  40. # Source directory /nfs/pronto/mozart/users/jv/src
  41. #
  42. # existing files will NOT be overwritten unless -c is specified
  43. #
  44. # This shar contains:
  45. # length  mode       name
  46. # ------ ---------- ------------------------------------------
  47. #   2883 -r--r--r-- relink.pl
  48. #
  49. # ============= relink.pl ==============
  50. if test -f 'relink.pl' -a X"$1" != X"-c"; then
  51.     echo 'x - skipping relink.pl (File already exists)'
  52. else
  53. echo 'x - extracting relink.pl (Text)'
  54. sed 's/^X//' << 'SHAR_EOF' > 'relink.pl' &&
  55. X#!/usr/local/bin/perl
  56. X# relink.pl -- change symlinks
  57. X# SCCS Status     : @(#)@ relink.pl    1.2
  58. X# Author          : Johan Vromans
  59. X# Created On      : Sun Sep  6 14:19:41 1992
  60. X# Last Modified By: Johan Vromans
  61. X# Last Modified On: Sun Sep  6 15:55:31 1992
  62. X# Update Count    : 31
  63. X# Status          : Unknown, Use with caution!
  64. X
  65. X# This program is almost identical to 'relink' from the perl examples.
  66. X# However, it has more extensive error detection.
  67. X# Also, it allows input lines to contain 'filename -> link-value'. 
  68. X# In this case, the filename is checked to point to link-value.
  69. X
  70. X&stdio;
  71. X&usage if @ARGV < 1;
  72. X&options if $ARGV[0] =~ /^-/;
  73. X$opt_verbose = defined $opt_verbose;
  74. X$opt_noaction = defined $opt_noaction;
  75. X
  76. X# Get the perl expression from the command line.
  77. X$expr = shift (@ARGV);
  78. X
  79. X# Go for it!
  80. Xif ( @ARGV ) {
  81. X    # Take filenames from command line.
  82. X    foreach ( @ARGV ) {
  83. X    &relink($_);
  84. X    }
  85. X}
  86. Xelse {
  87. X    # Take filenames from standard input.
  88. X    while ( <STDIN> ) {
  89. X    chop;
  90. X    if ( /\s+->\s+/ ) {
  91. X        # The link value is specified, check it.
  92. X        &relink($`, $');
  93. X    }
  94. X    else {
  95. X        # Normal procedure.
  96. X        &relink($_);
  97. X    }
  98. X    }
  99. X}
  100. X
  101. Xexit(0);
  102. X
  103. X################ Subroutines ################
  104. X
  105. Xsub relink {
  106. X    local ($name, $old) = @_;
  107. X    local ($new);
  108. X    local (@st) = lstat($name);
  109. X
  110. X    # Did the lstat succeed?
  111. X    unless ( defined $st[0] ) {
  112. X    print STDERR ("Warning: $!: $name (lstat)\n");
  113. X    return;
  114. X    }
  115. X
  116. X    # Is it a symlink?
  117. X    unless ( -l _ ) {
  118. X    print STDERR ("Warning: Not a symlink: $name\n");
  119. X    return;
  120. X    }
  121. X
  122. X    # Get the link value, and verify it if possible.
  123. X    $_ = readlink($name);
  124. X    if ( defined $old && $old ne $_ ) {
  125. X    print STDERR ("Error: Mismatch: $name ($old <-> $_)\n");
  126. X    return;
  127. X    }
  128. X
  129. X    # Transform.
  130. X    $old = $_;
  131. X    eval($expr);
  132. X    die ("Fatal: $@") if $@;
  133. X    $new = $_;
  134. X
  135. X    # Is it changed?
  136. X    if ( $old eq $new ) {
  137. X    print STDERR ("Warning: No change: $name\n");
  138. X    return;
  139. X    }
  140. X
  141. X    # Relink it.
  142. X    if ( $opt_noaction || unlink($name) == 1 ) {
  143. X    symlink($new, $name) unless $opt_noaction;
  144. X    print STDERR ($opt_noaction ? 'Info: ' : '',
  145. X              "$name: $old => $new\n") if $opt_verbose;
  146. X    }
  147. X    else {
  148. X    # Could not unlink.
  149. X    print STDERR ("Error: $!: $name (unlink)\n");
  150. X    return;
  151. X    }
  152. X}
  153. X
  154. Xsub usage {
  155. X    print STDERR <<EOD;
  156. XUsage: relink.pl perl-expr [options] [ filename ... ]
  157. X
  158. XOptions:
  159. X    -noaction    do not relink
  160. X    -verbose    supply verbose info
  161. X    -help    this info
  162. XFilenames will be read from STDIN if not supplied on the command line.
  163. XEOD
  164. X    exit(1);
  165. X}
  166. X
  167. Xsub options {
  168. X    local ($opt_help) = 0;
  169. X    local ($opt_ident) = 0;
  170. X    require "newgetopt.pl";
  171. X    if (!&NGetOpt("noaction", 
  172. X          "verbose", "ident", "help") || $opt_help) {
  173. X    &usage;
  174. X    }
  175. X    print STDERR ("This is relink.pl version 1.2\n") if $opt_ident;
  176. X}
  177. X
  178. Xsub stdio {
  179. X    if ( -t STDOUT && -t STDERR ) {
  180. X    close (STDERR);
  181. X    open (STDERR, '>-');
  182. X    select (STDERR); $| = 1;
  183. X    }
  184. X}
  185. SHAR_EOF
  186. chmod 0444 relink.pl ||
  187. echo 'restore of relink.pl failed'
  188. Wc_c="`wc -c < 'relink.pl'`"
  189. test 2883 -eq "$Wc_c" ||
  190.     echo 'relink.pl: original size 2883, current size' "$Wc_c"
  191. fi
  192. exit 0
  193. -- 
  194. Johan Vromans                       jv@mh.nl via internet backbones
  195. Multihouse Automatisering bv               uucp:..!{uunet,sun4nl}!mh.nl!jv
  196. Doesburgweg 7, 2803 PL Gouda, The Netherlands  phone/fax: +31 1820 62911/62500
  197. ------------------------ "Arms are made for hugging" -------------------------
  198.