LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - util.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 81.1 % 127 103
Test Date: 2026-11-24 01:16:08 Functions: 100.0 % 13 13
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 57.4 % 54 31

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  *  util.c
       3                 :             :  *
       4                 :             :  *  utility functions
       5                 :             :  *
       6                 :             :  *  Copyright (c) 2010-2026, PostgreSQL Global Development Group
       7                 :             :  *  src/bin/pg_upgrade/util.c
       8                 :             :  */
       9                 :             : 
      10                 :             : #include "postgres_fe.h"
      11                 :             : 
      12                 :             : #include <signal.h>
      13                 :             : 
      14                 :             : #include "common/username.h"
      15                 :             : #include "lib/stringinfo.h"
      16                 :             : #include "pg_upgrade.h"
      17                 :             : 
      18                 :             : LogOpts     log_opts;
      19                 :             : 
      20                 :             : static void pg_log_v(eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2, 0);
      21                 :             : 
      22                 :             : 
      23                 :             : /*
      24                 :             :  * report_status()
      25                 :             :  *
      26                 :             :  *  Displays the result of an operation (ok, failed, error message,...)
      27                 :             :  *
      28                 :             :  *  This is no longer functionally different from pg_log(), but we keep
      29                 :             :  *  it around to maintain a notational distinction between operation
      30                 :             :  *  results and other messages.
      31                 :             :  */
      32                 :             : void
      33                 :         542 : report_status(eLogType type, const char *fmt, ...)
      34                 :             : {
      35                 :             :     va_list     args;
      36                 :             : 
      37                 :         542 :     va_start(args, fmt);
      38                 :         542 :     pg_log_v(type, fmt, args);
      39                 :         542 :     va_end(args);
      40                 :         542 : }
      41                 :             : 
      42                 :             : 
      43                 :             : void
      44                 :          37 : end_progress_output(void)
      45                 :             : {
      46                 :             :     /*
      47                 :             :      * For output to a tty, erase prior contents of progress line. When either
      48                 :             :      * tty or verbose, indent so that report_status() output will align
      49                 :             :      * nicely.
      50                 :             :      */
      51         [ -  + ]:          37 :     if (log_opts.isatty)
      52                 :             :     {
      53                 :           0 :         printf("\r");
      54                 :           0 :         pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, "");
      55                 :             :     }
      56         [ -  + ]:          37 :     else if (log_opts.verbose)
      57                 :           0 :         pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, "");
      58                 :          37 : }
      59                 :             : 
      60                 :             : /*
      61                 :             :  * Remove any logs generated internally.  To be used once when exiting.
      62                 :             :  */
      63                 :             : void
      64                 :          13 : cleanup_output_dirs(void)
      65                 :             : {
      66                 :          13 :     fclose(log_opts.internal);
      67                 :             : 
      68                 :             :     /* Remove dump and log files? */
      69         [ -  + ]:          13 :     if (log_opts.retain)
      70                 :           0 :         return;
      71                 :             : 
      72                 :             :     /*
      73                 :             :      * Try twice.  The second time might wait for files to finish being
      74                 :             :      * unlinked, on Windows.
      75                 :             :      */
      76         [ -  + ]:          13 :     if (!rmtree(log_opts.basedir, true))
      77                 :           0 :         rmtree(log_opts.basedir, true);
      78                 :             : 
      79                 :             :     /* Remove pg_upgrade_output.d only if empty */
      80   [ -  +  +  - ]:          13 :     switch (pg_check_dir(log_opts.rootdir))
      81                 :             :     {
      82                 :           0 :         case 0:                 /* non-existent */
      83                 :             :         case 3:                 /* exists and contains a mount point */
      84                 :             :             Assert(false);
      85                 :           0 :             break;
      86                 :             : 
      87                 :          11 :         case 1:                 /* exists and empty */
      88                 :             :         case 2:                 /* exists and contains only dot files */
      89                 :             : 
      90                 :             :             /*
      91                 :             :              * Try twice.  The second time might wait for files to finish
      92                 :             :              * being unlinked, on Windows.
      93                 :             :              */
      94         [ -  + ]:          11 :             if (!rmtree(log_opts.rootdir, true))
      95                 :           0 :                 rmtree(log_opts.rootdir, true);
      96                 :          11 :             break;
      97                 :             : 
      98                 :           2 :         case 4:                 /* exists */
      99                 :             : 
     100                 :             :             /*
     101                 :             :              * Keep the root directory as this includes some past log
     102                 :             :              * activity.
     103                 :             :              */
     104                 :           2 :             break;
     105                 :             : 
     106                 :           0 :         default:
     107                 :             :             /* different failure, just report it */
     108                 :           0 :             pg_log(PG_WARNING, "could not access directory \"%s\": %m",
     109                 :             :                    log_opts.rootdir);
     110                 :           0 :             break;
     111                 :             :     }
     112                 :             : }
     113                 :             : 
     114                 :             : /*
     115                 :             :  * prep_status
     116                 :             :  *
     117                 :             :  *  Displays a message that describes an operation we are about to begin.
     118                 :             :  *  We pad the message out to MESSAGE_WIDTH characters so that all of the
     119                 :             :  *  "ok" and "failed" indicators line up nicely.  (Overlength messages
     120                 :             :  *  will be truncated, so don't get too verbose.)
     121                 :             :  *
     122                 :             :  *  A typical sequence would look like this:
     123                 :             :  *      prep_status("about to flarb the next %d files", fileCount);
     124                 :             :  *      if ((message = flarbFiles(fileCount)) == NULL)
     125                 :             :  *        report_status(PG_REPORT, "ok");
     126                 :             :  *      else
     127                 :             :  *        pg_log(PG_FATAL, "failed: %s", message);
     128                 :             :  */
     129                 :             : void
     130                 :         512 : prep_status(const char *fmt, ...)
     131                 :             : {
     132                 :             :     va_list     args;
     133                 :             :     char        message[MAX_STRING];
     134                 :             : 
     135                 :         512 :     va_start(args, fmt);
     136                 :         512 :     vsnprintf(message, sizeof(message), fmt, args);
     137                 :         512 :     va_end(args);
     138                 :             : 
     139                 :             :     /* trim strings */
     140                 :         512 :     pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, message);
     141                 :         512 : }
     142                 :             : 
     143                 :             : /*
     144                 :             :  * prep_status_progress
     145                 :             :  *
     146                 :             :  *   Like prep_status(), but for potentially longer running operations.
     147                 :             :  *   Details about what item is currently being processed can be displayed
     148                 :             :  *   with pg_log(PG_STATUS, ...). A typical sequence would look like this:
     149                 :             :  *
     150                 :             :  *   prep_status_progress("copying files");
     151                 :             :  *   for (...)
     152                 :             :  *     pg_log(PG_STATUS, "%s", filename);
     153                 :             :  *   end_progress_output();
     154                 :             :  *   report_status(PG_REPORT, "ok");
     155                 :             :  */
     156                 :             : void
     157                 :          37 : prep_status_progress(const char *fmt, ...)
     158                 :             : {
     159                 :             :     va_list     args;
     160                 :             :     char        message[MAX_STRING];
     161                 :             : 
     162                 :          37 :     va_start(args, fmt);
     163                 :          37 :     vsnprintf(message, sizeof(message), fmt, args);
     164                 :          37 :     va_end(args);
     165                 :             : 
     166                 :             :     /*
     167                 :             :      * If outputting to a tty or in verbose, append newline. pg_log_v() will
     168                 :             :      * put the individual progress items onto the next line.
     169                 :             :      */
     170   [ +  -  -  + ]:          37 :     if (log_opts.isatty || log_opts.verbose)
     171                 :           0 :         pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
     172                 :             :     else
     173                 :          37 :         pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, message);
     174                 :          37 : }
     175                 :             : 
     176                 :             : static void
     177                 :        8269 : pg_log_v(eLogType type, const char *fmt, va_list ap)
     178                 :             : {
     179                 :        8269 :     int         save_errno = errno;
     180                 :             :     StringInfoData buf;
     181                 :             :     char       *message;
     182                 :             : 
     183                 :             :     /* No incoming message should end in newline; we add that here. */
     184                 :             :     Assert(fmt);
     185                 :             :     Assert(fmt[0] == '\0' || fmt[strlen(fmt) - 1] != '\n');
     186                 :             : 
     187                 :        8269 :     fmt = _(fmt);
     188                 :        8269 :     initStringInfo(&buf);
     189                 :             :     for (;;)
     190                 :           0 :     {
     191                 :             :         va_list     ap2;
     192                 :             :         int         needed;
     193                 :             : 
     194                 :        8269 :         errno = save_errno;
     195                 :        8269 :         va_copy(ap2, ap);
     196                 :        8269 :         needed = appendStringInfoVA(&buf, fmt, ap2);
     197                 :        8269 :         va_end(ap2);
     198         [ +  - ]:        8269 :         if (needed == 0)
     199                 :        8269 :             break;
     200                 :           0 :         enlargeStringInfo(&buf, needed);
     201                 :             :     }
     202                 :        8269 :     message = buf.data;
     203                 :             : 
     204                 :             :     /* PG_VERBOSE and PG_STATUS are only output in verbose mode */
     205                 :             :     /* fopen() on log_opts.internal might have failed, so check it */
     206   [ +  +  +  +  :        8269 :     if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
                   -  + ]
     207         [ +  - ]:        1184 :         log_opts.internal != NULL)
     208                 :             :     {
     209         [ -  + ]:        1184 :         if (type == PG_STATUS)
     210                 :             :             /* status messages get two leading spaces, see below */
     211                 :           0 :             fprintf(log_opts.internal, "  %s\n", message);
     212         [ +  + ]:        1184 :         else if (type == PG_REPORT_NONL)
     213                 :         549 :             fprintf(log_opts.internal, "%s", message);
     214                 :             :         else
     215                 :         635 :             fprintf(log_opts.internal, "%s\n", message);
     216                 :        1184 :         fflush(log_opts.internal);
     217                 :             :     }
     218                 :             : 
     219   [ +  +  +  +  :        8269 :     switch (type)
                   +  - ]
     220                 :             :     {
     221                 :        4958 :         case PG_VERBOSE:
     222         [ -  + ]:        4958 :             if (log_opts.verbose)
     223                 :           0 :                 printf("%s\n", message);
     224                 :        4958 :             break;
     225                 :             : 
     226                 :        2127 :         case PG_STATUS:
     227                 :             : 
     228                 :             :             /*
     229                 :             :              * For output to a terminal, we add two leading spaces and no
     230                 :             :              * newline; instead append \r so that the next message is output
     231                 :             :              * on the same line.  Truncate on the left to fit into
     232                 :             :              * MESSAGE_WIDTH (counting the spaces as part of that).
     233                 :             :              *
     234                 :             :              * If going to non-interactive output, only display progress if
     235                 :             :              * verbose is enabled. Otherwise the output gets unreasonably
     236                 :             :              * large by default.
     237                 :             :              */
     238         [ -  + ]:        2127 :             if (log_opts.isatty)
     239                 :             :             {
     240                 :           0 :                 bool        itfits = (strlen(message) <= MESSAGE_WIDTH - 2);
     241                 :             : 
     242                 :             :                 /* prefix with "..." if we do leading truncation */
     243   [ #  #  #  # ]:           0 :                 printf("  %s%-*.*s\r",
     244                 :             :                        itfits ? "" : "...",
     245                 :             :                        MESSAGE_WIDTH - 2, MESSAGE_WIDTH - 2,
     246                 :             :                        itfits ? message :
     247                 :             :                        message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
     248                 :             :             }
     249         [ -  + ]:        2127 :             else if (log_opts.verbose)
     250                 :           0 :                 printf("  %s\n", message);
     251                 :        2127 :             break;
     252                 :             : 
     253                 :         549 :         case PG_REPORT_NONL:
     254                 :             :             /* This option is for use by prep_status and friends */
     255                 :         549 :             printf("%s", message);
     256                 :         549 :             break;
     257                 :             : 
     258                 :         626 :         case PG_REPORT:
     259                 :             :         case PG_WARNING:
     260                 :         626 :             printf("%s\n", message);
     261                 :         626 :             break;
     262                 :             : 
     263                 :           9 :         case PG_FATAL:
     264                 :             :             /* Extra newline in case we're interrupting status output */
     265                 :           9 :             printf("\n%s\n", message);
     266                 :           9 :             printf(_("Failure, exiting\n"));
     267                 :           9 :             exit(1);
     268                 :             :             break;
     269                 :             : 
     270                 :             :             /* No default:, we want a warning for omitted cases */
     271                 :             :     }
     272                 :        8260 :     pg_free(message);
     273                 :        8260 :     fflush(stdout);
     274                 :        8260 : }
     275                 :             : 
     276                 :             : 
     277                 :             : void
     278                 :        7718 : pg_log(eLogType type, const char *fmt, ...)
     279                 :             : {
     280                 :             :     va_list     args;
     281                 :             : 
     282                 :        7718 :     va_start(args, fmt);
     283                 :        7718 :     pg_log_v(type, fmt, args);
     284                 :        7718 :     va_end(args);
     285                 :        7718 : }
     286                 :             : 
     287                 :             : 
     288                 :             : void
     289                 :           9 : pg_fatal(const char *fmt, ...)
     290                 :             : {
     291                 :             :     va_list     args;
     292                 :             : 
     293                 :           9 :     va_start(args, fmt);
     294                 :           9 :     pg_log_v(PG_FATAL, fmt, args);
     295                 :           0 :     va_end(args);
     296                 :             :     /* NOTREACHED */
     297                 :           0 :     printf(_("Failure, exiting\n"));
     298                 :           0 :     exit(1);
     299                 :             : }
     300                 :             : 
     301                 :             : 
     302                 :             : void
     303                 :         542 : check_ok(void)
     304                 :             : {
     305                 :             :     /* all seems well */
     306                 :         542 :     report_status(PG_REPORT, "ok");
     307                 :         542 : }
     308                 :             : 
     309                 :             : 
     310                 :             : /*
     311                 :             :  * quote_identifier()
     312                 :             :  *      Properly double-quote a SQL identifier.
     313                 :             :  *
     314                 :             :  * The result should be pg_free'd, but most callers don't bother because
     315                 :             :  * memory leakage is not a big deal in this program.
     316                 :             :  */
     317                 :             : char *
     318                 :          22 : quote_identifier(const char *s)
     319                 :             : {
     320                 :          22 :     char       *result = pg_malloc(strlen(s) * 2 + 3);
     321                 :          22 :     char       *r = result;
     322                 :             : 
     323                 :          22 :     *r++ = '"';
     324         [ +  + ]:         220 :     while (*s)
     325                 :             :     {
     326         [ -  + ]:         198 :         if (*s == '"')
     327                 :           0 :             *r++ = *s;
     328                 :         198 :         *r++ = *s;
     329                 :         198 :         s++;
     330                 :             :     }
     331                 :          22 :     *r++ = '"';
     332                 :          22 :     *r++ = '\0';
     333                 :             : 
     334                 :          22 :     return result;
     335                 :             : }
     336                 :             : 
     337                 :             : 
     338                 :             : /*
     339                 :             :  * get_user_info()
     340                 :             :  */
     341                 :             : int
     342                 :          25 : get_user_info(char **user_name_p)
     343                 :             : {
     344                 :             :     int         user_id;
     345                 :             :     const char *user_name;
     346                 :             :     char       *errstr;
     347                 :             : 
     348                 :             : #ifndef WIN32
     349                 :          25 :     user_id = geteuid();
     350                 :             : #else
     351                 :             :     user_id = 1;
     352                 :             : #endif
     353                 :             : 
     354                 :          25 :     user_name = get_user_name(&errstr);
     355         [ -  + ]:          25 :     if (!user_name)
     356                 :           0 :         pg_fatal("%s", errstr);
     357                 :             : 
     358                 :             :     /* make a copy */
     359                 :          25 :     *user_name_p = pg_strdup(user_name);
     360                 :             : 
     361                 :          25 :     return user_id;
     362                 :             : }
     363                 :             : 
     364                 :             : 
     365                 :             : /*
     366                 :             :  *  str2uint()
     367                 :             :  *
     368                 :             :  *  convert string to oid
     369                 :             :  */
     370                 :             : unsigned int
     371                 :         878 : str2uint(const char *str)
     372                 :             : {
     373                 :         878 :     return strtoul(str, NULL, 10);
     374                 :             : }
     375                 :             : 
     376                 :             : /*
     377                 :             :  *  str2uint64()
     378                 :             :  *
     379                 :             :  *  convert string to uint64
     380                 :             :  */
     381                 :             : uint64
     382                 :          84 : str2uint64(const char *str)
     383                 :             : {
     384                 :          84 :     return strtou64(str, NULL, 10);
     385                 :             : }
        

Generated by: LCOV version 2.0-1