extract.c

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.1 kB
3
Indexable
Never
#include "extract.h"

int extract_header(header *curr_header) {
    /*create file with correct permissions*/
    /*return file descriptor*/
    int fd;
    char *full_path;
    long mode;

    full_path = rebuild_name(curr_header);
    mode = strtol(curr_header->mode, NULL, 0);

    if (S_ISDIR(mode)) {
        if (mkdir(full_path, mode) == -1) {
            perror("Mkdir");
            exit(EXIT_FAILURE);
        }
        return DIR_VAL;
    }

    if(S_ISLNK(mode)) {
        if ((symlink(full_path, curr_header->linkname)) == -1) {
            perror("symlink");
            exit(EXIT_FAILURE);
        }
        return SYM_VAL;
    }

    if(S_ISREG(mode)) {
        if ((fd = creat(full_path, mode)) == -1) {
            perror("creat");
            exit(EXIT_FAILURE);
        }

        return fd;
    }
}


void extract_contents(int target_fd, char block[BLOCK_SIZE], unsigned size) {
    if (size >= BLOCK_SIZE) {
        size = BLOCK_SIZE;
    }
    if (write(target_fd, block, size) == -1) {
        perror("Write");
        exit(EXIT_FAILURE);
    }
}
Leave a Comment