LCOV - differential code coverage report
Current view: top level - src/port - tar.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage HEAD vs 15 Lines: 86.9 % 61 53 8 53
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 4 4 4
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 86.9 % 61 53 8 53
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 100.0 % 4 4 4

 Age         Owner                  TLA  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
 2696 tgl                        22 CBC      981874 : print_tar_number(char *s, int len, uint64 val)
                                 23                 : {
                                 24          981874 :     if (val < (((uint64) 1) << ((len - 1) * 3)))
                                 25                 :     {
                                 26                 :         /* Use octal with trailing space */
                                 27          981874 :         s[--len] = ' ';
                                 28         8836884 :         while (len)
                                 29                 :         {
                                 30         7855010 :             s[--len] = (val & 7) + '0';
                                 31         7855010 :             val >>= 3;
                                 32                 :         }
                                 33                 :     }
                                 34                 :     else
                                 35                 :     {
                                 36                 :         /* Use base-256 with leading \200 */
 2696 tgl                        37 UBC           0 :         s[0] = '\200';
                                 38               0 :         while (len > 1)
                                 39                 :         {
                                 40               0 :             s[--len] = (val & 255);
                                 41               0 :             val >>= 8;
                                 42                 :         }
                                 43                 :     }
 2696 tgl                        44 CBC      981874 : }
                                 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          395197 : read_tar_number(const char *s, int len)
                                 59                 : {
                                 60          395197 :     uint64      result = 0;
                                 61                 : 
                                 62          395197 :     if (*s == '\200')
                                 63                 :     {
                                 64                 :         /* base-256 */
 2696 tgl                        65 UBC           0 :         while (--len)
                                 66                 :         {
                                 67               0 :             result <<= 8;
                                 68               0 :             result |= (unsigned char) (*++s);
                                 69                 :         }
                                 70                 :     }
                                 71                 :     else
                                 72                 :     {
                                 73                 :         /* octal */
 2696 tgl                        74 CBC     3556836 :         while (len-- && *s >= '0' && *s <= '7')
                                 75                 :         {
                                 76         3161639 :             result <<= 3;
                                 77         3161639 :             result |= (*s - '0');
                                 78         3161639 :             s++;
                                 79                 :         }
                                 80                 :     }
                                 81          395197 :     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
 3750 magnus                     90          122774 : tarChecksum(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 148) as though it contained 8 spaces.
                                 99                 :      */
                                100          122774 :     sum = 8 * ' ';              /* presumed value for checksum field */
                                101        62983062 :     for (i = 0; i < 512; i++)
                                102        62860288 :         if (i < 148 || i >= 156)
                                103        61878096 :             sum += 0xFF & header[i];
                                104          122774 :     return sum;
                                105                 : }
                                106                 : 
                                107                 : 
                                108                 : /*
                                109                 :  * Fill in the buffer pointed to by h with a tar format header. This buffer
                                110                 :  * must always have space for 512 characters, which is a requirement of
                                111                 :  * the tar format.
                                112                 :  */
                                113                 : enum tarError
                                114          122733 : tarCreateHeader(char *h, const char *filename, const char *linktarget,
                                115                 :                 pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime)
                                116                 : {
 2966 peter_e                   117          122733 :     if (strlen(filename) > 99)
                                118               1 :         return TAR_NAME_TOO_LONG;
                                119                 : 
                                120          122732 :     if (linktarget && strlen(linktarget) > 99)
 2966 peter_e                   121 UBC           0 :         return TAR_SYMLINK_TOO_LONG;
                                122                 : 
 3750 magnus                    123 CBC      122732 :     memset(h, 0, 512);          /* assume tar header size */
                                124                 : 
                                125                 :     /* Name 100 */
 2849 noah                      126          122732 :     strlcpy(&h[0], filename, 100);
 3750 magnus                    127          122732 :     if (linktarget != NULL || S_ISDIR(mode))
                                128                 :     {
                                129                 :         /*
                                130                 :          * We only support symbolic links to directories, and this is
                                131                 :          * indicated in the tar format by adding a slash at the end of the
                                132                 :          * name, the same as for regular directories.
                                133                 :          */
                                134            3133 :         int         flen = strlen(filename);
                                135                 : 
                                136            3133 :         flen = Min(flen, 99);
                                137            3133 :         h[flen] = '/';
                                138            3133 :         h[flen + 1] = '\0';
                                139                 :     }
                                140                 : 
                                141                 :     /* Mode 8 - this doesn't include the file type bits (S_IFMT)  */
 2696 tgl                       142          122732 :     print_tar_number(&h[100], 8, (mode & 07777));
                                143                 : 
                                144                 :     /* User ID 8 */
                                145          122732 :     print_tar_number(&h[108], 8, uid);
                                146                 : 
                                147                 :     /* Group 8 */
                                148          122732 :     print_tar_number(&h[116], 8, gid);
                                149                 : 
                                150                 :     /* File size 12 */
 3750 magnus                    151          122732 :     if (linktarget != NULL || S_ISDIR(mode))
                                152                 :         /* Symbolic link or directory has size zero */
 2696 tgl                       153            3133 :         print_tar_number(&h[124], 12, 0);
                                154                 :     else
                                155          119599 :         print_tar_number(&h[124], 12, size);
                                156                 : 
                                157                 :     /* Mod Time 12 */
                                158          122732 :     print_tar_number(&h[136], 12, mtime);
                                159                 : 
                                160                 :     /* Checksum 8 cannot be calculated until we've filled all other fields */
                                161                 : 
 3750 magnus                    162          122732 :     if (linktarget != NULL)
                                163                 :     {
                                164                 :         /* Type - Symbolic link */
 2696 tgl                       165              13 :         h[156] = '2';
                                166                 :         /* Link Name 100 */
 2849 noah                      167              13 :         strlcpy(&h[157], linktarget, 100);
                                168                 :     }
 3750 magnus                    169          122719 :     else if (S_ISDIR(mode))
                                170                 :     {
                                171                 :         /* Type - directory */
 2696 tgl                       172            3120 :         h[156] = '5';
                                173                 :     }
                                174                 :     else
                                175                 :     {
                                176                 :         /* Type - regular file */
                                177          119599 :         h[156] = '0';
                                178                 :     }
                                179                 : 
                                180                 :     /* Magic 6 */
                                181          122732 :     strcpy(&h[257], "ustar");
                                182                 : 
                                183                 :     /* Version 2 */
                                184          122732 :     memcpy(&h[263], "00", 2);
                                185                 : 
                                186                 :     /* User 32 */
                                187                 :     /* XXX: Do we need to care about setting correct username? */
 2849 noah                      188          122732 :     strlcpy(&h[265], "postgres", 32);
                                189                 : 
                                190                 :     /* Group 32 */
                                191                 :     /* XXX: Do we need to care about setting correct group name? */
                                192          122732 :     strlcpy(&h[297], "postgres", 32);
                                193                 : 
                                194                 :     /* Major Dev 8 */
 2696 tgl                       195          122732 :     print_tar_number(&h[329], 8, 0);
                                196                 : 
                                197                 :     /* Minor Dev 8 */
                                198          122732 :     print_tar_number(&h[337], 8, 0);
                                199                 : 
                                200                 :     /* Prefix 155 - not used, leave as nulls */
                                201                 : 
                                202                 :     /* Finally, compute and insert the checksum */
                                203          122732 :     print_tar_number(&h[148], 8, tarChecksum(h));
                                204                 : 
 2966 peter_e                   205          122732 :     return TAR_OK;
                                206                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a