@@ -1698,13 +1698,42 @@ static const cmdinfo_t flush_cmd = {
1698
1698
.oneline = "flush all in-core file state to disk" ,
1699
1699
};
1700
1700
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
+
1701
1713
static int truncate_f (BlockBackend * blk , int argc , char * * argv )
1702
1714
{
1703
1715
Error * local_err = NULL ;
1704
1716
int64_t offset ;
1705
- int ret ;
1717
+ int c , ret ;
1718
+ PreallocMode prealloc = PREALLOC_MODE_OFF ;
1706
1719
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 ]);
1708
1737
if (offset < 0 ) {
1709
1738
print_cvtnum_err (offset , argv [1 ]);
1710
1739
return offset ;
@@ -1715,7 +1744,7 @@ static int truncate_f(BlockBackend *blk, int argc, char **argv)
1715
1744
* exact=true. It is better to err on the "emit more errors" side
1716
1745
* than to be overly permissive.
1717
1746
*/
1718
- ret = blk_truncate (blk , offset , false, PREALLOC_MODE_OFF , 0 , & local_err );
1747
+ ret = blk_truncate (blk , offset , false, prealloc , 0 , & local_err );
1719
1748
if (ret < 0 ) {
1720
1749
error_report_err (local_err );
1721
1750
return ret ;
@@ -1724,17 +1753,6 @@ static int truncate_f(BlockBackend *blk, int argc, char **argv)
1724
1753
return 0 ;
1725
1754
}
1726
1755
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
-
1738
1756
static int length_f (BlockBackend * blk , int argc , char * * argv )
1739
1757
{
1740
1758
int64_t size ;
0 commit comments