0
  7  iˆl–Ãa¾”*i?u8 Y¸î41hß�þ^bYÇ 'št’ºÈüÿÚ„e¶‚ãÃß ?÷    i#!/usr/bin/perl -w

# Create new PostgreSQL cluster or integrate an existing data directory into
# the postgresql-common infrastructure.
#
# (C) 2005-2013 Martin Pitt <mpitt@debian.org>
# (C) 2012-2021 Christoph Berg <myon@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

use strict;
use warnings;
use PgCommon;
use Getopt::Long;
use POSIX qw/lchown setlocale LC_ALL LC_CTYPE/;

$ENV{'PATH'} = '/bin:/usr/bin'; # untaint

my @postgres_user = getpwnam 'postgres';
my %defaultconf;
my $explicit_auth_config = 0;
my $quiet;

chdir '/';

# call initdb
sub init_db {
    my ($version, $cluster, $datadir, $owneruid, $ownergid, $local_method, $host_method, $initdb_opts_from_cli) = @_;
    $datadir = readlink $datadir if (-l $datadir);

    if (system 'install', '-d', '-o', $owneruid, '-g', $ownergid, $datadir) {
        error 'could not create data directory; you might need to run this program with root privileges';
    }

    # disable copy-on-write semantics for PostgreSQL data on btrfs and similar;
    # this fails on file systems which don't support it, so ignore errors
    system "chattr +C '$datadir' 2>/dev/null";

    my @initdb = (get_program_path 'initdb', $version);
    die 'Internal error: could not determine initdb path' unless $initdb[0];
    push @initdb, ('-D', $datadir);
    if (my $waldir = $defaultconf{'waldir'} || $defaultconf{'xlogdir'}) {
        my $wal = $version >= 10 ? 'wal' : 'xlog'; # renamed in PG 10
	push @initdb, ("--${wal}dir", replace_v_c ($waldir, $version, $cluster));
    }
    unless ($explicit_auth_config) {
        if ($version >= '9.2') {
            push @initdb, ('--auth-local', $local_method);
            push @initdb, ('--auth-host', $host_method);
        } else {
            # trust is the default, but set it explicitly to mute a warning from initdb
            # the actual method will be filled in by setup_pg_hba()
            push @initdb, ('-A', 'trust');
        }
    }

    # cluster startup message
    if ($version >= 14) {
        push @initdb, '--no-instructions';
    } else {
        # ask initdb to print a different cluster start command (Debian patch)
        $ENV{CLUSTER_START_COMMAND} = "pg_ctlcluster $version $cluster start";
    }

    # options from config and command line
    if (my $options = $defaultconf{'initdb_options'}) {
        push @initdb, split (/\s+/, replace_v_c ($options, $version, $cluster));
    }
    push @initdb, @$initdb_opts_from_cli;

    if (fork) {
        wait;
        error 'initdb failed' if $?;
    } else{
        change_ugid $owneruid, $ownergid;
        print "@initdb\n" unless ($quiet);
        close STDOUT if ($quiet); # suppress initdb output
        exec @initdb;
    }
}

# move a file to a directory with defined permissions; set <postgresql.conf option> in
# postgresql.conf.
# Arguments: <source file> <target dir> <uid> <gid> <perms> <postgresql.conf option>
sub move_conffile {
    my ($file, $target, $version, $cluster, $uid, $gid, $perms, $confopt) = @_;
    my $realfile = $file;
    while (-l $realfile) {
        $realfile = readlink $realfile;
    }
    if (-e $file) {
	install_file $realfile, $target, $uid, $gid, $perms;
        unlink $file, $realfile;

	my @pathcomps = split ('/', $file);
	$target .= '/' . $pathcomps[-1];
	PgCommon::set_conf_value $version, $cluster, 'postgresql.conf', $confopt, $target if $confopt;
    } else {
        error "move_conffile: required configuration file $realfile does not exist";
    }
}

# Set up the default pg_hba.conf file:
# - Add a "local all" entry to pg_hba.conf for