/// <summary> /// Checks the path. /// </summary> /// <param name="path">The path.</param> /// <param name="create">if set to <c>true</c> [create].</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public static bool CheckPath(string path, bool create = false) { CDFMonitor.LogOutputHandler(string.Format("DEBUG:CheckPath: enter:{0}:{1}", path, create)); try { if (string.IsNullOrEmpty(path)) { CDFMonitor.LogOutputHandler("CheckPath: empty path. returning false."); return(false); } path = FileManager.GetFullPath(path); ResourceManagement rm = new ResourceManagement(); ResourceManagement.ResourceType rt = rm.GetPathType(path); if (rm.CheckResourceCredentials(path) != ResourceManagement.CommandResults.Successful) { // 131022 if multiple dirs in path do not exist above will fail. if (!create | rm.DeterminePathObj(path) != ResourceManagement.DeterminePathObjType.Directory) { CDFMonitor.LogOutputHandler("CheckPath: checkresourcecredentials failed. returning false."); return(false); } } if (rt == ResourceManagement.ResourceType.Unc && rm.CheckUncPath(path, create)) { CDFMonitor.LogOutputHandler("CheckPath: able to access unc. returning true."); return(true); } if (rt == ResourceManagement.ResourceType.Url) { return(true); } ResourceManagement.DeterminePathObjType dt = rm.DeterminePathObj(path); if (dt == ResourceManagement.DeterminePathObjType.Directory) { if (Directory.Exists(path)) { return(true); } if (create) { Directory.CreateDirectory(path); // reset creds in case they failed on non-existent dir above if (rm.CheckResourceCredentials(path, true) != ResourceManagement.CommandResults.Successful) { return(false); } else { return(true); } } CDFMonitor.LogOutputHandler("DEBUG:CheckPath: directory doesnt exist and not configured to create. returning false:" + path); return(false); } if (dt == ResourceManagement.DeterminePathObjType.File) { if (File.Exists(path) | Directory.Exists(Path.GetDirectoryName(path))) { return(true); } if (create) { Directory.CreateDirectory(Path.GetDirectoryName(path)); return(true); } CDFMonitor.LogOutputHandler("CheckPath: file doesnt exist and directory cannot be created. returning false."); return(false); } CDFMonitor.LogOutputHandler(string.Format("CheckPath: unknown object:{0}", dt)); return(false); } catch (Exception e) { CDFMonitor.LogOutputHandler("CheckPath: exception:" + e.ToString()); return(false); } }
/// <summary> /// enumerates base logfile name and logfilename with index ex. cdfmonitor.log and /// cdfmonitor.0000.log /// </summary> /// <param name="logFile">base log file name</param> /// <param name="pattern">The pattern.</param> /// <returns>list of files found in order of lastwrite</returns> private List <string> EnumerateLogFiles(string logFile, string pattern, bool includeBase) { logFile = FileManager.GetFullPath(logFile); string logFilePath = string.IsNullOrEmpty(Path.GetDirectoryName(logFile)) || !Directory.Exists(Path.GetDirectoryName(logFile)) ? AppDomain.CurrentDomain.BaseDirectory : Path.GetDirectoryName(logFile); if (string.IsNullOrEmpty(logFile)) { return(new List <string>()); } ResourceManagement rm = new ResourceManagement(); if (rm.CheckResourceCredentials(logFile) != ResourceManagement.CommandResults.Successful) { return(new List <string>()); } if (rm.GetPathType(Path.GetDirectoryName(logFile)) == ResourceManagement.ResourceType.Unc) { rm.ConnectUncPath(Path.GetDirectoryName(logFile)); } List <FileInfo> tempList = new List <FileInfo>(); DirectoryInfo dI = new DirectoryInfo(logFilePath); string logFileBaseName = Regex.Replace(Path.GetFileName(logFile), @"\.([0-9]{1,4})\..{1,3}.{0,3}$", Path.GetExtension(logFile)); pattern = string.Format(@"(?<beginning>{0})\.(?<index>{1})(?<end>{2})$", Path.GetFileNameWithoutExtension(logFileBaseName), pattern, Path.GetExtension(logFile)); // Add all files with index in name tempList.AddRange(dI.GetFiles().Where(f => Regex.IsMatch(f.Name, pattern, RegexOptions.IgnoreCase))); // Add original name if it exists and different if (includeBase & !tempList.Any(i => i.FullName == logFile) & FileManager.FileExists(logFile)) { // Add original name tempList.Add(new FileInfo(logFile)); } else if (includeBase & !tempList.Any(i => i.FullName == string.Format("{0}\\{1}", logFilePath, logFileBaseName)) & FileManager.FileExists(string.Format("{0}\\{1}", logFilePath, logFileBaseName))) { // Add base name tempList.AddRange(dI.GetFiles(logFileBaseName, SearchOption.TopDirectoryOnly)); } //tempList.Sort((x, y) => DateTime.Compare(x.CreationTime, y.CreationTime)); tempList.Sort((x, y) => DateTime.Compare(x.LastWriteTime, y.LastWriteTime)); _cdfMonitor.LogOutput("DEBUG:EnumerateLogFiles file list:"); foreach (FileInfo path in tempList) { _cdfMonitor.LogOutput("DEBUG:EnumerateLogFiles file:" + path.Name); } if (rm.GetPathType(logFile) == ResourceManagement.ResourceType.Unc) { rm.DisconnectUncPath(logFile); } return(tempList.Select(x => x.FullName).ToList()); }
/// <summary> /// Copies the files. /// </summary> /// <param name="files">The files.</param> /// <param name="destinationPath">The destination path.</param> /// <param name="createDestination">if set to <c>true</c> [create destination].</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public static bool CopyFiles(string[] files, string destinationPath, bool createDestination = false) { string sourcePathRoot = string.Empty; string destPathRoot = destinationPath.TrimEnd('\\'); string destFile = string.Empty; bool retval = true; try { CDFMonitor.LogOutputHandler(string.Format("DEBUG:CopyFiles:enter")); ResourceManagement rm = new ResourceManagement(); if (rm.CheckResourceCredentials(destinationPath, false) != ResourceManagement.CommandResults.Successful) { CDFMonitor.LogOutputHandler(string.Format("Error:CopyFiles:invalid credential return:{0}:{1}", rm, destinationPath)); return(false); } else if (!Directory.Exists(destinationPath)) { if (!createDestination) { CDFMonitor.LogOutputHandler("CopyFiles:destination does not exist and create = false:" + destinationPath); return(false); } CDFMonitor.LogOutputHandler(string.Format("CopyFiles:creating directory:{0}", destinationPath)); Directory.CreateDirectory(destinationPath); } foreach (string file in files) { string sourceFile = GetFullPath(file); // populate sourcePathRoot if empty to avoid errors below if (string.IsNullOrEmpty(sourcePathRoot)) { sourcePathRoot = Path.GetDirectoryName(sourceFile); } CDFMonitor.LogOutputHandler(string.Format("CopyFiles:copying file:{0} : {1}", sourceFile, destinationPath)); if (rm.GetPathType(sourceFile) == ResourceManagement.ResourceType.Unc && rm.CheckResourceCredentials(sourceFile) == ResourceManagement.CommandResults.Successful) { if (!rm.ConnectUncPath(sourceFile)) { retval = false; continue; } } // setup dest path and directory if (String.Compare(sourcePathRoot, Path.GetDirectoryName(sourceFile)) != 0) { string newSourcePath = Path.GetDirectoryName(sourceFile); if (newSourcePath.Contains(sourcePathRoot)) { // then its a subdirectory. add dir to dest destFile = sourceFile.Replace(sourcePathRoot, destPathRoot); string newdestPath = Path.GetDirectoryName(destFile); if (!Directory.Exists(newdestPath)) { Directory.CreateDirectory(newdestPath); } } else { // new root sourcePathRoot = newSourcePath; destFile = sourceFile.Replace(sourcePathRoot, destPathRoot); } } else { // no change in path destFile = sourceFile.Replace(sourcePathRoot, destPathRoot); } if (!File.Exists(sourceFile)) { CDFMonitor.LogOutputHandler(string.Format("Warning:CopyFiles:source file does not exist:{0}", sourceFile)); retval = false; continue; } if (File.Exists(destFile)) { File.Delete(destFile); } CDFMonitor.LogOutputHandler(string.Format("CopyFiles:copying file source:{0} dest:{1}", sourceFile, destFile)); File.Copy(sourceFile, destFile, true); if (rm.GetPathType(sourceFile) == ResourceManagement.ResourceType.Unc) { rm.DisconnectUncPath(destinationPath); } } if (rm.GetPathType(destinationPath) == ResourceManagement.ResourceType.Unc) { rm.DisconnectUncPath(destinationPath); } return(retval); } catch (Exception e) { CDFMonitor.LogOutputHandler("CopyFiles:exception:" + e.ToString()); return(false); } }
/// <summary> /// Manages the log file server. /// </summary> /// <param name="instance">The instance.</param> private static void ManageLogFileServerThreadProc(object instance) { LogManager fMInstance = (instance as LogManager); ResourceManagement rm = new ResourceManagement(); Dictionary <string, int> logFileList = new Dictionary <string, int>(); while (fMInstance.LogFileServerEnabled) { if (CDFMonitor.CloseCurrentSessionEvent.WaitOne(0)) { return; } if (!fMInstance.LogFileServerEvt.WaitOne(100)) { continue; } CDFMonitor.LogOutputHandler("ManageLogFileServer:received event"); if (rm.CheckResourceCredentials(fMInstance.LogFileServer) != ResourceManagement.CommandResults.Successful) { CDFMonitor.LogOutputHandler("Fail:ManageLogFileServer:no access to server. exiting"); return; } lock (fMInstance.LogFileLock) { foreach (string file in fMInstance.Logs) { if (!logFileList.ContainsKey(file) && String.Compare(file, fMInstance.LogFileName) != 0) { logFileList.Add(file, 0); } } foreach (string file in fMInstance.Logs) { if (!logFileList.ContainsKey(file)) { logFileList.Add(file, 0); } } // see if any have been removed and check state foreach (KeyValuePair <string, int> file in new Dictionary <string, int>(logFileList)) { if (!fMInstance.Logs.Contains(file.Key) && !fMInstance.Logs.Contains(file.Key)) { CDFMonitor.LogOutputHandler(string.Format("DEBUG:ManageLogFileServer:removing stale file:{0} with state:{1}", file.Key, file.Value)); logFileList.Remove(file.Key); } } } if (logFileList.Count < 1) { continue; } // copy any files set to 0 foreach (KeyValuePair <string, int> file in new Dictionary <string, int>(logFileList)) { if (file.Value != 0) { continue; } PackageManager pW = new PackageManager(); if (pW.CreatePackage(new string[] { file.Key }, AppDomain.CurrentDomain.BaseDirectory)) { // Rename zip string newFileName = string.Format("{0}\\{1}.zip", Path.GetDirectoryName(file.Key), Path.GetFileName(file.Key)); if (FileManager.RenameFile(pW.ZipFile, newFileName) && FileManager.CopyFile(newFileName, string.Format("{0}\\{1}", fMInstance.LogFileServer, Environment.MachineName), true)) { CDFMonitor.LogOutputHandler(string.Format("DEBUG:ManageLogFileServer:file copied:{0}", file.Key)); logFileList[file.Key] = 1; } FileManager.DeleteFile(newFileName); } } } }