pg_basebackup是PostgreSQL数据库提供的一个工具,用于进行基础备份。以下是pg_basebackup的核心代码段解析:
int
main(int argc, char **argv)
{
    ...
 
    while ((opt = getopt_long(argc, argv, "cD:F:l:p:P:R:S:T:X:h:v", long_options, &option_index)) != -1)
    {
        ...
    }
 
    ...
 
    /*
     * If the user didn't specify any non-default argument for pgdata, and
     * we're not in a standalone backend, use the system default.
     */
    if (PQisthreadsafe() && pgdata == NULL && !do_sync && !show_progress && !verbose && !check_mode)
    {
        pgdata = sysconf_pgdata;
        if (pgdata)
            fprintf(stderr, _("%s: using default data directory \"%s\"\n"), progname, pgdata);
    }
 
    /* Do not allow pgdata within the backend's data directory */
    if (pgdata && realpath_progname && strlen(pgdata) > strlen(realpath_progname) &&
        strncmp(pgdata, realpath_progname, strlen(realpath_progname)) == 0 &&
        (pgdata[strlen(realpath_progname)] == '/' || pgdata[strlen(realpath_progname)] == '\0'))
    {
        fprintf(stderr, _("%s: data directory \"%s\" is within the directory of the backend executable\n"), progname, pgdata);
        exit(1);
    }
 
    /*
     * Don't allow pgdata to be the same as the backend's dynamic library
     * directory (we'd not be able to determine which was which).
     */
#ifdef PGDATA_NEEDS_CREATE
    if (pgdata && get_dynamic_lib_path && strlen(pgdata) > strlen(libdir) &&
        strncmp(pgdata, libdir, strlen(libdir)) == 0 &&
        (pgdata[strlen(libdir)] == '/' || pgdata[strlen(libdir)] == '\0'))
    {
        fprintf(stderr, _("%s: data directory \"%s\" is the same as the directory for dynamic libraries \"%s\"\n"), progname, pgdata, libdir);
        exit(1);
    }
#endif
 
    /*
     * Set default basedir to installation's datadir if -D was not given.
     */
    if (pgdata && basedir == NULL)
        basedir = pgdata;
 
    /*
     * It's okay to use the backend's own bin directory as the basedir if we
     * are running in a backend that was installed normally.  But if we are in a
     * backend that was installed with --prefix, we had better not use its bin
     * directory as basedir.  In that case, the only safe choice is to use the
     * installation's datadir as basedir.
     */
    if (basedir == NULL && !found_valid_backend)
    {
        fprintf(stderr, _("%s: no base directory specified, and could not locate a