Mini-tutorial on using tar as the format for a ramdisk
Posted: Sun Mar 20, 2011 9:36 pm
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.
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:
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:
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:
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.
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: Select all
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];
};
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: Select all
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;
}
Code: Select all
struct tar_header *headers[32];
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: Select all
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;
}
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.