OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 5:06 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Mini-tutorial on using tar as the format for a ramdisk
PostPosted: Sun Mar 20, 2011 9:36 pm 
Offline
Member
Member
User avatar

Joined: Thu Jul 26, 2007 1:53 am
Posts: 395
Hi!

Many of you have problably used or at least glanced at JamesM's nice tutorial on how to create an operating system.

In his tutorial he used his own format for merging files together. It is very easy to understand and works well but at the same time it lacks a few features where subdirectories might be the most noticable. A good replacement could therefor be to use TAR that is a simular concept that most people are used to and probably exist on almost every Linux/BSD distribution out there. It also saves having to compile a tool to create your ramdisk.

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.

Code:
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. 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.

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:

Code:
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:

Code:
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. Stop when you hit a header that is zero. Here is a simple example that does this and returns the number of files found:

Code:
unsigned int parse(unsigned int address)
{

    unsigned int i;

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

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

        if (header->name[0] == '\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.

This pretty much completes the tutorial. It is very easy to use this header information when creating nodes in your virtual filesystem but because the implementation can differ in your os compared to mine you have to figure it out for yourself. Remember that the filename contains the whole path so you might need to chop that off when creating your nodes.

_________________
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/


Last edited by Jezze on Sun Mar 20, 2011 10:59 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: Mini-tutorial on using tar as the format for a ramdisk
PostPosted: Sun Mar 20, 2011 10:08 pm 
Offline
Member
Member
User avatar

Joined: Sat Jul 17, 2010 12:45 am
Posts: 487
Looks cool but I've my own implementation.
I'll give it a try though. 8)

_________________
Programming is not about using a language to solve a problem, it's about using logic to find a solution !


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 52 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group