Skip to content

Commit 42ba022

Browse files
Vladimir Sementsov-OgievskiyXanClic
Vladimir Sementsov-Ogievskiy
authored andcommitted
qemu-io: add preallocate mode parameter for truncate command
This will be used in further test. Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> Reviewed-by: Max Reitz <[email protected]> Message-Id: <[email protected]> Signed-off-by: Max Reitz <[email protected]>
1 parent 33fa222 commit 42ba022

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

qemu-io-cmds.c

+32-14
Original file line numberDiff line numberDiff line change
@@ -1698,13 +1698,42 @@ static const cmdinfo_t flush_cmd = {
16981698
.oneline = "flush all in-core file state to disk",
16991699
};
17001700

1701+
static int truncate_f(BlockBackend *blk, int argc, char **argv);
1702+
static const cmdinfo_t truncate_cmd = {
1703+
.name = "truncate",
1704+
.altname = "t",
1705+
.cfunc = truncate_f,
1706+
.perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1707+
.argmin = 1,
1708+
.argmax = 3,
1709+
.args = "[-m prealloc_mode] off",
1710+
.oneline = "truncates the current file at the given offset",
1711+
};
1712+
17011713
static int truncate_f(BlockBackend *blk, int argc, char **argv)
17021714
{
17031715
Error *local_err = NULL;
17041716
int64_t offset;
1705-
int ret;
1717+
int c, ret;
1718+
PreallocMode prealloc = PREALLOC_MODE_OFF;
17061719

1707-
offset = cvtnum(argv[1]);
1720+
while ((c = getopt(argc, argv, "m:")) != -1) {
1721+
switch (c) {
1722+
case 'm':
1723+
prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
1724+
PREALLOC_MODE__MAX, NULL);
1725+
if (prealloc == PREALLOC_MODE__MAX) {
1726+
error_report("Invalid preallocation mode '%s'", optarg);
1727+
return -EINVAL;
1728+
}
1729+
break;
1730+
default:
1731+
qemuio_command_usage(&truncate_cmd);
1732+
return -EINVAL;
1733+
}
1734+
}
1735+
1736+
offset = cvtnum(argv[optind]);
17081737
if (offset < 0) {
17091738
print_cvtnum_err(offset, argv[1]);
17101739
return offset;
@@ -1715,7 +1744,7 @@ static int truncate_f(BlockBackend *blk, int argc, char **argv)
17151744
* exact=true. It is better to err on the "emit more errors" side
17161745
* than to be overly permissive.
17171746
*/
1718-
ret = blk_truncate(blk, offset, false, PREALLOC_MODE_OFF, 0, &local_err);
1747+
ret = blk_truncate(blk, offset, false, prealloc, 0, &local_err);
17191748
if (ret < 0) {
17201749
error_report_err(local_err);
17211750
return ret;
@@ -1724,17 +1753,6 @@ static int truncate_f(BlockBackend *blk, int argc, char **argv)
17241753
return 0;
17251754
}
17261755

1727-
static const cmdinfo_t truncate_cmd = {
1728-
.name = "truncate",
1729-
.altname = "t",
1730-
.cfunc = truncate_f,
1731-
.perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1732-
.argmin = 1,
1733-
.argmax = 1,
1734-
.args = "off",
1735-
.oneline = "truncates the current file at the given offset",
1736-
};
1737-
17381756
static int length_f(BlockBackend *blk, int argc, char **argv)
17391757
{
17401758
int64_t size;

0 commit comments

Comments
 (0)