Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <strings.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <file/nbio.h>
- #include <aio.h>
- #define BUFSIZE 111
- struct nbio_t
- {
- int f;
- void* data;
- size_t len;
- struct aiocb aiocb;
- /*
- * possible values:
- * NBIO_READ, NBIO_WRITE - obvious
- * -1 - currently doing nothing
- * -2 - the pointer was reallocated since the last operation
- */
- signed char op;
- signed char mode;
- };
- static const char * modes[]={ "rb", "wb", "r+b", "rb", "wb", "r+b" };
- struct nbio_t* nbio_open(const char * filename, unsigned mode)
- {
- unsigned aio_mode = 0;
- int fd, ret;
- struct nbio_t* handle = NULL;
- switch (mode)
- {
- case 0:
- case 2:
- case 3:
- case 5:
- aio_mode = O_RDONLY;
- break;
- case 1:
- case 4:
- aio_mode = O_WRONLY | O_CREAT | O_TRUNC;
- #ifndef _WIN32
- aio_mode |= S_IRUSR | S_IWUSR;
- #endif
- break;
- default:
- break;
- }
- unlink(filename);
- int f = open(filename, aio_mode);
- if (f < 0)
- return NULL;
- unlink(filename);
- handle = (struct nbio_t*)malloc(sizeof(struct nbio_t));
- if (!handle)
- goto error;
- handle->f = f;
- handle->len = 0;
- memset((char*)&handle->aiocb, 0, sizeof(struct aiocb));
- handle->aiocb.aio_buf = malloc(BUFSIZE+1);
- if (!handle->aiocb.aio_buf)
- goto error;
- /* Initialize the necessary fields in the aiocb */
- handle->aiocb.aio_fildes = f;
- handle->aiocb.aio_nbytes = BUFSIZE;
- handle->aiocb.aio_offset = 0;
- switch (mode)
- {
- case NBIO_WRITE:
- case BIO_WRITE:
- break;
- default:
- #if 0
- fseek(handle->f, 0, SEEK_END);
- handle->len = ftell(handle->f);
- #endif
- break;
- }
- handle->mode = mode;
- handle->op = -2;
- return handle;
- error:
- if (handle)
- {
- free(handle);
- }
- handle = NULL;
- close(f);
- return NULL;
- }
- void nbio_begin_read(struct nbio_t* handle)
- {
- if (!handle)
- return;
- if (handle->op >= 0)
- {
- puts("ERROR - attempted file read operation while busy");
- abort();
- }
- if (aio_read(&handle->aiocb) != 0)
- {
- puts("aio_read_error");
- abort();
- }
- handle->op = NBIO_READ;
- }
- void nbio_begin_write(struct nbio_t* handle)
- {
- if (!handle)
- return;
- if (handle->op >= 0)
- {
- puts("ERROR - attempted file write operation while busy");
- abort();
- }
- if (aio_write(&handle->aiocb) != 0)
- {
- puts("aio_write_error");
- abort();
- }
- handle->op = NBIO_WRITE;
- }
- bool nbio_iterate(struct nbio_t* handle)
- {
- int err, ret;
- if (!handle)
- return false;
- if (aio_error(&handle->aiocb) == EINPROGRESS)
- return false;
- err = aio_error(&handle->aiocb);
- ret = aio_return(&handle->aiocb);
- if (err != 0)
- {
- puts("ERROR - at aio_error():\n");
- }
- else if (ret != BUFSIZE)
- {
- puts("ERROR - at aio_return()\n");
- }
- else
- handle->op = -1;
- return (handle->op < 0);
- }
- void nbio_resize(struct nbio_t* handle, size_t len)
- {
- if (!handle)
- return;
- if (handle->op >= 0)
- {
- puts("ERROR - attempted file resize operation while busy");
- abort();
- }
- if (len < handle->len)
- {
- puts("ERROR - attempted file shrink operation, not implemented");
- abort();
- }
- handle->len = len;
- handle->op = -1;
- }
- void* nbio_get_ptr(struct nbio_t* handle, size_t* len)
- {
- if (!handle)
- return NULL;
- if (len)
- *len = handle->len;
- return NULL;
- }
- void nbio_cancel(struct nbio_t* handle)
- {
- if (!handle)
- return;
- handle->op = -1;
- }
- void nbio_free(struct nbio_t* handle)
- {
- if (!handle)
- return;
- if (handle->op >= 0)
- {
- puts("ERROR - attempted free() while busy");
- abort();
- }
- close(handle->f);
- handle->f = 0;
- free(handle);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement