20

I'm doing a tar like in C, and I've got a problem. I just want to archive and unarchive files and diretories, so I operate this command: tar -cvf NAME.tar FILE1 [FILE2...]

Now I'm trying to get the header POSIX of this archive :

struct posix_header
{                              /* byte offset */
  char name[100];               /*   0 */
  char mode[8];                 /* 100 */
  char uid[8];                  /* 108 */
  char gid[8];                  /* 116 */
  char size[12];                /* 124 */
  char mtime[12];               /* 136 */
  char chksum[8];               /* 148 */
  char typeflag;                /* 156 */
  char linkname[100];           /* 157 */
  char magic[6];                /* 257 */
  char version[2];              /* 263 */
  char uname[32];               /* 265 */
  char gname[32];               /* 297 */
  char devmajor[8];             /* 329 */
  char devminor[8];             /* 337 */
  char prefix[155];             /* 345 */
                                /* 500 */
};
int main()
{
    struct posix_header header;
    int fd;

    if ((fd = open("./obj.tar", O_RDONLY)) < 2)     //open tarball
        return (1);
    read(fd, &header, 500);                         //fill struct
    printf("%s\n", header.name);
    close(fd);
}

but the output is always as ./PaxHeaders.NUMBER/FILE_NAME. When I try to open the archive in an editor, it shows a PaxHeader before each file.

What is PaxHeaders? Is there a header added to be fully compatible with Pax? If there is, is it possible to remove them?

I'm a bit lost, do I have to parse them or jump after these headers?

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
Liroo Pierre
  • 875
  • 2
  • 9
  • 25

1 Answers1

29

POSIX.1 2008 defines an extension to the ustar format called the pax format. It's new features are introduced using special files with typeflag set to x for an extended pax header (affects the next file) or g for a global pax header (affects all further files). These headers allow archiving programs to store additional attributes in tar files, like access time or extended length (to store file too large for conventional tar).

Space in this answer is too restricted to replicate the full specification, read POSIX for more details.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • Ok thanks a lot, is this header necessary to "untar" a archive file for tar ? And i don't understand if we could remove this header ? – Liroo Pierre Jan 09 '16 at 01:40
  • @LirooPierre The only thing in an extended header that might possibly screw up unpacking is `size` keys which override the length of the next file. POSIX says that you are supposed to unpack an extended header as an ordinary file if you don't support them and that's what I suggest you to do. – fuz Jan 09 '16 at 01:59
  • Can you add a pax header to your answer and append the full specification there? – Andrew Moylan Aug 26 '17 at 04:19
  • @AndrewMoylan The specification is [here](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_03). It's rather simple, so just give it a read. – fuz Aug 26 '17 at 11:32