]> granicus.if.org Git - postgresql/blob - src/port/tar.c
Don't dump core when destroying an unused ParallelContext.
[postgresql] / src / port / tar.c
1 #include "c.h"
2 #include "pgtar.h"
3 #include <sys/stat.h>
4
5 /*
6  * Utility routine to print possibly larger than 32 bit integers in a
7  * portable fashion.  Filled with zeros.
8  */
9 static void
10 print_val(char *s, uint64 val, unsigned int base, size_t len)
11 {
12         int                     i;
13
14         for (i = len; i > 0; i--)
15         {
16                 int                     digit = val % base;
17
18                 s[i - 1] = '0' + digit;
19                 val = val / base;
20         }
21 }
22
23
24 /*
25  * Calculate the tar checksum for a header. The header is assumed to always
26  * be 512 bytes, per the tar standard.
27  */
28 int
29 tarChecksum(char *header)
30 {
31         int                     i,
32                                 sum;
33
34         /*
35          * Per POSIX, the checksum is the simple sum of all bytes in the header,
36          * treating the bytes as unsigned, and treating the checksum field (at
37          * offset 148) as though it contained 8 spaces.
38          */
39         sum = 8 * ' ';                          /* presumed value for checksum field */
40         for (i = 0; i < 512; i++)
41                 if (i < 148 || i >= 156)
42                         sum += 0xFF & header[i];
43         return sum;
44 }
45
46
47 /*
48  * Fill in the buffer pointed to by h with a tar format header. This buffer
49  * must always have space for 512 characters, which is a requirement by
50  * the tar format.
51  */
52 enum tarError
53 tarCreateHeader(char *h, const char *filename, const char *linktarget,
54                                 size_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime)
55 {
56         if (strlen(filename) > 99)
57                 return TAR_NAME_TOO_LONG;
58
59         if (linktarget && strlen(linktarget) > 99)
60                 return TAR_SYMLINK_TOO_LONG;
61
62         /*
63          * Note: most of the fields in a tar header are not supposed to be
64          * null-terminated.  We use sprintf, which will write a null after the
65          * required bytes; that null goes into the first byte of the next field.
66          * This is okay as long as we fill the fields in order.
67          */
68         memset(h, 0, 512);                      /* assume tar header size */
69
70         /* Name 100 */
71         strlcpy(&h[0], filename, 100);
72         if (linktarget != NULL || S_ISDIR(mode))
73         {
74                 /*
75                  * We only support symbolic links to directories, and this is
76                  * indicated in the tar format by adding a slash at the end of the
77                  * name, the same as for regular directories.
78                  */
79                 int                     flen = strlen(filename);
80
81                 flen = Min(flen, 99);
82                 h[flen] = '/';
83                 h[flen + 1] = '\0';
84         }
85
86         /* Mode 8 - this doesn't include the file type bits (S_IFMT)  */
87         sprintf(&h[100], "%07o ", (int) (mode & 07777));
88
89         /* User ID 8 */
90         sprintf(&h[108], "%07o ", (int) uid);
91
92         /* Group 8 */
93         sprintf(&h[116], "%07o ", (int) gid);
94
95         /* File size 12 - 11 digits, 1 space; use print_val for 64 bit support */
96         if (linktarget != NULL || S_ISDIR(mode))
97                 /* Symbolic link or directory has size zero */
98                 print_val(&h[124], 0, 8, 11);
99         else
100                 print_val(&h[124], size, 8, 11);
101         sprintf(&h[135], " ");
102
103         /* Mod Time 12 */
104         sprintf(&h[136], "%011o ", (int) mtime);
105
106         /* Checksum 8 cannot be calculated until we've filled all other fields */
107
108         if (linktarget != NULL)
109         {
110                 /* Type - Symbolic link */
111                 sprintf(&h[156], "2");
112                 /* Link Name 100 */
113                 strlcpy(&h[157], linktarget, 100);
114         }
115         else if (S_ISDIR(mode))
116                 /* Type - directory */
117                 sprintf(&h[156], "5");
118         else
119                 /* Type - regular file */
120                 sprintf(&h[156], "0");
121
122         /* Magic 6 */
123         sprintf(&h[257], "ustar");
124
125         /* Version 2 */
126         sprintf(&h[263], "00");
127
128         /* User 32 */
129         /* XXX: Do we need to care about setting correct username? */
130         strlcpy(&h[265], "postgres", 32);
131
132         /* Group 32 */
133         /* XXX: Do we need to care about setting correct group name? */
134         strlcpy(&h[297], "postgres", 32);
135
136         /* Major Dev 8 */
137         sprintf(&h[329], "%07o ", 0);
138
139         /* Minor Dev 8 */
140         sprintf(&h[337], "%07o ", 0);
141
142         /* Prefix 155 - not used, leave as nulls */
143
144         /*
145          * We mustn't overwrite the next field while inserting the checksum.
146          * Fortunately, the checksum can't exceed 6 octal digits, so we just write
147          * 6 digits, a space, and a null, which is legal per POSIX.
148          */
149         sprintf(&h[148], "%06o ", tarChecksum(h));
150
151         return TAR_OK;
152 }