Doing this in streaming mode, such as tar, is probably the easiest implementation. First write the magic number so that you can determine that this is your archive format. Then I suggested using stat (2) (this syntax for the man page man, section 2) to get the size of the archive. Actually, look carefully at the statistics fields available to you, maybe there is some interesting information that you want to keep.
Write the necessary information in the method = mode value, one in each line. For instance:
FileName=file1.txt FileSize=10 FileDir=./blah/blah FilePerms=0700
End the title with two new lines so you know when to start pushing FileSize bytes to disk. You do not need to start the header marker, because you know that the file size is being written out, so you know when to start parsing the header again.
I suggest you use a text format for your header information, because then you do not need to worry about ordering bytes, etc., which you will need to worry about if you write a raw binary structure to disk.
When reading your archive, analyze the title lines one by one and fill out the local structure to store this information. Then write the file to disk and set any file properties that need updating, based on the header information that you extracted.
Hope this helps. Good luck.
source share