LCOV - differential code coverage report
Current view: top level - src/bin/pg_checksums - pg_checksums.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 77.7 % 260 202 58 202
Current Date: 2025-09-06 07:49:51 +0900 Functions: 83.3 % 6 5 1 5
Baseline: lcov-20250906-005545-baseline Branches: 68.3 % 186 127 59 127
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 85.7 % 7 6 1 6
(360..) days: 77.5 % 253 196 57 196
Function coverage date bins:
(360..) days: 83.3 % 6 5 1 5
Branch coverage date bins:
(360..) days: 68.3 % 186 127 59 127

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

Generated by: LCOV version 2.4-beta