/cpe357/assn4/test2/extractpseudo.c
https://bitbucket.org/hlew/college-code · C · 253 lines · 179 code · 47 blank · 27 comment · 27 complexity · c0c8bf486cf27e2cfdbcdc9efe5162ae MD5 · raw file
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <math.h>
- #include "mytar.h"
- #define BLOCK_SIZE 512
- #define END_OF_ARCHIVE 1
- #define TABLE_OF_CONTENTS 1
- #define EXTRACT 2
- /* global to keep track of current block */
- static int curblock;
- static int open_archive(void);
- static void get_next_block(int, char *);
- static int read_header(char *, headerVar *);
- static void fill_headerstruct(char *, headerVar *);
- static int is_header(char *);
- static void print_pathname();
- static void process(const int);
- static int num_blocks_to_advance(headerVar);
- void extract_file(int fd, headerVar headerbuf);
- static void make_dir();
- static void make_link();
- static void make_file();
- int main(int argc, char *argv[]) {
- // headerVar headerbuf;
- // int n;
- // snprintf(headerbuf.size, 12, "%o", (unsigned int) 513);
- process(TABLE_OF_CONTENTS);
- // int num = num_blocks_to_advance(headerbuf);
- // printf("Number of blocks to advance: %i\n", num);
- return 0;
- }
- static void process(const int mode) {
- int fd;
- char blockbuf[BLOCK_SIZE], zerobuf[BLOCK_SIZE];
- headerVar headerbuf;
- memset(zerobuf, 0, BLOCK_SIZE);
- int endOfArchive = 0;
- fd = open_archive();
- while (!endOfArchive)
- {
- get_next_block(fd, blockbuf);
- printf("Done with reading block %i\n", curblock - 1);
- /* End of Archive check */
- if ( memcmp(blockbuf, zerobuf, BLOCK_SIZE) == 0 )
- {
- printf("inside end of archive\n");
- get_next_block(fd, blockbuf);
- printf("after end of archive !!!\n");
- if ( memcmp(blockbuf, zerobuf, BLOCK_SIZE) == 0) {
- printf("Comparison");
- endOfArchive = 1;
- continue;
- }
- else curblock--;
- }
- /* Decompose header into headerVar */
- if (read_header(blockbuf, &headerbuf) == -1) {
- fprintf(stderr, "process: Malformed header found. Bailing.\n");
- exit(1);
- }
- if (mode == TABLE_OF_CONTENTS) {
- print_pathname();
- curblock += num_blocks_to_advance(headerbuf); /* advance the current block */
- /* by the filesize in blocks */
-
- }
- else if (mode == EXTRACT) {
- /* extract_file(); */
- curblock += num_blocks_to_advance(headerbuf); /* advance the current block */
- /* by the filesize in blocks */
- }
-
- }
- close(fd);
- }
- /* calculates the number of blocks occupied by certain filesize */
- static int num_blocks_to_advance(headerVar headerbuf) {
- long double filesize = strtol(headerbuf.size, NULL, 8);
- long double numblocks = filesize / BLOCK_SIZE;
-
- return ( (int) ceil(numblocks) );
- }
- /* initializes file descriptor fd to archive name */
- static int open_archive(void) {
- int fd;
- /* set current block to 0 */
- curblock = 0;
-
- if ( (fd = open("archive", O_RDONLY)) == -1 )
- {
- perror("archive");
- exit(1);
- }
- fchmod(fd, S_IRUSR | S_IWUSR);
- printf("open_archive fd: %i\n", fd);
- return fd;
- }
- /* reads into buffer a block from the file */
- static void get_next_block(int fd, char *blockbuf) {
- int readBytes;
- printf("get_nextblcok fd: %i\n", fd);
- printf("Current block is: %i\n", curblock);
- if ( lseek(fd, BLOCK_SIZE * curblock, SEEK_SET) == -1)
- {
- perror("lseek in get_next_block");
- }
- printf("Begin reading new header.\n");
- if ( (readBytes = read(fd, blockbuf, BLOCK_SIZE)) == -1 )
- perror("read in get_next_block");
- else if (readBytes < BLOCK_SIZE)
- fprintf(stderr, "Unexpected end of archive. Bailing\n");
- curblock++; /* increment the current block */
- printf("After block is: %i\n", curblock);
- }
- /* read_header populates headerbuf from block.
- * returns -1 for error, 0 for sucess */
- static int read_header(char *blockbuf, headerVar* headerbuf) {
-
- printf("In read_header\n");
- printf("Printed Block is '%s'\n", blockbuf);
- if ( is_header(blockbuf) ) {
- printf("Header found");
- fill_headerstruct(blockbuf, headerbuf);
- }
- else
- return -1; /* not a header */
-
- return 0;
- }
- static void fill_headerstruct(char *blockbuf, headerVar *headerbuf)
- {
- memmove(headerbuf->pathname, blockbuf, 100);
- memmove(headerbuf->mode, blockbuf + 100, 8);
- memmove(headerbuf->uid, blockbuf + 108, 8);
- memmove(headerbuf->gid, blockbuf + 116, 8);
- memmove(headerbuf->size, blockbuf + 124, 12);
- memmove(headerbuf->mtime, blockbuf + 136, 12);
- memmove(headerbuf->chksum, blockbuf + 148, 8);
- memmove(&headerbuf->typeflag, blockbuf + 156, 1);
- memmove(headerbuf->linkname, blockbuf + 157, 100);
- memmove(headerbuf->magic, blockbuf + 257, 6);
- memmove(headerbuf->version, blockbuf + 263, 2);
- memmove(headerbuf->uname, blockbuf + 265, 32);
- memmove(headerbuf->gname, blockbuf + 297, 32);
- memmove(headerbuf->devmajor, blockbuf + 329, 8);
- memmove(headerbuf->devminor, blockbuf + 337, 8);
- memmove(headerbuf->prefix, blockbuf + 345, 155);
-
- }
- /* checks if blockbuf is a header by comparing ustar and checksum.
- * returns 1 on success, 0 if not a header. ERROR: returns -1 */
- static int is_header(char *blockbuf) {
- int i; /* counter */
- unsigned int sum = 0;
- char c; /* current char */
- char checksum[8]; /* store the real checksum */
- char calculatedchecksum[8]; /* to store the calculated checksum */
- // char magic[6] = "ustar";
- char tempbuf[BLOCK_SIZE];
-
- printf("in is_header\n");
- /* check to make sure ustar is present */
- if ( strncmp("ustar", &blockbuf[257], 6) != 0) {
- return 0; /* not a header */
- }
- printf("ustar success\n");
- /* extract checksum from buffer */
- memcpy(checksum, &blockbuf[148], 8);
- printf("%c\n", blockbuf[148]);
- printf("%c\n", blockbuf[149]);
- printf("%c\n", blockbuf[150]);
- printf("%c\n", blockbuf[151]);
- printf("%c\n", blockbuf[152]);
- printf("%c\n", blockbuf[153]);
- printf("%c\n", blockbuf[154]);
- printf("%c\n", blockbuf[155]);
- /* copy buffer into a a temp buffer */
- memcpy(tempbuf, blockbuf, BLOCK_SIZE);
- /* set tempbuf checksum field to all spaces) */
- memset(&tempbuf[148], ' ', 8);
-
- /* calculate full sum of temp buffer 0 through 512 */
- for (i = 0; i < BLOCK_SIZE; i++)
- {
- c = tempbuf[i];
- sum += (unsigned char) c;
- }
- printf("sum is %i\n", sum);
- /* compare the checksum with the calculated checksum */
- sprintf(calculatedchecksum, "%07o", sum);
- /* return 1 if the checksum matches */
-
- printf("Checksum is %s\n", checksum);
- printf("Calculated Checksum is %s\n", calculatedchecksum);
- if ( strncmp(checksum, calculatedchecksum, 8) == 0 ) {
- printf("checksum success\n");
- return 1; /* success */
- }
- else
- printf("Checksum failure\n");
- return 0; /* not a header */
- }
- static void print_pathname() {}
- void extract_file(int fd, headerVar headerbuf) {
- }
- static void make_dir()
- {
-
- }
- static void make_link()
- {
- }
- static void make_file()
- {
- }