static int ssh_create(const char *filename, QEMUOptionParameter *options) { int r, ret; Error *local_err = NULL; int64_t total_size = 0; QDict *uri_options = NULL; BDRVSSHState s; ssize_t r2; char c[1] = { '\0' }; ssh_state_init(&s); /* Get desired file size. */ while (options && options->name) { if (!strcmp(options->name, BLOCK_OPT_SIZE)) { total_size = options->value.n; } options++; } DPRINTF("total_size=%" PRIi64, total_size); uri_options = qdict_new(); r = parse_uri(filename, uri_options, &local_err); if (r < 0) { qerror_report_err(local_err); error_free(local_err); ret = r; goto out; } r = connect_to_ssh(&s, uri_options, LIBSSH2_FXF_READ|LIBSSH2_FXF_WRITE| LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, 0644); if (r < 0) { ret = r; goto out; } if (total_size > 0) { libssh2_sftp_seek64(s.sftp_handle, total_size-1); r2 = libssh2_sftp_write(s.sftp_handle, c, 1); if (r2 < 0) { sftp_error_report(&s, "truncate failed"); ret = -EINVAL; goto out; } s.attrs.filesize = total_size; } ret = 0; out: ssh_state_free(&s); if (uri_options != NULL) { QDECREF(uri_options); } return ret; }
static int ssh_create(const char *filename, QemuOpts *opts, Error **errp) { int r, ret; int64_t total_size = 0; QDict *uri_options = NULL; BDRVSSHState s; ssize_t r2; char c[1] = { '\0' }; ssh_state_init(&s); /* Get desired file size. */ total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), BDRV_SECTOR_SIZE); DPRINTF("total_size=%" PRIi64, total_size); uri_options = qdict_new(); r = parse_uri(filename, uri_options, errp); if (r < 0) { ret = r; goto out; } r = connect_to_ssh(&s, uri_options, LIBSSH2_FXF_READ|LIBSSH2_FXF_WRITE| LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, 0644, errp); if (r < 0) { ret = r; goto out; } if (total_size > 0) { libssh2_sftp_seek64(s.sftp_handle, total_size-1); r2 = libssh2_sftp_write(s.sftp_handle, c, 1); if (r2 < 0) { sftp_error_setg(errp, &s, "truncate failed"); ret = -EINVAL; goto out; } s.attrs.filesize = total_size; } ret = 0; out: ssh_state_free(&s); if (uri_options != NULL) { QDECREF(uri_options); } return ret; }
static void ssh_close(BlockDriverState *bs) { BDRVSSHState *s = bs->opaque; ssh_state_free(s); }