66
77use camino:: { Utf8Path , Utf8PathBuf } ;
88use clap:: { Parser , ValueEnum } ;
9- use color_eyre:: eyre;
9+ use color_eyre:: eyre:: { self , eyre } ;
1010use color_eyre:: { eyre:: Context , Result } ;
1111use std:: fs;
12+ use std:: io:: Write ;
1213use std:: str:: FromStr ;
1314use tracing:: { debug, info} ;
1415
@@ -633,8 +634,13 @@ fn ensure_default_pool(connect_uri: Option<&str>) -> Result<()> {
633634 ) ;
634635
635636 // Write XML to temporary file
636- let xml_path = "/tmp/default-pool.xml" ;
637- std:: fs:: write ( xml_path, & pool_xml) . with_context ( || "Failed to write pool XML" ) ?;
637+ let mut tmpf = tempfile:: NamedTempFile :: with_prefix ( "bcvk-libvirt" ) ?;
638+ tmpf. write_all ( pool_xml. as_bytes ( ) )
639+ . with_context ( || "Failed to write pool XML" ) ?;
640+ let xml_path = tmpf
641+ . path ( )
642+ . to_str ( )
643+ . ok_or_else ( || eyre ! ( "Invalid UTF-8 in tempfile" ) ) ?;
638644
639645 // Define the pool
640646 let mut cmd = virsh_command ( connect_uri) ?;
@@ -662,7 +668,6 @@ fn ensure_default_pool(connect_uri: Option<&str>) -> Result<()> {
662668
663669 if !output. status . success ( ) {
664670 let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
665- let _ = std:: fs:: remove_file ( xml_path) ;
666671 return Err ( color_eyre:: eyre:: eyre!(
667672 "Failed to start default pool: {}" ,
668673 stderr
@@ -674,9 +679,6 @@ fn ensure_default_pool(connect_uri: Option<&str>) -> Result<()> {
674679 cmd. args ( & [ "pool-autostart" , "default" ] ) ;
675680 let _ = cmd. output ( ) ; // Not critical if this fails
676681
677- // Clean up temporary XML file
678- let _ = std:: fs:: remove_file ( xml_path) ;
679-
680682 info ! ( "Default storage pool created successfully" ) ;
681683 Ok ( ( ) )
682684}
@@ -1296,8 +1298,15 @@ fn create_libvirt_domain_from_disk(
12961298 . with_context ( || "Failed to build domain XML" ) ?;
12971299
12981300 // Write XML to temporary file
1299- let xml_path = format ! ( "/tmp/{}.xml" , domain_name) ;
1300- std:: fs:: write ( & xml_path, domain_xml) . with_context ( || "Failed to write domain XML" ) ?;
1301+ let mut tmp_domain_file = tempfile:: NamedTempFile :: with_prefix ( "bcvk-libvirt" ) ?;
1302+ tmp_domain_file
1303+ . as_file_mut ( )
1304+ . write_all ( domain_xml. as_bytes ( ) )
1305+ . with_context ( || "Failed to write domain XML" ) ?;
1306+ let xml_path = tmp_domain_file
1307+ . path ( )
1308+ . to_str ( )
1309+ . ok_or_else ( || eyre ! ( "Invalid UTF-8 in tempfile" ) ) ?;
13011310
13021311 let connect_uri = global_opts. connect . as_deref ( ) ;
13031312
@@ -1323,8 +1332,5 @@ fn create_libvirt_domain_from_disk(
13231332 ) ?;
13241333 }
13251334
1326- // Clean up temporary XML file
1327- let _ = std:: fs:: remove_file ( & xml_path) ;
1328-
13291335 Ok ( ( ) )
13301336}
0 commit comments