User: ben
Date: 05/12/19 03:56:18
Modified: xemacs-builds/ben global-replace
Log:
preserve owner and mode; skip, don't check on, directories and non-writable files
Revision Changes Path
1.6 +21 -13 XEmacs/xemacs-builds/ben/global-replace
Index: global-replace
===================================================================
RCS file: /pack/xemacscvs/XEmacs/xemacs-builds/ben/global-replace,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -p -r1.5 -r1.6
--- global-replace 2005/10/14 00:53:43 1.5
+++ global-replace 2005/12/19 02:56:17 1.6
@@ -79,19 +79,19 @@ $Getopt::Long::ignorecase = 0;
die $usage if $options{"help"} or @ARGV <= 1;
my $code = shift;
-my @dirs = grep (-d, @ARGV);
-if (@dirs)
-{
- print STDERR "Directories among list of files: ", join (' ', @dirs),
"\n";
- die $usage;
-}
-
-my @nonwrit = grep (!-w, @ARGV);
-if (@nonwrit)
-{
- print STDERR "Non-writable files among list of files: ", join (' ',
@nonwrit), "\n";
- die $usage;
-}
+#my @dirs = grep (-d, @ARGV);
+#if (@dirs)
+#{
+# print STDERR "Directories among list of files: ", join (' ', @dirs),
"\n";
+# die $usage;
+#}
+#
+#my @nonwrit = grep (!-w, @ARGV);
+#if (@nonwrit)
+#{
+# print STDERR "Non-writable files among list of files: ", join (' ',
@nonwrit), "\n";
+# die $usage;
+#}
sub SafeOpen {
open ((my $fh = new FileHandle), $_[0]);
@@ -120,6 +120,8 @@ sub WriteStringToFile {
}
foreach my $file (@ARGV) {
+ (warn "Skipping directory $file\n"), next if -d $file;
+ (warn "Skipping non-writable $file\n"), next if ! -w $file;
my $changed_p = 0;
my $new_contents = "";
if ($options{"line-mode"}) {
@@ -138,6 +140,8 @@ foreach my $file (@ARGV) {
$new_contents = $_;
}
}
+ # Get the mode of the file so we can preserve it in the new file
+ my ($dev,$ino,$mode,$nlink,$uid,$gid) = stat $file;
if ($changed_p) {
my $backdir = $options{"backup-dir"};
@@ -151,5 +155,9 @@ foreach my $file (@ARGV) {
rename $file, $backfile;
}
WriteStringToFile ($file, $new_contents);
+ # Preserve the old mode
+ chmod $mode, $file;
+ # Preserve the old owner and group (might not be possible)
+ chown $uid, $gid, $file;
}
}
Show replies by thread