4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "qemu/coroutine.h"
26 #include "migration/qemu-file.h"
28 typedef struct QEMUFileStdio
{
33 static int stdio_get_fd(void *opaque
)
35 QEMUFileStdio
*s
= opaque
;
37 return fileno(s
->stdio_file
);
40 static ssize_t
stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
,
43 QEMUFileStdio
*s
= opaque
;
46 res
= fwrite(buf
, 1, size
, s
->stdio_file
);
54 static ssize_t
stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
,
57 QEMUFileStdio
*s
= opaque
;
58 FILE *fp
= s
->stdio_file
;
63 bytes
= fread(buf
, 1, size
, fp
);
64 if (bytes
!= 0 || !ferror(fp
)) {
67 if (errno
== EAGAIN
) {
68 yield_until_fd_readable(fileno(fp
));
69 } else if (errno
!= EINTR
) {
76 static int stdio_pclose(void *opaque
)
78 QEMUFileStdio
*s
= opaque
;
80 ret
= pclose(s
->stdio_file
);
83 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
84 /* close succeeded, but non-zero exit code: */
85 ret
= -EIO
; /* fake errno value */
91 static int stdio_fclose(void *opaque
)
93 QEMUFileStdio
*s
= opaque
;
96 if (qemu_file_is_writable(s
->file
)) {
97 int fd
= fileno(s
->stdio_file
);
100 ret
= fstat(fd
, &st
);
101 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
103 * If the file handle is a regular file make sure the
104 * data is flushed to disk before signaling success.
113 if (fclose(s
->stdio_file
) == EOF
) {
120 static const QEMUFileOps stdio_pipe_read_ops
= {
121 .get_fd
= stdio_get_fd
,
122 .get_buffer
= stdio_get_buffer
,
123 .close
= stdio_pclose
126 static const QEMUFileOps stdio_pipe_write_ops
= {
127 .get_fd
= stdio_get_fd
,
128 .put_buffer
= stdio_put_buffer
,
129 .close
= stdio_pclose
132 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
137 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
138 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
142 stdio_file
= popen(command
, mode
);
143 if (stdio_file
== NULL
) {
147 s
= g_new0(QEMUFileStdio
, 1);
149 s
->stdio_file
= stdio_file
;
151 if (mode
[0] == 'r') {
152 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
154 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
159 static const QEMUFileOps stdio_file_read_ops
= {
160 .get_fd
= stdio_get_fd
,
161 .get_buffer
= stdio_get_buffer
,
162 .close
= stdio_fclose
165 static const QEMUFileOps stdio_file_write_ops
= {
166 .get_fd
= stdio_get_fd
,
167 .put_buffer
= stdio_put_buffer
,
168 .close
= stdio_fclose
171 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
175 if (qemu_file_mode_is_not_valid(mode
)) {
179 s
= g_new0(QEMUFileStdio
, 1);
181 s
->stdio_file
= fopen(filename
, mode
);
182 if (!s
->stdio_file
) {
186 if (mode
[0] == 'w') {
187 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
189 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);