-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConvertClassActions.cs
137 lines (110 loc) · 4.94 KB
/
ConvertClassActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using log4net;
namespace AnyConvertVM
{
class ConvertClassActions
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private const string TAG = "ConvertClassActions: ";
public enum FormatType { VDI,VHDX,RAW,QCOW2,VMDK,VHD,QED };
public static bool ErrorFlag = false;
public static int ConvertQEMU(FormatType fromFormat, FormatType toFormat, String FromFolderWithFile, String FileName,String SaveFolderPath)
{
try
{
//qemu-img convert -f raw -O qcow2 image.img image.qcow2
// String args = null;
Log.Debug(TAG+"Will start to convert " + fromFormat + " to " + toFormat);
string fromFromatArgs = fromFormat.ToString().ToLower();
string toFromatArgs = toFormat.ToString().ToLower();
if (fromFromatArgs.Equals("vhd")) { fromFromatArgs = "vpc"; }
if (toFromatArgs.Equals("vhd")) { toFromatArgs = "vpc"; }
String Arguments = "/c Tools\\qemu-img-win-x64-2_3_0\\qemu-img.exe convert -f " + fromFromatArgs + " -O " + toFromatArgs
+ " " +"\""+ FromFolderWithFile +"\"" + " " + "\"" + SaveFolderPath+@"\"+FileName+"."+ toFormat.ToString().ToLower() + "\"";
//Misc.Utils.StartProcess(Arguments,"Tools\\qemu-img-win-x64-2_3_0\\qemu-img.exe");
Log.Debug(TAG+"Command: " + Arguments);
ErrorFlag = false;
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = Arguments,
}
};
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.EnableRaisingEvents = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
var std_out_error_reader = proc.StandardError;
var std_out_data_reader = proc.StandardOutput;
WriteToLog(std_out_data_reader, "d");
WriteToLog(std_out_error_reader, "e");
proc.WaitForExit();
if (ErrorFlag)
{
Log.Error(TAG+ "Failed to convert the disks.");
return 1;
}
else
{
Log.Debug(TAG+ "Converted "+toFormat +" disk from "+fromFormat+". Converted disk saved at :"
+SaveFolderPath + @"\" + FileName + "." + toFormat.ToString().ToLower());
MessageBox.Show("Converted "+toFormat +" disk from "+fromFormat+".Converted disk saved at: "
+ SaveFolderPath + @"\" + FileName + "." + toFormat.ToString().ToLower());
return 0;
}
}
catch (Exception ex)
{
Misc.Utils.ExceptionHandleMsg(TAG, "Could not convert the image.", ex);
Misc.Utils.ErrorBox("Could not convert the image.", ex);
return 1;
}
}
public static void WriteToLog(StreamReader std_out_reader, string level)
{
try
{
while (!std_out_reader.EndOfStream)
{
// the point is that the stream does not end until the process has
// finished all of its output.
var nextLine = std_out_reader.ReadLine();
string errorMsg = null;
if (level.Equals("e"))
{
Log.Error(TAG + nextLine);
ErrorFlag = true;
//Misc.Utils.ErrorBox("Failed to convert the disks.");
if ((nextLine.Contains("bad signature")) || (nextLine.Contains("Image not in")))
{
errorMsg = "The selected disk is not in the correct format.";
}
else
errorMsg = "Failed to convert the disks";
Misc.Utils.ErrorBox(errorMsg+"\nError message: " + nextLine);
}
else
Log.Debug(TAG + nextLine);
}
}
catch (Exception ex)
{
Misc.Utils.ExceptionHandleMsg(TAG, "Failed to write to Log", ex);
//Misc.Utils.ErrorBox("Could not convert the image.", ex);
}
}
}
}