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.
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
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.)
22 print_tar_number(char *s
, int len
, uint64 val
)
24 if (val
< (((uint64
) 1) << ((len
- 1) * 3)))
26 /* Use octal with trailing space */
30 s
[--len
] = (val
& 7) + '0';
36 /* Use base-256 with leading \200 */
40 s
[--len
] = (val
& 255);
48 * Read a numeric field in a tar header. The field starts at *s and is of
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.)
58 read_tar_number(const char *s
, int len
)
68 result
|= (unsigned char) (*++s
);
74 while (len
-- && *s
>= '0' && *s
<= '7')
86 * Calculate the tar checksum for a header. The header is assumed to always
87 * be 512 bytes, per the tar standard.
90 tarChecksum(char *header
)
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.
100 sum
= 8 * ' '; /* presumed value for checksum field */
101 for (i
= 0; i
< 512; i
++)
102 if (i
< 148 || i
>= 156)
103 sum
+= 0xFF & header
[i
];
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
114 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
)
117 if (strlen(filename
) > 99)
118 return TAR_NAME_TOO_LONG
;
120 if (linktarget
&& strlen(linktarget
) > 99)
121 return TAR_SYMLINK_TOO_LONG
;
123 memset(h
, 0, TAR_BLOCK_SIZE
);
126 strlcpy(&h
[TAR_OFFSET_NAME
], filename
, 100);
127 if (linktarget
!= NULL
|| S_ISDIR(mode
))
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.
134 int flen
= strlen(filename
);
136 flen
= Min(flen
, 99);
141 /* Mode 8 - this doesn't include the file type bits (S_IFMT) */
142 print_tar_number(&h
[TAR_OFFSET_MODE
], 8, (mode
& 07777));
145 print_tar_number(&h
[TAR_OFFSET_UID
], 8, uid
);
148 print_tar_number(&h
[TAR_OFFSET_GID
], 8, gid
);
151 if (linktarget
!= NULL
|| S_ISDIR(mode
))
152 /* Symbolic link or directory has size zero */
153 print_tar_number(&h
[TAR_OFFSET_SIZE
], 12, 0);
155 print_tar_number(&h
[TAR_OFFSET_SIZE
], 12, size
);
158 print_tar_number(&h
[TAR_OFFSET_MTIME
], 12, mtime
);
160 /* Checksum 8 cannot be calculated until we've filled all other fields */
162 if (linktarget
!= NULL
)
164 /* Type - Symbolic link */
165 h
[TAR_OFFSET_TYPEFLAG
] = TAR_FILETYPE_SYMLINK
;
167 strlcpy(&h
[TAR_OFFSET_LINKNAME
], linktarget
, 100);
169 else if (S_ISDIR(mode
))
171 /* Type - directory */
172 h
[TAR_OFFSET_TYPEFLAG
] = TAR_FILETYPE_DIRECTORY
;
176 /* Type - regular file */
177 h
[TAR_OFFSET_TYPEFLAG
] = TAR_FILETYPE_PLAIN
;
181 strcpy(&h
[TAR_OFFSET_MAGIC
], "ustar");
184 memcpy(&h
[TAR_OFFSET_VERSION
], "00", 2);
187 /* XXX: Do we need to care about setting correct username? */
188 strlcpy(&h
[TAR_OFFSET_UNAME
], "postgres", 32);
191 /* XXX: Do we need to care about setting correct group name? */
192 strlcpy(&h
[TAR_OFFSET_GNAME
], "postgres", 32);
195 print_tar_number(&h
[TAR_OFFSET_DEVMAJOR
], 8, 0);
198 print_tar_number(&h
[TAR_OFFSET_DEVMINOR
], 8, 0);
200 /* Prefix 155 - not used, leave as nulls */
202 /* Finally, compute and insert the checksum */
203 print_tar_number(&h
[TAR_OFFSET_CHECKSUM
], 8, tarChecksum(h
));