LCOV - differential code coverage report
Current view: top level - src/bin/pg_checksums - pg_checksums.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: 77aeca80249c9e640c811e80633a2e334a9320de vs 38afc3dcb25c45b744d4025029ce0a6c90b7059f Lines: 77.0 % 265 204 1 60 2 202 1 2
Current Date: 2026-07-25 19:08:27 +0900 Functions: 83.3 % 6 5 1 1 4
Baseline: lcov-20260725-baseline Branches: 68.1 % 188 128 60 128
Baseline Date: 2026-07-25 19:09:19 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 66.7 % 3 2 1 2
(30,360] days: 66.7 % 9 6 3 6
(360..) days: 77.5 % 253 196 57 196
Function coverage date bins:
(360..) days: 83.3 % 6 5 1 1 4
Branch coverage date bins:
(30,360] days: 87.5 % 8 7 1 7
(360..) days: 67.2 % 180 121 59 121

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_checksums.c
                                  4                 :                :  *    Checks, enables or disables page level checksums for an offline
                                  5                 :                :  *    cluster
                                  6                 :                :  *
                                  7                 :                :  * Copyright (c) 2010-2026, PostgreSQL Global Development Group
                                  8                 :                :  *
                                  9                 :                :  * IDENTIFICATION
                                 10                 :                :  *    src/bin/pg_checksums/pg_checksums.c
                                 11                 :                :  *
                                 12                 :                :  *-------------------------------------------------------------------------
                                 13                 :                :  */
                                 14                 :                : 
                                 15                 :                : #include "postgres_fe.h"
                                 16                 :                : 
                                 17                 :                : #include <dirent.h>
                                 18                 :                : #include <limits.h>
                                 19                 :                : #include <sys/stat.h>
                                 20                 :                : #include <time.h>
                                 21                 :                : #include <unistd.h>
                                 22                 :                : 
                                 23                 :                : #include "common/controldata_utils.h"
                                 24                 :                : #include "common/file_utils.h"
                                 25                 :                : #include "common/logging.h"
                                 26                 :                : #include "common/relpath.h"
                                 27                 :                : #include "fe_utils/option_utils.h"
                                 28                 :                : #include "fe_utils/version.h"
                                 29                 :                : #include "getopt_long.h"
                                 30                 :                : #include "pg_getopt.h"
                                 31                 :                : #include "storage/bufpage.h"
                                 32                 :                : #include "storage/checksum.h"
                                 33                 :                : #include "storage/checksum_impl.h"
                                 34                 :                : 
                                 35                 :                : 
                                 36                 :                : static int64 files_scanned = 0;
                                 37                 :                : static int64 files_written = 0;
                                 38                 :                : static int64 blocks_scanned = 0;
                                 39                 :                : static int64 blocks_written = 0;
                                 40                 :                : static int64 badblocks = 0;
                                 41                 :                : static ControlFileData *ControlFile;
                                 42                 :                : 
                                 43                 :                : static char *only_filenode = NULL;
                                 44                 :                : static bool do_sync = true;
                                 45                 :                : static bool verbose = false;
                                 46                 :                : static bool showprogress = false;
                                 47                 :                : static DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
                                 48                 :                : 
                                 49                 :                : typedef enum
                                 50                 :                : {
                                 51                 :                :     PG_MODE_CHECK,
                                 52                 :                :     PG_MODE_DISABLE,
                                 53                 :                :     PG_MODE_ENABLE,
                                 54                 :                : } PgChecksumMode;
                                 55                 :                : 
                                 56                 :                : static PgChecksumMode mode = PG_MODE_CHECK;
                                 57                 :                : 
                                 58                 :                : static const char *progname;
                                 59                 :                : 
                                 60                 :                : /*
                                 61                 :                :  * Progress status information.
                                 62                 :                :  */
                                 63                 :                : static int64 total_size = 0;
                                 64                 :                : static int64 current_size = 0;
                                 65                 :                : static pg_time_t last_progress_report = 0;
                                 66                 :                : 
                                 67                 :                : static void
 2885 tgl@sss.pgh.pa.us          68                 :CBC           1 : usage(void)
                                 69                 :                : {
 2581 peter@eisentraut.org       70                 :              1 :     printf(_("%s enables, disables, or verifies data checksums in a PostgreSQL database cluster.\n\n"), progname);
 3033 magnus@hagander.net        71                 :              1 :     printf(_("Usage:\n"));
 2893 peter_e@gmx.net            72                 :              1 :     printf(_("  %s [OPTION]... [DATADIR]\n"), progname);
 3033 magnus@hagander.net        73                 :              1 :     printf(_("\nOptions:\n"));
 2613 michael@paquier.xyz        74                 :              1 :     printf(_(" [-D, --pgdata=]DATADIR    data directory\n"));
                                 75                 :              1 :     printf(_("  -c, --check              check data checksums (default)\n"));
                                 76                 :              1 :     printf(_("  -d, --disable            disable data checksums\n"));
                                 77                 :              1 :     printf(_("  -e, --enable             enable data checksums\n"));
                                 78                 :              1 :     printf(_("  -f, --filenode=FILENODE  check only relation with specified filenode\n"));
                                 79                 :              1 :     printf(_("  -N, --no-sync            do not wait for changes to be written safely to disk\n"));
                                 80                 :              1 :     printf(_("  -P, --progress           show progress information\n"));
 1053 nathan@postgresql.or       81                 :              1 :     printf(_("      --sync-method=METHOD set method for syncing files to disk\n"));
 2613 michael@paquier.xyz        82                 :              1 :     printf(_("  -v, --verbose            output verbose messages\n"));
                                 83                 :              1 :     printf(_("  -V, --version            output version information, then exit\n"));
                                 84                 :              1 :     printf(_("  -?, --help               show this help, then exit\n"));
 3033 magnus@hagander.net        85                 :              1 :     printf(_("\nIf no data directory (DATADIR) is specified, "
                                 86                 :                :              "the environment variable PGDATA\nis used.\n\n"));
 2339 peter@eisentraut.org       87                 :              1 :     printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
                                 88                 :              1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
 3033 magnus@hagander.net        89                 :              1 : }
                                 90                 :                : 
                                 91                 :                : /*
                                 92                 :                :  * Definition of one element part of an exclusion list, used for files
                                 93                 :                :  * to exclude from checksum validation.  "name" is the name of the file
                                 94                 :                :  * or path to check for exclusion.  If "match_prefix" is true, any items
                                 95                 :                :  * matching the name as prefix are excluded.
                                 96                 :                :  */
                                 97                 :                : struct exclude_list_item
                                 98                 :                : {
                                 99                 :                :     const char *name;
                                100                 :                :     bool        match_prefix;
                                101                 :                : };
                                102                 :                : 
                                103                 :                : /*
                                104                 :                :  * List of files excluded from checksum validation.
                                105                 :                :  *
                                106                 :                :  * Note: this list should be kept in sync with what basebackup.c includes.
                                107                 :                :  */
                                108                 :                : static const struct exclude_list_item skip[] = {
                                109                 :                :     {"pg_control", false},
                                110                 :                :     {"pg_filenode.map", false},
                                111                 :                :     {"pg_internal.init", true},
                                112                 :                :     {"PG_VERSION", false},
                                113                 :                : #ifdef EXEC_BACKEND
                                114                 :                :     {"config_exec_params", true},
                                115                 :                : #endif
                                116                 :                :     {NULL, false}
                                117                 :                : };
                                118                 :                : 
                                119                 :                : /*
                                120                 :                :  * Report current progress status.  Parts borrowed from
                                121                 :                :  * src/bin/pg_basebackup/pg_basebackup.c.
                                122                 :                :  */
                                123                 :                : static void
 2168 heikki.linnakangas@i      124                 :UBC           0 : progress_report(bool finished)
                                125                 :                : {
                                126                 :                :     int         percent;
                                127                 :                :     pg_time_t   now;
                                128                 :                : 
 2671 michael@paquier.xyz       129         [ #  # ]:              0 :     Assert(showprogress);
                                130                 :                : 
                                131                 :              0 :     now = time(NULL);
 2168 heikki.linnakangas@i      132   [ #  #  #  # ]:              0 :     if (now == last_progress_report && !finished)
 2671 michael@paquier.xyz       133                 :              0 :         return;                 /* Max once per second */
                                134                 :                : 
                                135                 :                :     /* Save current time */
                                136                 :              0 :     last_progress_report = now;
                                137                 :                : 
                                138                 :                :     /* Adjust total size if current_size is larger */
                                139         [ #  # ]:              0 :     if (current_size > total_size)
                                140                 :              0 :         total_size = current_size;
                                141                 :                : 
                                142                 :                :     /* Calculate current percentage of size done */
                                143         [ #  # ]:              0 :     percent = total_size ? (int) ((current_size) * 100 / total_size) : 0;
                                144                 :                : 
  510 peter@eisentraut.org      145                 :              0 :     fprintf(stderr, _("%" PRId64 "/%" PRId64 " MB (%d%%) computed"),
                                146                 :                :             (current_size / (1024 * 1024)),
                                147                 :                :             (total_size / (1024 * 1024)),
                                148                 :                :             percent);
                                149                 :                : 
                                150                 :                :     /*
                                151                 :                :      * Stay on the same line if reporting to a terminal and we're not done
                                152                 :                :      * yet.
                                153                 :                :      */
 2167 heikki.linnakangas@i      154   [ #  #  #  # ]:              0 :     fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
                                155                 :                : }
                                156                 :                : 
                                157                 :                : static bool
 2794 michael@paquier.xyz       158                 :CBC       15674 : skipfile(const char *fn)
                                159                 :                : {
                                160                 :                :     int         excludeIdx;
                                161                 :                : 
 2343                           162         [ +  + ]:          77985 :     for (excludeIdx = 0; skip[excludeIdx].name != NULL; excludeIdx++)
                                163                 :                :     {
                                164                 :          62477 :         int         cmplen = strlen(skip[excludeIdx].name);
                                165                 :                : 
                                166         [ +  + ]:          62477 :         if (!skip[excludeIdx].match_prefix)
                                167                 :          46883 :             cmplen++;
                                168         [ +  + ]:          62477 :         if (strncmp(skip[excludeIdx].name, fn, cmplen) == 0)
 2794                           169                 :            166 :             return true;
                                170                 :                :     }
                                171                 :                : 
                                172                 :          15508 :     return false;
                                173                 :                : }
                                174                 :                : 
                                175                 :                : static void
 1683 peter@eisentraut.org      176                 :          11436 : scan_file(const char *fn, int segmentno)
                                177                 :                : {
                                178                 :                :     PGIOAlignedBlock buf;
 2884 tgl@sss.pgh.pa.us         179                 :          11436 :     PageHeader  header = (PageHeader) buf.data;
                                180                 :                :     int         f;
                                181                 :                :     BlockNumber blockno;
                                182                 :                :     int         flags;
 1851 michael@paquier.xyz       183                 :          11436 :     int64       blocks_written_in_file = 0;
                                184                 :                : 
 2681                           185   [ +  +  -  + ]:          11436 :     Assert(mode == PG_MODE_ENABLE ||
                                186                 :                :            mode == PG_MODE_CHECK);
                                187                 :                : 
                                188         [ +  + ]:          11436 :     flags = (mode == PG_MODE_ENABLE) ? O_RDWR : O_RDONLY;
                                189                 :          11436 :     f = open(fn, PG_BINARY | flags, 0);
                                190                 :                : 
 3033 magnus@hagander.net       191         [ -  + ]:          11436 :     if (f < 0)
 1569 tgl@sss.pgh.pa.us         192                 :UBC           0 :         pg_fatal("could not open file \"%s\": %m", fn);
                                193                 :                : 
 1851 michael@paquier.xyz       194                 :CBC       11436 :     files_scanned++;
                                195                 :                : 
 3033 magnus@hagander.net       196                 :          11436 :     for (blockno = 0;; blockno++)
                                197                 :          33359 :     {
                                198                 :                :         uint16      csum;
                                199                 :                :         ssize_t     r;
                                200                 :                : 
   10 peter@eisentraut.org      201                 :GNC       44795 :         r = read(f, buf.data, BLCKSZ);
 3033 magnus@hagander.net       202         [ +  + ]:CBC       44795 :         if (r == 0)
                                203                 :          11428 :             break;
                                204         [ +  + ]:          33367 :         if (r != BLCKSZ)
                                205                 :                :         {
 2517 peter@eisentraut.org      206         [ -  + ]:              8 :             if (r < 0)
 1569 tgl@sss.pgh.pa.us         207                 :UBC           0 :                 pg_fatal("could not read block %u in file \"%s\": %m",
                                208                 :                :                          blockno, fn);
                                209                 :                :             else
   10 peter@eisentraut.org      210                 :GNC           8 :                 pg_fatal("could not read block %u in file \"%s\": read %zd of %zu",
                                211                 :                :                          blockno, fn, r, (size_t) BLCKSZ);
                                212                 :                :         }
 1851 michael@paquier.xyz       213                 :CBC       33359 :         blocks_scanned++;
                                214                 :                : 
                                215                 :                :         /*
                                216                 :                :          * Since the file size is counted as total_size for progress status
                                217                 :                :          * information, the sizes of all pages including new ones in the file
                                218                 :                :          * should be counted as current_size. Otherwise the progress reporting
                                219                 :                :          * calculated using those counters may not reach 100%.
                                220                 :                :          */
 1939 fujii@postgresql.org      221                 :          33359 :         current_size += r;
                                222                 :                : 
                                223                 :                :         /* New pages have no checksum yet */
 1475 peter@eisentraut.org      224         [ +  + ]:          33359 :         if (PageIsNew(buf.data))
 3023 magnus@hagander.net       225                 :            155 :             continue;
                                226                 :                : 
 2884 tgl@sss.pgh.pa.us         227                 :          33204 :         csum = pg_checksum_page(buf.data, blockno + segmentno * RELSEG_SIZE);
 2681 michael@paquier.xyz       228         [ +  + ]:          33204 :         if (mode == PG_MODE_CHECK)
                                229                 :                :         {
                                230         [ +  + ]:          21253 :             if (csum != header->pd_checksum)
                                231                 :                :             {
                                232         [ +  - ]:              4 :                 if (ControlFile->data_checksum_version == PG_DATA_CHECKSUM_VERSION)
 2672 peter@eisentraut.org      233                 :              4 :                     pg_log_error("checksum verification failed in file \"%s\", block %u: calculated checksum %X but block contains %X",
                                234                 :                :                                  fn, blockno, csum, header->pd_checksum);
 2681 michael@paquier.xyz       235                 :              4 :                 badblocks++;
                                236                 :                :             }
                                237                 :                :         }
                                238         [ +  - ]:          11951 :         else if (mode == PG_MODE_ENABLE)
                                239                 :                :         {
                                240                 :                :             ssize_t     w;
                                241                 :                : 
                                242                 :                :             /*
                                243                 :                :              * Do not rewrite if the checksum is already set to the expected
                                244                 :                :              * value.
                                245                 :                :              */
 1851                           246         [ +  + ]:          11951 :             if (header->pd_checksum == csum)
                                247                 :           5973 :                 continue;
                                248                 :                : 
                                249                 :           5978 :             blocks_written_in_file++;
                                250                 :                : 
                                251                 :                :             /* Set checksum in page header */
 2681                           252                 :           5978 :             header->pd_checksum = csum;
                                253                 :                : 
                                254                 :                :             /* Seek back to beginning of block */
                                255         [ -  + ]:           5978 :             if (lseek(f, -BLCKSZ, SEEK_CUR) < 0)
 1569 tgl@sss.pgh.pa.us         256                 :UBC           0 :                 pg_fatal("seek failed for block %u in file \"%s\": %m", blockno, fn);
                                257                 :                : 
                                258                 :                :             /* Write block with checksum */
 2517 peter@eisentraut.org      259                 :CBC        5978 :             w = write(f, buf.data, BLCKSZ);
                                260         [ -  + ]:           5978 :             if (w != BLCKSZ)
                                261                 :                :             {
 2517 peter@eisentraut.org      262         [ #  # ]:UBC           0 :                 if (w < 0)
 1569 tgl@sss.pgh.pa.us         263                 :              0 :                     pg_fatal("could not write block %u in file \"%s\": %m",
                                264                 :                :                              blockno, fn);
                                265                 :                :                 else
   10 peter@eisentraut.org      266                 :UNC           0 :                     pg_fatal("could not write block %u in file \"%s\": wrote %zd of %zu",
                                267                 :                :                              blockno, fn, w, (size_t) BLCKSZ);
                                268                 :                :             }
                                269                 :                :         }
                                270                 :                : 
 2671 michael@paquier.xyz       271         [ -  + ]:CBC       27231 :         if (showprogress)
 2671 michael@paquier.xyz       272                 :UBC           0 :             progress_report(false);
                                273                 :                :     }
                                274                 :                : 
 2886 alvherre@alvh.no-ip.      275         [ -  + ]:CBC       11428 :     if (verbose)
                                276                 :                :     {
 2681 michael@paquier.xyz       277         [ #  # ]:UBC           0 :         if (mode == PG_MODE_CHECK)
 2672 peter@eisentraut.org      278                 :              0 :             pg_log_info("checksums verified in file \"%s\"", fn);
 2681 michael@paquier.xyz       279         [ #  # ]:              0 :         if (mode == PG_MODE_ENABLE)
 2672 peter@eisentraut.org      280                 :              0 :             pg_log_info("checksums enabled in file \"%s\"", fn);
                                281                 :                :     }
                                282                 :                : 
                                283                 :                :     /* Update write counters if any write activity has happened */
 1851 michael@paquier.xyz       284         [ +  + ]:CBC       11428 :     if (blocks_written_in_file > 0)
                                285                 :                :     {
                                286                 :           1644 :         files_written++;
                                287                 :           1644 :         blocks_written += blocks_written_in_file;
                                288                 :                :     }
                                289                 :                : 
 3033 magnus@hagander.net       290                 :          11428 :     close(f);
                                291                 :          11428 : }
                                292                 :                : 
                                293                 :                : /*
                                294                 :                :  * Scan the given directory for items which can be checksummed and
                                295                 :                :  * operate on each one of them.  If "sizeonly" is true, the size of
                                296                 :                :  * all the items which have checksums is computed and returned back
                                297                 :                :  * to the caller without operating on the files.  This is used to compile
                                298                 :                :  * the total size of the data directory for progress reports.
                                299                 :                :  */
                                300                 :                : static int64
 2671 michael@paquier.xyz       301                 :            108 : scan_directory(const char *basedir, const char *subdir, bool sizeonly)
                                302                 :                : {
                                303                 :            108 :     int64       dirsize = 0;
                                304                 :                :     char        path[MAXPGPATH];
                                305                 :                :     DIR        *dir;
                                306                 :                :     struct dirent *de;
                                307                 :                : 
 3032 peter_e@gmx.net           308                 :            108 :     snprintf(path, sizeof(path), "%s/%s", basedir, subdir);
 3033 magnus@hagander.net       309                 :            108 :     dir = opendir(path);
                                310         [ -  + ]:            108 :     if (!dir)
 1569 tgl@sss.pgh.pa.us         311                 :UBC           0 :         pg_fatal("could not open directory \"%s\": %m", path);
 3033 magnus@hagander.net       312         [ +  + ]:CBC       16085 :     while ((de = readdir(dir)) != NULL)
                                313                 :                :     {
                                314                 :                :         char        fn[MAXPGPATH];
                                315                 :                :         struct stat st;
                                316                 :                : 
 2794 michael@paquier.xyz       317         [ +  + ]:          15985 :         if (strcmp(de->d_name, ".") == 0 ||
                                318         [ +  + ]:          15882 :             strcmp(de->d_name, "..") == 0)
 3033 magnus@hagander.net       319                 :           4494 :             continue;
                                320                 :                : 
                                321                 :                :         /* Skip temporary files */
 2794 michael@paquier.xyz       322         [ +  + ]:          15779 :         if (strncmp(de->d_name,
                                323                 :                :                     PG_TEMP_FILE_PREFIX,
                                324                 :                :                     strlen(PG_TEMP_FILE_PREFIX)) == 0)
                                325                 :             31 :             continue;
                                326                 :                : 
                                327                 :                :         /* Skip temporary folders */
                                328         [ -  + ]:          15748 :         if (strncmp(de->d_name,
                                329                 :                :                     PG_TEMP_FILES_DIR,
                                330                 :                :                     strlen(PG_TEMP_FILES_DIR)) == 0)
 2690 michael@paquier.xyz       331                 :UBC           0 :             continue;
                                332                 :                : 
                                333                 :                :         /* Skip macOS system files */
  893 dgustafsson@postgres      334         [ +  + ]:CBC       15748 :         if (strcmp(de->d_name, ".DS_Store") == 0)
                                335                 :             19 :             continue;
                                336                 :                : 
 3032 peter_e@gmx.net           337                 :          15729 :         snprintf(fn, sizeof(fn), "%s/%s", path, de->d_name);
 3033 magnus@hagander.net       338         [ -  + ]:          15729 :         if (lstat(fn, &st) < 0)
 1569 tgl@sss.pgh.pa.us         339                 :UBC           0 :             pg_fatal("could not stat file \"%s\": %m", fn);
 3033 magnus@hagander.net       340         [ +  + ]:CBC       15729 :         if (S_ISREG(st.st_mode))
                                341                 :                :         {
                                342                 :                :             char        fnonly[MAXPGPATH];
                                343                 :                :             char       *forkpath,
                                344                 :                :                        *segmentpath;
 1683 peter@eisentraut.org      345                 :          15674 :             int         segmentno = 0;
                                346                 :                : 
 2794 michael@paquier.xyz       347         [ +  + ]:          15674 :             if (skipfile(de->d_name))
                                348                 :           4238 :                 continue;
                                349                 :                : 
                                350                 :                :             /*
                                351                 :                :              * Cut off at the segment boundary (".") to get the segment number
                                352                 :                :              * in order to mix it into the checksum. Then also cut off at the
                                353                 :                :              * fork boundary, to get the filenode the file belongs to for
                                354                 :                :              * filtering.
                                355                 :                :              */
 2885 tgl@sss.pgh.pa.us         356                 :          15508 :             strlcpy(fnonly, de->d_name, sizeof(fnonly));
                                357                 :          15508 :             segmentpath = strchr(fnonly, '.');
 3033 magnus@hagander.net       358         [ +  + ]:          15508 :             if (segmentpath != NULL)
                                359                 :                :             {
                                360                 :             71 :                 *segmentpath++ = '\0';
                                361                 :             71 :                 segmentno = atoi(segmentpath);
                                362         [ -  + ]:             71 :                 if (segmentno == 0)
 1569 tgl@sss.pgh.pa.us         363                 :UBC           0 :                     pg_fatal("invalid segment number %d in file name \"%s\"",
                                364                 :                :                              segmentno, fn);
                                365                 :                :             }
                                366                 :                : 
 2885 tgl@sss.pgh.pa.us         367                 :CBC       15508 :             forkpath = strchr(fnonly, '_');
 3033 magnus@hagander.net       368         [ +  + ]:          15508 :             if (forkpath != NULL)
                                369                 :           3617 :                 *forkpath++ = '\0';
                                370                 :                : 
 2613 michael@paquier.xyz       371   [ +  +  +  + ]:          15508 :             if (only_filenode && strcmp(only_filenode, fnonly) != 0)
                                372                 :                :                 /* filenode not to be included */
 3033 magnus@hagander.net       373                 :           4072 :                 continue;
                                374                 :                : 
 2671 michael@paquier.xyz       375                 :          11436 :             dirsize += st.st_size;
                                376                 :                : 
                                377                 :                :             /*
                                378                 :                :              * No need to work on the file when calculating only the size of
                                379                 :                :              * the items in the data folder.
                                380                 :                :              */
                                381         [ +  - ]:          11436 :             if (!sizeonly)
                                382                 :          11436 :                 scan_file(fn, segmentno);
                                383                 :                :         }
 3033 magnus@hagander.net       384   [ +  +  +  - ]:             55 :         else if (S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
                                385                 :                :         {
                                386                 :                :             /*
                                387                 :                :              * If going through the entries of pg_tblspc, we assume to operate
                                388                 :                :              * on tablespace locations where only TABLESPACE_VERSION_DIRECTORY
                                389                 :                :              * is valid, resolving the linked locations and dive into them
                                390                 :                :              * directly.
                                391                 :                :              */
  690 michael@paquier.xyz       392         [ +  + ]:             55 :             if (strncmp(PG_TBLSPC_DIR, subdir, strlen(PG_TBLSPC_DIR)) == 0)
                                393                 :                :             {
                                394                 :                :                 char        tblspc_path[MAXPGPATH];
                                395                 :                :                 struct stat tblspc_st;
                                396                 :                : 
                                397                 :                :                 /*
                                398                 :                :                  * Resolve tablespace location path and check whether
                                399                 :                :                  * TABLESPACE_VERSION_DIRECTORY exists.  Not finding a valid
                                400                 :                :                  * location is unexpected, since there should be no orphaned
                                401                 :                :                  * links and no links pointing to something else than a
                                402                 :                :                  * directory.
                                403                 :                :                  */
 2340                           404                 :              5 :                 snprintf(tblspc_path, sizeof(tblspc_path), "%s/%s/%s",
                                405                 :              5 :                          path, de->d_name, TABLESPACE_VERSION_DIRECTORY);
                                406                 :                : 
                                407         [ -  + ]:              5 :                 if (lstat(tblspc_path, &tblspc_st) < 0)
 1569 tgl@sss.pgh.pa.us         408                 :UBC           0 :                     pg_fatal("could not stat file \"%s\": %m",
                                409                 :                :                              tblspc_path);
                                410                 :                : 
                                411                 :                :                 /*
                                412                 :                :                  * Move backwards once as the scan needs to happen for the
                                413                 :                :                  * contents of TABLESPACE_VERSION_DIRECTORY.
                                414                 :                :                  */
 2340 michael@paquier.xyz       415                 :CBC           5 :                 snprintf(tblspc_path, sizeof(tblspc_path), "%s/%s",
                                416                 :              5 :                          path, de->d_name);
                                417                 :                : 
                                418                 :                :                 /* Looks like a valid tablespace location */
                                419                 :              5 :                 dirsize += scan_directory(tblspc_path,
                                420                 :                :                                           TABLESPACE_VERSION_DIRECTORY,
                                421                 :                :                                           sizeonly);
                                422                 :                :             }
                                423                 :                :             else
                                424                 :                :             {
                                425                 :             50 :                 dirsize += scan_directory(path, de->d_name, sizeonly);
                                426                 :                :             }
                                427                 :                :         }
                                428                 :                :     }
 3033 magnus@hagander.net       429                 :            100 :     closedir(dir);
 2671 michael@paquier.xyz       430                 :            100 :     return dirsize;
                                431                 :                : }
                                432                 :                : 
                                433                 :                : int
 3033 magnus@hagander.net       434                 :             34 : main(int argc, char *argv[])
                                435                 :                : {
                                436                 :                :     static struct option long_options[] = {
                                437                 :                :         {"check", no_argument, NULL, 'c'},
                                438                 :                :         {"pgdata", required_argument, NULL, 'D'},
                                439                 :                :         {"disable", no_argument, NULL, 'd'},
                                440                 :                :         {"enable", no_argument, NULL, 'e'},
                                441                 :                :         {"filenode", required_argument, NULL, 'f'},
                                442                 :                :         {"no-sync", no_argument, NULL, 'N'},
                                443                 :                :         {"progress", no_argument, NULL, 'P'},
                                444                 :                :         {"verbose", no_argument, NULL, 'v'},
                                445                 :                :         {"sync-method", required_argument, NULL, 1},
                                446                 :                :         {NULL, 0, NULL, 0}
                                447                 :                :     };
                                448                 :                : 
                                449                 :             34 :     char       *DataDir = NULL;
                                450                 :                :     int         c;
                                451                 :                :     int         option_index;
                                452                 :                :     bool        crc_ok;
                                453                 :                :     uint32      major_version;
                                454                 :                :     char       *version_str;
                                455                 :                : 
 2672 peter@eisentraut.org      456                 :             34 :     pg_logging_init(argv[0]);
 2691 michael@paquier.xyz       457                 :             34 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_checksums"));
 3033 magnus@hagander.net       458                 :             34 :     progname = get_progname(argv[0]);
                                459                 :                : 
                                460         [ +  - ]:             34 :     if (argc > 1)
                                461                 :                :     {
                                462   [ +  +  -  + ]:             34 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
                                463                 :                :         {
                                464                 :              1 :             usage();
                                465                 :              1 :             exit(0);
                                466                 :                :         }
                                467   [ +  +  -  + ]:             33 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
                                468                 :                :         {
 2691 michael@paquier.xyz       469                 :              1 :             puts("pg_checksums (PostgreSQL) " PG_VERSION);
 3033 magnus@hagander.net       470                 :              1 :             exit(0);
                                471                 :                :         }
                                472                 :                :     }
                                473                 :                : 
 1321 peter@eisentraut.org      474         [ +  + ]:            102 :     while ((c = getopt_long(argc, argv, "cdD:ef:NPv", long_options, &option_index)) != -1)
                                475                 :                :     {
 3033 magnus@hagander.net       476   [ +  +  +  +  :             71 :         switch (c)
                                     +  +  -  -  -  
                                                 + ]
                                477                 :                :         {
 2681 michael@paquier.xyz       478                 :             19 :             case 'c':
                                479                 :             19 :                 mode = PG_MODE_CHECK;
                                480                 :             19 :                 break;
                                481                 :              4 :             case 'd':
                                482                 :              4 :                 mode = PG_MODE_DISABLE;
                                483                 :              4 :                 break;
 1321 peter@eisentraut.org      484                 :             31 :             case 'D':
                                485                 :             31 :                 DataDir = optarg;
                                486                 :             31 :                 break;
 2681 michael@paquier.xyz       487                 :              6 :             case 'e':
                                488                 :              6 :                 mode = PG_MODE_ENABLE;
                                489                 :              6 :                 break;
 2613                           490                 :              6 :             case 'f':
 1396 rhaas@postgresql.org      491         [ -  + ]:              6 :                 if (!option_parse_int(optarg, "-f/--filenode", 0,
                                492                 :                :                                       INT_MAX,
                                493                 :                :                                       NULL))
 2613 michael@paquier.xyz       494                 :UBC           0 :                     exit(1);
 2613 michael@paquier.xyz       495                 :CBC           6 :                 only_filenode = pstrdup(optarg);
                                496                 :              6 :                 break;
 2681                           497                 :              4 :             case 'N':
                                498                 :              4 :                 do_sync = false;
                                499                 :              4 :                 break;
 2671 michael@paquier.xyz       500                 :UBC           0 :             case 'P':
                                501                 :              0 :                 showprogress = true;
                                502                 :              0 :                 break;
 1321 peter@eisentraut.org      503                 :              0 :             case 'v':
                                504                 :              0 :                 verbose = true;
                                505                 :              0 :                 break;
 1053 nathan@postgresql.or      506                 :              0 :             case 1:
                                507         [ #  # ]:              0 :                 if (!parse_sync_method(optarg, &sync_method))
                                508                 :              0 :                     exit(1);
                                509                 :              0 :                 break;
 3033 magnus@hagander.net       510                 :CBC           1 :             default:
                                511                 :                :                 /* getopt_long already emitted a complaint */
 1569 tgl@sss.pgh.pa.us         512                 :              1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 3033 magnus@hagander.net       513                 :              1 :                 exit(1);
                                514                 :                :         }
                                515                 :                :     }
                                516                 :                : 
                                517         [ -  + ]:             31 :     if (DataDir == NULL)
                                518                 :                :     {
 3033 magnus@hagander.net       519         [ #  # ]:UBC           0 :         if (optind < argc)
                                520                 :              0 :             DataDir = argv[optind++];
                                521                 :                :         else
                                522                 :              0 :             DataDir = getenv("PGDATA");
                                523                 :                : 
                                524                 :                :         /* If no DataDir was specified, and none could be found, error out */
                                525         [ #  # ]:              0 :         if (DataDir == NULL)
                                526                 :                :         {
 2672 peter@eisentraut.org      527                 :              0 :             pg_log_error("no data directory specified");
 1569 tgl@sss.pgh.pa.us         528                 :              0 :             pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 3033 magnus@hagander.net       529                 :              0 :             exit(1);
                                530                 :                :         }
                                531                 :                :     }
                                532                 :                : 
                                533                 :                :     /* Complain if any arguments remain */
 3033 magnus@hagander.net       534         [ -  + ]:CBC          31 :     if (optind < argc)
                                535                 :                :     {
 2672 peter@eisentraut.org      536                 :UBC           0 :         pg_log_error("too many command-line arguments (first is \"%s\")",
                                537                 :                :                      argv[optind]);
 1569 tgl@sss.pgh.pa.us         538                 :              0 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 3033 magnus@hagander.net       539                 :              0 :         exit(1);
                                540                 :                :     }
                                541                 :                : 
                                542                 :                :     /* filenode checking only works in --check mode */
 2613 michael@paquier.xyz       543   [ +  +  +  + ]:CBC          31 :     if (mode != PG_MODE_CHECK && only_filenode)
                                544                 :                :     {
 2573 peter@eisentraut.org      545                 :              2 :         pg_log_error("option -f/--filenode can only be used with --check");
 1569 tgl@sss.pgh.pa.us         546                 :              2 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 2681 michael@paquier.xyz       547                 :              2 :         exit(1);
                                548                 :                :     }
                                549                 :                : 
                                550                 :                :     /*
                                551                 :                :      * Retrieve the contents of this cluster's PG_VERSION.  We require
                                552                 :                :      * compatibility with the same major version as the one this tool is
                                553                 :                :      * compiled with.
                                554                 :                :      */
  278                           555                 :             29 :     major_version = GET_PG_MAJORVERSION_NUM(get_pg_version(DataDir, &version_str));
                                556         [ -  + ]:             29 :     if (major_version != PG_MAJORVERSION_NUM)
                                557                 :                :     {
  278 michael@paquier.xyz       558                 :UBC           0 :         pg_log_error("data directory is of wrong version");
                                559                 :              0 :         pg_log_error_detail("File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".",
                                560                 :                :                             "PG_VERSION", version_str, PG_MAJORVERSION);
                                561                 :              0 :         exit(1);
                                562                 :                :     }
                                563                 :                : 
                                564                 :                :     /* Read the control file and check compatibility */
 2672 peter@eisentraut.org      565                 :CBC          29 :     ControlFile = get_controlfile(DataDir, &crc_ok);
 3033 magnus@hagander.net       566         [ -  + ]:             29 :     if (!crc_ok)
 1569 tgl@sss.pgh.pa.us         567                 :UBC           0 :         pg_fatal("pg_control CRC value is incorrect");
                                568                 :                : 
 2691 michael@paquier.xyz       569         [ -  + ]:CBC          29 :     if (ControlFile->pg_control_version != PG_CONTROL_VERSION)
 1569 tgl@sss.pgh.pa.us         570                 :UBC           0 :         pg_fatal("cluster is not compatible with this version of pg_checksums");
                                571                 :                : 
 2686 michael@paquier.xyz       572         [ -  + ]:CBC          29 :     if (ControlFile->blcksz != BLCKSZ)
                                573                 :                :     {
 2672 peter@eisentraut.org      574                 :UBC           0 :         pg_log_error("database cluster is not compatible");
 1569 tgl@sss.pgh.pa.us         575                 :              0 :         pg_log_error_detail("The database cluster was initialized with block size %u, but pg_checksums was compiled with block size %u.",
                                576                 :                :                             ControlFile->blcksz, BLCKSZ);
 2686 michael@paquier.xyz       577                 :              0 :         exit(1);
                                578                 :                :     }
                                579                 :                : 
                                580                 :                :     /*
                                581                 :                :      * Check if cluster is running.  A clean shutdown is required to avoid
                                582                 :                :      * random checksum failures caused by torn pages.  Note that this doesn't
                                583                 :                :      * guard against someone starting the cluster concurrently.
                                584                 :                :      */
 3033 magnus@hagander.net       585         [ +  + ]:CBC          29 :     if (ControlFile->state != DB_SHUTDOWNED &&
                                586         [ +  - ]:              1 :         ControlFile->state != DB_SHUTDOWNED_IN_RECOVERY)
 1569 tgl@sss.pgh.pa.us         587                 :              1 :         pg_fatal("cluster must be shut down");
                                588                 :                : 
  113 dgustafsson@postgres      589         [ +  + ]:             28 :     if (ControlFile->data_checksum_version != PG_DATA_CHECKSUM_VERSION &&
 2681 michael@paquier.xyz       590         [ +  + ]:              6 :         mode == PG_MODE_CHECK)
 1569 tgl@sss.pgh.pa.us         591                 :              1 :         pg_fatal("data checksums are not enabled in cluster");
                                592                 :                : 
  110 dgustafsson@postgres      593         [ +  + ]:             27 :     if (ControlFile->data_checksum_version == PG_DATA_CHECKSUM_OFF &&
 2681 michael@paquier.xyz       594         [ +  + ]:              5 :         mode == PG_MODE_DISABLE)
 1569 tgl@sss.pgh.pa.us         595                 :              1 :         pg_fatal("data checksums are already disabled in cluster");
                                596                 :                : 
  113 dgustafsson@postgres      597         [ +  + ]:             26 :     if (ControlFile->data_checksum_version == PG_DATA_CHECKSUM_VERSION &&
 2681 michael@paquier.xyz       598         [ +  + ]:             22 :         mode == PG_MODE_ENABLE)
 1569 tgl@sss.pgh.pa.us         599                 :              1 :         pg_fatal("data checksums are already enabled in cluster");
                                600                 :                : 
                                601                 :                :     /* Operate on all files if checking or enabling checksums */
 2681 michael@paquier.xyz       602   [ +  +  +  + ]:             25 :     if (mode == PG_MODE_CHECK || mode == PG_MODE_ENABLE)
                                603                 :                :     {
                                604                 :                :         /*
                                605                 :                :          * If progress status information is requested, we need to scan the
                                606                 :                :          * directory tree twice: once to know how much total data needs to be
                                607                 :                :          * processed and once to do the real work.
                                608                 :                :          */
 2671                           609         [ -  + ]:             23 :         if (showprogress)
                                610                 :                :         {
 2671 michael@paquier.xyz       611                 :UBC           0 :             total_size = scan_directory(DataDir, "global", true);
                                612                 :              0 :             total_size += scan_directory(DataDir, "base", true);
  690                           613                 :              0 :             total_size += scan_directory(DataDir, PG_TBLSPC_DIR, true);
                                614                 :                :         }
                                615                 :                : 
 2671 michael@paquier.xyz       616                 :CBC          23 :         (void) scan_directory(DataDir, "global", false);
                                617                 :             15 :         (void) scan_directory(DataDir, "base", false);
  690                           618                 :             15 :         (void) scan_directory(DataDir, PG_TBLSPC_DIR, false);
                                619                 :                : 
 2671                           620         [ -  + ]:             15 :         if (showprogress)
 2671 michael@paquier.xyz       621                 :UBC           0 :             progress_report(true);
                                622                 :                : 
 2681 michael@paquier.xyz       623                 :CBC          15 :         printf(_("Checksum operation completed\n"));
  510 peter@eisentraut.org      624                 :             15 :         printf(_("Files scanned:   %" PRId64 "\n"), files_scanned);
                                625                 :             15 :         printf(_("Blocks scanned:  %" PRId64 "\n"), blocks_scanned);
 2681 michael@paquier.xyz       626         [ +  + ]:             15 :         if (mode == PG_MODE_CHECK)
                                627                 :                :         {
  510 peter@eisentraut.org      628                 :             11 :             printf(_("Bad checksums:  %" PRId64 "\n"), badblocks);
 2062 bruce@momjian.us          629                 :             11 :             printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
                                630                 :                : 
 2681 michael@paquier.xyz       631         [ +  + ]:             11 :             if (badblocks > 0)
                                632                 :              4 :                 exit(1);
                                633                 :                :         }
 1851                           634         [ +  - ]:              4 :         else if (mode == PG_MODE_ENABLE)
                                635                 :                :         {
  510 peter@eisentraut.org      636                 :              4 :             printf(_("Files written:  %" PRId64 "\n"), files_written);
                                637                 :              4 :             printf(_("Blocks written: %" PRId64 "\n"), blocks_written);
                                638                 :                :         }
                                639                 :                :     }
                                640                 :                : 
                                641                 :                :     /*
                                642                 :                :      * Finally make the data durable on disk if enabling or disabling
                                643                 :                :      * checksums.  Flush first the data directory for safety, and then update
                                644                 :                :      * the control file to keep the switch consistent.
                                645                 :                :      */
 2681 michael@paquier.xyz       646   [ +  +  +  + ]:             13 :     if (mode == PG_MODE_ENABLE || mode == PG_MODE_DISABLE)
                                647                 :                :     {
                                648                 :              6 :         ControlFile->data_checksum_version =
  110 dgustafsson@postgres      649                 :              6 :             (mode == PG_MODE_ENABLE) ? PG_DATA_CHECKSUM_VERSION : PG_DATA_CHECKSUM_OFF;
                                650                 :                : 
 2681 michael@paquier.xyz       651         [ +  + ]:              6 :         if (do_sync)
                                652                 :                :         {
 2672 peter@eisentraut.org      653                 :              4 :             pg_log_info("syncing data directory");
  487 nathan@postgresql.or      654                 :              4 :             sync_pgdata(DataDir, PG_VERSION_NUM, sync_method, true);
                                655                 :                :         }
                                656                 :                : 
 2672 peter@eisentraut.org      657                 :              6 :         pg_log_info("updating control file");
                                658                 :              6 :         update_controlfile(DataDir, ControlFile, do_sync);
                                659                 :                : 
 2681 michael@paquier.xyz       660         [ -  + ]:              6 :         if (verbose)
 2062 bruce@momjian.us          661                 :UBC           0 :             printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
 2681 michael@paquier.xyz       662         [ +  + ]:CBC           6 :         if (mode == PG_MODE_ENABLE)
                                663                 :              4 :             printf(_("Checksums enabled in cluster\n"));
                                664                 :                :         else
                                665                 :              2 :             printf(_("Checksums disabled in cluster\n"));
                                666                 :                :     }
                                667                 :                : 
 3033 magnus@hagander.net       668                 :             13 :     return 0;
                                669                 :                : }
        

Generated by: LCOV version 2.0-1