Question: Problem 1 We are going to develop our own tar and untar tools to copy / extract files into / from an archive. The first
Problem
We are going to develop our own tar and untar tools to copyextract files intofrom an
archive.
The first thing is to figure out the format of the archive. In addition to storing the file content,
the archive file must also store the original filename so that it can be recreated on extraction.
The following is a suggested archive file format:
mytar
filename
filesize
filedatafilename
filesize
filedata
A couple comments on the archive format:
The file begins with the magic word mytar
for identification purposes. This is a
common technique for application to identify if the file is of the right type instead of
relying on file extension and if not, abort the operation.
Files are stored in the archive one after the other, prefixed by the filename and the
file size. The file size is essential for determining where the files data ends and the
next file begins.
There is no endofarchive marker its just the end of the file.
Filename and file size are stored as ASCII text, followed by a newline char
Both text and binary files can be archived.
a Develop mytar.c which will copy source files specified in the commandline
arguments into the archive file specified.
You are highly recommended to test your program in a separate directory so
that any bugs will not destroy your C source files!
To facilitate debugging, try archiving short text files first eg C source files
The resultant archive file can then be inspected in gedit directly.
Example: No argument given, show usage.
$ mytar
Usage: mytar outputfile infileinfile
Example: Archive files. Note the wildcard c will expand by the shell into a list of matching
filenames and be passed to mains argcargv parameters.
$ mytar myfiles.mt c
Creating archive file
Adding file mytar.c to archive
Adding file myuntar.c to archive
Total files and bytes archived successfully.
Example: Archive files. Overwrite handling.
$ mytar myfiles.mt c
Output file myfiles.mt already exists, overwrite? yN: n
Aborted!
b Develop myuntar.c which will extract files from an archive file to an optional output
path.
You are highly recommended to test your program in a separate directory so that any
bugs will not destroy your C source files!
The output path, if not specified, is the current working path. Otherwise, the output
path will be prepended to the original filenames. See below for examples.
If the output file already exists then it will be truncated and overwritten.
Example: No argument given, show usage.
$ myuntar
Usage: myuntar inputfile outputpath
Example: Extract files to an output path out Note the output path must exist or it will
abort.
$ myuntar myfiles.mt nodir
Reading archive file
Failed to open destination file nodirmytarc reason: No such file or
directory
$ mkdir outdir
$ myuntar myfiles.mt outdir
Reading archive file
Extracting file outdirmytarc
Extracting file outdirmyuntarc
Total files and bytes extracted successfully.
c Does your mytar.c work correctly if one of the input files is a directory? Try the
following and see what happens. Add the logic to detect directories and skip them.
Put your solution into mytarc
Page
Example: Extract files to an output path out Note the output path must exist or it will
abort.
$ mytar myfiles.mt
Creating archive file
Adding file mytar to archive
Adding file mytar.c to archive
Adding file myuntar to archive
Adding file myuntar.c to archive
Input file outdir is a directory, skipped..
Total files and bytes archived successfully.
d Another annoyance is when you already have the archive file and you want to
overwrite it But if your input wildcard is then the archive file will be included as
well which is not correct behavior. Add the logic to detect this and put your solution
into mytarc copied from mytarc
Example: Automatically skip the archive file from the list of source files.
$ mytar myfiles.mt
Output file myfiles.mt already exists, overwrite? yN: y
Creating archive file
Input file same as output file, skipped.
Adding file mytar to archive
Adding file mytar.c to archive
Adding file myuntar to archive
Adding file myuntar.c to archive
Input file outdir is a directory, skipped..
Total files and bytes archived successfully.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
