Exemplo n.º 1
0
    /* 
 * PURPOSE: imports block store
 * INPUTS: char, filename
 * RETURN: block_store_t
 **/
block_store_t *block_store_import(const char *const filename) {
    block_store_t *bs = block_store_create(); //create blockstore bs
    if(bs){
        bitmap_destroy(bs -> fbm); //destroy fbm in bs
        bitmap_format(bs -> dbm, 0); //format dbm in bs
        struct stat file_info; //
        size_t urf = 0;
        if(stat(filename, &file_info) != -1){
            if(file_info.st_size == BLOCK_SIZE * BLOCK_COUNT){
                int fd;
                fd = open(filename, O_RDONLY);
                if(fd != -1){
                    size_t ct = FBM_SIZE * BLOCK_SIZE;
                    urf = utility_read_file(fd, bs -> data_blocks, ct);
                    if (urf)
                    {
                        bs -> fbm = bitmap_import(BLOCK_COUNT, bs -> data_blocks);
                        if (bs -> fbm)
                        {
                            ct = (BLOCK_COUNT - FBM_SIZE) * BLOCK_SIZE;
                            urf = utility_read_file(fd, bs -> data_blocks, ct);
                            if (urf)
                            {
                                block_store_errno = BS_OK;
                                close(fd);
                            }return 0;
                        }
                    }
                }
            }
        }
    }
    


// struct stat {
//                dev_t     st_dev;         /* ID of device containing file */
//                ino_t     st_ino;         /* inode number */
//                mode_t    st_mode;        /* protection */
//                nlink_t   st_nlink;       /* number of hard links */
//                uid_t     st_uid;         /* user ID of owner */
//                gid_t     st_gid;         /* group ID of owner */
//                dev_t     st_rdev;        /* device ID (if special file) */
//                off_t     st_size;        /* total size, in bytes */
//                blksize_t st_blksize;     /* blocksize for filesystem I/O */
//                blkcnt_t  st_blocks;      /* number of 512B blocks allocated */


    block_store_errno = BS_FATAL;
    return NULL;
}
Exemplo n.º 2
0
block_store_t *block_store_import(const char *const filename) {
    block_store_t *bs = NULL;
    int fd = 0;
    if (filename) {
        struct stat file_stat;
        // macro count * size ?
        if (!stat(filename, &file_stat) && file_stat.st_size == (BLOCK_COUNT * BLOCK_SIZE)) {
            fd = open(filename, O_RDONLY);
            if (fd != -1) {
                bs = block_store_create();
                if (bs) {
                    if (utility_read_file(fd, bs->data_blocks, BLOCK_COUNT * BLOCK_SIZE) == BLOCK_COUNT * BLOCK_SIZE) {
                        // We're good to go, attempt to link.

                        close(fd);
                        // manual override because I'm not going to call link
                        //  and just have it write what we just read back out
                        bs->fd = open(filename, O_WRONLY);
                        if (bs->fd != -1) {
                            // Wipe it, we have a link to something we JUST read
                            // So it SHOULD be in sync with us unless something else is using the file
                            FLAG_SET(bs, FILE_LINKED);
                            FLAG_CLEAR(bs, DIRTY);
                            bitmap_format(bs->dbm, 0x00);
                        }
                        bs_errno = ((bs->fd == -1) ? BS_NO_LINK : BS_OK);
                        return bs;
                    }
                    // WAY less branching and resource management
                    //  no real need for the dreaded goto for maintainability
                    bs_errno = BS_FILE_IO;
                    block_store_destroy(bs, BS_NO_FLUSH);
                    close(fd);
                    return NULL;
                }
                close(fd);
            }
        }
        bs_errno = BS_FILE_ACCESS;
        return NULL;
    }
    bs_errno = BS_PARAM;
    return NULL;
}