Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.6 kB
6
Indexable
Never
#include "create.h"

int create_archive(char *tarfile){
    int tar_fd;

    /* Open tar_file with read write user permissions */
    tar_fd = open(tarfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);

    if(tar_fd == -1){
        perror("open");
        exit(EXIT_FAILURE);
    }

    return tar_fd;
}

void archive_file(int fd, char *curr_path, char *path_name){
    struct stat file_sb;
    struct passwd *file_pwd;
    struct group *file_group;
    struct dirent *dir_entry;
    DIR *directory;
    char *full_path;

    /* Stat current file and exit if it failed */
    if(stat(path_name, &file_sb) == -1){
        perror("stat");
        exit(EXIT_FAILURE);
    }

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

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

    /* Create full path up to this point */
    full_path = create_full_path(curr_path, path_name, file_sb.st_mode);

    if(S_ISDIR(file_sb.st_mode)){
        if(verbose_option == 1){
            printf("%s\n", full_path);
        }

        /* Change to paths directory */
        if(chdir(path_name) == -1){
            perror("chdir");
            exit(EXIT_FAILURE);
        }

        /* Open the current directory, exit on failure */
        directory = opendir(".");

        if(directory == NULL){
            perror("opendir: ");
            exit(EXIT_FAILURE);
        }

        while((dir_entry = readdir(directory)) != NULL){
            /* If the entry name is not . or .. archive the file */
            if(strcmp(dir_entry->d_name, ".") &&
                strcmp(dir_entry->d_name, "..")){

                /* Recurse on child entry */
                archive_file(fd, full_path, dir_entry->d_name);
            }
        }

        closedir(directory);
    }
    else if (S_ISLNK(file_sb.st_mode)){
        printf("L %s/%s    %d %s\n", file_pwd->pw_name, file_group->gr_name
                                , file_sb.st_size, path_name);
    }
    else if (S_ISREG(file_sb.st_mode)){
        if(verbose_option == 1){
            printf("%s\n", full_path);
        }
    }
    else{
        /* If file is not a dir, sym link, or a regular file exit */
        perror("unsupported file type: ");
    }

    return;
}

/* Takes curr path and curr file name and appends them with a / in between */
char *create_full_path(char *curr_path, char *file_name, mode_t file_mode){
    char *full_path;
    size_t curr_path_len, file_name_len;
    int i;

    curr_path_len = strlen(curr_path);
    file_name_len = strlen(file_name);

    /* Malloc string of size path and file name with 2 spaces for null and / */
    if(S_ISDIR(file_mode)){
        full_path = malloc(curr_path_len + file_name_len + 2);
    }
    else if(S_ISREG(file_mode) || S_ISLNK(file_mode)){
        full_path = malloc(curr_path_len + file_name_len + 1);
    }

    /* Copy curr path over */
    for(i = 0; i < curr_path_len; i++){
        full_path[i] = curr_path[i];
    }

    /* Copy file name over */
    for(i = 0; i < file_name_len; i++){
        full_path[curr_path_len + i] = file_name[i];
    }

    /* Add null terminating character and slash */
    if(S_ISDIR(file_mode)){
        full_path[curr_path_len + file_name_len] = '/';
        full_path[curr_path_len + file_name_len + 1] = '\0';
    }
    else if(S_ISREG(file_mode) || S_ISLNK(file_mode)){
        full_path[curr_path_len + file_name_len] = '\0';
    }

    return full_path;
}
Leave a Comment