PostgreSQL Source Code  git master
pgtar.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define TAR_BLOCK_SIZE   512
 

Enumerations

enum  tarError { TAR_OK = 0 , TAR_NAME_TOO_LONG , TAR_SYMLINK_TOO_LONG }
 
enum  tarHeaderOffset {
  TAR_OFFSET_NAME = 0 , TAR_OFFSET_MODE = 100 , TAR_OFFSET_UID = 108 , TAR_OFFSET_GID = 116 ,
  TAR_OFFSET_SIZE = 124 , TAR_OFFSET_MTIME = 136 , TAR_OFFSET_CHECKSUM = 148 , TAR_OFFSET_TYPEFLAG = 156 ,
  TAR_OFFSET_LINKNAME = 157 , TAR_OFFSET_MAGIC = 257 , TAR_OFFSET_VERSION = 263 , TAR_OFFSET_UNAME = 265 ,
  TAR_OFFSET_GNAME = 297 , TAR_OFFSET_DEVMAJOR = 329 , TAR_OFFSET_DEVMINOR = 337 , TAR_OFFSET_PREFIX = 345
}
 
enum  tarFileType { TAR_FILETYPE_PLAIN = '0' , TAR_FILETYPE_SYMLINK = '2' , TAR_FILETYPE_DIRECTORY = '5' }
 

Functions

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)
 
uint64 read_tar_number (const char *s, int len)
 
void print_tar_number (char *s, int len, uint64 val)
 
int tarChecksum (char *header)
 
static size_t tarPaddingBytesRequired (size_t len)
 

Macro Definition Documentation

◆ TAR_BLOCK_SIZE

#define TAR_BLOCK_SIZE   512

Definition at line 17 of file pgtar.h.

Enumeration Type Documentation

◆ tarError

enum tarError
Enumerator
TAR_OK 
TAR_NAME_TOO_LONG 
TAR_SYMLINK_TOO_LONG 

Definition at line 19 of file pgtar.h.

20 {
21  TAR_OK = 0,
24 };
@ TAR_SYMLINK_TOO_LONG
Definition: pgtar.h:23
@ TAR_OK
Definition: pgtar.h:21
@ TAR_NAME_TOO_LONG
Definition: pgtar.h:22

◆ tarFileType

Enumerator
TAR_FILETYPE_PLAIN 
TAR_FILETYPE_SYMLINK 
TAR_FILETYPE_DIRECTORY 

Definition at line 58 of file pgtar.h.

59 {
60  TAR_FILETYPE_PLAIN = '0',
63 };
@ TAR_FILETYPE_SYMLINK
Definition: pgtar.h:61
@ TAR_FILETYPE_DIRECTORY
Definition: pgtar.h:62
@ TAR_FILETYPE_PLAIN
Definition: pgtar.h:60

◆ tarHeaderOffset

Enumerator
TAR_OFFSET_NAME 
TAR_OFFSET_MODE 
TAR_OFFSET_UID 
TAR_OFFSET_GID 
TAR_OFFSET_SIZE 
TAR_OFFSET_MTIME 
TAR_OFFSET_CHECKSUM 
TAR_OFFSET_TYPEFLAG 
TAR_OFFSET_LINKNAME 
TAR_OFFSET_MAGIC 
TAR_OFFSET_VERSION 
TAR_OFFSET_UNAME 
TAR_OFFSET_GNAME 
TAR_OFFSET_DEVMAJOR 
TAR_OFFSET_DEVMINOR 
TAR_OFFSET_PREFIX 

Definition at line 37 of file pgtar.h.

38 {
39  TAR_OFFSET_NAME = 0, /* 100 byte string */
40  TAR_OFFSET_MODE = 100, /* 8 byte tar number, excludes S_IFMT */
41  TAR_OFFSET_UID = 108, /* 8 byte tar number */
42  TAR_OFFSET_GID = 116, /* 8 byte tar number */
43  TAR_OFFSET_SIZE = 124, /* 8 byte tar number */
44  TAR_OFFSET_MTIME = 136, /* 12 byte tar number */
45  TAR_OFFSET_CHECKSUM = 148, /* 8 byte tar number */
46  TAR_OFFSET_TYPEFLAG = 156, /* 1 byte file type, see TAR_FILETYPE_* */
47  TAR_OFFSET_LINKNAME = 157, /* 100 byte string */
48  TAR_OFFSET_MAGIC = 257, /* "ustar" with terminating zero byte */
49  TAR_OFFSET_VERSION = 263, /* "00" */
50  TAR_OFFSET_UNAME = 265, /* 32 byte string */
51  TAR_OFFSET_GNAME = 297, /* 32 byte string */
52  TAR_OFFSET_DEVMAJOR = 329, /* 8 byte tar number */
53  TAR_OFFSET_DEVMINOR = 337, /* 8 byte tar number */
54  TAR_OFFSET_PREFIX = 345, /* 155 byte string */
55  /* last 12 bytes of the 512-byte block are unassigned */
56 };
@ TAR_OFFSET_DEVMINOR
Definition: pgtar.h:53
@ TAR_OFFSET_MODE
Definition: pgtar.h:40
@ TAR_OFFSET_PREFIX
Definition: pgtar.h:54
@ TAR_OFFSET_VERSION
Definition: pgtar.h:49
@ 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_CHECKSUM
Definition: pgtar.h:45
@ 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_OFFSET_MAGIC
Definition: pgtar.h:48

Function Documentation

◆ 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 }
long val
Definition: informix.c:670
const void size_t len

References len, and val.

Referenced by tar_close().

◆ 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 }

References len.

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

◆ tarChecksum()

int tarChecksum ( 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 148) as though it contained 8 spaces.
99  */
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];
104  return sum;
105 }
int i
Definition: isn.c:73

References i.

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

◆ 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 90 of file tar.c.

116 {
117  if (strlen(filename) > 99)
118  return TAR_NAME_TOO_LONG;
119 
120  if (linktarget && strlen(linktarget) > 99)
121  return TAR_SYMLINK_TOO_LONG;
122 
123  memset(h, 0, TAR_BLOCK_SIZE);
124 
125  /* Name 100 */
126  strlcpy(&h[TAR_OFFSET_NAME], filename, 100);
127  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  int flen = strlen(filename);
135 
136  flen = Min(flen, 99);
137  h[flen] = '/';
138  h[flen + 1] = '\0';
139  }
140 
141  /* Mode 8 - this doesn't include the file type bits (S_IFMT) */
142  print_tar_number(&h[TAR_OFFSET_MODE], 8, (mode & 07777));
143 
144  /* User ID 8 */
145  print_tar_number(&h[TAR_OFFSET_UID], 8, uid);
146 
147  /* Group 8 */
148  print_tar_number(&h[TAR_OFFSET_GID], 8, gid);
149 
150  /* File size 12 */
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);
154  else
156 
157  /* Mod Time 12 */
158  print_tar_number(&h[TAR_OFFSET_MTIME], 12, mtime);
159 
160  /* Checksum 8 cannot be calculated until we've filled all other fields */
161 
162  if (linktarget != NULL)
163  {
164  /* Type - Symbolic link */
166  /* Link Name 100 */
167  strlcpy(&h[TAR_OFFSET_LINKNAME], linktarget, 100);
168  }
169  else if (S_ISDIR(mode))
170  {
171  /* Type - directory */
173  }
174  else
175  {
176  /* Type - regular file */
178  }
179 
180  /* Magic 6 */
181  strcpy(&h[TAR_OFFSET_MAGIC], "ustar");
182 
183  /* Version 2 */
184  memcpy(&h[TAR_OFFSET_VERSION], "00", 2);
185 
186  /* User 32 */
187  /* XXX: Do we need to care about setting correct username? */
188  strlcpy(&h[TAR_OFFSET_UNAME], "postgres", 32);
189 
190  /* Group 32 */
191  /* XXX: Do we need to care about setting correct group name? */
192  strlcpy(&h[TAR_OFFSET_GNAME], "postgres", 32);
193 
194  /* Major Dev 8 */
196 
197  /* Minor Dev 8 */
199 
200  /* Prefix 155 - not used, leave as nulls */
201 
202  /* Finally, compute and insert the checksum */
204 
205  return TAR_OK;
206 }
#define Min(x, y)
Definition: c.h:991
static PgChecksumMode mode
Definition: pg_checksums.c:56
static char * filename
Definition: pg_dumpall.c:121
#define TAR_BLOCK_SIZE
Definition: pgtar.h:17
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static pg_noinline void Size size
Definition: slab.c:607
int tarChecksum(char *header)
Definition: tar.c:90
void print_tar_number(char *s, int len, uint64 val)
Definition: tar.c:22
#define S_ISDIR(m)
Definition: win32_port.h:325

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

◆ tarPaddingBytesRequired()

static size_t tarPaddingBytesRequired ( size_t  len)
inlinestatic

Definition at line 79 of file pgtar.h.

80 {
81  return TYPEALIGN(TAR_BLOCK_SIZE, len) - len;
82 }
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:791

References len, TAR_BLOCK_SIZE, and TYPEALIGN.

Referenced by _tarAddFile(), _tarPositionTo(), _tarWritePadding(), bbstreamer_tar_archiver_content(), bbstreamer_tar_header(), sendDir(), and tar_close().