Annotation of libaitio/src/aio.c, revision 1.3

1.3     ! misho       1: /*************************************************************************
        !             2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
        !             3: *  by Michael Pounov <misho@elwix.org>
        !             4: *
        !             5: * $Author: misho $
        !             6: * $Id: aio.c,v 1.2.8.3 2011/04/20 22:55:41 misho Exp $
        !             7: *
        !             8: **************************************************************************
        !             9: The ELWIX and AITNET software is distributed under the following
        !            10: terms:
        !            11: 
        !            12: All of the documentation and software included in the ELWIX and AITNET
        !            13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
        !            14: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
        !            16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
        !            17: 
        !            18: Redistribution and use in source and binary forms, with or without
        !            19: modification, are permitted provided that the following conditions
        !            20: are met:
        !            21: 1. Redistributions of source code must retain the above copyright
        !            22:    notice, this list of conditions and the following disclaimer.
        !            23: 2. Redistributions in binary form must reproduce the above copyright
        !            24:    notice, this list of conditions and the following disclaimer in the
        !            25:    documentation and/or other materials provided with the distribution.
        !            26: 3. All advertising materials mentioning features or use of this software
        !            27:    must display the following acknowledgement:
        !            28: This product includes software developed by Michael Pounov <misho@elwix.org>
        !            29: ELWIX - Embedded LightWeight unIX and its contributors.
        !            30: 4. Neither the name of AITNET nor the names of its contributors
        !            31:    may be used to endorse or promote products derived from this software
        !            32:    without specific prior written permission.
        !            33: 
        !            34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
        !            35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            44: SUCH DAMAGE.
        !            45: */
1.2       misho      46: #include "global.h"
                     47: 
                     48: 
                     49: /*
                     50:  * io_rread() Raw VFS read function
                     51:  * @fd = File handle
                     52:  * @buf = Read buffer
                     53:  * @nbytes = Read buffer size
                     54:  * @offset = Read from position, if -1 read nbytes from current position
                     55:  * @update = Update file handle position !0
                     56:  * return: -1 error or !=-1 readed bytes
                     57:  */
                     58: inline int
                     59: io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update)
                     60: {
                     61:        int ret;
                     62: #ifdef AIO_OPS
                     63:        off_t old = 0;
                     64:        struct aiocb acb;
                     65: #endif
                     66: 
                     67:        if (!buf) {
                     68:                io_SetErr(EINVAL, "Error:: invalid arguments ...\n");
                     69:                return -1;
                     70:        }
                     71:        if (!nbytes)
                     72:                return 0;
                     73:        if (offset == -1) {
                     74:                offset = lseek(fd, 0, SEEK_CUR);
                     75:                if (offset == -1) {
                     76:                        LOGERR;
                     77:                        return -1;
                     78:                }
                     79:        }
                     80: 
                     81: #ifdef AIO_OPS
                     82:        /* This made for generate EOF for file */
                     83:        if (!update && (old = lseek(fd, 0, SEEK_CUR)) == -1) {
                     84:                LOGERR;
                     85:                return -1;
                     86:        }
                     87:        if (offset >= lseek(fd, 0, SEEK_END))
                     88:                return 0;
                     89:        if (!update)
                     90:                lseek(fd, old, SEEK_SET);
                     91: 
                     92:        memset(buf, 0, nbytes);
                     93:        memset(&acb, 0, sizeof acb);
                     94:        acb.aio_fildes = fd;
                     95:        acb.aio_nbytes = nbytes;
                     96:        acb.aio_buf = buf;
                     97:        acb.aio_offset = offset;
                     98: 
                     99:        if (aio_read(&acb) == -1) {
                    100:                LOGERR;
                    101:                return -1;
                    102:        }
                    103: 
                    104:        while (aio_error(&acb) == EINPROGRESS);
                    105:        ret = aio_return(&acb);
                    106:        if (ret == -1) {
                    107:                LOGERR;
                    108:                return -1;
                    109:        } else
                    110:                ret = acb.aio_nbytes;
                    111: #else
                    112:        ret = pread(fd, buf, nbytes, offset);
                    113:        if (ret == -1) {
                    114:                LOGERR;
                    115:                return -1;
                    116:        }
                    117: #endif
                    118: 
                    119:        if (update)
                    120:                lseek(fd, offset + ret, SEEK_SET);
                    121: 
                    122:        return ret;
                    123: }
                    124: 
                    125: /*
                    126:  * io_rwrite() Raw VFS write function
                    127:  * @fd = File handle
                    128:  * @buf = Write buffer
                    129:  * @nbytes = Write bytes from buffer
                    130:  * @offset = Write at position, if -1 write nbytes from current position
                    131:  * @update = Update file handle position !0
                    132:  * return: -1 error or !=-1 writed bytes
                    133:  */
                    134: inline int
                    135: io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update)
                    136: {
                    137:        int ret;
                    138: #ifdef AIO_OPS
                    139:        struct aiocb acb;
                    140: #endif
                    141: 
                    142:        if (!buf) {
                    143:                io_SetErr(EINVAL, "Error:: invalid arguments ...\n");
                    144:                return -1;
                    145:        }
                    146:        if (!nbytes)
                    147:                return 0;
                    148:        if (offset == -1) {
                    149:                offset = lseek(fd, 0, SEEK_CUR);
                    150:                if (offset == -1) {
                    151:                        LOGERR;
                    152:                        return -1;
                    153:                }
                    154:        }
                    155: 
                    156: #ifdef AIO_OPS
                    157:        memset(&acb, 0, sizeof acb);
                    158:        acb.aio_fildes = fd;
                    159:        acb.aio_nbytes = nbytes;
                    160:        acb.aio_buf = buf;
                    161:        acb.aio_offset = offset;
                    162: 
                    163:        if (aio_write(&acb) == -1) {
                    164:                LOGERR;
                    165:                return -1;
                    166:        }
                    167: 
                    168:        while (aio_error(&acb) == EINPROGRESS);
                    169:        ret = aio_return(&acb);
                    170:        if (ret == -1) {
                    171:                LOGERR;
                    172:                return -1;
                    173:        } else
                    174:                ret = acb.aio_nbytes;
                    175: #else
                    176:        ret = pwrite(fd, buf, nbytes, offset);
                    177:        if (ret == -1) {
                    178:                LOGERR;
                    179:                return -1;
                    180:        }
                    181: #endif
                    182: 
                    183:        if (update)
                    184:                lseek(fd, offset + ret, SEEK_SET);
                    185: 
                    186: 
                    187:        return ret;
                    188: }

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