Skip to content
Snippets Groups Projects
Commit 32da7ce1 authored by Martin Pitt's avatar Martin Pitt
Browse files

initial Perl version of pg_wrapper

parent 78c5de1b
No related branches found
No related tags found
No related merge requests found
......@@ -5,21 +5,9 @@ BINDIR = $(PREFIX)/bin
SBINDIR = $(PREFIX)/sbin
MANDIR = $(PREFIX)/man
CFLAGS = -g -Wall
PROGS = pg_wrapper
LINKS = pg_exec pg_default psql pg_dump pg_dumpall createdb createuser dropdb dropuser pg_restore clusterdb vacuumdb psql
SRCS = pg_wrapper.c pg_default.c pg_files.c pg_exec.c
OBJS = pg_wrapper.o pg_default.o pg_files.o pg_exec.o
all: pg_wrapper
LINKS = clusterdb createdb createlang createuser dropdb droplang dropuser pg_dump pg_dumpall pg_restore psql vacuumdb vacuumlo
clean:
-rm $(PROGS) $(LINKS) $(OBJS)
-rm *~
install: pg_wrapper
[ -z "$(DESTDIR)" ] || mkdir -p "$(DESTDIR)"
......@@ -34,8 +22,3 @@ install: pg_wrapper
for f in $(LINKS); do \
ln -sf pg_wrapper $$f; \
done
pg_wrapper: $(OBJS)
gcc $(CFLAGS) $(OBJS) -o $@
$(OBJS): pg_wrapper.h
......@@ -48,3 +48,5 @@ Write manpages for: pg_lsclusters, pg_{create,start,stop,reload}cluster
add $CONFDIR/port, add support for that in pg_{create,start}cluster and
pg_lscclusters, prefer this in init scripts
determine db version of the default port in pg_wrapper
#!/usr/bin/perl -w
# configuration
$mapfile = "/etc/postgresql-common/user_clusters";
$confroot = "/etc/postgresql";
$binroot = "/usr/lib/postgresql";
$socketdir = "/var/run/postgresql";
@commands = qw/clusterdb createdb createlang createuser dropdb droplang dropuser pg_dump
pg_dumpall pg_restore psql vacuumdb vacuumlo/;
# Return parameter from a PostgreSQL configuration file.
# Parameters: <version> <cluster> <parameter name>
sub get_conf_value {
return 0 unless $_[0] && $_[1];
open F, "$confroot/$_[0]/$_[1]/postgresql.conf" or die "Could not open configuration file: $!";
while (<F>) {
return $1 if /^\s*$_[2]\s*=\s*(\w+)\b/;
}
return 0;
}
# Return the PostgreSQL version, cluster, and database to connect to. Return
# ("","","") if $mapfile does not exist or has no entry for the current user
sub get_version_cluster_db {
my ($user, $pwd, $uid, $gid) = getpwuid $>;
my $group = (getgrgid $gid)[0];
if (! open MAP, $mapfile) {
print "Warning: could not open $mapfile, connecting to default port\n";
return ('','',$user);
}
while (<MAP>) {
s/(.*?)#.*/$1/;
next if /^\s*$/;
($u,$g,$v,$c,$db) = split;
if (!$db) {
print "Warning: ignoring invalid line $. in $mapfile\n";
next;
}
if (($u eq "*" || $u eq $user) && ($g eq "*" || $g eq $group)) {
return ($v,$c, ($db eq "*") ? '' : $db);
}
}
return ('','','');
}
#
# main
#
$cmd = (split '/', $0)[-1];
grep { $cmd eq $_ } @commands or die "pg_wrapper: invalid command name $cmd";
# Determine $version, $cluster, $db, $port
($version, $cluster, $db) = get_version_cluster_db();
if ($version && $cluster) {
$port = (get_conf_value $version, $cluster, 'port');
} else {
$version = "8.0"; # TODO: determine version from default port
}
#print "pg_wrapper debug: version $version, cluster $cluster, port $port, db $db\n";
$ENV{'PGPORT'} = "$port" if $port;
$ENV{'PGHOST'} = $socketdir;
$ENV{'PGDATABASE'} = $db if $db;
@args = ("$binroot/$version/bin/$cmd");
push @args, @ARGV;
exec @args;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment