File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / aio.c
Revision 1.2.8.1: download - view: text, annotated - select for diffs - revision graph
Tue Apr 19 21:07:32 2011 UTC (13 years, 2 months ago) by misho
Branches: io1_7
Diff to: branchpoint 1.2: preferred, unified
added new feature
dynamic array!

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aio.c,v 1.2.8.1 2011/04/19 21:07:32 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: 
   11: 
   12: /*
   13:  * io_rread() Raw VFS read function
   14:  * @fd = File handle
   15:  * @buf = Read buffer
   16:  * @nbytes = Read buffer size
   17:  * @offset = Read from position, if -1 read nbytes from current position
   18:  * @update = Update file handle position !0
   19:  * return: -1 error or !=-1 readed bytes
   20:  */
   21: inline int
   22: io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update)
   23: {
   24: 	int ret;
   25: #ifdef AIO_OPS
   26: 	off_t old = 0;
   27: 	struct aiocb acb;
   28: #endif
   29: 
   30: 	if (!buf) {
   31: 		io_SetErr(EINVAL, "Error:: invalid arguments ...\n");
   32: 		return -1;
   33: 	}
   34: 	if (!nbytes)
   35: 		return 0;
   36: 	if (offset == -1) {
   37: 		offset = lseek(fd, 0, SEEK_CUR);
   38: 		if (offset == -1) {
   39: 			LOGERR;
   40: 			return -1;
   41: 		}
   42: 	}
   43: 
   44: #ifdef AIO_OPS
   45: 	/* This made for generate EOF for file */
   46: 	if (!update && (old = lseek(fd, 0, SEEK_CUR)) == -1) {
   47: 		LOGERR;
   48: 		return -1;
   49: 	}
   50: 	if (offset >= lseek(fd, 0, SEEK_END))
   51: 		return 0;
   52: 	if (!update)
   53: 		lseek(fd, old, SEEK_SET);
   54: 
   55: 	memset(buf, 0, nbytes);
   56: 	memset(&acb, 0, sizeof acb);
   57: 	acb.aio_fildes = fd;
   58: 	acb.aio_nbytes = nbytes;
   59: 	acb.aio_buf = buf;
   60: 	acb.aio_offset = offset;
   61: 
   62: 	if (aio_read(&acb) == -1) {
   63: 		LOGERR;
   64: 		return -1;
   65: 	}
   66: 
   67: 	while (aio_error(&acb) == EINPROGRESS);
   68: 	ret = aio_return(&acb);
   69: 	if (ret == -1) {
   70: 		LOGERR;
   71: 		return -1;
   72: 	} else
   73: 		ret = acb.aio_nbytes;
   74: #else
   75: 	ret = pread(fd, buf, nbytes, offset);
   76: 	if (ret == -1) {
   77: 		LOGERR;
   78: 		return -1;
   79: 	}
   80: #endif
   81: 
   82: 	if (update)
   83: 		lseek(fd, offset + ret, SEEK_SET);
   84: 
   85: 	return ret;
   86: }
   87: 
   88: /*
   89:  * io_rwrite() Raw VFS write function
   90:  * @fd = File handle
   91:  * @buf = Write buffer
   92:  * @nbytes = Write bytes from buffer
   93:  * @offset = Write at position, if -1 write nbytes from current position
   94:  * @update = Update file handle position !0
   95:  * return: -1 error or !=-1 writed bytes
   96:  */
   97: inline int
   98: io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update)
   99: {
  100: 	int ret;
  101: #ifdef AIO_OPS
  102: 	struct aiocb acb;
  103: #endif
  104: 
  105: 	if (!buf) {
  106: 		io_SetErr(EINVAL, "Error:: invalid arguments ...\n");
  107: 		return -1;
  108: 	}
  109: 	if (!nbytes)
  110: 		return 0;
  111: 	if (offset == -1) {
  112: 		offset = lseek(fd, 0, SEEK_CUR);
  113: 		if (offset == -1) {
  114: 			LOGERR;
  115: 			return -1;
  116: 		}
  117: 	}
  118: 
  119: #ifdef AIO_OPS
  120: 	memset(&acb, 0, sizeof acb);
  121: 	acb.aio_fildes = fd;
  122: 	acb.aio_nbytes = nbytes;
  123: 	acb.aio_buf = buf;
  124: 	acb.aio_offset = offset;
  125: 
  126: 	if (aio_write(&acb) == -1) {
  127: 		LOGERR;
  128: 		return -1;
  129: 	}
  130: 
  131: 	while (aio_error(&acb) == EINPROGRESS);
  132: 	ret = aio_return(&acb);
  133: 	if (ret == -1) {
  134: 		LOGERR;
  135: 		return -1;
  136: 	} else
  137: 		ret = acb.aio_nbytes;
  138: #else
  139: 	ret = pwrite(fd, buf, nbytes, offset);
  140: 	if (ret == -1) {
  141: 		LOGERR;
  142: 		return -1;
  143: 	}
  144: #endif
  145: 
  146: 	if (update)
  147: 		lseek(fd, offset + ret, SEEK_SET);
  148: 
  149: 
  150: 	return ret;
  151: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>