Environment
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace Aspera.Transfer
{
public class Environment
{
private static string faspScpPathDiscovered = findAscp();
private static string faspScpPathProvided = "";
private static string portFilePath = "";
private static int mgmt_port = 0;
public static string getFaspScpPath()
{
if (faspScpPathProvided.Length > 0)
return faspScpPathProvided;
return faspScpPathDiscovered;
}
public static Version getFaspVersion()
{
string faspScpPath = getFaspScpPath();
if (faspScpPath.Length == 0)
throw new FaspManagerException("ascp.exe not found");
ProcessStartInfo processStartInfo = new ProcessStartInfo(faspScpPath, "-A");
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
Process process = Process.Start(processStartInfo);
StreamReader standardOutput = process.StandardOutput;
string input = standardOutput.ReadToEnd();
string pattern = ".*ascp version\\s([0-9\\.])+.*";
Match match = Regex.Match(input, pattern);
Group group = match.Groups[1];
string[] array = new string[4] {
"",
"",
"",
""
};
int num = 0;
foreach (Capture capture in group.Captures) {
if (capture.Value == ".")
num++;
else {
string[] array2;
string[] array3 = array2 = array;
int num2 = num;
IntPtr intPtr = (IntPtr)num2;
array3[num2] = array2[(long)intPtr] + capture.Value;
}
if (num == 4)
break;
}
return new Version(int.Parse(array[0]), int.Parse(array[1]), int.Parse(array[2]), int.Parse(array[3]));
}
private static string findAscp()
{
string ascpName = "ascp.exe";
Process currentProcess = Process.GetCurrentProcess();
string directoryName = Path.GetDirectoryName(currentProcess.MainModule.FileName);
string text = searchSysPath(ascpName);
if (text != null && text.Length > 0)
return text;
string text2 = searchAround(directoryName, ascpName);
if (text2 != null && text2.Length > 0)
return text2;
return "";
}
private static string FindInPath(string pathName, string ascp)
{
try {
if (Path.GetFullPath(pathName) != null) {
string text = Path.Combine(pathName, ascp);
if (File.Exists(text))
return text;
}
} catch (Exception ex) {
Logger.Log(TraceEventType.Warning, "Error combining [" + pathName + "] and [" + ascp + "] : " + ex.Message);
}
return null;
}
private static string searchSysPath(string ascpName)
{
string[] array = System.Environment.GetEnvironmentVariable("PATH").Split(new char[1] {
Path.PathSeparator
});
string[] array2 = array;
foreach (string pathName in array2) {
string text = FindInPath(pathName, ascpName);
if (text != null)
return text;
}
return null;
}
private static string searchAround(string sDir, string ascpName)
{
string text = FindInPath(sDir, ascpName);
if (text != null)
return text;
string[] directories = Directory.GetDirectories(sDir);
foreach (string pathName in directories) {
text = FindInPath(pathName, ascpName);
if (text != null)
return text;
}
string text2 = Directory.GetParent(sDir).ToString();
text = FindInPath(text2, ascpName);
if (text != null)
return text;
string[] directories2 = Directory.GetDirectories(text2);
foreach (string pathName2 in directories2) {
text = FindInPath(pathName2, ascpName);
if (text != null)
return text;
}
return null;
}
public static string getPortFilePath()
{
if (portFilePath.Length > 0)
return portFilePath;
Process currentProcess = Process.GetCurrentProcess();
string path = currentProcess.ProcessName + "." + System.Environment.UserName + "." + currentProcess.Id + ".optport";
string path2 = Path.Combine(getFaspScpPath(), "..\\..\\var\\run\\aspera");
path2 = Path.Combine(path2, path);
path2 = (portFilePath = Path.GetFullPath(path2));
return portFilePath;
}
public static void setFaspScpPath(string faspScpPath)
{
faspScpPathProvided = faspScpPath;
}
public static void setManagementPort(int mgmtPort)
{
mgmt_port = mgmtPort;
}
public static int getManagementPort()
{
return mgmt_port;
}
}
}