using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Xml; // ReSharper disable UnusedMember.Local // ReSharper disable UnusedVariable namespace DTS.Common.Utils { [SuppressMessage("ReSharper", "InconsistentNaming")] public static class FileUtils { [Flags] enum MoveFileFlags { MOVEFILE_REPLACE_EXISTING = 0x00000001, MOVEFILE_COPY_ALLOWED = 0x00000002, MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004, MOVEFILE_WRITE_THROUGH = 0x00000008, MOVEFILE_CREATE_HARDLINK = 0x00000010, MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020 } [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool DeleteFile(string lpFileName); public const double DataPROPre20XmlVersion = 2.0D; public const double DataPRO20XmlVersion = 3.0D; public const double DataPRO21XmlVersion = 4.0D; public const double DataPRO22XmlVersion = 5.0D; //FB 13120 Added FilterClass and deleted CFC in version 6.0 //15390 Store the ZMO in EU in the DataPRO DB in version 7.0, adds top level field SensorChangeHistory //13065 Sensor "First Use" Date, version 8, adds LatestCalibrationId, CalibrationId and FirstUse tags //15727Building and Replacing Racks/Mods - version 9, adds TestId,GroupId, StandIn to hardware and Id to tests //Version 10 adds TSR Air settings public const double CurrentXmlVersion = 10.0D; public static XmlElement GetImportXmlNode(string filename, string xmlDoc, out double importVersion) { var doc = new XmlDocument(); importVersion = 0D; if (!string.IsNullOrWhiteSpace(xmlDoc)) { doc.LoadXml(xmlDoc); } else { doc.Load(filename); } foreach (var node in doc.ChildNodes) { if (node is XmlDeclaration) continue; if (!(node is XmlElement)) continue; if ((node as XmlElement).Name != "ExportFile") continue; var itemsToComplete = Convert.ToDouble(((XmlElement) node).GetAttribute("TotalItems"), System.Globalization.CultureInfo.InvariantCulture); importVersion = Convert.ToDouble(((XmlElement) node).GetAttribute("Version"), System.Globalization.CultureInfo.InvariantCulture); if (importVersion > CurrentXmlVersion) { throw new NotSupportedException("Unsupported version: " + importVersion); } return (XmlElement) node; } throw new Exception("Invalid Import XML"); } /// /// This API can accept a parameter “MOVEFILE_DELAY_UNTIL_REBOOT ” to remove the file only after reboot. /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx /// /// Entire path of the file we want to move public static void moveFileEx(string filePath) { MoveFileEx(filePath, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT); } /// /// FB16400: ISO exports error in 3rd Party Software, related to FB15801 /// By default, C# returns UTF-8 Encoding with Byte-Order Mark turned on. Need to explicitly call the overloaded constructor to turn it off: /// https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding.utf8?view=netcore-3.1 /// /// /// public static Encoding GetEncoding(int codepage) { var encoding = Encoding.GetEncoding(codepage); if (encoding is UTF8Encoding) { encoding = new UTF8Encoding(false); } return encoding; } public static XmlWriter GetExportWriter(int count, double version, string software, string softwareVersion, LogDelegate logDelegate, out StringBuilder sb) { sb = new StringBuilder(5000000); var xSet = new XmlWriterSettings { Indent = true, CheckCharacters = true }; var writer = XmlWriter.Create(sb, xSet); writer.WriteStartDocument(); writer.WriteStartElement("ExportFile"); writer.WriteAttributeString("TotalItems", count.ToString(System.Globalization.CultureInfo.InvariantCulture)); writer.WriteAttributeString("Version", version.ToString(System.Globalization.CultureInfo.InvariantCulture)); writer.WriteAttributeString("Software", software); writer.WriteAttributeString("SoftwareVersion", softwareVersion); return writer; } /// /// This API deletes an existing file. /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx /// /// Entire path of the file to be deleted. /// Error if failed to delete file /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero (0). /// To get extended error information, call GetLastError. public static bool deleteFile(string filePath, ref int lastError) { if (!File.Exists(filePath)) return true; var deleted = DeleteFile(filePath); if (!deleted) { lastError = Marshal.GetLastWin32Error(); } return deleted; } public delegate void LogDelegate(params object[] paramlist); public static void DeleteFileOrMove(string filepath, LogDelegate logfunction) { var lastError = 0; if (!deleteFile(filepath, ref lastError)) { logfunction("failed to delete file: ", filepath, " error: ", lastError); moveFileEx(filepath); } } /// /// One time sql script conversion from SQLite syntax to MS SQL Server /// all you need to know about Regex is here - http://rubular.com/r/kcqDdLJBpx /// /// SQLite script file path /// public static string ScriptFromSQLiteToSQL(string script) { if (string.IsNullOrEmpty(script)) return script; const string BLOB = "BLOB"; const string NVCHAR = "NVCHAR"; const string NVARCHAR = "NVARCHAR"; const string COLLATE_NOCASE = "COLLATE NOCASE"; const string Couldnt = "Couldn't"; const string Couldnot = "Could not"; const string IDENTITY = "IDENTITY(1,1)"; const string AUTOINCREMENT = "AUTOINCREMENT"; const string varbinary_max = "varbinary(max)"; const string varchar_max = "varchar(max)"; const string max = "(max)"; const string BigInt = "bigint"; const string Integer = "integer"; const string Int = "int"; const string _2048 = "(2048)"; const string _5000 = "(5000)"; var sql = new StringBuilder(); long count = 0; using (var inputStream = File.OpenRead(script)) { using (var inputReader = new StreamReader(inputStream)) { string tempLineValue; while (null != (tempLineValue = inputReader.ReadLine())) { Debug.Print("Line: " + count++); var addGo = false; const string dropTable = "DROP TABLE IF EXISTS"; const string dropTableNew = @"if exists(SELECT * FROM sysobjects where name = '{0}') DROP TABLE [dbo].[{0}];" ; const string insertInto = "INSERT INTO"; const string convert = "convert(varbinary(max), {0})"; if (tempLineValue.StartsWith(dropTable)) { var tableName = tempLineValue.Replace(dropTable, string.Empty).Replace("\"", string.Empty).Replace(";", string.Empty).Trim(); tempLineValue = string.Format(dropTableNew, tableName); addGo = true; } else if (tempLineValue.StartsWith(insertInto)) { //find SQLite (or C#) date (Format: '2016-06-06 15:15:38.0630927' ) const string patternSQLiteDate = @"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}).\d{3,7}"; var regexSQLiteDate = new Regex(patternSQLiteDate, RegexOptions.IgnoreCase); var matchSQLiteDate = regexSQLiteDate.Match(tempLineValue); while (matchSQLiteDate.Success) { var foundSQLiteDate = matchSQLiteDate.Value; //find SQL date in SQLite date (Format: '2016-06-06 15:15:38.063' ) const string patternSQLDate = @"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})"; var rDate = new Regex(patternSQLDate, RegexOptions.IgnoreCase); var mDate = rDate.Match(foundSQLiteDate); if (mDate.Success) { //replace date tempLineValue = tempLineValue.Replace(foundSQLiteDate, mDate.Value); } matchSQLiteDate = matchSQLiteDate.NextMatch(); } const string patternBinary = @"[X]'\w+'"; var regexBinary = new Regex(patternBinary, RegexOptions.IgnoreCase); var matchBinary = regexBinary.Match(tempLineValue); while (matchBinary.Success) { var matchValue = matchBinary.Value; var newValue = string.Format(convert, matchValue.Replace("X", string.Empty).Replace(",", string.Empty)); tempLineValue = tempLineValue.Replace(matchValue, newValue); matchBinary = matchBinary.NextMatch(); } addGo = true; } else { const string patternText = @"text\s{0,}\(\w+\)"; var regexText = new Regex(patternText, RegexOptions.IgnoreCase); var matchText = regexText.Match(tempLineValue); if (matchText.Success) { tempLineValue = tempLineValue.Replace(matchText.Value, varchar_max); } const string patternMaxMemory = @"\[MaxMemory\]\sinteger,"; var regexMaxMemory = new Regex(patternMaxMemory, RegexOptions.IgnoreCase); var matchMaxMemory = regexMaxMemory.Match(tempLineValue); if (matchMaxMemory.Success) { tempLineValue = tempLineValue.Replace(Integer, BigInt); } } sql.AppendLine(tempLineValue); if (addGo) sql.AppendLine("GO"); } } } return sql .Replace(AUTOINCREMENT, string.Empty) .Replace(Couldnt, Couldnot) .Replace(_2048, max) .Replace(_5000, max) .Replace(Integer, Int) .Replace(COLLATE_NOCASE, string.Empty) .Replace(BLOB, varbinary_max) .Replace(BLOB.ToLower(), varbinary_max) .Replace(NVCHAR, NVARCHAR) .Replace(NVCHAR.ToLower(), NVARCHAR).ToString(); } #region File List private static List _fileList = new List(); public static List FileList { get => _fileList; set => _fileList = value; } private static List _newFileList = new List(); public static List NewFileList { get => _newFileList; set => _newFileList = value; } /// /// recursively search directory and subdirectories and return a list of files /// /// /// *.dts /// public static void FindFiles(string path, string pattern) { _fileList.AddRange(FindFiles(path, pattern, SearchOption.AllDirectories)); } /// /// Search directory and return a list of files /// /// /// *.dts /// public static List FindFilesInDirectory(string path, string pattern) { return FindFiles(path, pattern, SearchOption.TopDirectoryOnly); } private static List FindFiles(string path, string pattern, SearchOption searchOption) { return Directory.GetFiles(path, "*" + pattern, searchOption). Select(fn => new FileInfo(fn)). OrderBy(f => Regex.Replace(f.Name, @"\d+", n => n.Value.PadLeft(4, '0'))). Select(f => f.FullName). ToList(); } #endregion File List [StructLayout(LayoutKind.Sequential)] private struct RM_UNIQUE_PROCESS { public int dwProcessId; public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime; } private const int RmRebootReasonNone = 0; private const int CCH_RM_MAX_APP_NAME = 255; private const int CCH_RM_MAX_SVC_NAME = 63; private enum RM_APP_TYPE { RmUnknownApp = 0, RmMainWindow = 1, RmOtherWindow = 2, RmService = 3, RmExplorer = 4, RmConsole = 5, RmCritical = 1000 } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct RM_PROCESS_INFO { public RM_UNIQUE_PROCESS Process; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_APP_NAME + 1)] public string strAppName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_SVC_NAME + 1)] public string strServiceShortName; public RM_APP_TYPE ApplicationType; public uint AppStatus; public uint TSSessionId; [MarshalAs(UnmanagedType.Bool)] public bool bRestartable; } [DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)] private static extern int RmRegisterResources(uint pSessionHandle, UInt32 nFiles, string[] rgsFilenames, UInt32 nApplications, [In] RM_UNIQUE_PROCESS[] rgApplications, UInt32 nServices, string[] rgsServiceNames); [DllImport("rstrtmgr.dll", CharSet = CharSet.Auto)] private static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey); [DllImport("rstrtmgr.dll")] private static extern int RmEndSession(uint pSessionHandle); [DllImport("rstrtmgr.dll")] private static extern int RmGetList(uint dwSessionHandle, out uint pnProcInfoNeeded, ref uint pnProcInfo, [In, Out] RM_PROCESS_INFO[] rgAffectedApps, ref uint lpdwRebootReasons); /// /// Find out what process(es) have a lock on the specified file. /// /// Path of the file. /// Processes locking the file /// See also: /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa373661(v=vs.85).aspx /// http://wyupdate.googlecode.com/svn-history/r401/trunk/frmFilesInUse.cs (no copyright in code at time of viewing) /// /// public static List WhoIsLocking(string path) { uint handle; string key = Guid.NewGuid().ToString(); List processes = new List(); int res = RmStartSession(out handle, 0, key); if (res != 0) throw new Exception("Could not begin restart session. Unable to determine file locker."); try { const int ERROR_MORE_DATA = 234; uint pnProcInfoNeeded = 0, pnProcInfo = 0, lpdwRebootReasons = RmRebootReasonNone; string[] resources = new string[] { path }; // Just checking on one resource. res = RmRegisterResources(handle, (uint)resources.Length, resources, 0, null, 0, null); if (res != 0) throw new Exception("Could not register resource."); //Note: there's a race condition here -- the first call to RmGetList() returns // the total number of process. However, when we call RmGetList() again to get // the actual processes this number may have increased. res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons); if (res == ERROR_MORE_DATA) { // Create an array to store the process results RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded]; pnProcInfo = pnProcInfoNeeded; // Get the list res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons); if (res == 0) { processes = new List((int)pnProcInfo); // Enumerate all of the results and add them to the // list to be returned for (int i = 0; i < pnProcInfo; i++) { try { processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId)); } // catch the error -- in case the process is no longer running catch (ArgumentException) { } } } else throw new Exception("Could not list processes locking resource."); } else if (res != 0) throw new Exception("Could not list processes locking resource. Failed to get size of result."); } finally { RmEndSession(handle); } return processes; } } }