LCOV - code coverage report
Current view: top level - src/port - tar.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 81.1 % 74 60
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 68.0 % 50 34

             Branch data     Line data    Source code
       1                 :             : #include "c.h"
       2                 :             : 
       3                 :             : #include <sys/stat.h>
       4                 :             : 
       5                 :             : #include "pgtar.h"
       6                 :             : 
       7                 :             : /*
       8                 :             :  * Print a numeric field in a tar header.  The field starts at *s and is of
       9                 :             :  * length len; val is the value to be written.
      10                 :             :  *
      11                 :             :  * Per POSIX, the way to write a number is in octal with leading zeroes and
      12                 :             :  * one trailing space (or NUL, but we use space) at the end of the specified
      13                 :             :  * field width.
      14                 :             :  *
      15                 :             :  * However, the given value may not fit in the available space in octal form.
      16                 :             :  * If that's true, we use the GNU extension of writing \200 followed by the
      17                 :             :  * number in base-256 form (ie, stored in binary MSB-first).  (Note: here we
      18                 :             :  * support only non-negative numbers, so we don't worry about the GNU rules
      19                 :             :  * for handling negative numbers.)
      20                 :             :  */
      21                 :             : void
      22                 :     1537848 : print_tar_number(char *s, int len, uint64 val)
      23                 :             : {
      24         [ +  - ]:     1537848 :     if (val < (((uint64) 1) << ((len - 1) * 3)))
      25                 :             :     {
      26                 :             :         /* Use octal with trailing space */
      27                 :     1537848 :         s[--len] = ' ';
      28         [ +  + ]:    13840664 :         while (len)
      29                 :             :         {
      30                 :    12302816 :             s[--len] = (val & 7) + '0';
      31                 :    12302816 :             val >>= 3;
      32                 :             :         }
      33                 :             :     }
      34                 :             :     else
      35                 :             :     {
      36                 :             :         /* Use base-256 with leading \200 */
      37                 :           0 :         s[0] = '\200';
      38         [ #  # ]:           0 :         while (len > 1)
      39                 :             :         {
      40                 :           0 :             s[--len] = (val & 255);
      41                 :           0 :             val >>= 8;
      42                 :             :         }
      43                 :             :     }
      44                 :     1537848 : }
      45                 :             : 
      46                 :             : 
      47                 :             : /*
      48                 :             :  * Read a numeric field in a tar header.  The field starts at *s and is of
      49                 :             :  * length len.
      50                 :             :  *
      51                 :             :  * The POSIX-approved format for a number is octal, ending with a space or
      52                 :             :  * NUL.  However, for values that don't fit, we recognize the GNU extension
      53                 :             :  * of \200 followed by the number in base-256 form (ie, stored in binary
      54                 :             :  * MSB-first).  (Note: here we support only non-negative numbers, so we don't
      55                 :             :  * worry about the GNU rules for handling negative numbers.)
      56                 :             :  */
      57                 :             : uint64
      58                 :      972512 : read_tar_number(const char *s, int len)
      59                 :             : {
      60                 :      972512 :     uint64      result = 0;
      61                 :             : 
      62         [ -  + ]:      972512 :     if (*s == '\200')
      63                 :             :     {
      64                 :             :         /* base-256 */
      65         [ #  # ]:           0 :         while (--len)
      66                 :             :         {
      67                 :           0 :             result <<= 8;
      68                 :           0 :             result |= (unsigned char) (*++s);
      69                 :             :         }
      70                 :             :     }
      71                 :             :     else
      72                 :             :     {
      73                 :             :         /* octal */
      74   [ +  -  +  +  :     8550534 :         while (len-- && *s >= '0' && *s <= '7')
                   +  - ]
      75                 :             :         {
      76                 :     7578022 :             result <<= 3;
      77                 :     7578022 :             result |= (*s - '0');
      78                 :     7578022 :             s++;
      79                 :             :         }
      80                 :             :     }
      81                 :      972512 :     return result;
      82                 :             : }
      83                 :             : 
      84                 :             : 
      85                 :             : /*
      86                 :             :  * Calculate the tar checksum for a header. The header is assumed to always
      87                 :             :  * be 512 bytes, per the tar standard.
      88                 :             :  */
      89                 :             : int
      90                 :      386775 : tarChecksum(const char *header)
      91                 :             : {
      92                 :             :     int         i,
      93                 :             :                 sum;
      94                 :             : 
      95                 :             :     /*
      96                 :             :      * Per POSIX, the checksum is the simple sum of all bytes in the header,
      97                 :             :      * treating the bytes as unsigned, and treating the checksum field (at
      98                 :             :      * offset TAR_OFFSET_CHECKSUM) as though it contained 8 spaces.
      99                 :             :      */
     100                 :      386775 :     sum = 8 * ' ';              /* presumed value for checksum field */
     101         [ +  + ]:   198415575 :     for (i = 0; i < TAR_BLOCK_SIZE; i++)
     102   [ +  +  +  + ]:   198028800 :         if (i < TAR_OFFSET_CHECKSUM || i >= TAR_OFFSET_CHECKSUM + 8)
     103                 :   194934600 :             sum += 0xFF & header[i];
     104                 :      386775 :     return sum;
     105                 :             : }
     106                 :             : 
     107                 :             : /*
     108                 :             :  * Check validity of a tar header (assumed to be 512 bytes long).
     109                 :             :  * We verify the checksum and the magic number / version.
     110                 :             :  */
     111                 :             : bool
     112                 :      194484 : isValidTarHeader(const char *header)
     113                 :             : {
     114                 :             :     int         sum;
     115                 :      194484 :     int         chk = tarChecksum(header);
     116                 :             : 
     117                 :      194484 :     sum = read_tar_number(&header[TAR_OFFSET_CHECKSUM], 8);
     118                 :             : 
     119         [ -  + ]:      194484 :     if (sum != chk)
     120                 :           0 :         return false;
     121                 :             : 
     122                 :             :     /* POSIX tar format */
     123         [ +  - ]:      194484 :     if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar\0", 6) == 0 &&
     124         [ +  - ]:      194484 :         memcmp(&header[TAR_OFFSET_VERSION], "00", 2) == 0)
     125                 :      194484 :         return true;
     126                 :             :     /* GNU tar format */
     127         [ #  # ]:           0 :     if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar  \0", 8) == 0)
     128                 :           0 :         return true;
     129                 :             :     /* not-quite-POSIX format written by pre-9.3 pg_dump */
     130         [ #  # ]:           0 :     if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar00\0", 8) == 0)
     131                 :           0 :         return true;
     132                 :             : 
     133                 :           0 :     return false;
     134                 :             : }
     135                 :             : 
     136                 :             : 
     137                 :             : /*
     138                 :             :  * Fill in the buffer pointed to by h with a tar format header. This buffer
     139                 :             :  * must always have space for 512 characters, which is a requirement of
     140                 :             :  * the tar format.
     141                 :             :  */
     142                 :             : enum tarError
     143                 :      192228 : tarCreateHeader(char *h, const char *filename, const char *linktarget,
     144                 :             :                 pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime)
     145                 :             : {
     146         [ +  + ]:      192228 :     if (strlen(filename) > 99)
     147                 :           1 :         return TAR_NAME_TOO_LONG;
     148                 :             : 
     149   [ +  +  -  + ]:      192227 :     if (linktarget && strlen(linktarget) > 99)
     150                 :           0 :         return TAR_SYMLINK_TOO_LONG;
     151                 :             : 
     152                 :      192227 :     memset(h, 0, TAR_BLOCK_SIZE);
     153                 :             : 
     154                 :             :     /* Name 100 */
     155                 :      192227 :     strlcpy(&h[TAR_OFFSET_NAME], filename, 100);
     156   [ +  +  +  + ]:      192227 :     if (linktarget != NULL || S_ISDIR(mode))
     157                 :             :     {
     158                 :             :         /*
     159                 :             :          * We only support symbolic links to directories, and this is
     160                 :             :          * indicated in the tar format by adding a slash at the end of the
     161                 :             :          * name, the same as for regular directories.
     162                 :             :          */
     163                 :        4754 :         int         flen = strlen(filename);
     164                 :             : 
     165                 :        4754 :         flen = Min(flen, 99);
     166                 :        4754 :         h[flen] = '/';
     167                 :        4754 :         h[flen + 1] = '\0';
     168                 :             :     }
     169                 :             : 
     170                 :             :     /* Mode 8 - this doesn't include the file type bits (S_IFMT)  */
     171                 :      192227 :     print_tar_number(&h[TAR_OFFSET_MODE], 8, (mode & 07777));
     172                 :             : 
     173                 :             :     /* User ID 8 */
     174                 :      192227 :     print_tar_number(&h[TAR_OFFSET_UID], 8, uid);
     175                 :             : 
     176                 :             :     /* Group 8 */
     177                 :      192227 :     print_tar_number(&h[TAR_OFFSET_GID], 8, gid);
     178                 :             : 
     179                 :             :     /* File size 12 */
     180   [ +  +  +  + ]:      192227 :     if (linktarget != NULL || S_ISDIR(mode))
     181                 :             :         /* Symbolic link or directory has size zero */
     182                 :        4754 :         print_tar_number(&h[TAR_OFFSET_SIZE], 12, 0);
     183                 :             :     else
     184                 :      187473 :         print_tar_number(&h[TAR_OFFSET_SIZE], 12, size);
     185                 :             : 
     186                 :             :     /* Mod Time 12 */
     187                 :      192227 :     print_tar_number(&h[TAR_OFFSET_MTIME], 12, mtime);
     188                 :             : 
     189                 :             :     /* Checksum 8 cannot be calculated until we've filled all other fields */
     190                 :             : 
     191         [ +  + ]:      192227 :     if (linktarget != NULL)
     192                 :             :     {
     193                 :             :         /* Type - Symbolic link */
     194                 :          16 :         h[TAR_OFFSET_TYPEFLAG] = TAR_FILETYPE_SYMLINK;
     195                 :             :         /* Link Name 100 */
     196                 :          16 :         strlcpy(&h[TAR_OFFSET_LINKNAME], linktarget, 100);
     197                 :             :     }
     198         [ +  + ]:      192211 :     else if (S_ISDIR(mode))
     199                 :             :     {
     200                 :             :         /* Type - directory */
     201                 :        4738 :         h[TAR_OFFSET_TYPEFLAG] = TAR_FILETYPE_DIRECTORY;
     202                 :             :     }
     203                 :             :     else
     204                 :             :     {
     205                 :             :         /* Type - regular file */
     206                 :      187473 :         h[TAR_OFFSET_TYPEFLAG] = TAR_FILETYPE_PLAIN;
     207                 :             :     }
     208                 :             : 
     209                 :             :     /* Magic 6 */
     210                 :      192227 :     strcpy(&h[TAR_OFFSET_MAGIC], "ustar");
     211                 :             : 
     212                 :             :     /* Version 2 */
     213                 :      192227 :     memcpy(&h[TAR_OFFSET_VERSION], "00", 2);
     214                 :             : 
     215                 :             :     /* User 32 */
     216                 :             :     /* XXX: Do we need to care about setting correct username? */
     217                 :      192227 :     strlcpy(&h[TAR_OFFSET_UNAME], "postgres", 32);
     218                 :             : 
     219                 :             :     /* Group 32 */
     220                 :             :     /* XXX: Do we need to care about setting correct group name? */
     221                 :      192227 :     strlcpy(&h[TAR_OFFSET_GNAME], "postgres", 32);
     222                 :             : 
     223                 :             :     /* Major Dev 8 */
     224                 :      192227 :     print_tar_number(&h[TAR_OFFSET_DEVMAJOR], 8, 0);
     225                 :             : 
     226                 :             :     /* Minor Dev 8 */
     227                 :      192227 :     print_tar_number(&h[TAR_OFFSET_DEVMINOR], 8, 0);
     228                 :             : 
     229                 :             :     /* Prefix 155 - not used, leave as nulls */
     230                 :             : 
     231                 :             :     /* Finally, compute and insert the checksum */
     232                 :      192227 :     print_tar_number(&h[TAR_OFFSET_CHECKSUM], 8, tarChecksum(h));
     233                 :             : 
     234                 :      192227 :     return TAR_OK;
     235                 :             : }
        

Generated by: LCOV version 2.0-1