6 * Utility routine to print possibly larger than 32 bit integers in a
7 * portable fashion. Filled with zeros.
10 print_val(char *s, uint64 val, unsigned int base, size_t len)
14 for (i = len; i > 0; i--)
16 int digit = val % base;
18 s[i - 1] = '0' + digit;
25 * Calculate the tar checksum for a header. The header is assumed to always
26 * be 512 bytes, per the tar standard.
29 tarChecksum(char *header)
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.
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];
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
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)
57 * Note: most of the fields in a tar header are not supposed to be
58 * null-terminated. We use sprintf, which will write a null after the
59 * required bytes; that null goes into the first byte of the next field.
60 * This is okay as long as we fill the fields in order.
62 memset(h, 0, 512); /* assume tar header size */
65 sprintf(&h[0], "%.99s", filename);
66 if (linktarget != NULL || S_ISDIR(mode))
69 * We only support symbolic links to directories, and this is
70 * indicated in the tar format by adding a slash at the end of the
71 * name, the same as for regular directories.
73 int flen = strlen(filename);
80 /* Mode 8 - this doesn't include the file type bits (S_IFMT) */
81 sprintf(&h[100], "%07o ", (int) (mode & 07777));
84 sprintf(&h[108], "%07o ", (int) uid);
87 sprintf(&h[116], "%07o ", (int) gid);
89 /* File size 12 - 11 digits, 1 space; use print_val for 64 bit support */
90 if (linktarget != NULL || S_ISDIR(mode))
91 /* Symbolic link or directory has size zero */
92 print_val(&h[124], 0, 8, 11);
94 print_val(&h[124], size, 8, 11);
95 sprintf(&h[135], " ");
98 sprintf(&h[136], "%011o ", (int) mtime);
100 /* Checksum 8 cannot be calculated until we've filled all other fields */
102 if (linktarget != NULL)
104 /* Type - Symbolic link */
105 sprintf(&h[156], "2");
107 sprintf(&h[157], "%.99s", linktarget);
109 else if (S_ISDIR(mode))
110 /* Type - directory */
111 sprintf(&h[156], "5");
113 /* Type - regular file */
114 sprintf(&h[156], "0");
117 sprintf(&h[257], "ustar");
120 sprintf(&h[263], "00");
123 /* XXX: Do we need to care about setting correct username? */
124 sprintf(&h[265], "%.31s", "postgres");
127 /* XXX: Do we need to care about setting correct group name? */
128 sprintf(&h[297], "%.31s", "postgres");
131 sprintf(&h[329], "%07o ", 0);
134 sprintf(&h[337], "%07o ", 0);
136 /* Prefix 155 - not used, leave as nulls */
139 * We mustn't overwrite the next field while inserting the checksum.
140 * Fortunately, the checksum can't exceed 6 octal digits, so we just write
141 * 6 digits, a space, and a null, which is legal per POSIX.
143 sprintf(&h[148], "%06o ", tarChecksum(h));