LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - util.c (source / functions) Coverage Total Hit UIC GIC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 81.4 % 129 105 24 105
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 13 13 13
Baseline: lcov-20261128-baseline Branches: 56.7 % 60 34 26 34
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 84.6 % 13 11 2 11
(30,360] days: 100.0 % 7 7 7
(360..) days: 79.8 % 109 87 22 87
Function coverage date bins:
(30,360] days: 100.0 % 6 6 6
(360..) days: 100.0 % 7 7 7
Branch coverage date bins:
(7,30] days: 50.0 % 2 1 1 1
(360..) days: 56.9 % 58 33 25 33

 Age         Owner                    Branch data    TLA  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
  199 tgl@sss.pgh.pa.us          33                 :GIC         542 : report_status(eLogType type, const char *fmt, ...)
                                 34                 :                : {
                                 35                 :                :     va_list     args;
                                 36                 :                : 
 6044 bruce@momjian.us           37                 :            542 :     va_start(args, fmt);
 1600 tgl@sss.pgh.pa.us          38                 :            542 :     pg_log_v(type, fmt, args);
 6044 bruce@momjian.us           39                 :            542 :     va_end(args);
                                 40                 :            542 : }
                                 41                 :                : 
                                 42                 :                : 
                                 43                 :                : void
 5111                            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                 :                :      */
 1741 andres@anarazel.de         51         [ -  + ]:             37 :     if (log_opts.isatty)
                                 52                 :                :     {
 1650 peter@eisentraut.org       53                 :UIC           0 :         printf("\r");
 1600 tgl@sss.pgh.pa.us          54                 :              0 :         pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, "");
                                 55                 :                :     }
 1741 andres@anarazel.de         56         [ -  + ]:GIC          37 :     else if (log_opts.verbose)
 1600 tgl@sss.pgh.pa.us          57                 :UIC           0 :         pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, "");
 5111 bruce@momjian.us           58                 :GIC          37 : }
                                 59                 :                : 
                                 60                 :                : /*
                                 61                 :                :  * Remove any logs generated internally.  To be used once when exiting.
                                 62                 :                :  */
                                 63                 :                : void
 1634 michael@paquier.xyz        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)
 1634 michael@paquier.xyz        70                 :UIC           0 :         return;
                                 71                 :                : 
                                 72                 :                :     /*
                                 73                 :                :      * Try twice.  The second time might wait for files to finish being
                                 74                 :                :      * unlinked, on Windows.
                                 75                 :                :      */
 1396 tmunro@postgresql.or       76         [ -  + ]:GIC          13 :     if (!rmtree(log_opts.basedir, true))
 1396 tmunro@postgresql.or       77                 :UIC           0 :         rmtree(log_opts.basedir, true);
                                 78                 :                : 
                                 79                 :                :     /* Remove pg_upgrade_output.d only if empty */
 1634 michael@paquier.xyz        80   [ -  +  +  - ]:GIC          13 :     switch (pg_check_dir(log_opts.rootdir))
                                 81                 :                :     {
 1634 michael@paquier.xyz        82                 :UIC           0 :         case 0:                 /* non-existent */
                                 83                 :                :         case 3:                 /* exists and contains a mount point */
                                 84                 :              0 :             Assert(false);
                                 85                 :                :             break;
                                 86                 :                : 
 1634 michael@paquier.xyz        87                 :GIC          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                 :                :              */
 1396 tmunro@postgresql.or       94         [ -  + ]:             11 :             if (!rmtree(log_opts.rootdir, true))
 1396 tmunro@postgresql.or       95                 :UIC           0 :                 rmtree(log_opts.rootdir, true);
 1634 michael@paquier.xyz        96                 :GIC          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                 :                : 
 1634 michael@paquier.xyz       106                 :UIC           0 :         default:
                                107                 :                :             /* different failure, just report it */
 1600 tgl@sss.pgh.pa.us         108                 :              0 :             pg_log(PG_WARNING, "could not access directory \"%s\": %m",
                                109                 :                :                    log_opts.rootdir);
 1634 michael@paquier.xyz       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
  199 tgl@sss.pgh.pa.us         130                 :GIC         512 : prep_status(const char *fmt, ...)
                                131                 :                : {
                                132                 :                :     va_list     args;
                                133                 :                :     char        message[MAX_STRING];
                                134                 :                : 
 6044 bruce@momjian.us          135                 :            512 :     va_start(args, fmt);
                                136                 :            512 :     vsnprintf(message, sizeof(message), fmt, args);
                                137                 :            512 :     va_end(args);
                                138                 :                : 
                                139                 :                :     /* trim strings */
 1600 tgl@sss.pgh.pa.us         140                 :            512 :     pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, message);
 1741 andres@anarazel.de        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
  199 tgl@sss.pgh.pa.us         157                 :             37 : prep_status_progress(const char *fmt, ...)
                                158                 :                : {
                                159                 :                :     va_list     args;
                                160                 :                :     char        message[MAX_STRING];
                                161                 :                : 
 1741 andres@anarazel.de        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)
 5104 bruce@momjian.us          171                 :UIC           0 :         pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
                                172                 :                :     else
 1600 tgl@sss.pgh.pa.us         173                 :GIC          37 :         pg_log(PG_REPORT_NONL, "%-*s", MESSAGE_WIDTH, message);
 6044 bruce@momjian.us          174                 :             37 : }
                                175                 :                : 
                                176                 :                : static void
 4806 peter_e@gmx.net           177                 :           8269 : pg_log_v(eLogType type, const char *fmt, va_list ap)
                                178                 :                : {
    8 heikki.linnakangas@i      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. */
 1600 tgl@sss.pgh.pa.us         184         [ -  + ]:           8269 :     Assert(fmt);
                                185   [ +  -  -  + ]:           8269 :     Assert(fmt[0] == '\0' || fmt[strlen(fmt) - 1] != '\n');
                                186                 :                : 
    8 heikki.linnakangas@i      187                 :           8269 :     fmt = _(fmt);
                                188                 :           8269 :     initStringInfo(&buf);
                                189                 :                :     for (;;)
    8 heikki.linnakangas@i      190                 :UIC           0 :     {
                                191                 :                :         va_list     ap2;
                                192                 :                :         int         needed;
                                193                 :                : 
    8 heikki.linnakangas@i      194                 :GIC        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;
    8 heikki.linnakangas@i      200                 :UIC           0 :         enlargeStringInfo(&buf, needed);
                                201                 :                :     }
    8 heikki.linnakangas@i      202                 :GIC        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 */
 5104 bruce@momjian.us          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 */
 5104 bruce@momjian.us          211                 :UIC           0 :             fprintf(log_opts.internal, "  %s\n", message);
 1600 tgl@sss.pgh.pa.us         212         [ +  + ]:GIC        1184 :         else if (type == PG_REPORT_NONL)
 5104 bruce@momjian.us          213                 :            549 :             fprintf(log_opts.internal, "%s", message);
                                214                 :                :         else
 1600 tgl@sss.pgh.pa.us         215                 :            635 :             fprintf(log_opts.internal, "%s\n", message);
 5374 bruce@momjian.us          216                 :           1184 :         fflush(log_opts.internal);
                                217                 :                :     }
                                218                 :                : 
 6044                           219   [ +  +  +  +  :           8269 :     switch (type)
                                              +  - ]
                                220                 :                :     {
 5374                           221                 :           4958 :         case PG_VERBOSE:
 5883                           222         [ -  + ]:           4958 :             if (log_opts.verbose)
 1600 tgl@sss.pgh.pa.us         223                 :UIC           0 :                 printf("%s\n", message);
 6044 bruce@momjian.us          224                 :GIC        4958 :             break;
                                225                 :                : 
 5104                           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                 :                :              */
 1741 andres@anarazel.de        238         [ -  + ]:           2127 :             if (log_opts.isatty)
                                239                 :                :             {
 1600 tgl@sss.pgh.pa.us         240                 :UIC           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                 :                :             }
 1741 andres@anarazel.de        249         [ -  + ]:GIC        2127 :             else if (log_opts.verbose)
 3673 peter_e@gmx.net           250                 :UIC           0 :                 printf("  %s\n", message);
 5104 bruce@momjian.us          251                 :GIC        2127 :             break;
                                252                 :                : 
 1600 tgl@sss.pgh.pa.us         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                 :                : 
 6044 bruce@momjian.us          258                 :            626 :         case PG_REPORT:
                                259                 :                :         case PG_WARNING:
 1600 tgl@sss.pgh.pa.us         260                 :            626 :             printf("%s\n", message);
 6044 bruce@momjian.us          261                 :            626 :             break;
                                262                 :                : 
                                263                 :              9 :         case PG_FATAL:
                                264                 :                :             /* Extra newline in case we're interrupting status output */
 1600 tgl@sss.pgh.pa.us         265                 :              9 :             printf("\n%s\n", message);
 3697 peter_e@gmx.net           266                 :              9 :             printf(_("Failure, exiting\n"));
 4707                           267                 :              9 :             exit(1);
                                268                 :                :             break;
                                269                 :                : 
                                270                 :                :             /* No default:, we want a warning for omitted cases */
                                271                 :                :     }
    8 heikki.linnakangas@i      272                 :           8260 :     pg_free(message);
 6044 bruce@momjian.us          273                 :           8260 :     fflush(stdout);
                                274                 :           8260 : }
                                275                 :                : 
                                276                 :                : 
                                277                 :                : void
  199 tgl@sss.pgh.pa.us         278                 :           7718 : pg_log(eLogType type, const char *fmt, ...)
                                279                 :                : {
                                280                 :                :     va_list     args;
                                281                 :                : 
 4806 peter_e@gmx.net           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
  199 tgl@sss.pgh.pa.us         289                 :              9 : pg_fatal(const char *fmt, ...)
                                290                 :                : {
                                291                 :                :     va_list     args;
                                292                 :                : 
 4806 peter_e@gmx.net           293                 :              9 :     va_start(args, fmt);
                                294                 :              9 :     pg_log_v(PG_FATAL, fmt, args);
 4806 peter_e@gmx.net           295                 :UIC           0 :     va_end(args);
                                296                 :                :     /* NOTREACHED */
 3697                           297                 :              0 :     printf(_("Failure, exiting\n"));
 4806                           298                 :              0 :     exit(1);
                                299                 :                : }
                                300                 :                : 
                                301                 :                : 
                                302                 :                : void
 5884 bruce@momjian.us          303                 :GIC         542 : check_ok(void)
                                304                 :                : {
                                305                 :                :     /* all seems well */
                                306                 :            542 :     report_status(PG_REPORT, "ok");
 6044                           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 *
 5884                           318                 :             22 : quote_identifier(const char *s)
                                319                 :                : {
                                320                 :             22 :     char       *result = pg_malloc(strlen(s) * 2 + 3);
 6044                           321                 :             22 :     char       *r = result;
                                322                 :                : 
                                323                 :             22 :     *r++ = '"';
                                324         [ +  + ]:            220 :     while (*s)
                                325                 :                :     {
                                326         [ -  + ]:            198 :         if (*s == '"')
 6044 bruce@momjian.us          327                 :UIC           0 :             *r++ = *s;
 6044 bruce@momjian.us          328                 :GIC         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
 4728 rhaas@postgresql.org      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
 6044 bruce@momjian.us          349                 :             25 :     user_id = geteuid();
                                350                 :                : #else
                                351                 :                :     user_id = 1;
                                352                 :                : #endif
                                353                 :                : 
 4728 rhaas@postgresql.org      354                 :             25 :     user_name = get_user_name(&errstr);
                                355         [ -  + ]:             25 :     if (!user_name)
 1600 tgl@sss.pgh.pa.us         356                 :UIC           0 :         pg_fatal("%s", errstr);
                                357                 :                : 
                                358                 :                :     /* make a copy */
 4728 rhaas@postgresql.org      359                 :GIC          25 :     *user_name_p = pg_strdup(user_name);
                                360                 :                : 
 6044 bruce@momjian.us          361                 :             25 :     return user_id;
                                362                 :                : }
                                363                 :                : 
                                364                 :                : 
                                365                 :                : /*
                                366                 :                :  *  str2uint()
                                367                 :                :  *
                                368                 :                :  *  convert string to oid
                                369                 :                :  */
                                370                 :                : unsigned int
 5905                           371                 :            878 : str2uint(const char *str)
                                372                 :                : {
 5904                           373                 :            878 :     return strtoul(str, NULL, 10);
                                374                 :                : }
                                375                 :                : 
                                376                 :                : /*
                                377                 :                :  *  str2uint64()
                                378                 :                :  *
                                379                 :                :  *  convert string to uint64
                                380                 :                :  */
                                381                 :                : uint64
   93 msawada@postgresql.o      382                 :             84 : str2uint64(const char *str)
                                383                 :                : {
                                384                 :             84 :     return strtou64(str, NULL, 10);
                                385                 :                : }
        

Generated by: LCOV version 2.0-1