* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
+
#include "qemu/osdep.h"
#include <getopt.h>
#include <libgen.h>
+#ifndef _WIN32
+#include <termios.h>
+#endif
#include "qapi/error.h"
#include "qemu-io.h"
#include "qemu/readline.h"
#include "qemu/log.h"
#include "qapi/qmp/qstring.h"
-#include "qapi/qmp/qbool.h"
+#include "qapi/qmp/qdict.h"
#include "qom/object_interfaces.h"
#include "sysemu/block-backend.h"
#include "block/block_int.h"
static ReadLineState *readline_state;
+static int ttyEOF;
+
+static int get_eof_char(void)
+{
+#ifdef _WIN32
+ return 0x4; /* Ctrl-D */
+#else
+ struct termios tty;
+ if (tcgetattr(STDIN_FILENO, &tty) != 0) {
+ if (errno == ENOTTY) {
+ return 0x0; /* just expect read() == 0 */
+ } else {
+ return 0x4; /* Ctrl-D */
+ }
+ }
+
+ return tty.c_cc[VEOF];
+#endif
+}
+
static int close_f(BlockBackend *blk, int argc, char **argv)
{
blk_unref(qemuio_blk);
" Opens a file for subsequent use by all of the other qemu-io commands.\n"
" -r, -- open file read-only\n"
" -s, -- use snapshot file\n"
+" -C, -- use copy-on-read\n"
" -n, -- disable host cache, short for -t none\n"
" -U, -- force shared permissions\n"
" -k, -- use kernel AIO implementation (on Linux only)\n"
.argmin = 1,
.argmax = -1,
.flags = CMD_NOFILE_OK,
- .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]",
+ .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
.oneline = "open the file specified by path",
.help = open_help,
};
QDict *opts;
bool force_share = false;
- while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) {
+ while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
switch (c) {
case 's':
flags |= BDRV_O_SNAPSHOT;
flags |= BDRV_O_NOCACHE;
writethrough = false;
break;
+ case 'C':
+ flags |= BDRV_O_COPY_ON_READ;
+ break;
case 'r':
readonly = 1;
break;
" -r, --read-only export read-only\n"
" -s, --snapshot use snapshot file\n"
" -n, --nocache disable host cache, short for -t none\n"
+" -C, --copy-on-read enable copy-on-read\n"
" -m, --misalign misalign allocations for O_DIRECT\n"
" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
" -t, --cache=MODE use the given cache mode for the image\n"
readline_start(readline_state, get_prompt(), 0, readline_func, &line);
while (!line) {
int ch = getchar();
- if (ch == EOF) {
+ if (ttyEOF != 0x0 && ch == ttyEOF) {
+ printf("\n");
break;
}
readline_handle_byte(readline_state, ch);
int main(int argc, char **argv)
{
int readonly = 0;
- const char *sopt = "hVc:d:f:rsnmkt:T:U";
+ const char *sopt = "hVc:d:f:rsnCmkt:T:U";
const struct option lopt[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "read-only", no_argument, NULL, 'r' },
{ "snapshot", no_argument, NULL, 's' },
{ "nocache", no_argument, NULL, 'n' },
+ { "copy-on-read", no_argument, NULL, 'C' },
{ "misalign", no_argument, NULL, 'm' },
{ "native-aio", no_argument, NULL, 'k' },
{ "discard", required_argument, NULL, 'd' },
#endif
module_call_init(MODULE_INIT_TRACE);
- progname = basename(argv[0]);
+ progname = g_path_get_basename(argv[0]);
qemu_init_exec_dir(argv[0]);
qcrypto_init(&error_fatal);
flags |= BDRV_O_NOCACHE;
writethrough = false;
break;
+ case 'C':
+ flags |= BDRV_O_COPY_ON_READ;
+ break;
case 'd':
if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
error_report("Invalid discard option: %s", optarg);
trace_file = trace_opt_parse(optarg);
break;
case 'V':
- printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
+ printf("%s version " QEMU_FULL_VERSION "\n"
QEMU_COPYRIGHT "\n", progname);
exit(0);
case 'h':
qemuio_add_command(&close_cmd);
if (isatty(STDIN_FILENO)) {
+ ttyEOF = get_eof_char();
readline_state = readline_init(readline_printf_func,
readline_flush_func,
NULL,