Read the specific number bytes for the file path and file data
How to define and use structures in C? (Module 2 MLO 3)
Specifications
DESCRIPTION
Recursively store files and directories into a single archive file.
You!
NOTES
Detailed instructions
In this assignment, you will write a program that parses files and directory structures to produce archive files that can then be unpacked to reproduce the archived files and directories.
8:testdir/8:testfile13:hello world!
0:
I recommend that you begin by writing the algorithm to archive a single text file. Remember that the "%zu" format specifier can be used to write size_t variables (the value of sizeof) to a stream using printf. Never use a string variable as the format string, such as:
printf(filename) BAD!
At this point, you are ready to encode directories. Add an if-block to your code to differentiate whether the current file is a directory or a regular file (you can ignore the possibility of other types of files). If the file is a directory, write its name to the archive file as normal, change the working directory to point to the directory in question, and then open the directory (which should now be "./") and iterate over its contents. Don't forget, you'll come across "." and ".." in the directory entries, so you'll want to check dirent.d_name for matches to "." or ".." and skip them to avoid infinite recursion. For each child, simply call your encoding function on it recursively. Once all children are exhausted, change back to the previous working directory. Write a zero to the archive to indicate the end of the encoding of that directory, and continue on with the next file(s) to process.
Once you can pack() directories and files recursively, it's time to write the unpacker. First, read the filename length, allocate a buffer to hold the filename, and read it in. If the filename ends in a "/", it is a directory and you should call mkpath to ensure that it exists, and then change the working directory to it. Then, continue on parsing the next files. Whenever you hit a filename length of zero, you know you're done with that directory, and can cd back to where you started from (go up a level in the directory structure). The output of your unpacking should be identical to the file(s) that went into the archive, including any empty directories, etc.
<stat.h>(0p) for overview of file properties.
stat(2) for file size, type, etc.
getcwd(3) for saving the current directory before recursing.
chdir(2) for changing the current directory before and after recursing.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
* @see mkdir(2)
*/
strcpy(tmp, pathname);
for (char *p = tmp; *p != '\0'; ++p)
struct stat st;
if (stat(tmp, &st))
return -1;
}
return -1;
}
return 0;
}
*/
char *
{
pwd = realloc(pwd, sz);
}
/**
*/
void
if (S_ISDIR(st.st_mode))
{
{
fprintf(stderr, "Packing `%s'\n", fn);
fprintf(stderr, "Skipping non-regular file `%s'.\n", fn);
}
* @param fp The archive to unpack
*/
if (fn[strlen(fn)-1] == '/')
{
else
{
}
int
" %s INFILE\n", argv[0], argv[0]);
exit(1);
FILE *fp = fopen(fn, "w");
for (int argind = 1; argind < argc - 1; ++argind)
else
{ /* Unpacking an archive file */
What to turn in
Submit your source code to Canvas as a single archive.c source file. The name of the file is not important, but only submit one single document that can be compiled as shown above. We will rename your program to archive before we run it.
Archive Rubric | ||||
---|---|---|---|---|
Criteria | Ratings | Pts | ||
0 pts | ||||
Source code must compile without errors for FULL credit, with the
following flags: |
|
5 pts | ||
This criterion is linked to a Learning OutcomeProgram does not crash during testing | 15 pts | |||
|
5 pts | |||
5 pts | ||||
|
5 pts | |||
This criterion is linked to a Learning OutcomeCorrectly archives empty file | 5 pts | |||
This criterion is linked to a Learning OutcomeCorrectly archives file with random content |
|
10 pts | ||
This criterion is linked to a Learning OutcomeCorrectly archives multiple files | 5 pts | |||
This criterion is linked to a Learning OutcomeCorrectly archives an empty directory |
|
5 pts | ||
This criterion is linked to a Learning OutcomeCorrectly archives a directory containing files | 5 pts | |||
This criterion is linked to a Learning OutcomeCorrectly archives multiple directories, each containing files |
|
5 pts | ||
This criterion is linked to a Learning OutcomeCorrectly archives multiple nested directory structures | 15 pts | |||
This criterion is linked to a Learning OutcomeIgnores files that are not directories or regular files |
|
5 pts | ||
This criterion is linked to a Learning OutcomeCorrectly unpacks empty file | 5 pts | |||
This criterion is linked to a Learning OutcomeCorrectly unpacks populated file |
|
5 pts | ||
This criterion is linked to a Learning OutcomeCorrectly unpacks empty directory | 5 pts | |||
This criterion is linked to a Learning Outcomecorrectly unpacks directory containing files |
|
10 pts | ||
This criterion is linked to a Learning OutcomeCorrectly unpacks multiple fully nested directory trees |