Assignment 2: blobby, Blobby Aims · building a concrete understanding of file system objects · practising C including byte-level operations · understanding file operations, including I/O of binary...

please use C language and compile with dcc


Assignment 2: blobby, Blobby Aims · building a concrete understanding of file system objects · practising C including byte-level operations · understanding file operations, including I/O of binary data and robust error-handling Background A file archive is a single file which can contain the contents, names and other metadata of multiple files. File archives make backups and transport of files more convenient, and make compression more efficient. The some common archive formats are: · tar for linux. (uses external compression tools) · zip for windows. (with inbuilt compression) Wikipedia's list of common file archive formats. Your Task Your task in this assignment is to write a C program blobby.c, a file archiver. The file archives in this assignment are called blobs. Each blob contains one or more blobettes. Each blobette records one file system object. Their format is described below. blobby.c should be able to: · list the contents of a blob (subset 0), · list the permissions of files in a blob (subset 0), · list the size (number of bytes) of files in a blob (subset 0), · check the blobette magic number (subset 0), · extract files from a blob (subset 1), · check blobette integrity (hashes) (subset 1), · set the file permissions of files extracted from a blob (subset 1), · create a blob from a list of files (subset 2), Subset 0 Given the -l command line argument blobby.c should for each file in the specified blob print: 1. The file/directory permissions in octal 2. The file/directory size in bytes 3. The file/directory pathname It should check blob magic number on each blobette, and emit an error if it is incorrect. #List the details of each item in the blob called text_file.blob, which is in #the examples directory $./blobby -l examples/text_file.blob 100644 56 hello.txt #List the details of each item in the blob called 4_files.blob, which is in #the examples directory $./blobby -l examples/4_files.blob 100644 256 256.bin 100644 56 hello.txt 100444 166 last_goodbye.txt 100464 148 these_days.txt #List the details of each item in the blob called hello_world.blob, which is #in the examples directory $./blobby -l examples/hello_world.blob 100644 87 hello.c 100644 82 hello.cpp 100644 65 hello.d 100644 77 hello.go 100644 32 hello.hs 100644 117 hello.java 100644 30 hello.js 100755 47 hello.pl 100755 97 hello.py 100644 45 hello.rs 100644 107 hello.s 100755 41 hello.sh 100644 24 hello.sql Hint: Use fopen to open the blob file. Use fgetc to read bytes. Use C bitwise operations such as < & and | to combine="" bytes="" into="" integers.="" think="" carefully="" about="" the="" functions="" you="" can="" construct="" to="" avoid="" repeated="" code.="" review print_borts_file.c, from="" our week="" 8="" tutorial and print_bytes.c from="" our week="" 8="" lab.="" fseek can="" be="" used="" to="" skip="" over="" parts="" of="" the blob file,="" but="" you="" can="" also="" use="" a="" loop="" and fgetc="" note:="" the="" order="" you="" list="" files="" is="" the="" order="" they="" appear="" in="" the blob.="" blob files="" do="" not="" necessarily="" end="" with .blob.="" this="" has="" been="" done="" with="" the="" provided="" example="" files="" purely="" as="" a="" convenience.="" the="" starting="" code="" contains="" a="" suitable="" printf="" to="" match="" the="" required="" format.="" the="" correct="" format="" string="" for="" this="" output "%06lo="" %5lu="" %s\n" is="" provided="" in="" the="" given blobby.c file.="" subset="" 1="" given="" the -x command="" line="" argument blobby.c should:="" extract="" the="" files="" in="" the="" specified blob.="" it="" should="" set="" file="" permissions="" for="" extracted="" files="" to="" the="" permissions="" specified="" in="" the blob.="" it="" should="" check blob integrity="" by="" checking="" each blobette hash,="" and="" emit="" an="" error="" if="" any="" are="" incorrect.="" #your="" program="" extracts="" the="" files="" into="" the="" current="" working="" directory.="" #so="" we="" will="" run="" your="" program="" from="" a="" temporary="" directory="" (tmp)="" as="" to="" not="" clutter="" #the="" base="" directory.="" #once="" in="" the="" tmp="" directory="" your="" program="" and="" examples/="" will="" be="" in="" the="" parent="" #directory.="" #hence="" the="" use="" of="" ..="" in="" pathnames.="" #make="" a="" directory="" called="" tmp.="" $mkdir="" -p="" tmp/="" #change="" into="" the="" tmp="" directory.="" $cd="" tmp/="" #forcibly="" remove="" all="" files="" inside="" the="" tmp="" directory.="" $rm="" -f="" *="" .*="" #use="" your="" program="" to="" extract="" the="" contents="" of="" text_file.blob.="" $../blobby="" -x="" ../examples/text_file.blob="" extracting:="" hello.txt="" #show="" the="" contents="" of="" hello.txt="" in="" the="" terminal.="" #you="" can="" manually="" open="" it="" in="" your="" text="" editor="" too,="" if="" you="" like.="" $cat="" hello.txt="" hello="" comp1521="" i="" hope="" you="" are="" enjoying="" this="" assignment.="" #forcibly="" remove="" all="" files="" inside="" the="" tmp="" directory.="" $rm="" -f="" *="" .*="" #use="" your="" program="" to="" extract="" the="" contents="" of="" hello_world.blob.="" $../blobby="" -x="" ../examples/hello_world.blob="" extracting:="" hello.c="" extracting:="" hello.cpp="" extracting:="" hello.d="" extracting:="" hello.go="" extracting:="" hello.hs="" extracting:="" hello.java="" extracting:="" hello.js="" extracting:="" hello.pl="" extracting:="" hello.py="" extracting:="" hello.rs="" extracting:="" hello.s="" extracting:="" hello.sh="" extracting:="" hello.sql="" #show="" the="" first="" 25="" lines="" from="" the="" extracted="" files="" to="" confirm="" the="" extraction="" #was="" successful.="" $cat="" $(echo="" *="" |="" sort)="" |="" head="" -n="" 25="" extern="" int="" puts(const="" char="" *s);="" int="" main(void)="" {="" puts("hello,="" world!");="" return="" 0;="" }="" #include=""> int main () { std::cout < "hello,="" world!"="">< std::endl;="" }="" import="" std.stdio;="" void="" main()="" {="" writeln("hello,="" world!");="" }="" package="" main="" import="" "fmt"="" func="" main()="" {="" fmt.println("hello,="" world!")="" }="" main="putStrLn" "hello,="" world!"="" #forcibly="" remove="" all="" files="" inside="" the="" tmp="" directory="" $rm="" -f="" *="" .*="" #use="" your="" program="" to="" extract="" the="" contents="" of="" meta.blob.="" $../blobby="" -x="" ../examples/meta.blob="" extracting:="" 1_file.subdirectory.blob="" extracting:="" 1_file.subdirectory.compressed.blob="" extracting:="" 2_files.blob="" extracting:="" 2_files.compressed.blob="" extracting:="" 3_files.bad_hash.blob="" extracting:="" 3_files.bad_magic.blob="" extracting:="" 3_files.blob="" extracting:="" 3_files.compressed.blob="" extracting:="" 3_files.subdirectory.bad_hash.blob="" extracting:="" 3_files.subdirectory.bad_magic.blob="" extracting:="" 3_files.subdirectory.blob="" extracting:="" 3_files.subdirectory.compressed.blob="" extracting:="" 4_files.blob="" extracting:="" 4_files.compressed.blob="" extracting:="" all_the_modes.subdirectory.blob="" extracting:="" all_the_modes.subdirectory.compressed.blob="" extracting:="" binary_file.blob="" extracting:="" binary_file.compressed.blob="" extracting:="" hello_world.bad_hash.blob="" extracting:="" hello_world.bad_magic.blob="" extracting:="" hello_world.blob="" extracting:="" hello_world.compressed.blob="" extracting:="" lecture_code.subdirectory.blob="" extracting:="" lecture_code.subdirectory.compressed.blob="" extracting:="" text_file.bad_hash.blob="" extracting:="" text_file.bad_magic.blob="" extracting:="" text_file.blob="" extracting:="" text_file.compressed.blob="" #show="" the="" first="" 10="" items="" in="" this="" directory="" alphabetically="" to="" check="" extraction="" #was="" successful.="" $ls="" -1="" $(echo="" *="" |="" sort)="" |="" head="" 1_file.subdirectory.blob="" 1_file.subdirectory.compressed.blob="" 2_files.blob="" 2_files.compressed.blob="" 3_files.bad_hash.blob="" 3_files.bad_magic.blob="" 3_files.blob="" 3_files.compressed.blob="" 3_files.subdirectory.bad_hash.blob="" 3_files.subdirectory.bad_magic.blob="" #go="" back="" into="" the="" directory="" with="" your="" code.="" $cd="" ../="" #remove="" the="" tmp="" directory="" and="" everything="" inside="" it.="" $rm="" -rf="" tmp/="" hint:="" use fopen to="" open="" each="" file="" you="" are="" extracting.="" use fputc to="" write="" bytes="" to="" each="" file..="" in="" our lectures="" on="" files we="" covered="" copying="" bytes="" to="" a="" file="" in="" the cp_fgetc.c example="" and="" setting="" the="" permisisons="" of="" a="" file="" in="" the chmod.c example.="" think="" carefully="" about="" the="" functions="" you="" can="" construct="" to="" avoid="" repeated="" code.="" for="" example,="" for="" every="" byte="" you="" read="" with fgetc you="" need="" to="" call blobby_hash to="" calculate="" a="" new="" hash="" value,="" so="" write="" a="" function="" that="" does="" both.="" hint:="" have="" the="" function="" take="" a="" pointer="" to="" a="" hash="" value="" which="" it="" can="" update.="" note:="" blobby="" should="" overwrite="" an="" files="" that="" already="" exist.="" blobby="" can="" leave="" already="" extracted/partially="" extracted="" files="" in="" the="" event="" of="" an="" error.="" subset="" 2="" given="" the -c command="" line="" argument blobby.c should:="" create="" a blob containing="" the="" specified="" files.="" #these="" "echo"="" lines="" show="" you="" how="" to="" create="" these="" test="" files="" and="" what="" their="" #contents="" are.="" #create="" a="" file="" called="" hello.txt="" with="" the="" contents="" "hello".="" $echo="" hello=""> hello.txt #Create a file called hola.txt with the contents "hola". $echo hola > hola.txt #Create a file called hi.txt with the contents "hi". $echo hi > hi.txt #Set the permissions of these files to 644 (octal permission string #(equivalent to rw-r--r--)). #When you list the contents of the blob, the permissions should match this. $chmod 644 hello.txt hola.txt hi.txt #Create a blob called selamat.blob with the files hello.txt, hola.txt, and #hi.txt. $./blobby -c selamat.blob hello.txt hola.txt hi.txt Adding: hello.txt Adding: hola.txt Adding: hi.txt #List the contents of selamat.blob. $./blobby -l selamat.blob 100644 6 hello.txt 100644 5 hola.txt 100644 3 hi.txt #Make a directory called tmp. $mkdir -p tmp/ #Change into the tmp directory. $cd tmp/ #Forcibly remove all files inside the tmp directory. $rm -f * .* #Use your program to extract the contents of selamat.blob. $../blobby -x ../selamat.blob Extracting: hello.txt Extracting: hola.txt Extracting: hi.txt #Check that the extracted file hello.txt is the same as the source file #../hello.txt. $diff -s ../hello.txt hello.txt Files ../hello.txt and hello.txt are identical #Check that the extracted file hola.txt is the same as the source file #../hola.txt. $diff -s ../hola.txt hola.txt Files ../hola.txt and hola.txt are identical #Check that the extracted file hi.txt is the same as the source file #../hi.txt. $diff -s ../hi.txt hi.txt Files ../hi.txt and hi.txt are identical #Go back into the directory with your code. $cd ../ #Remove the tmp directory and everything inside it. $rm -rf tmp/ Hint: Use fopen and fputc to create the new blob. In our lectures on files we covered obtaining file metadata including its size and mode (permissions) in the stat.c example. Note: You must add/store files in the order they are given. Blob Format blobs must follow exactly the format produced by the reference implementation. A blob consists of 1 or more blobettes. Each blobette contains the information about one file or directory. blobette format Field Length (bytes) Field Name Possible Values Description 1 magic_number 0x42 byte 0 in every blobette must be 0x42 (ASCII 'B') 3 mode file type and permissions as returned in st_mode field from lstat 2 pathname_length 1..65535 number of bytes in file/directory pathname 6 content_length 0..281474976710655 number of bytes in file 0 for directories pathname_length pathname file/directory pathname content_length contents bytes (contents) of file empty for directories 1 hash 0..255 blobby_hash() of all bytes of this blobette except this byte mode, pathname_length and content_length are always stored in big-endian format. It is recommended you construct their values from their individual bytes using bit-operations. Side-note: an interesting property of the blob format is the concatenation of 2 or more blobs is a valid blob. We can use the reference implementation to create simple blobs and inspect their contents. For example, using the print_bytes.c program written as a lab exercise: echo hola > hi.txt chmod 640 hi.txt 1521 blobby -c h.blob hi.txt Adding: hi.txt ./print_bytes h.blob byte 0: 66 0x42 'B' byte 1: 0 0x00 byte 2: 129 0x81 byte 3: 160 0xa0 byte 4: 0 0x00 byte 5: 6 0x06 byte 6: 0 0x00 byte 7: 0 0x00 byte 8: 0 0x00 byte 9: 0 0x00
Nov 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here