<commit_before>/*-------------------------------------------------------------------------
 *
 * database.c--
 *	  miscellanious initialization support stuff
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/misc/Attic/database.c,v 1.1 1997/11/07 07:05:46 thomas Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include "postgres.h"
#include "miscadmin.h"

#include "utils/elog.h"
#include "utils/builtins.h"
#include "utils/syscache.h"

#include "access/heapam.h"
#include "storage/bufmgr.h"
#include "catalog/catname.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_database.h"

#include "access/xact.h"
#include "storage/bufpage.h"

/* GetDatabaseInfo()
 * Pull database information from pg_database.
 */
int
GetDatabaseInfo(char *name, Oid *owner, char *path)
{
	Oid			dbowner,
				dbid;
	char		dbpath[MAXPGPATH+1];
	text	   *dbtext;

	Relation	dbrel;
	HeapTuple   dbtup;
	HeapTuple   tup;
	Buffer      buf;
	HeapScanDesc scan;
	ScanKeyData scanKey;

	dbrel = heap_openr(DatabaseRelationName);
	if (!RelationIsValid(dbrel))
		elog(FATAL,"GetDatabaseInfo: cannot open relation \"%-.*s\"",
			DatabaseRelationName);

	ScanKeyEntryInitialize(&scanKey, 0, Anum_pg_database_datname,
		NameEqualRegProcedure, NameGetDatum(name));

	scan = heap_beginscan(dbrel, 0, NowTimeQual, 1, &scanKey);
	if (!HeapScanIsValid(scan))
		elog(WARN, "GetDatabaseInfo: cannot begin scan of %s", DatabaseRelationName);

	/*
	 * Since we're going to close the relation, copy the tuple.
	 */
	tup = heap_getnext(scan, 0, &buf);

	if (HeapTupleIsValid(tup))
	{
		dbtup = heap_copytuple(tup);
		ReleaseBuffer(buf);
	}
	else
		dbtup = tup;

	heap_endscan(scan);

	if (!HeapTupleIsValid(dbtup))
	{
		elog(NOTICE,"GetDatabaseInfo: %s entry not found %s",
			DatabaseRelationName,name);
		return TRUE;
	}

	dbowner = (Oid) heap_getattr(dbtup, InvalidBuffer,
									Anum_pg_database_datdba,
									RelationGetTupleDescriptor(dbrel),
									(char *) NULL);
	dbid = dbtup->t_oid;
 
	dbtext = (text *) heap_getattr(dbtup, InvalidBuffer,
									Anum_pg_database_datpath,
									RelationGetTupleDescriptor(dbrel),
									(char *) NULL);

	memcpy(dbpath, VARDATA(dbtext), (VARSIZE(dbtext)-VARHDRSZ));
		*(dbpath+(VARSIZE(dbtext)-VARHDRSZ)) = '\0';

	heap_close(dbrel);

	owner = palloc(sizeof(Oid));
	*owner = dbowner;
	path = palloc(strlen(dbpath)+1);
	strcpy(path, dbpath);

	return FALSE;
} /* GetDatabaseInfo() */

char *
ExpandDatabasePath(char *dbpath)
{
	char	   *path;
	char	   *cp;
	char		buf[MAXPGPATH+1];

	/* leading path delimiter? then already absolute path */
	if (*dbpath == SEP_CHAR)
	{
		cp = strrchr(dbpath, SEP_CHAR);
		strncpy(buf,dbpath,(cp-dbpath));
		sprintf(&buf[cp-dbpath], "%cbase%c%s", SEP_CHAR, SEP_CHAR, (cp+1));
	}
	/* path delimiter somewhere? then has leading environment variable */
	else if (strchr(dbpath, SEP_CHAR) != NULL)
	{
		cp = strchr(dbpath, SEP_CHAR);
		strncpy(buf,dbpath,(cp-dbpath));
		buf[cp-dbpath] = '\0';
		path = getenv(buf);
		sprintf(buf, "%s%cbase%c%s", path, SEP_CHAR, SEP_CHAR, (cp+1));
	}
	/* no path delimiter? then add the default path prefixes */
	else
	{
		sprintf(buf, "%s%cbase%c%s", DataDir, SEP_CHAR, SEP_CHAR, dbpath);
	}

	path = palloc(strlen(buf)+1);
	strcpy(path,buf);

	return path;
} /* ExpandDatabasePath() */


/* --------------------------------
 *	GetRawDatabaseInfo() -- Find the OID and path of the database.
 *
 *		The database's oid forms half of the unique key for the system<commit_msg>Clean up handling of environment variables in path names.<commit_after>/*-------------------------------------------------------------------------
 *
 * database.c--
 *	  miscellanious initialization support stuff
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/misc/Attic/database.c,v 1.2 1997/11/10 15:14:34 thomas Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include "postgres.h"
#include "miscadmin.h"

#include "utils/elog.h"
#include "utils/builtins.h"
#include "utils/syscache.h"

#include "access/heapam.h"
#include "storage/bufmgr.h"
#include "catalog/catname.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_database.h"

#include "access/xact.h"
#include "storage/bufpage.h"

/* GetDatabaseInfo()
 * Pull database information from pg_database.
 */
int
GetDatabaseInfo(char *name, Oid *owner, char *path)
{
	Oid			dbowner,
				dbid;
	char		dbpath[MAXPGPATH+1];
	text	   *dbtext;

	Relation	dbrel;
	HeapTuple   dbtup;
	HeapTuple   tup;
	Buffer      buf;
	HeapScanDesc scan;
	ScanKeyData scanKey;

	dbrel = heap_openr(DatabaseRelationName);
	if (!RelationIsValid(dbrel))
		elog(FATAL,"GetDatabaseInfo: cannot open relation \"%-.*s\"",
			DatabaseRelationName);

	ScanKeyEntryInitialize(&scanKey, 0, Anum_pg_database_datname,
		NameEqualRegProcedure, NameGetDatum(name));

	scan = heap_beginscan(dbrel, 0, NowTimeQual, 1, &scanKey);
	if (!HeapScanIsValid(scan))
		elog(WARN, "GetDatabaseInfo: cannot begin scan of %s", DatabaseRelationName);

	/*
	 * Since we're going to close the relation, copy the tuple.
	 */
	tup = heap_getnext(scan, 0, &buf);

	if (HeapTupleIsValid(tup))
	{
		dbtup = heap_copytuple(tup);
		ReleaseBuffer(buf);
	}
	else
		dbtup = tup;

	heap_endscan(scan);

	if (!HeapTupleIsValid(dbtup))
	{
		elog(NOTICE,"GetDatabaseInfo: %s entry not found %s",
			DatabaseRelationName,name);
		return TRUE;
	}

	dbowner = (Oid) heap_getattr(dbtup, InvalidBuffer,
									Anum_pg_database_datdba,
									RelationGetTupleDescriptor(dbrel),
									(char *) NULL);
	dbid = dbtup->t_oid;
 
	dbtext = (text *) heap_getattr(dbtup, InvalidBuffer,
									Anum_pg_database_datpath,
									RelationGetTupleDescriptor(dbrel),
									(char *) NULL);

	memcpy(dbpath, VARDATA(dbtext), (VARSIZE(dbtext)-VARHDRSZ));
		*(dbpath+(VARSIZE(dbtext)-VARHDRSZ)) = '\0';

	heap_close(dbrel);

	owner = palloc(sizeof(Oid));
	*owner = dbowner;
	path = palloc(strlen(dbpath)+1);
	strcpy(path, dbpath);

	return FALSE;
} /* GetDatabaseInfo() */

char *
ExpandDatabasePath(char *dbpath)
{
	char	   *path;
	char	   *cp;
	char		buf[MAXPGPATH+1];

	/* leading path delimiter? then already absolute path */
	if (*dbpath == SEP_CHAR)
	{
		cp = strrchr(dbpath, SEP_CHAR);
		strncpy(buf,dbpath,(cp-dbpath));
		sprintf(&buf[cp-dbpath], "%cbase%c%s", SEP_CHAR, SEP_CHAR, (cp+1));
	}
	/* path delimiter somewhere? then has leading environment variable */
	else if (strchr(dbpath, SEP_CHAR) != NULL)
	{
		cp = strchr(dbpath, SEP_CHAR);
		strncpy(buf,dbpath,(cp-dbpath));
		buf[cp-dbpath] = '\0';
		path = getenv(buf);
		/* problem getting environment variable? let calling routine handle it */
		if (path == NULL)
			return path;

		sprintf(buf, "%s%cbase%c%s", path, SEP_CHAR, SEP_CHAR, (cp+1));
	}
	/* no path delimiter? then add the default path prefixes */
	else
	{
		sprintf(buf, "%s%cbase%c%s", DataDir, SEP_CHAR, SEP_CHAR, dbpath);
	}

	path = palloc(strlen(buf)+1);
	strcpy(path,buf);

	return path;
} /* ExpandDatabasePath() */


/* --------------------------------
 *	GetRawDatabaseInfo() -- Find the OID and path of the database.
 *
 *		The database's oid forms half of the unique key for the system
{'fraction_non_alphanumeric': 0.18626015177739316, 'fraction_numerical': 0.008520836107043003, 'mean_word_length': 3.3801749271137025, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7537303', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2759, 'n_words': 599}