qapi: drop the sentinel in enum array
[qemu/armbru.git] / io / channel-file.c
blobb383273201c280cc62f444d981299c06fcf0b486
1 /*
2 * QEMU I/O channels files driver
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "io/channel-file.h"
23 #include "io/channel-watch.h"
24 #include "qapi/error.h"
25 #include "qemu/sockets.h"
26 #include "trace.h"
28 QIOChannelFile *
29 qio_channel_file_new_fd(int fd)
31 QIOChannelFile *ioc;
33 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
35 ioc->fd = fd;
37 trace_qio_channel_file_new_fd(ioc, fd);
39 return ioc;
43 QIOChannelFile *
44 qio_channel_file_new_path(const char *path,
45 int flags,
46 mode_t mode,
47 Error **errp)
49 QIOChannelFile *ioc;
51 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
53 if (flags & O_WRONLY) {
54 ioc->fd = open(path, flags, mode);
55 } else {
56 ioc->fd = open(path, flags);
58 if (ioc->fd < 0) {
59 object_unref(OBJECT(ioc));
60 error_setg_errno(errp, errno,
61 "Unable to open %s", path);
62 return NULL;
65 trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd);
67 return ioc;
71 static void qio_channel_file_init(Object *obj)
73 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
74 ioc->fd = -1;
77 static void qio_channel_file_finalize(Object *obj)
79 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
80 if (ioc->fd != -1) {
81 close(ioc->fd);
82 ioc->fd = -1;
87 static ssize_t qio_channel_file_readv(QIOChannel *ioc,
88 const struct iovec *iov,
89 size_t niov,
90 int **fds,
91 size_t *nfds,
92 Error **errp)
94 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
95 ssize_t ret;
97 retry:
98 ret = readv(fioc->fd, iov, niov);
99 if (ret < 0) {
100 if (errno == EAGAIN) {
101 return QIO_CHANNEL_ERR_BLOCK;
103 if (errno == EINTR) {
104 goto retry;
107 error_setg_errno(errp, errno,
108 "Unable to read from file");
109 return -1;
112 return ret;
115 static ssize_t qio_channel_file_writev(QIOChannel *ioc,
116 const struct iovec *iov,
117 size_t niov,
118 int *fds,
119 size_t nfds,
120 Error **errp)
122 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
123 ssize_t ret;
125 retry:
126 ret = writev(fioc->fd, iov, niov);
127 if (ret <= 0) {
128 if (errno == EAGAIN) {
129 return QIO_CHANNEL_ERR_BLOCK;
131 if (errno == EINTR) {
132 goto retry;
134 error_setg_errno(errp, errno,
135 "Unable to write to file");
136 return -1;
138 return ret;
141 static int qio_channel_file_set_blocking(QIOChannel *ioc,
142 bool enabled,
143 Error **errp)
145 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
147 if (enabled) {
148 qemu_set_block(fioc->fd);
149 } else {
150 qemu_set_nonblock(fioc->fd);
152 return 0;
156 static off_t qio_channel_file_seek(QIOChannel *ioc,
157 off_t offset,
158 int whence,
159 Error **errp)
161 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
162 off_t ret;
164 ret = lseek(fioc->fd, offset, whence);
165 if (ret == (off_t)-1) {
166 error_setg_errno(errp, errno,
167 "Unable to seek to offset %lld whence %d in file",
168 (long long int)offset, whence);
169 return -1;
171 return ret;
175 static int qio_channel_file_close(QIOChannel *ioc,
176 Error **errp)
178 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
180 if (close(fioc->fd) < 0) {
181 error_setg_errno(errp, errno,
182 "Unable to close file");
183 return -1;
185 return 0;
189 static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc,
190 AioContext *ctx,
191 IOHandler *io_read,
192 IOHandler *io_write,
193 void *opaque)
195 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
196 aio_set_fd_handler(ctx, fioc->fd, false, io_read, io_write, NULL, opaque);
199 static GSource *qio_channel_file_create_watch(QIOChannel *ioc,
200 GIOCondition condition)
202 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
203 return qio_channel_create_fd_watch(ioc,
204 fioc->fd,
205 condition);
208 static void qio_channel_file_class_init(ObjectClass *klass,
209 void *class_data G_GNUC_UNUSED)
211 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
213 ioc_klass->io_writev = qio_channel_file_writev;
214 ioc_klass->io_readv = qio_channel_file_readv;
215 ioc_klass->io_set_blocking = qio_channel_file_set_blocking;
216 ioc_klass->io_seek = qio_channel_file_seek;
217 ioc_klass->io_close = qio_channel_file_close;
218 ioc_klass->io_create_watch = qio_channel_file_create_watch;
219 ioc_klass->io_set_aio_fd_handler = qio_channel_file_set_aio_fd_handler;
222 static const TypeInfo qio_channel_file_info = {
223 .parent = TYPE_QIO_CHANNEL,
224 .name = TYPE_QIO_CHANNEL_FILE,
225 .instance_size = sizeof(QIOChannelFile),
226 .instance_init = qio_channel_file_init,
227 .instance_finalize = qio_channel_file_finalize,
228 .class_init = qio_channel_file_class_init,
231 static void qio_channel_file_register_types(void)
233 type_register_static(&qio_channel_file_info);
236 type_init(qio_channel_file_register_types);