write.c

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.2 kB
2
Indexable
Never
  1 #include "write.h"
  2 #include <fcntl.h>
  3 /*this function verifies a header, exits on failure, void on success*/
  4 /*DIFFERENT CHECKS BASED ON -S OPTION*/
  5 void verify_header(header *curr_header) {
  6     if (strcmp(curr_header->version, "00") != 0) {
  7         perror("Wrong version");
  8         exit(EXIT_FAILURE);
  9     }
 10     if (strcmp(curr_header->magic, "ustar") != 0) {
 11         perror("Wrong magic number");
 12         exit(EXIT_FAILURE);
 13     }
 14     /*ADD CHKSUM CHECK*/
 15 }
 16
 17 /*this function writes a header to a tar file*/
 18 /*write two null blocks*/
 19 void write_header(int tar_fd, header *curr_header) {
 20     verify_header(curr_header);
 21     printf("Write name %s\n", curr_header->name);
 22     printf("Write gname %s\n", curr_header->gname);
 23     printf("Write uname %s\n", curr_header->uname);
 24     write_field(tar_fd, curr_header->name, NAME_SIZE);
 25     write_field(tar_fd, curr_header->mode, MODE_SIZE);
 26     write_field(tar_fd, curr_header->uid, UID_SIZE);
 27     write_field(tar_fd, curr_header->gid, GID_SIZE);
 28     write_field(tar_fd, curr_header->size, FILESIZE_SIZE);
 29     write_field(tar_fd, curr_header->mtime, MTIME_SIZE);
 30     write_field(tar_fd, curr_header->chksum, CHKSUM_SIZE);
 31     if (write(tar_fd, &curr_header->typeflag, TYPEFLAG_SIZE) == -1) {
 32         perror("Write failure typeflag");
 33         exit(EXIT_FAILURE);
 34     }
 35     write_field(tar_fd, curr_header->linkname, LINKNAME_SIZE);
 36     if (write(tar_fd, curr_header->magic, MAGIC_SIZE) == -1){
 37         perror("Write failure magic");
 38         exit(EXIT_FAILURE);
 39     }
 40     if (write(tar_fd, curr_header->version, VERSION_SIZE) == -1) {
 41         perror("Write failure version");
 42         exit(EXIT_FAILURE);
 43     }
 44     write_field(tar_fd, curr_header->uname, UNAME_SIZE);
 45     write_field(tar_fd, curr_header->gname, GNAME_SIZE);
 46     write_field(tar_fd, curr_header->devmajor, DEVMAJOR_SIZE);
 47     write_field(tar_fd, curr_header->devminor, DEVMINOR_SIZE);
 48     write_field(tar_fd, curr_header->prefix, PREFIX_SIZE);
 49 }
 50
"write.c" 74L, 2500C
Leave a Comment