Tar

From OSDev Wiki
Jump to navigation Jump to search

Note: This page describes a minimalist subset of the standard USTAR archive format. It can hold only plain files and directories, and pathname length is limited to 99 characters. USTAR is backwards-compatible with this format if these limits are respected.

Tar is a very simple file format and is well suited for things like ramdisks that you can tell Grub to load for you together with your kernel in the early boot stage by supplying the keyword module to your menu.lst file. Grub will load your ramdisk to some address in memory and to find this address you will have to look in the multiboot header. Once you have found your ramdisk you will need to parse it in order to extract the files within.

Parsing

Tar works by concatenating all files together in one big file where each file gets a header that is padded up to 512 bytes. The headers are each aligned on a 512 byte boundry and the content follows directly afterwards. This is what a tar header in it's simplest form could look like.

struct tar_header
{
    char filename[100];
    char mode[8];
    char uid[8];
    char gid[8];
    char size[12];
    char mtime[12];
    char chksum[8];
    char typeflag[1];
};

As you can see the header is not 512 bytes big so the rest of the header is padded up with zeros. At the end of the tar file there is a header of 512 bytes filled with only zeros to indicate the end. (There may be more empty headers to pad to the tape block size; ustar format specifies 10KiB blocks.) A header contains a lot of information but for this tutorial we'll only be interested in two of them.

Filename: This actually contains the whole path, not only the file. It ends with the '\0' character. If 100 bytes is not enough, the ustar format supports longer names by a more complicated mechanism not covered here. Caution: paths of 100 bytes or longer may not be terminated with '\0'.

Size: This contains the size of the file. Important to note here is that the size is in base 8 and written using ascii characters so when you try to figure out the size you must keep this in mind. Actually all fields containing numbers uses this not just size. This is a code snippet that calculates this for you:

unsigned int getsize(const char *in)
{

    unsigned int size = 0;
    unsigned int j;
    unsigned int count = 1;

    for (j = 11; j > 0; j--, count *= 8)
        size += ((in[j - 1] - '0') * count);

    return size;

}

To parse a tar file is not very hard. Start by creating an array big enough to hold all the files your ramdisk will have. Each entry should contain a pointer to structs of type struct tar_header like this:

struct tar_header *headers[32];

A better way would of course be to allocate this dynamically by using malloc() but for the rest of the tutorial I assume you've done like I did.

Starting from the address of the first header loop through them all by reading the size of the current header and the next header should be located at the nearest boundry after the current header's address + 512 + the file size, rounded up to the next 512-byte boundary. Stop when you hit a header that is zero. Here is a simple example that does this and returns the number of files found:

unsigned int parse(unsigned int address)
{

    unsigned int i;

    for (i = 0; ; i++)
    {

        struct tar_header *header = (struct tar_header *)address;

        if (header->filename[i] == '\0')
            break;

        unsigned int size = getsize(header->size);

        headers[i] = header;

        address += ((size / 512) + 1) * 512;

        if (size % 512)
            address += 512;

    }

    return i;

}

Now you have all the headers in your array. To get the actual content you only need to get the value of the pointer and add 512.

It is very easy to use this header information when creating nodes in your virtual filesystem. Remember that the filename contains the whole path so you might need to chop that off when creating your nodes.