1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "qapi/error.h"
26#include "qemu-common.h"
27#include "chardev/char.h"
28
29#ifdef _WIN32
30#include "chardev/char-win.h"
31#else
32#include "chardev/char-fd.h"
33#endif
34
35static void qmp_chardev_open_file(Chardev *chr,
36 ChardevBackend *backend,
37 bool *be_opened,
38 Error **errp)
39{
40 ChardevFile *file = backend->u.file.data;
41#ifdef _WIN32
42 HANDLE out;
43 DWORD accessmode;
44 DWORD flags;
45
46 if (file->has_in) {
47 error_setg(errp, "input file not supported");
48 return;
49 }
50
51 if (file->has_append && file->append) {
52
53 accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
54 flags = OPEN_ALWAYS;
55 } else {
56
57 accessmode = GENERIC_WRITE;
58 flags = CREATE_ALWAYS;
59 }
60
61 out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
62 FILE_ATTRIBUTE_NORMAL, NULL);
63 if (out == INVALID_HANDLE_VALUE) {
64 error_setg(errp, "open %s failed", file->out);
65 return;
66 }
67
68 win_chr_set_file(chr, out, false);
69#else
70 int flags, in = -1, out;
71
72 flags = O_WRONLY | O_CREAT | O_BINARY;
73 if (file->has_append && file->append) {
74 flags |= O_APPEND;
75 } else {
76 flags |= O_TRUNC;
77 }
78
79 out = qmp_chardev_open_file_source(file->out, flags, errp);
80 if (out < 0) {
81 return;
82 }
83
84 if (file->has_in) {
85 flags = O_RDONLY;
86 in = qmp_chardev_open_file_source(file->in, flags, errp);
87 if (in < 0) {
88 qemu_close(out);
89 return;
90 }
91 }
92
93 qemu_chr_open_fd(chr, in, out);
94#endif
95}
96
97static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
98 Error **errp)
99{
100 const char *path = qemu_opt_get(opts, "path");
101 ChardevFile *file;
102
103 backend->type = CHARDEV_BACKEND_KIND_FILE;
104 if (path == NULL) {
105 error_setg(errp, "chardev: file: no filename given");
106 return;
107 }
108 file = backend->u.file.data = g_new0(ChardevFile, 1);
109 qemu_chr_parse_common(opts, qapi_ChardevFile_base(file));
110 file->out = g_strdup(path);
111
112 file->has_append = true;
113 file->append = qemu_opt_get_bool(opts, "append", false);
114}
115
116static void char_file_class_init(ObjectClass *oc, void *data)
117{
118 ChardevClass *cc = CHARDEV_CLASS(oc);
119
120 cc->parse = qemu_chr_parse_file_out;
121 cc->open = qmp_chardev_open_file;
122}
123
124static const TypeInfo char_file_type_info = {
125 .name = TYPE_CHARDEV_FILE,
126#ifdef _WIN32
127 .parent = TYPE_CHARDEV_WIN,
128#else
129 .parent = TYPE_CHARDEV_FD,
130#endif
131 .class_init = char_file_class_init,
132};
133
134static void register_types(void)
135{
136 type_register_static(&char_file_type_info);
137}
138
139type_init(register_types);
140