COMP1521 18s1 COMP1521 18s1 Final Exam Comp Sys Fundamentals
[Instructions] [Documentation]
[Q1] [Q2] [Q3] [Q4] [Q5] [Q6] [Q7] [Q8] [Q9] [Q10]

Question 2 (8 marks)

When paper documents need to be destroyed, we would use a shredder to slice up the paper so that the text is no longer able to be reconstructed. With digital documents, simply removing them (e.g. via Unix's rm command), leaves the contents of the document on the disk where it could be recovered by scanning the raw disk (i.e. bypassing the file system). To render digital documents less likely to be available for discovery, it would be better to write random bytes over the top of the file containing the data.

In this question, you must write a function which takes an open file descriptor, and writes 64-byte blocks of "random" bytes for at least all of the bytes in the input file. Ideally, we would write these blocks over the top of the original file. However, we don't want to destroy each input file the first time you test your program, so we write the random blocks onto an output file with the suffix .out.

In the q2 directory are four files and a testing directory:

Makefile

Organises the compilation of the shred program

shred.c

A C main program that ....

  • accepts a file name given in argv[1]
  • opens the file called argv[1] for reading
  • opens a file called argv[1].out for writing
  • calls the shred() function
  • closes the two file descriptors

q2.c

Contains the shred(infd,outfd) function.

file

A small text file that you can use for testing.

Exercise

In this question, you must implement the shred() function, which takes two parameters:

and does the following:

for the whole infd file {
   read a 64-byte block into a buffer
   // may have < 64 bytes in buffer at end of file
   overwrite the buffer with random characters from 'A'..'Z'
   write the buffer to outfd, then write a newline ('\n') to outfd
}

Notes:

The following diagram gives an idea of what the shred() function does:

You can execute the program on the sample data file using e.g.

$ make
gcc -std=c99 -Wall -Werror   -c -o shred.o shred.c
gcc -std=c99 -Wall -Werror   -c -o q2.o q2.c
gcc   shred.o q2.o   -o shred
$ ./shred file

Warning! Do not run the shred command without calling it ./shred. There is another shred command on the system that will destroy file if you accidentally call it. The check tests all invoke the shred program appropriately, so that you can safely run these tests multiple times.

Once your program is working correctly, the above example would produce a file called file.out whose size is 130 bytes (two 64-byte buffers and two newlines).

$ wc -c file.out
130 file.out
$ cat file.out
HFUCRNRPSPZHKZFRIAOQLYBEUGXZTIBCQXEHNXZFOYMBXTSGUIYFGZLCIIDDRGFH
EJQTIPYXQMYNHSVBBUIJVUMFCPIVWNECYVVHMVGCJEQQYNUZJCLHYXMBOVYMKDQL

You can test your q2 program with all of the data files in the tests directory using the command:

$ check q2

Once you are satisfied with your program, submit it using the command:

$ submit q2

This will make a copy of the q2.c file as your answer for this question. You can run the submit command as many times as you like. Test your program thoroughly, possibly using test cases additional to those supplied. Your program will be tested using inputs which may be different to the examples in the tests directory.

If, at some stage, you need to "re-install" the files (although you should not need to), you can copy all of the original files into the q2 directory by running the command:

$ re-start q2

Beware: this will overwrite all of your exsting files for this question, so only do it if you seriously mess things up.