Annotation of libaitio/src/aio.c, revision 1.3.16.1
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 $
1.3.16.1! misho 6: * $Id: aio.c,v 1.3 2011/04/20 22:56:32 misho Exp $
1.3 misho 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:
1.3.16.1! misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.3 misho 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
1.3.16.1! misho 51: *
1.2 misho 52: * @fd = File handle
53: * @buf = Read buffer
54: * @nbytes = Read buffer size
55: * @offset = Read from position, if -1 read nbytes from current position
56: * @update = Update file handle position !0
57: * return: -1 error or !=-1 readed bytes
58: */
59: inline int
60: io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update)
61: {
62: int ret;
63: #ifdef AIO_OPS
64: off_t old = 0;
65: struct aiocb acb;
66: #endif
67:
68: if (!buf) {
69: io_SetErr(EINVAL, "Error:: invalid arguments ...\n");
70: return -1;
71: }
72: if (!nbytes)
73: return 0;
74: if (offset == -1) {
75: offset = lseek(fd, 0, SEEK_CUR);
76: if (offset == -1) {
77: LOGERR;
78: return -1;
79: }
80: }
81:
82: #ifdef AIO_OPS
83: /* This made for generate EOF for file */
84: if (!update && (old = lseek(fd, 0, SEEK_CUR)) == -1) {
85: LOGERR;
86: return -1;
87: }
88: if (offset >= lseek(fd, 0, SEEK_END))
89: return 0;
90: if (!update)
91: lseek(fd, old, SEEK_SET);
92:
93: memset(buf, 0, nbytes);
94: memset(&acb, 0, sizeof acb);
95: acb.aio_fildes = fd;
96: acb.aio_nbytes = nbytes;
97: acb.aio_buf = buf;
98: acb.aio_offset = offset;
99:
100: if (aio_read(&acb) == -1) {
101: LOGERR;
102: return -1;
103: }
104:
105: while (aio_error(&acb) == EINPROGRESS);
106: ret = aio_return(&acb);
107: if (ret == -1) {
108: LOGERR;
109: return -1;
110: } else
111: ret = acb.aio_nbytes;
112: #else
113: ret = pread(fd, buf, nbytes, offset);
114: if (ret == -1) {
115: LOGERR;
116: return -1;
117: }
118: #endif
119:
120: if (update)
121: lseek(fd, offset + ret, SEEK_SET);
122:
123: return ret;
124: }
125:
126: /*
127: * io_rwrite() Raw VFS write function
1.3.16.1! misho 128: *
1.2 misho 129: * @fd = File handle
130: * @buf = Write buffer
131: * @nbytes = Write bytes from buffer
132: * @offset = Write at position, if -1 write nbytes from current position
133: * @update = Update file handle position !0
134: * return: -1 error or !=-1 writed bytes
135: */
136: inline int
137: io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update)
138: {
139: int ret;
140: #ifdef AIO_OPS
141: struct aiocb acb;
142: #endif
143:
144: if (!buf) {
145: io_SetErr(EINVAL, "Error:: invalid arguments ...\n");
146: return -1;
147: }
148: if (!nbytes)
149: return 0;
150: if (offset == -1) {
151: offset = lseek(fd, 0, SEEK_CUR);
152: if (offset == -1) {
153: LOGERR;
154: return -1;
155: }
156: }
157:
158: #ifdef AIO_OPS
159: memset(&acb, 0, sizeof acb);
160: acb.aio_fildes = fd;
161: acb.aio_nbytes = nbytes;
162: acb.aio_buf = buf;
163: acb.aio_offset = offset;
164:
165: if (aio_write(&acb) == -1) {
166: LOGERR;
167: return -1;
168: }
169:
170: while (aio_error(&acb) == EINPROGRESS);
171: ret = aio_return(&acb);
172: if (ret == -1) {
173: LOGERR;
174: return -1;
175: } else
176: ret = acb.aio_nbytes;
177: #else
178: ret = pwrite(fd, buf, nbytes, offset);
179: if (ret == -1) {
180: LOGERR;
181: return -1;
182: }
183: #endif
184:
185: if (update)
186: lseek(fd, offset + ret, SEEK_SET);
187:
188:
189: return ret;
190: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>