5

I have a project for school which implies making a c program that works like tar in unix system. I have some questions that I would like someone to explain to me:

  1. The dimension of the archive. I understood (from browsing the internet) that an archive has a define number of blocks 512 bytes each. So the header has 512 bytes, then it's followed by the content of the file (if it's only one file to archive) organized in blocks of 512 bytes then 2 more blocks of 512 bytes.

    For example: Let's say that I have a txt file of 0 bytes to archive. This should mean a number of 512*3 bytes to use. Why when I'm doing with the tar function in unix and click properties it has 10.240 bytes? I think it adds some 0 (NULL) bytes, but I don't know where and why and how many...

  2. The header chcksum. As I know this should be the size of the archive. When I check it with hexdump -C it appears like a number near the real size (when clicking properties) of the archive. For example 11200 or 11205 or something similar if I archive a 0 byte txt file. Is this size in octal or decimal? My bets are that is in octal because all information you put in the header it needs to be in octal. My second question at this point is what is added more from the original size of 10240 bytes?

  3. Header Mode. Let's say that I have a file with 664, the format file will be 0, then I should put in header 0664. Why, on a authentic archive is printed 3 more 0 at the start (000064) ?

3
  • check out en.wikipedia.org/wiki/Tar_(computing) ?
    – SSpoke
    Jan 12, 2014 at 18:22
  • I've checked it but I don't understood what "The checksum is calculated by taking the sum of the unsigned byte values of the header record with the eight checksum bytes taken to be ascii spaces (decimal value 32)" means ... Jan 12, 2014 at 18:25
  • No idea why they would be decimal value 32 which is ascii for space, seems they are byte 0 if checksum is short and could be up to 8 bytes long. Try reading this gnu.org/software/tar/manual/html_node/Standard.html
    – SSpoke
    Jan 12, 2014 at 18:29

1 Answer 1

8

There have been various versions of the tar format, and not all of the extensions to previous formats were always compatible with each other. So there's always a bit of guessing involved. For example, in very old unix systems, file names were not allowed to have more than 14 bytes, so the space for the file name (including path) was plenty; later, with longer file names, it had to be extended but there wasn't space, so the file name got split in 2 parts; even later, gnu tar introduced the @@LongLink pseudo-symbolic links that would make older tars at least restore the file to its original name.

1) Tar was originally a *T*ape *Ar*chiver. To achieve constant througput to tapes and avoid starting/stopping the tape too much, several blocks needed to be written at once. 20 Blocks of 512 bytes were the default, and the -b option is there to set the number of blocks. Very often, this size was pre-defined by the hardware and using wrong blocking factors made the resulting tape unusable. This is why tar appends \0-filled blocks until the tar size is a multiple of the block size.

2) The file size is in octal, and contains the true size of the original file that was put into the tar. It has nothing to do with the size of the tar file. The checksum is calculated from the sum of the header bytes, but then stored in the header as well. So the act of storing the checksum would change the header, thus invalidate the checksum. That's why you store all other header fields first, set the checksum to spaces, then calculate the checksum, then replace the spaces with your calculated value.

Note that the header of a tarred file is pure ascii. This way, In those old days, when a tar file (whose components were plain ascii) got corrupted, an admin could just open the tar file with an editor and restore the components manually. That's why the designers of the tar format were afraid of \0 bytes and used spaces instead.

3) Tar files can store block devices, character devices, directories and such stuff. Unix stores these file modes in the same place as the permission flags, and the header file mode contains the whole file mode, including file type bits. That's why the number is longer than the pure permission.

There's a lot of information at http://en.wikipedia.org/wiki/Tar_%28computing%29 as well.

4
  • 1) Ok I understand, but where are those blocks added(to the final of the 2 blocks of 512 at the end of the tar)? Jan 12, 2014 at 18:54
  • 2)The maximum syze of the header can be 512 bytes(although all the elements in my array cand't make 512 bytes toghether if they are full), right? Why it shows me a value like 11205 ?From where it's comes from I don't understand Jan 12, 2014 at 18:57
  • 1) Yes, they get appended to the end to get the correct number of blocks in the tar file (multiple of 20 / -b option). Jan 12, 2014 at 18:57
  • 2) The header is always exactly 512 byte, independent of the length of the file name. For the checksum, sum up the ascii values of all bytes in your header ('\0' => 0, '\r' => 13, '0' => 48, 'A' => 65 etc.), convert the result to octal, and you get your 11205. Remember its a checksum over the header, not the file. Jan 12, 2014 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.