/* * GetDatabasePath - construct path to a database dir * * Result is a palloc'd string. * * XXX this must agree with relpath()! */ char * GetDatabasePath(Oid dbNode, Oid spcNode) { int pathlen; char *path; int snprintfResult; if (spcNode == GLOBALTABLESPACE_OID) { /* Shared system relations live in {datadir}/global */ Assert(dbNode == 0); pathlen = 6 + 1; path = (char *) palloc(pathlen); // Using strncpy is error prone. snprintfResult = snprintf(path, pathlen, "global"); } else if (spcNode == DEFAULTTABLESPACE_OID) { /* The default tablespace is {datadir}/base */ pathlen = 5 + OIDCHARS + 1; path = (char *) palloc(pathlen); snprintfResult = snprintf(path, pathlen, "base/%u", dbNode); } else { char *primary_path; /* All other tablespaces are accessed via filespace locations */ GetFilespacePathForTablespace( spcNode, &primary_path); /* * We should develop an interface for the above that doesn't * require reallocating to a slightly larger size... */ pathlen = strlen(primary_path)+1+OIDCHARS+1+OIDCHARS+1; path = (char *) palloc(pathlen); snprintfResult = snprintf(path, pathlen, "%s/%u/%u", primary_path, spcNode, dbNode); /* Throw away the allocation we got from persistent layer */ pfree(primary_path); } Assert(snprintfResult >= 0); Assert(snprintfResult < pathlen); return path; }
/* * get_hdfs_location_from_filespace * * Get hdfs location from pg_filespace_entry * The returned path needs to be pfreed by the caller. */ void get_hdfs_location_from_filespace(char** path) { Assert(NULL != path); Oid dtsoid = get_database_dts(MyDatabaseId); GetFilespacePathForTablespace(dtsoid, path); Assert(NULL != *path); Assert(strlen(*path) < FilespaceLocationBlankPaddedWithNullTermLen); elog(DEBUG2, "found hdfs location is %s", *path); }
void CopyDatabasePath(char *target, int targetMaxLen, Oid dbNode, Oid spcNode) { int snprintfResult; if (spcNode == GLOBALTABLESPACE_OID) { /* Shared system relations live in {datadir}/global */ Assert(dbNode == 0); // Using strncpy is error prone. snprintfResult = snprintf(target, targetMaxLen, "global"); } else if (spcNode == DEFAULTTABLESPACE_OID) { /* The default tablespace is {datadir}/base */ snprintfResult = snprintf(target, targetMaxLen, "base/%u", dbNode); } else { char *primary_path; /* All other tablespaces are accessed via filespace locations */ GetFilespacePathForTablespace( spcNode, &primary_path); /* Copy path into the passed in target location */ snprintfResult = snprintf(target, targetMaxLen, "%s/%u/%u", primary_path, spcNode, dbNode); /* Throw away the allocation we got from persistent layer */ pfree(primary_path); } if (snprintfResult < 0) elog(ERROR, "CopyDatabasePath formatting error"); /* * Magically truncating the result to fit in the target string is unacceptable here * because it can result in the wrong file-system object being referenced. */ if (snprintfResult >= targetMaxLen) elog(ERROR, "CopyDatabasePath formatting result length %d exceeded the maximum length %d", snprintfResult, targetMaxLen); }