Annotation of libaitio/src/aio.c, revision 1.1.2.1
1.1.2.1 ! misho 1: #include "global.h"
! 2:
! 3:
! 4: /*
! 5: * io_rread() Raw read function
! 6: * @fd = File handle
! 7: * @buf = Read buffer
! 8: * @nbytes = Read buffer size
! 9: * @offset = Read from position, if -1 read nbytes from current position
! 10: * @update = Update file handle position !0
! 11: * return: -1 error or !=-1 readed bytes
! 12: */
! 13: inline int
! 14: io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update)
! 15: {
! 16: int ret;
! 17: #ifdef AIO_OPS
! 18: struct aiocb acb;
! 19: #endif
! 20:
! 21: if (!buf) {
! 22: io_SetErr(EINVAL, "Error:: invalid arguments ...\n");
! 23: return -1;
! 24: }
! 25: if (!nbytes)
! 26: return 0;
! 27: if (offset == -1) {
! 28: offset = lseek(fd, 0, SEEK_CUR);
! 29: if (offset == -1) {
! 30: LOGERR;
! 31: return -1;
! 32: }
! 33: }
! 34:
! 35: #ifdef AIO_OPS
! 36: memset(buf, 0, nbytes);
! 37: memset(&acb, 0, sizeof acb);
! 38: acb.aio_fildes = fd;
! 39: acb.aio_nbytes = nbytes;
! 40: acb.aio_buf = buf;
! 41: acb.aio_offset = offset;
! 42:
! 43: if (aio_read(&acb) == -1) {
! 44: LOGERR;
! 45: return -1;
! 46: }
! 47:
! 48: while (aio_error(&acb) == EINPROGRESS);
! 49: ret = aio_return(&acb);
! 50: if (ret == -1) {
! 51: LOGERR;
! 52: return -1;
! 53: } else
! 54: ret = acb.aio_nbytes;
! 55: #else
! 56: ret = pread(fd, buf, nbytes, offset);
! 57: if (ret == -1) {
! 58: LOGERR;
! 59: return -1;
! 60: }
! 61: #endif
! 62:
! 63: if (update)
! 64: lseek(fd, offset + ret, SEEK_SET);
! 65:
! 66: return ret;
! 67: }
! 68:
! 69: /*
! 70: * io_rwrite() Raw write function
! 71: * @fd = File handle
! 72: * @buf = Write buffer
! 73: * @nbytes = Write bytes from buffer
! 74: * @offset = Write at position, if -1 write nbytes from current position
! 75: * @update = Update file handle position !0
! 76: * return: -1 error or !=-1 writed bytes
! 77: */
! 78: inline int
! 79: io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update)
! 80: {
! 81: int ret;
! 82: #ifdef AIO_OPS
! 83: struct aiocb acb;
! 84: #endif
! 85:
! 86: if (!buf) {
! 87: io_SetErr(EINVAL, "Error:: invalid arguments ...\n");
! 88: return -1;
! 89: }
! 90: if (!nbytes)
! 91: return 0;
! 92: if (offset == -1) {
! 93: offset = lseek(fd, 0, SEEK_CUR);
! 94: if (offset == -1) {
! 95: LOGERR;
! 96: return -1;
! 97: }
! 98: }
! 99:
! 100: #ifdef AIO_OPS
! 101: memset(buf, 0, nbytes);
! 102: memset(&acb, 0, sizeof acb);
! 103: acb.aio_fildes = fd;
! 104: acb.aio_nbytes = nbytes;
! 105: acb.aio_buf = buf;
! 106: acb.aio_offset = offset;
! 107:
! 108: if (aio_write(&acb) == -1) {
! 109: LOGERR;
! 110: return -1;
! 111: }
! 112:
! 113: while (aio_error(&acb) == EINPROGRESS);
! 114: ret = aio_return(&acb);
! 115: if (ret == -1) {
! 116: LOGERR;
! 117: return -1;
! 118: } else
! 119: ret = acb.aio_nbytes;
! 120: #else
! 121: ret = pwrite(fd, buf, nbytes, offset);
! 122: if (ret == -1) {
! 123: LOGERR;
! 124: return -1;
! 125: }
! 126: #endif
! 127:
! 128: if (update)
! 129: lseek(fd, offset + ret, SEEK_SET);
! 130:
! 131:
! 132: return ret;
! 133: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>