/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

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <math.h>
  6. #include "mytar.h"
  7. #define BLOCK_SIZE 512
  8. #define END_OF_ARCHIVE 1
  9. #define TABLE_OF_CONTENTS 1
  10. #define EXTRACT 2
  11. /* global to keep track of current block */
  12. static int curblock;
  13. static int open_archive(void);
  14. static void get_next_block(int, char *);
  15. static int read_header(char *, headerVar *);
  16. static void fill_headerstruct(char *, headerVar *);
  17. static int is_header(char *);
  18. static void print_pathname();
  19. static void process(const int);
  20. static int num_blocks_to_advance(headerVar);
  21. void extract_file(int fd, headerVar headerbuf);
  22. static void make_dir();
  23. static void make_link();
  24. static void make_file();
  25. int main(int argc, char *argv[]) {
  26. // headerVar headerbuf;
  27. // int n;
  28. // snprintf(headerbuf.size, 12, "%o", (unsigned int) 513);
  29. process(TABLE_OF_CONTENTS);
  30. // int num = num_blocks_to_advance(headerbuf);
  31. // printf("Number of blocks to advance: %i\n", num);
  32. return 0;
  33. }
  34. static void process(const int mode) {
  35. int fd;
  36. char blockbuf[BLOCK_SIZE], zerobuf[BLOCK_SIZE];
  37. headerVar headerbuf;
  38. memset(zerobuf, 0, BLOCK_SIZE);
  39. int endOfArchive = 0;
  40. fd = open_archive();
  41. while (!endOfArchive)
  42. {
  43. get_next_block(fd, blockbuf);
  44. printf("Done with reading block %i\n", curblock - 1);
  45. /* End of Archive check */
  46. if ( memcmp(blockbuf, zerobuf, BLOCK_SIZE) == 0 )
  47. {
  48. printf("inside end of archive\n");
  49. get_next_block(fd, blockbuf);
  50. printf("after end of archive !!!\n");
  51. if ( memcmp(blockbuf, zerobuf, BLOCK_SIZE) == 0) {
  52. printf("Comparison");
  53. endOfArchive = 1;
  54. continue;
  55. }
  56. else curblock--;
  57. }
  58. /* Decompose header into headerVar */
  59. if (read_header(blockbuf, &headerbuf) == -1) {
  60. fprintf(stderr, "process: Malformed header found. Bailing.\n");
  61. exit(1);
  62. }
  63. if (mode == TABLE_OF_CONTENTS) {
  64. print_pathname();
  65. curblock += num_blocks_to_advance(headerbuf); /* advance the current block */
  66. /* by the filesize in blocks */
  67. }
  68. else if (mode == EXTRACT) {
  69. /* extract_file(); */
  70. curblock += num_blocks_to_advance(headerbuf); /* advance the current block */
  71. /* by the filesize in blocks */
  72. }
  73. }
  74. close(fd);
  75. }
  76. /* calculates the number of blocks occupied by certain filesize */
  77. static int num_blocks_to_advance(headerVar headerbuf) {
  78. long double filesize = strtol(headerbuf.size, NULL, 8);
  79. long double numblocks = filesize / BLOCK_SIZE;
  80. return ( (int) ceil(numblocks) );
  81. }
  82. /* initializes file descriptor fd to archive name */
  83. static int open_archive(void) {
  84. int fd;
  85. /* set current block to 0 */
  86. curblock = 0;
  87. if ( (fd = open("archive", O_RDONLY)) == -1 )
  88. {
  89. perror("archive");
  90. exit(1);
  91. }
  92. fchmod(fd, S_IRUSR | S_IWUSR);
  93. printf("open_archive fd: %i\n", fd);
  94. return fd;
  95. }
  96. /* reads into buffer a block from the file */
  97. static void get_next_block(int fd, char *blockbuf) {
  98. int readBytes;
  99. printf("get_nextblcok fd: %i\n", fd);
  100. printf("Current block is: %i\n", curblock);
  101. if ( lseek(fd, BLOCK_SIZE * curblock, SEEK_SET) == -1)
  102. {
  103. perror("lseek in get_next_block");
  104. }
  105. printf("Begin reading new header.\n");
  106. if ( (readBytes = read(fd, blockbuf, BLOCK_SIZE)) == -1 )
  107. perror("read in get_next_block");
  108. else if (readBytes < BLOCK_SIZE)
  109. fprintf(stderr, "Unexpected end of archive. Bailing\n");
  110. curblock++; /* increment the current block */
  111. printf("After block is: %i\n", curblock);
  112. }
  113. /* read_header populates headerbuf from block.
  114. * returns -1 for error, 0 for sucess */
  115. static int read_header(char *blockbuf, headerVar* headerbuf) {
  116. printf("In read_header\n");
  117. printf("Printed Block is '%s'\n", blockbuf);
  118. if ( is_header(blockbuf) ) {
  119. printf("Header found");
  120. fill_headerstruct(blockbuf, headerbuf);
  121. }
  122. else
  123. return -1; /* not a header */
  124. return 0;
  125. }
  126. static void fill_headerstruct(char *blockbuf, headerVar *headerbuf)
  127. {
  128. memmove(headerbuf->pathname, blockbuf, 100);
  129. memmove(headerbuf->mode, blockbuf + 100, 8);
  130. memmove(headerbuf->uid, blockbuf + 108, 8);
  131. memmove(headerbuf->gid, blockbuf + 116, 8);
  132. memmove(headerbuf->size, blockbuf + 124, 12);
  133. memmove(headerbuf->mtime, blockbuf + 136, 12);
  134. memmove(headerbuf->chksum, blockbuf + 148, 8);
  135. memmove(&headerbuf->typeflag, blockbuf + 156, 1);
  136. memmove(headerbuf->linkname, blockbuf + 157, 100);
  137. memmove(headerbuf->magic, blockbuf + 257, 6);
  138. memmove(headerbuf->version, blockbuf + 263, 2);
  139. memmove(headerbuf->uname, blockbuf + 265, 32);
  140. memmove(headerbuf->gname, blockbuf + 297, 32);
  141. memmove(headerbuf->devmajor, blockbuf + 329, 8);
  142. memmove(headerbuf->devminor, blockbuf + 337, 8);
  143. memmove(headerbuf->prefix, blockbuf + 345, 155);
  144. }
  145. /* checks if blockbuf is a header by comparing ustar and checksum.
  146. * returns 1 on success, 0 if not a header. ERROR: returns -1 */
  147. static int is_header(char *blockbuf) {
  148. int i; /* counter */
  149. unsigned int sum = 0;
  150. char c; /* current char */
  151. char checksum[8]; /* store the real checksum */
  152. char calculatedchecksum[8]; /* to store the calculated checksum */
  153. // char magic[6] = "ustar";
  154. char tempbuf[BLOCK_SIZE];
  155. printf("in is_header\n");
  156. /* check to make sure ustar is present */
  157. if ( strncmp("ustar", &blockbuf[257], 6) != 0) {
  158. return 0; /* not a header */
  159. }
  160. printf("ustar success\n");
  161. /* extract checksum from buffer */
  162. memcpy(checksum, &blockbuf[148], 8);
  163. printf("%c\n", blockbuf[148]);
  164. printf("%c\n", blockbuf[149]);
  165. printf("%c\n", blockbuf[150]);
  166. printf("%c\n", blockbuf[151]);
  167. printf("%c\n", blockbuf[152]);
  168. printf("%c\n", blockbuf[153]);
  169. printf("%c\n", blockbuf[154]);
  170. printf("%c\n", blockbuf[155]);
  171. /* copy buffer into a a temp buffer */
  172. memcpy(tempbuf, blockbuf, BLOCK_SIZE);
  173. /* set tempbuf checksum field to all spaces) */
  174. memset(&tempbuf[148], ' ', 8);
  175. /* calculate full sum of temp buffer 0 through 512 */
  176. for (i = 0; i < BLOCK_SIZE; i++)
  177. {
  178. c = tempbuf[i];
  179. sum += (unsigned char) c;
  180. }
  181. printf("sum is %i\n", sum);
  182. /* compare the checksum with the calculated checksum */
  183. sprintf(calculatedchecksum, "%07o", sum);
  184. /* return 1 if the checksum matches */
  185. printf("Checksum is %s\n", checksum);
  186. printf("Calculated Checksum is %s\n", calculatedchecksum);
  187. if ( strncmp(checksum, calculatedchecksum, 8) == 0 ) {
  188. printf("checksum success\n");
  189. return 1; /* success */
  190. }
  191. else
  192. printf("Checksum failure\n");
  193. return 0; /* not a header */
  194. }
  195. static void print_pathname() {}
  196. void extract_file(int fd, headerVar headerbuf) {
  197. }
  198. static void make_dir()
  199. {
  200. }
  201. static void make_link()
  202. {
  203. }
  204. static void make_file()
  205. {
  206. }