LCOV - code coverage report
Current view: top level - src/common - relpath.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 35 43 81.4 %
Date: 2019-06-19 16:07:09 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  * relpath.c
       3             :  *      Shared frontend/backend code to compute pathnames of relation files
       4             :  *
       5             :  * This module also contains some logic associated with fork names.
       6             :  *
       7             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/common/relpath.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #ifndef FRONTEND
      16             : #include "postgres.h"
      17             : #else
      18             : #include "postgres_fe.h"
      19             : #endif
      20             : 
      21             : #include "catalog/pg_tablespace_d.h"
      22             : #include "common/relpath.h"
      23             : #include "storage/backendid.h"
      24             : 
      25             : 
      26             : /*
      27             :  * Lookup table of fork name by fork number.
      28             :  *
      29             :  * If you add a new entry, remember to update the errhint in
      30             :  * forkname_to_number() below, and update the SGML documentation for
      31             :  * pg_relation_size().
      32             :  */
      33             : const char *const forkNames[] = {
      34             :     "main",                       /* MAIN_FORKNUM */
      35             :     "fsm",                        /* FSM_FORKNUM */
      36             :     "vm",                     /* VISIBILITYMAP_FORKNUM */
      37             :     "init"                        /* INIT_FORKNUM */
      38             : };
      39             : 
      40             : /*
      41             :  * forkname_to_number - look up fork number by name
      42             :  *
      43             :  * In backend, we throw an error for no match; in frontend, we just
      44             :  * return InvalidForkNumber.
      45             :  */
      46             : ForkNumber
      47         106 : forkname_to_number(const char *forkName)
      48             : {
      49             :     ForkNumber  forkNum;
      50             : 
      51         128 :     for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
      52         126 :         if (strcmp(forkNames[forkNum], forkName) == 0)
      53         104 :             return forkNum;
      54             : 
      55             : #ifndef FRONTEND
      56           2 :     ereport(ERROR,
      57             :             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
      58             :              errmsg("invalid fork name"),
      59             :              errhint("Valid fork names are \"main\", \"fsm\", "
      60             :                      "\"vm\", and \"init\".")));
      61             : #endif
      62             : 
      63           0 :     return InvalidForkNumber;
      64             : }
      65             : 
      66             : /*
      67             :  * forkname_chars
      68             :  *      We use this to figure out whether a filename could be a relation
      69             :  *      fork (as opposed to an oddly named stray file that somehow ended
      70             :  *      up in the database directory).  If the passed string begins with
      71             :  *      a fork name (other than the main fork name), we return its length,
      72             :  *      and set *fork (if not NULL) to the fork number.  If not, we return 0.
      73             :  *
      74             :  * Note that the present coding assumes that there are no fork names which
      75             :  * are prefixes of other fork names.
      76             :  */
      77             : int
      78       76142 : forkname_chars(const char *str, ForkNumber *fork)
      79             : {
      80             :     ForkNumber  forkNum;
      81             : 
      82      114302 :     for (forkNum = 1; forkNum <= MAX_FORKNUM; forkNum++)
      83             :     {
      84      114302 :         int         len = strlen(forkNames[forkNum]);
      85             : 
      86      114302 :         if (strncmp(forkNames[forkNum], str, len) == 0)
      87             :         {
      88       76142 :             if (fork)
      89       76122 :                 *fork = forkNum;
      90       76142 :             return len;
      91             :         }
      92             :     }
      93           0 :     if (fork)
      94           0 :         *fork = InvalidForkNumber;
      95           0 :     return 0;
      96             : }
      97             : 
      98             : 
      99             : /*
     100             :  * GetDatabasePath - construct path to a database directory
     101             :  *
     102             :  * Result is a palloc'd string.
     103             :  *
     104             :  * XXX this must agree with GetRelationPath()!
     105             :  */
     106             : char *
     107      158846 : GetDatabasePath(Oid dbNode, Oid spcNode)
     108             : {
     109      158846 :     if (spcNode == GLOBALTABLESPACE_OID)
     110             :     {
     111             :         /* Shared system relations live in {datadir}/global */
     112             :         Assert(dbNode == 0);
     113           0 :         return pstrdup("global");
     114             :     }
     115      158846 :     else if (spcNode == DEFAULTTABLESPACE_OID)
     116             :     {
     117             :         /* The default tablespace is {datadir}/base */
     118      158652 :         return psprintf("base/%u", dbNode);
     119             :     }
     120             :     else
     121             :     {
     122             :         /* All other tablespaces are accessed via symlinks */
     123         194 :         return psprintf("pg_tblspc/%u/%s/%u",
     124             :                         spcNode, TABLESPACE_VERSION_DIRECTORY, dbNode);
     125             :     }
     126             : }
     127             : 
     128             : /*
     129             :  * GetRelationPath - construct path to a relation's file
     130             :  *
     131             :  * Result is a palloc'd string.
     132             :  *
     133             :  * Note: ideally, backendId would be declared as type BackendId, but relpath.h
     134             :  * would have to include a backend-only header to do that; doesn't seem worth
     135             :  * the trouble considering BackendId is just int anyway.
     136             :  */
     137             : char *
     138     1735332 : GetRelationPath(Oid dbNode, Oid spcNode, Oid relNode,
     139             :                 int backendId, ForkNumber forkNumber)
     140             : {
     141             :     char       *path;
     142             : 
     143     1735332 :     if (spcNode == GLOBALTABLESPACE_OID)
     144             :     {
     145             :         /* Shared system relations live in {datadir}/global */
     146             :         Assert(dbNode == 0);
     147             :         Assert(backendId == InvalidBackendId);
     148      123666 :         if (forkNumber != MAIN_FORKNUM)
     149       46252 :             path = psprintf("global/%u_%s",
     150             :                             relNode, forkNames[forkNumber]);
     151             :         else
     152       77414 :             path = psprintf("global/%u", relNode);
     153             :     }
     154     1611666 :     else if (spcNode == DEFAULTTABLESPACE_OID)
     155             :     {
     156             :         /* The default tablespace is {datadir}/base */
     157     1609598 :         if (backendId == InvalidBackendId)
     158             :         {
     159     1569394 :             if (forkNumber != MAIN_FORKNUM)
     160      509878 :                 path = psprintf("base/%u/%u_%s",
     161             :                                 dbNode, relNode,
     162             :                                 forkNames[forkNumber]);
     163             :             else
     164     1059516 :                 path = psprintf("base/%u/%u",
     165             :                                 dbNode, relNode);
     166             :         }
     167             :         else
     168             :         {
     169       40204 :             if (forkNumber != MAIN_FORKNUM)
     170       19836 :                 path = psprintf("base/%u/t%d_%u_%s",
     171             :                                 dbNode, backendId, relNode,
     172             :                                 forkNames[forkNumber]);
     173             :             else
     174       20368 :                 path = psprintf("base/%u/t%d_%u",
     175             :                                 dbNode, backendId, relNode);
     176             :         }
     177             :     }
     178             :     else
     179             :     {
     180             :         /* All other tablespaces are accessed via symlinks */
     181        2068 :         if (backendId == InvalidBackendId)
     182             :         {
     183        2068 :             if (forkNumber != MAIN_FORKNUM)
     184         844 :                 path = psprintf("pg_tblspc/%u/%s/%u/%u_%s",
     185             :                                 spcNode, TABLESPACE_VERSION_DIRECTORY,
     186             :                                 dbNode, relNode,
     187             :                                 forkNames[forkNumber]);
     188             :             else
     189        1224 :                 path = psprintf("pg_tblspc/%u/%s/%u/%u",
     190             :                                 spcNode, TABLESPACE_VERSION_DIRECTORY,
     191             :                                 dbNode, relNode);
     192             :         }
     193             :         else
     194             :         {
     195           0 :             if (forkNumber != MAIN_FORKNUM)
     196           0 :                 path = psprintf("pg_tblspc/%u/%s/%u/t%d_%u_%s",
     197             :                                 spcNode, TABLESPACE_VERSION_DIRECTORY,
     198             :                                 dbNode, backendId, relNode,
     199             :                                 forkNames[forkNumber]);
     200             :             else
     201           0 :                 path = psprintf("pg_tblspc/%u/%s/%u/t%d_%u",
     202             :                                 spcNode, TABLESPACE_VERSION_DIRECTORY,
     203             :                                 dbNode, backendId, relNode);
     204             :         }
     205             :     }
     206     1735332 :     return path;
     207             : }

Generated by: LCOV version 1.13