header.c

 avatar
unknown
plain_text
a year ago
7.1 kB
5
Indexable
#include "header.h"

header *create_header(struct stat sb, char *path){
    struct header *curr_header;
    struct passwd *file_pwd;
    struct group *file_group;
    char *size8_buff, *size12_buff, *header_field;
    int chksum_val;

    /* Initialize header and string buffers */
    curr_header = malloc(sizeof(header));
    size8_buff = malloc(sizeof(char) * 8);
    size12_buff = malloc(sizeof(char) * 12);

    /* Check if malloc failed */
    if(curr_header == NULL || size8_buff == NULL || size12_buff == NULL){
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    /* Call function that packs name and prefix fields */
    if(pack_name_and_prefix(path, curr_header) == -1){
        return NULL;
    }

    /* Write mode as an octal num and store in header */
    header_field = duplicate_buff(size8_buff);
    if(snprintf(header_field, MODE_SIZE, "%0o", (unsigned int) sb.st_mode) < 0){
        perror("word overflow");
    }
    curr_header->mode = header_field;

    /* Write uid as an octal num and store in header */
    header_field = duplicate_buff(size8_buff);
    if(snprintf(header_field, UID_SIZE, "%0o", (unsigned int) sb.st_uid) < 0){
        perror("word overflow");
    }
    curr_header->uid = header_field;

    /* Write gid as an octal num and store in header */
    header_field = duplicate_buff(size8_buff);
    if(snprintf(header_field, GID_SIZE, "%0o", (unsigned int) sb.st_gid) < 0){
        perror("word overflow");
    }
    curr_header->gid = header_field;

    /* Write size as an octal num and store in header */
    header_field = duplicate_buff(size12_buff);
    if(snprintf(header_field, FILESIZE_SIZE, "%0o", (unsigned int) sb.st_size)
        < 0){
        perror("word overflow");
    }
    curr_header->size = header_field;

    /* Write mtime as an octal num and store in header */
    header_field = duplicate_buff(size12_buff);
    if(snprintf(header_field, MTIME_SIZE, "%0o", (unsigned int) sb.st_mtime)
        < 0){
        perror("word overflow");
    }
    curr_header->mtime = header_field;

    if(S_ISREG(sb.st_mode)){
        curr_header->typeflag = '0';
    }
    else if(S_ISDIR(sb.st_mode)){
        curr_header->typeflag = '5';
    }
    else if (S_ISLNK(sb.st_mode)){
        curr_header->typeflag = '2';
    }
    else{
        curr_header->typeflag = '\0';
    }

    /* FIX THIS LATER */
    if(S_ISLNK(sb.st_mode)){
        curr_header->linkname = NULL;
    }
    else{
        curr_header->linkname = NULL;
    }

    /* Adding constant header fields */
    curr_header->magic = "ustar";
    curr_header->version = "00";
    curr_header->devmajor = NULL;
    curr_header->devminor = NULL;

    /* Get user password and group fro user name and group name */
    file_pwd = getpwuid(sb.st_uid);
    file_group = getgrgid(sb.st_gid);

    if(file_pwd == NULL || file_group == NULL){
        perror("get user name and group name");
        exit(EXIT_FAILURE);
    }

    /* Assign user name and group name */
    curr_header->uname = file_pwd->pw_name;
    curr_header->gname = file_group->gr_name;

    /* Write chksum as an octal num and store in header */
    header_field = duplicate_buff(size8_buff);
    chksum_val = get_chksum_val(sb, path, file_pwd, file_group);
    if(snprintf(header_field, CHKSUM_SIZE, "%0o", chksum_val) < 0){
        perror("word overflow");
    }
    curr_header->chksum = header_field;

    free(size8_buff);
    free(size12_buff);

    return curr_header;
}

char *duplicate_buff(char *buff){
    char *header_field;

    /* Duplicate buff and exit on failure */
    if((header_field = strdup(buff)) == NULL){
        perror("strdup");
        exit(EXIT_FAILURE);
    }

    return header_field;
}

int pack_name_and_prefix(char *path, header *curr_header){
    char *name_buff, *prefix_buff;
    size_t path_len;
    int i;

    prefix_buff = malloc(sizeof(char) * PREFIX_SIZE);
    name_buff = malloc(sizeof(char) * NAME_SIZE);

    /* Check if malloc failed */
    if(name_buff == NULL || prefix_buff == NULL){
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    path_len = strlen(path);

    /* Pack name and prefix fields */
    if(path_len >= MAX_NAME_LEN){
        perror("file name too long");
        return -1;
    }
    else if(path_len < NAME_SIZE){
        /* If name fits in name block */
        if((curr_header->name = strdup(path)) == NULL){
            perror("strdup");
            exit(EXIT_FAILURE);
        }

        curr_header->prefix = NULL;
    }
    else if(path_len == NAME_SIZE){
        /* If name is exactly 100 chars decide if it goes in prefix or name */
        for(i = 0; i < NAME_SIZE; i++){
            if(path[i] == '\0'){
                curr_header->name = NULL;

                if((curr_header->prefix = strdup(path)) == NULL){
                    perror("strdup");
                    exit(EXIT_FAILURE);
                }
            }
            else if(path[i] == '/'){
                if((curr_header->name = strdup(path)) == NULL){
                    perror("strdup");
                    exit(EXIT_FAILURE);
                }

                curr_header->prefix = NULL;
                break;
            }
        }
    }
    else{
        /* If name is exactly 100 chars decide if it goes in prefix or name */
        for(i = path_len - NAME_SIZE; i < path_len; i++){
            if(path[i] == '\0'){
                perror("unable to construct header.");
                return -1;
            }
            else if(path[i] == '/'){
                /* Split the path and copy each part to its respective field */
                strncpy(prefix_buff, path, i);
                strncpy(name_buff, &path[i + 1], path_len - i);

                /* Add null char to prefix */
                prefix_buff[i] = '\0';

                /* Realloc prefix to exact length */
                if((prefix_buff = realloc(prefix_buff, i + 1)) == NULL){
                    perror("realloc");
                    exit(EXIT_FAILURE);
                }

                /* Realloc name to exact length */
                if((name_buff = realloc(name_buff, path_len - i)) == NULL){
                    perror("realloc");
                    exit(EXIT_FAILURE);
                }

                /* Set name and prefix fields */
                curr_header->name = name_buff;
                curr_header->prefix = prefix_buff;

                break;
            }
        }
    }

    return 0;
}

unsigned int get_chksum_val(struct stat sb, char *name, struct passwd *pw,
                            struct group *gr){
    unsigned int chksum_val = 0;
    int i;

    chksum_val += (unsigned int) sb.st_mode;
    chksum_val += (unsigned int) sb.st_uid;
    chksum_val += (unsigned int) sb.st_gid;
    chksum_val += (unsigned int) sb.st_size;
    chksum_val += (unsigned int) sb.st_mtime;

    for(i = 0; i < strlen(name); i++){
        chksum_val += name[i];
    }

    for(i = 0; i < strlen(pw->pw_name); i++){
        chksum_val += pw->pw_name[i];
    }

    for(i = 0; i < strlen(gr->gr_name); i++){
        chksum_val += gr->gr_name[i];
    }

    return chksum_val;
}
Editor is loading...
Leave a Comment