77using System . Text ;
88using System . Threading ;
99using System . Threading . Tasks ;
10+ using System . Windows . Forms ;
1011using log4net ;
1112
1213namespace AnyConvertVM
@@ -20,91 +21,117 @@ class ConvertClassActions
2021
2122 public enum FormatType { VDI , VHDX , RAW , QCOW2 , VMDK , VHD , QED } ;
2223
23- public static void ConvertQEMU ( FormatType fromFormat , FormatType toFormat , String FromFolderWithFile , String FileName , String SaveFolderPath )
24+ public static bool ErrorFlag = false ;
25+ public static int ConvertQEMU ( FormatType fromFormat , FormatType toFormat , String FromFolderWithFile , String FileName , String SaveFolderPath )
2426 {
2527
2628 try
2729 {
2830 //qemu-img convert -f raw -O qcow2 image.img image.qcow2
2931 // String args = null;
30- Console . WriteLine ( "Will start to convert " + fromFormat + " to " + toFormat ) ;
3132
32- //string fromFormatTextLower = fromFormat.ToString().ToLower();
33-
34- //Log.Info(TAG + "Trying to update MTU values :" + Settings.MTU);
33+ Log . Debug ( TAG + "Will start to convert " + fromFormat + " to " + toFormat ) ;
3534
3635 string fromFromatArgs = fromFormat . ToString ( ) . ToLower ( ) ;
3736 string toFromatArgs = toFormat . ToString ( ) . ToLower ( ) ;
37+
3838 if ( fromFromatArgs . Equals ( "vhd" ) ) { fromFromatArgs = "vpc" ; }
3939 if ( toFromatArgs . Equals ( "vhd" ) ) { toFromatArgs = "vpc" ; }
4040
4141 String Arguments = "/c Tools\\ qemu-img-win-x64-2_3_0\\ qemu-img.exe convert -f " + fromFromatArgs + " -O " + toFromatArgs
42- + " " + @FromFolderWithFile + " " + @SaveFolderPath + @"\" + FileName + "." + toFormat . ToString ( ) . ToLower ( ) ;
43- Console . WriteLine ( "ARGs: " + Arguments ) ;
42+ + " " + "\" " + FromFolderWithFile + "\" " + " " + "\" " + SaveFolderPath + @"\" + FileName + "." + toFormat . ToString ( ) . ToLower ( ) + "\" " ;
43+
44+ //Misc.Utils.StartProcess(Arguments,"Tools\\qemu-img-win-x64-2_3_0\\qemu-img.exe");
45+
46+ Log . Debug ( TAG + "Command: " + Arguments ) ;
47+ ErrorFlag = false ;
4448 var proc = new Process
4549 {
4650 StartInfo = new ProcessStartInfo
4751 {
4852 FileName = "cmd.exe" ,
4953 Arguments = Arguments ,
50- UseShellExecute = false ,
51- RedirectStandardOutput = false ,
52- CreateNoWindow = false
53-
5454 }
5555 } ;
56+
57+ proc . StartInfo . UseShellExecute = false ;
58+ proc . StartInfo . CreateNoWindow = true ;
59+ proc . EnableRaisingEvents = true ;
60+ proc . StartInfo . RedirectStandardOutput = true ;
61+ proc . StartInfo . RedirectStandardError = true ;
5662 proc . Start ( ) ;
63+
5764 var std_out_error_reader = proc . StandardError ;
5865 var std_out_data_reader = proc . StandardOutput ;
59- WriteToLog ( std_out_data_reader ) ;
60- WriteToLog ( std_out_error_reader ) ;
66+ WriteToLog ( std_out_data_reader , "d" ) ;
67+ WriteToLog ( std_out_error_reader , "e" ) ;
6168
62- Thread . Sleep ( 10000 ) ;
6369 proc . WaitForExit ( ) ;
70+
71+ if ( ErrorFlag )
72+ {
73+ Log . Error ( TAG + "Failed to convert the disks." ) ;
74+ return 1 ;
75+ }
76+ else
77+ {
78+ Log . Debug ( TAG + "Converted " + toFormat + " disk from " + fromFormat + ". Converted disk saved at :"
79+ + SaveFolderPath + @"\" + FileName + "." + toFormat . ToString ( ) . ToLower ( ) ) ;
80+
81+ MessageBox . Show ( "Converted " + toFormat + " disk from " + fromFormat + ".Converted disk saved at: "
82+ + SaveFolderPath + @"\" + FileName + "." + toFormat . ToString ( ) . ToLower ( ) ) ;
83+ return 0 ;
84+ }
85+
6486 }
6587 catch ( Exception ex )
6688 {
67-
89+ Misc . Utils . ExceptionHandleMsg ( TAG , "Could not convert the image." , ex ) ;
90+ Misc . Utils . ErrorBox ( "Could not convert the image." , ex ) ;
91+ return 1 ;
6892 }
6993
7094 }
7195
72- public static void WriteToLog ( StreamReader std_out_reader )
96+
97+ public static void WriteToLog ( StreamReader std_out_reader , string level )
7398 {
99+ try
100+ {
101+ while ( ! std_out_reader . EndOfStream )
102+ {
103+ // the point is that the stream does not end until the process has
104+ // finished all of its output.
105+ var nextLine = std_out_reader . ReadLine ( ) ;
106+ string errorMsg = null ;
107+ if ( level . Equals ( "e" ) )
108+ {
109+ Log . Error ( TAG + nextLine ) ;
110+ ErrorFlag = true ;
111+ //Misc.Utils.ErrorBox("Failed to convert the disks.");
112+ if ( ( nextLine . Contains ( "bad signature" ) ) || ( nextLine . Contains ( "Image not in" ) ) )
113+ {
114+ errorMsg = "The selected disk is not in the correct format." ;
115+ }
116+ else
117+ errorMsg = "Failed to convert the disks" ;
118+
119+ Misc . Utils . ErrorBox ( errorMsg + "\n Error message: " + nextLine ) ;
120+ }
121+ else
122+ Log . Debug ( TAG + nextLine ) ;
74123
75- while ( ! std_out_reader . EndOfStream )
124+ }
125+ }
126+ catch ( Exception ex )
76127 {
77- // the point is that the stream does not end until the process has
78- // finished all of its output.
79- var nextLine = std_out_reader . ReadLine ( ) ;
80-
81- Console . WriteLine ( nextLine ) ;
82- //if (level.Equals("e"))
83- //{
84- // Log.Error(TAG + nextLine);
85- //}
86- //else Log.Debug(TAG + nextLine);
87-
88- //if (isVpnError(nextLine))
89- //{
90- // Connection.UeAgent.Client.onIpsecError(nextLine);
91- //}
92- //else
93- //{
94- // if (nextLine.Contains(OpenVPN_Parsing.VPN_CONNECTED))
95- // {
96- // Connection.UeAgent.Client.onIpsecConnected();
97- // Thread.Sleep(3000);
98- // }
99- //}
100-
101-
102-
103-
104- //CreateFile failed on TAP device:
128+ Misc . Utils . ExceptionHandleMsg ( TAG , "Failed to write to Log" , ex ) ;
129+ //Misc.Utils.ErrorBox("Could not convert the image.", ex);
105130 }
106- // Log.Debug(TAG+ line);
131+
107132 }
133+
134+
108135 }
109136
110137}
0 commit comments