(minor) some changes to system(), ``, etc.

(suggested by cmyers and ryan_c on #gitolite)

Between wrap_print(), which now takes a list, and the new slurp(),
pretty much everything to do with 'cat' or 'echo' has been converted to
pure perl.

----

Personally, I consider these changes to be somewhat gratuitous, because
none of these had a security *or* a performance concern.  But since the
amount of new perl code was not too high (just the slurp() function,
really), I figure it's not a big deal to do it.
This commit is contained in:
Sitaram Chamarty 2011-03-08 06:18:59 +05:30
parent 91a8379f9f
commit 9ad7ea4e19

View file

@ -100,10 +100,16 @@ sub wrap_open {
} }
sub wrap_print { sub wrap_print {
my ($file, $text) = @_; my ($file, @text) = @_;
my $fh = wrap_open(">", $file); my $fh = wrap_open(">", $file);
print $fh $text; print $fh @text;
close($fh); close($fh) or die "$ABRT close $file failed: $! at ", (caller)[1], " line ", (caller)[2], "\n";
}
sub slurp {
local $/ = undef;
my $fh = wrap_open("<", $_[0]);
return <$fh>;
} }
sub add_del_line { sub add_del_line {
@ -224,7 +230,7 @@ sub new_repo
wrap_chdir("$repo.git"); wrap_chdir("$repo.git");
system("git --bare init >&2"); system("git --bare init >&2");
if ($creator) { if ($creator) {
system("echo $creator > gl-creater"); wrap_print("gl-creater", $creator);
system("git", "config", "gitweb.owner", $creator); system("git", "config", "gitweb.owner", $creator);
} }
# propagate our own, plus any local admin-defined, hooks # propagate our own, plus any local admin-defined, hooks
@ -238,7 +244,7 @@ sub new_repo
# wildcard create but on a normal (config file) create it will actually be # wildcard create but on a normal (config file) create it will actually be
# set to "gitolite-admin", so we need to make sure that for the duration # set to "gitolite-admin", so we need to make sure that for the duration
# of the hook it is set correctly. # of the hook it is set correctly.
system("env GL_REPO='$repo' hooks/gl-post-init") if -x "hooks/gl-post-init"; system("env", "GL_REPO=$repo", "hooks/gl-post-init") if -x "hooks/gl-post-init";
} }
sub new_wild_repo { sub new_wild_repo {
@ -351,14 +357,14 @@ sub get_set_perms
wrap_chdir("$repo.git"); wrap_chdir("$repo.git");
if ($verb eq 'getperms') { if ($verb eq 'getperms') {
return unless -f "gl-perms"; return unless -f "gl-perms";
my $perms = `cat gl-perms`; my $perms = slurp("gl-perms");
# convert R and RW to the actual category names in the config file # convert R and RW to the actual category names in the config file
$perms =~ s/^\s*R /READERS /mg; $perms =~ s/^\s*R /READERS /mg;
$perms =~ s/^\s*RW /WRITERS /mg; $perms =~ s/^\s*RW /WRITERS /mg;
print $perms; print $perms;
} else { } else {
system("cat > gl-perms"); wrap_print("gl-perms", <>); # eqvt to: system("cat > gl-perms");
my $perms = `cat gl-perms`; my $perms = slurp("gl-perms");
# convert R and RW to the actual category names in the config file # convert R and RW to the actual category names in the config file
$perms =~ s/^\s*R /READERS /mg; $perms =~ s/^\s*R /READERS /mg;
$perms =~ s/^\s*RW /WRITERS /mg; $perms =~ s/^\s*RW /WRITERS /mg;
@ -387,11 +393,11 @@ sub get_set_desc
wrap_chdir("$ENV{GL_REPO_BASE_ABS}"); wrap_chdir("$ENV{GL_REPO_BASE_ABS}");
wrap_chdir("$repo.git"); wrap_chdir("$repo.git");
if ($verb eq 'getdesc') { if ($verb eq 'getdesc') {
system("cat", "description") if -f "description"; print slurp("description") if -f "description";
} else { } else {
system("cat > description"); wrap_print("description", <>);
print "New description is:\n"; print "New description is:\n";
system("cat", "description"); print slurp("description");
} }
} }
@ -480,7 +486,7 @@ sub setup_gitweb_access
sub report_version { sub report_version {
my($user) = @_; my($user) = @_;
print "hello $user, the gitolite version here is "; print "hello $user, the gitolite version here is ";
system("cat", ($GL_PACKAGE_CONF || "$GL_ADMINDIR/conf") . "/VERSION"); print slurp( ($GL_PACKAGE_CONF || "$GL_ADMINDIR/conf") . "/VERSION" );
} }
sub perm_code { sub perm_code {
@ -986,11 +992,10 @@ sub setup_authkeys
print $newkeys_fh "# gitolite end\n"; print $newkeys_fh "# gitolite end\n";
close $newkeys_fh or die "$ABRT close newkeys failed: $!\n"; close $newkeys_fh or die "$ABRT close newkeys failed: $!\n";
# all done; overwrite the file (use cat to avoid perm changes) # all done; overwrite the file
system("cat $ENV{HOME}/.ssh/authorized_keys > $ENV{HOME}/.ssh/old_authkeys"); wrap_print("$ENV{HOME}/.ssh/old_authkeys", slurp("$ENV{HOME}/.ssh/authorized_keys"));
system("cat $ENV{HOME}/.ssh/new_authkeys > $ENV{HOME}/.ssh/authorized_keys") wrap_print("$ENV{HOME}/.ssh/authorized_keys", slurp("$ENV{HOME}/.ssh/new_authkeys"));
and die "couldn't write authkeys file\n"; unlink "$ENV{HOME}/.ssh/new_authkeys";
system("rm $ENV{HOME}/.ssh/new_authkeys");
} }
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------