Item978: rewriteshbang.pl does not work with files as extracted from tgz

pencil
Priority: Low
Current State: Closed
Released In: 1.0.5
Target Release: patch
Applies To: Engine
Component: rewriteshbang.pl
Branches:
Reported By: GiulianoProcida
Waiting For:
Last Change By: GiulianoProcida
The InstallationGuide refers to rewriteshbang.pl as a helpful tool to rewrite #! interpreter paths.

Unfortunately, the installation .tgz has all the Perl executables as read only and the Perl script dies.

Also, I think the InstallationGuide should refer to both bin and tools directories.

Personally I got around this issue with a bit of shell:

find bin tools -type f | xargs file | grep perl | sed 's,:.*,,' | while read x; do cp -a "$x" "$x.new"; chmod u+w "$x.new"; sed 's,#!/usr/bin/perl,#!/path/to/my/perl/bin/perl,' <"$x" >"$x.new"; chmod u-w "$x.new"; mv "$x.new" "$x"; done

It may be that the 555 permissions in the tar ball were a mistake. Fixing this should be simple. Otherwise (assuming stat and chmod do the right thing), try this change:

    open(F, "<$file") || die $!;
    my $mode = stat(F)[2]; # may need to stat($file) on Win32?
    my $contents = <F>;
    close F;

    my $tmp_file = "$file.tmp_copy"; 
    if( $contents =~ s/^#!\s*\S+/#!$new/s ) {
        open(F, $tmp_file, '>') || die $!;
        print F $contents;
        close F;
        chmod $mode, $tmp_file;
        rename $tmp_file, $file || die $!;
        print "$file modified\n";
        $changed++;
    } else {
        print "$file not modified\n"; # incorrect in original
    }

I'm not sure how well this will work on non-Unix machines.

-- GiulianoProcida - 04 Feb 2009

There is a tacit assumption with rewriteshebang.pl that you will run it as root, I guess. I think your chmod approach is the right one in this case. The rename call is the most dodgy bit of your patch - I'd use File::Copy::move myself.

-- CrawfordCurrie - 05 Feb 2009

The following patch tests okay on windows and linux. I didn't see the above patch, so already tested the below"

Index: rewriteshbang.pl
===================================================================
--- rewriteshbang.pl    (revision 3656)
+++ rewriteshbang.pl    (working copy)
@@ -68,9 +68,12 @@
     close F;

     if( $contents =~ s/^#!\s*\S+/#!$new/s ) {
+        my $mode = (stat($file))[2];
+        chmod( oct(666), "$file");
         open(F, ">$file") || die $!;
         print F $contents;
         close F;
+        chmod( $mode, "$file");
         print "$file modified\n";
         $changed++;
     } else {

-- GiulianoProcida

I decided and Crawford agrees that this fix also goes into 1.0.5

-- KennethLavrsen - 25 Apr 2009

I would never recommend overwriting files in place unless there is a real need for it. You also are still missing the second (cosmetic) fix I gave above.

    } else {
        print "$file not modified\n";
    }

-- GiulianoProcida - 27 Apr 2009

ItemTemplate edit

Summary rewriteshbang.pl does not work with files as extracted from tgz
ReportedBy GiulianoProcida
Codebase 1.0.0
SVN Range Foswiki-1.0.0, Thu, 08 Jan 2009, build 1878
AppliesTo Engine
Component rewriteshbang.pl
Priority Low
CurrentState Closed
WaitingFor
Checkins distro:7a56dcaf530f distro:d432e88abee9 distro:be00a1635856
TargetRelease patch
ReleasedIn 1.0.5
Topic revision: r12 - 27 Apr 2009, GiulianoProcida
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy