// TODO: Implement, comment, param check // Gotta make a new BS object and read it from the file // Need to remember to get the file format right, where's the FBM?? // Since it's just loaded from file, the DBM is easy // Should probably make sure that the file is actually the right size // There should be POSIX stuff for everything file-related // Probably going to have a lot of resource management, better be careful // Lots of different errors can happen block_store_t *block_store_import(const char *const filename) { if(filename) { block_store_t *bs = block_store_create(); if(bs) { const int fd = open(filename, O_RDONLY); if (fd != -1 ) { /* if ( utility_read_file( fd, (bitmap_import(bitmap_get_bits(bs->fbm), bs->data_blocks)), (FBM_SIZE * BLOCK_SIZE) == (FBM_SIZE * BLOCK_SIZE))) { if (utility_write_file(fd, bs->data_blocks, BLOCK_SIZE * (BLOCK_COUNT - FBM_SIZE)) == (BLOCK_SIZE * (BLOCK_COUNT - FBM_SIZE))) { block_store_errno = BS_OK; close(fd); return bs; } }*/ block_store_errno = BS_FILE_IO; close(fd); free(bs); return NULL; } block_store_errno = BS_FILE_ACCESS; free(bs); return NULL; } block_store_errno = BS_FATAL; return NULL; } block_store_errno = BS_PARAM; return NULL; }
block_store_t *block_store_import(const char *const filename) { if(filename){ fd = open(filename, O_RDONLY); if(fd != -1) { block_store_t *bs = block_store_create(); } } block_store_errno = BS_FATAL; return NULL; }
/* * 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; }
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; }
void page_swap_algorithm::init_backing_store() { backing_store = block_store_create(); if (backing_store) return; throw std::runtime_error(std::string("Could not create block_store because ").append(block_store_strerror(bs_errno))); }