File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / aio.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Fri Sep 10 12:39:41 2010 UTC (13 years, 9 months ago) by misho
Branches: MAIN
CVS tags: io1_7, io1_6, io1_5, io1_4, IO1_6, IO1_5, IO1_4, IO1_3, HEAD
new version 1.3

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

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