In this assignment you will create a program capable of archiving the contents o
ID: 3808093 • Letter: I
Question
In this assignment you will create a program capable of archiving the contents of a directory into a single file much like tar does. You will write a makefile that compiles your program into an executable named arch, which has usage arch c to create a new archive file action c to expand an existing archive file Specifies the path-name of a directory that contains the file to be archived (-c) or of the archive file (-e) being expanded. Specifies the path-name of the archive file that is to be created (-e) or of the directory to be created to hold the contents of the archive file. Based on the command line arguments does one of the following: Create: Create a new archive file (target) that, for each file in the directory specified by source, contains the data required to restore the file's name and data. The source directory is unchanged. Expand: Create a new directory (target), which will contain all of the files and directories in the achieve file. in their directory structure Recurse the entire sub-tree rooted by (source) on create or expand Proper care (error reporting/program termination) when opening and creating file and directories is required./arch -c. my_archive archiving Done_my_archive as been created ./arch -e./expand_test mu_archive expanding to ./expand_test Done_my_archive as been expanded Each file will be saved in consecutives bytes in the archive file as where pathname: a null terminated string containing the relative path from the source directory to the file size: a 4 byte unsigned integer data: the contents of the file So when expanding alike you will need to repeatedly: 1. from the current location in the file, find the next null character: this is the pathname 2. treat the next 4 bytes as an unsigned int; this is the size 3. finally, now that you know the size read that many bytes into a buffer, then create a file of the given name and write those bytes into the fileExplanation / Answer
using tar to archive
the program tar (in the beginning for tape archive) is useful for archiving and transmitting documents. as an example, you could need to 'tar up' all of your paintings for a course at the acpub and keep it on your personal pc's disk pressure so you don't run into quota troubles. you would possibly also want to submit (e.g., for cps 108 or cps one hundred) a whole directory without delay instead of the man or woman documents within the directory. The tar application is beneficial for those and different tasks and is simple to apply.
you could see more records by means of reading the man web page, kind guy tar The examples underneath aren't meant to be exhaustive.
note: on the acpub machine you in all likelihood want to apply /afs/acpub/projct/cps/bin/tar since it understands the z alternative. you may use gtar rather.
Create, Extract, See Contents
The tar software takes considered one of three funcion command line arguments (there are others I might not talk approximately).
• c --- to create a tar document, writing the record starts at the start.
• t --- table of contents, see the names of all documents or those specified in other command line arguments.
• x --- extract (repair) the contents of the tar record.
(the other options are u for update and r for update, see the man page for info).
precisely one function argument, c, t, x, is used along side different command line arguments shown underneath. once more, those examples aren't intended to be whole, simply useful.
Compression, Verbose, record exact
further to a feature command line argument the arguments below are beneficial. I typically use z and f all the time, and v when developing/extracting.
• f --- specifies the filename (which follows the f) used to tar into or to tar out from; see the examples underneath.
• z --- use zip/gzip to compress the tar document or to examine from a compressed tar file.
• v --- verbose output, show, e.g., at some point of create or extract, the files being stored into or restored from the tar file.
Examples
To tar all .cc and .h documents right into a tar file named foo.tgz use:
tar cvzf foo.tgz *.cc *.h
This creates (c) a compressed (z) tar document named foo.tgz (f) and indicates the files being saved into the tar file (v). The .tgz suffix is a conference for gzipped tar documents, it's useful to use the conference given that you will recognise to use z to restore/extract.
it's often greater beneficial to tar a listing (which tars all documents and subdirectories recursively until you specify otherwise). The exceptional component approximately tarring a directory is that it is untarred as a listing rather than as character documents.
tar cvzf foo.tgz cps100
will tar the directory cps100 (and its files/subdirectories) into a tar file named foo.tgz.
to peer a tar document's table of contents use:
tar tzf foo.tgz
To extract the contents of a tar report use:
tar xvzf foo.tgz
This untars/extracts (x) into the directory from which the command is invoked, and prints the documents being extracted (v).
if you want to untar right into a distinctive directory, alternate into that listing after which use tar. for example, to untar into a directory named newdir:
mkdir newdir
cd newdir
tar xvzf ../foo.tgz
you can extract handiest one (or numerous) documents in case you know the call of the record. as
Many laptop/Mac programs may be capable of repair documents that have been archived using tar. for instance, on Macs, the Stuffit Deluxe program can deal with Unix tar documents. On desktops, the pkunzip application will cope with Unix tar documents. This makes it possible to tar documents up on acpub and then use ftp to deliver them to your non-public device where you can keep the tar files and repair while wanted. Of route you can run Linux too.
The tar Command
The tar (i.e., tape archive) command is used to convert a set of files into an archive.
An archive is a single file that carries any number of person files plus facts to permit them to be restored to their original form through one or extra extraction programs. archives are convenient for storing documents in addition to for for transmitting statistics and distributing applications. moreover, they're very smooth to paintings with, frequently plenty extra so than coping with huge numbers of character documents.
even though tar changed into at the start designed for backups on magnetic tape, it could now be used to create archive files everywhere on a filesystem. documents that have been created with tar are commonly called tarballs.
unlike a few other archiving programs, and steady with the Unix philosophy that each man or woman program need to be designed to do handiest one aspect however do it nicely, tar does no longer perform compression. however, it's far very smooth to compress data created with tar through the use of specialised compression utilities.
tar's simple syntax is
tar choice(s) archive_name file_name(s)
tar has numerous options, lots of which are not frequently used. in contrast to many instructions, tar requires the usage of at the least one alternative, and generally or extra are necessary.
tar documents are created by using the use of each the -c and -f options. the former instructs tar to create an archive and the latter shows that the next argument (i.e., piece of input records in a command) will be the call of the brand new archive report. therefore, for example, the subsequent would create an archive document called record.tar from the three files named file1, file2 and file3 which might be positioned within the modern-day listing (i.e., the listing wherein the person is presently working):
tar -cf record.tar file1 file2 file3
It it no longer genuinely important that the new report have the .tar extension; but, the usage of this extension can be could be very convenient as it allows the kind of file to be visually recognized. it is necessary, however, that the -f choice be the very last choice in a series of contiguous, single-letter options; in any other case, the system will become confused as to the desired call for the brand new record and could use the following option in the sequence as the name.
The -v (i.e., verbose) choice is commonly used collectively with the -c and -f options in order to show a list of the documents which are blanketed within the archive. In such case, the above instance might turn out to be
tar -cvf document.tar file1 file2 file3
tar can also be used to make archives from the contents of 1 or more directories. The result is recursive; this is, it includes all objects (e.g., directories and documents) inside every stage of directories. for example, the contents of directories named dir1 and dir2 could be archived into a file named dir.tar with the following:
tar -cvf dir.tar dir1 dir2
it's miles often convenient to apply tar with a wildcard (i.e., a man or woman that could represent a few particular magnificence of characters or collection of characters). the subsequent instance uses the celebrity wildcard (i.e., an asterisk), which represents any man or woman or collection of characters, to create an archive of each object within the cutting-edge directory:
tar -cf *
through default, tar creates an archive of copies of the unique documents and/or directories, and the originals are retained. however, they can be eliminated when the usage of tar by using including the --take away-documents choice.
tar -cvjf files.tar.bz2 file4 file5 file6
tar can also be used for unpacking tar documents. but, earlier than doing this, there are numerous steps that must be taken. One is to confirm that enough space is available at the difficult disk power (HDD). every other is to move to an empty directory (which typically involves developing one with the correct call) to prevent the reconstituted documents from cluttering up the cutting-edge directory and overwriting any files or directories with same names which are in it. similarly, if the archive has been compressed, it have to first be decompressed the use of the precise decompression program (which could usually be determined via the filename extension).
with a purpose to unpack a tar record, the -x (for extract) and -f options are required. it's also not unusual to add the -v option to provide a walking list of the documents being unpacked. as a result, as an instance, to unpack the archive document.tar created in a previous instance the subsequent might be used:
tar -xvf document.tar
simply as alternatives are available to permit 3 compression applications to automatically compress newly created tar files, the identical alternatives can be used to have the compression applications robotically decompress tar files previous to extraction. for this reason, as an example, the following would decompress and extract the contents of the compressed archive files.tar.bz2 that became created in an above instance:
tar -xjvf files.tar.bz2
files can be brought to an current archive the usage of the -r choice. As is always the case with tar, it's also essential to use the -f alternative to indicate that the following string (i.e., sequence of characters) is the name of the archive. as an example, the following could append a document named file7 to file.tar: