PostgreSQL Source Code git master
Loading...
Searching...
No Matches
tar.c File Reference
#include "c.h"
#include <sys/stat.h>
#include "pgtar.h"
Include dependency graph for tar.c:

Go to the source code of this file.

Functions

void print_tar_number (char *s, int len, uint64 val)
 
uint64 read_tar_number (const char *s, int len)
 
int tarChecksum (const char *header)
 
bool isValidTarHeader (const char *header)
 
enum tarError tarCreateHeader (char *h, const char *filename, const char *linktarget, pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime)
 

Function Documentation

◆ isValidTarHeader()

bool isValidTarHeader ( const char header)

Definition at line 112 of file tar.c.

113{
114 int sum;
115 int chk = tarChecksum(header);
116
117 sum = read_tar_number(&header[TAR_OFFSET_CHECKSUM], 8);
118
119 if (sum != chk)
120 return false;
121
122 /* POSIX tar format */
123 if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar\0", 6) == 0 &&
124 memcmp(&header[TAR_OFFSET_VERSION], "00", 2) == 0)
125 return true;
126 /* GNU tar format */
127 if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar \0", 8) == 0)
128 return true;
129 /* not-quite-POSIX format written by pre-9.3 pg_dump */
130 if (memcmp(&header[TAR_OFFSET_MAGIC], "ustar00\0", 8) == 0)
131 return true;
132
133 return false;
134}
@ TAR_OFFSET_VERSION
Definition pgtar.h:49
@ TAR_OFFSET_CHECKSUM
Definition pgtar.h:45
@ TAR_OFFSET_MAGIC
Definition pgtar.h:48
static int fb(int x)
uint64 read_tar_number(const char *s, int len)
Definition tar.c:58
int tarChecksum(const char *header)
Definition tar.c:90

References fb(), read_tar_number(), TAR_OFFSET_CHECKSUM, TAR_OFFSET_MAGIC, TAR_OFFSET_VERSION, and tarChecksum().

Referenced by _discoverArchiveFormat(), and astreamer_tar_header().

◆ print_tar_number()

void print_tar_number ( char s,
int  len,
uint64  val 
)

Definition at line 22 of file tar.c.

23{
24 if (val < (((uint64) 1) << ((len - 1) * 3)))
25 {
26 /* Use octal with trailing space */
27 s[--len] = ' ';
28 while (len)
29 {
30 s[--len] = (val & 7) + '0';
31 val >>= 3;
32 }
33 }
34 else
35 {
36 /* Use base-256 with leading \200 */
37 s[0] = '\200';
38 while (len > 1)
39 {
40 s[--len] = (val & 255);
41 val >>= 8;
42 }
43 }
44}
uint64_t uint64
Definition c.h:625
long val
Definition informix.c:689
const void size_t len

References len, and val.

Referenced by tar_close(), and tarCreateHeader().

◆ read_tar_number()

uint64 read_tar_number ( const char s,
int  len 
)

Definition at line 58 of file tar.c.

59{
60 uint64 result = 0;
61
62 if (*s == '\200')
63 {
64 /* base-256 */
65 while (--len)
66 {
67 result <<= 8;
68 result |= (unsigned char) (*++s);
69 }
70 }
71 else
72 {
73 /* octal */
74 while (len-- && *s >= '0' && *s <= '7')
75 {
76 result <<= 3;
77 result |= (*s - '0');
78 s++;
79 }
80 }
81 return result;
82}
uint32 result

References fb(), len, and result.

Referenced by _tarGetHeader(), astreamer_tar_header(), and isValidTarHeader().

◆ tarChecksum()

int tarChecksum ( const char header)

Definition at line 90 of file tar.c.

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 sum = 8 * ' '; /* presumed value for checksum field */
101 for (i = 0; i < TAR_BLOCK_SIZE; i++)
103 sum += 0xFF & header[i];
104 return sum;
105}
int i
Definition isn.c:77
#define TAR_BLOCK_SIZE
Definition pgtar.h:17

References fb(), i, TAR_BLOCK_SIZE, and TAR_OFFSET_CHECKSUM.

Referenced by _tarGetHeader(), isValidTarHeader(), tar_close(), and tarCreateHeader().

◆ tarCreateHeader()

enum tarError tarCreateHeader ( char h,
const char filename,
const char linktarget,
pgoff_t  size,
mode_t  mode,
uid_t  uid,
gid_t  gid,
time_t  mtime 
)

Definition at line 143 of file tar.c.

145{
146 if (strlen(filename) > 99)
147 return TAR_NAME_TOO_LONG;
148
149 if (linktarget && strlen(linktarget) > 99)
151
152 memset(h, 0, TAR_BLOCK_SIZE);
153
154 /* Name 100 */
156 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 int flen = strlen(filename);
164
165 flen = Min(flen, 99);
166 h[flen] = '/';
167 h[flen + 1] = '\0';
168 }
169
170 /* Mode 8 - this doesn't include the file type bits (S_IFMT) */
171 print_tar_number(&h[TAR_OFFSET_MODE], 8, (mode & 07777));
172
173 /* User ID 8 */
174 print_tar_number(&h[TAR_OFFSET_UID], 8, uid);
175
176 /* Group 8 */
177 print_tar_number(&h[TAR_OFFSET_GID], 8, gid);
178
179 /* File size 12 */
180 if (linktarget != NULL || S_ISDIR(mode))
181 /* Symbolic link or directory has size zero */
183 else
184 print_tar_number(&h[TAR_OFFSET_SIZE], 12, size);
185
186 /* Mod Time 12 */
188
189 /* Checksum 8 cannot be calculated until we've filled all other fields */
190
191 if (linktarget != NULL)
192 {
193 /* Type - Symbolic link */
195 /* Link Name 100 */
196 strlcpy(&h[TAR_OFFSET_LINKNAME], linktarget, 100);
197 }
198 else if (S_ISDIR(mode))
199 {
200 /* Type - directory */
202 }
203 else
204 {
205 /* Type - regular file */
207 }
208
209 /* Magic 6 */
210 strcpy(&h[TAR_OFFSET_MAGIC], "ustar");
211
212 /* Version 2 */
213 memcpy(&h[TAR_OFFSET_VERSION], "00", 2);
214
215 /* User 32 */
216 /* XXX: Do we need to care about setting correct username? */
217 strlcpy(&h[TAR_OFFSET_UNAME], "postgres", 32);
218
219 /* Group 32 */
220 /* XXX: Do we need to care about setting correct group name? */
221 strlcpy(&h[TAR_OFFSET_GNAME], "postgres", 32);
222
223 /* Major Dev 8 */
225
226 /* Minor Dev 8 */
228
229 /* Prefix 155 - not used, leave as nulls */
230
231 /* Finally, compute and insert the checksum */
233
234 return TAR_OK;
235}
#define Min(x, y)
Definition c.h:1091
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
static PgChecksumMode mode
static char * filename
Definition pg_dumpall.c:133
@ TAR_FILETYPE_SYMLINK
Definition pgtar.h:63
@ TAR_FILETYPE_DIRECTORY
Definition pgtar.h:64
@ TAR_FILETYPE_PLAIN
Definition pgtar.h:61
@ TAR_OFFSET_DEVMINOR
Definition pgtar.h:53
@ TAR_OFFSET_MODE
Definition pgtar.h:40
@ TAR_OFFSET_DEVMAJOR
Definition pgtar.h:52
@ TAR_OFFSET_UID
Definition pgtar.h:41
@ TAR_OFFSET_TYPEFLAG
Definition pgtar.h:46
@ TAR_OFFSET_NAME
Definition pgtar.h:39
@ TAR_OFFSET_SIZE
Definition pgtar.h:43
@ TAR_OFFSET_GNAME
Definition pgtar.h:51
@ TAR_OFFSET_GID
Definition pgtar.h:42
@ TAR_OFFSET_LINKNAME
Definition pgtar.h:47
@ TAR_OFFSET_MTIME
Definition pgtar.h:44
@ TAR_OFFSET_UNAME
Definition pgtar.h:50
@ TAR_SYMLINK_TOO_LONG
Definition pgtar.h:23
@ TAR_OK
Definition pgtar.h:21
@ TAR_NAME_TOO_LONG
Definition pgtar.h:22
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
void print_tar_number(char *s, int len, uint64 val)
Definition tar.c:22
#define S_ISDIR(m)
Definition win32_port.h:315

References fb(), filename, memcpy(), Min, mode, print_tar_number(), S_ISDIR, strlcpy(), TAR_BLOCK_SIZE, TAR_FILETYPE_DIRECTORY, TAR_FILETYPE_PLAIN, TAR_FILETYPE_SYMLINK, TAR_NAME_TOO_LONG, TAR_OFFSET_CHECKSUM, TAR_OFFSET_DEVMAJOR, TAR_OFFSET_DEVMINOR, TAR_OFFSET_GID, TAR_OFFSET_GNAME, TAR_OFFSET_LINKNAME, TAR_OFFSET_MAGIC, TAR_OFFSET_MODE, TAR_OFFSET_MTIME, TAR_OFFSET_NAME, TAR_OFFSET_SIZE, TAR_OFFSET_TYPEFLAG, TAR_OFFSET_UID, TAR_OFFSET_UNAME, TAR_OFFSET_VERSION, TAR_OK, TAR_SYMLINK_TOO_LONG, and tarChecksum().

Referenced by _tarWriteHeader(), _tarWriteHeader(), astreamer_tar_archiver_content(), and tar_open_for_write().