/// <summary> /// 判断文件是否发生变化,若变化则copy文件 /// </summary> /// <param name="config"></param> /// <param name="epi"></param> public void SSHCopy(Config config, ErrorPathInfo epi) { //获取mac端信息 MonitorServerDAL msd = new MonitorServerDAL(); MonitorServer ms = msd.GetMonitorServer(config, epi); if (ms == null) { Common.Util.LogManager.WriteLog(LogFile.Warning, "there is no MonitorServer that meet the conditions !"); return; } SFTPProxy sftpProxy = null; var sshlog = new Common.Util.SSHLogManager(); try { sftpProxy = new SFTPProxy(ms.monitorServerIP, ms.account, ms.password); #region 处理 foreach (ErrorEntry ee in epi.PathList) { string macPath = string.Empty; try { //获取mac路径 macPath = MacPathConvert(epi.source, ee.Path, ms.monitorMacPath, ms.startFile); //获取远端文件属性 SftpFile sf = sftpProxy.GetFileInfo(macPath); if (!ErrorPathFilter.IsUpdate(config, sf, ms)) { continue; } if (sf.IsDirectory) { #region 除job端已经删除的文件 #endregion foreach (SftpFile sfile in sftpProxy.Ls(sf)) { #region 过滤无关文件 if (String.Compare(sfile.Name.Trim('\r'), ".DS_Store", true) == 0 || String.Compare(sfile.Name.Trim('\r'), ".com.apple.timemachine.supported", true) == 0 || String.Compare(sfile.Name.Trim('\r'), "Icon", true) == 0) { continue; } #endregion if (!ErrorPathFilter.IsUpdate(config, sfile, ms)) { continue; } SSHCopyFile(sfile, ms, config.Path.OutputPath, sftpProxy); } } else { SSHCopyFile(sf, ms, config.Path.OutputPath, sftpProxy); } } catch (System.Exception ex) { Common.Util.LogManager.WriteLog(LogFile.Error, MessageUtil.GetExceptionMsg(ex, "")); sshlog.WriteLog(new Common.Util.SSHLog() { DateTime = DateTime.Now, LogType = Common.Util.SSHLogType.Failure, Message = ee.Path }); } } #endregion } catch (System.Exception ex) { Common.Util.LogManager.WriteLog(LogFile.Error, MessageUtil.GetExceptionMsg(ex, "")); } finally { if (sftpProxy != null) { sftpProxy.Close(); } } }
/// <summary> /// 遍历路径 规则 一层一层的获取 先文件后子目录 /// /// 由于递归不便处理异常,特修改为循环遍历树形结构,以实现递归效果 xiecongwen 20140716 /// </summary> /// <param name="path"></param> /// <returns></returns> public IEnumerable Ls(SftpFile file) { var sshlog = new Common.Util.SSHLogManager(); NTree <SftpFile> parent = new NTree <SftpFile>(file); //后序遍历树 NTree <SftpFile> child = null; #region 自下而上 后序遍历 while (true) { string path = parent.Data.FullName; #region 判断是否有子节点 ps:将子目录添加到子节点,返回子文件 SftpFile[] dirs = null; SftpFile[] files = null; #region 获取 try { #region 获取子目录,并添加到子节点 dirs = TopDirLs(path); if (dirs != null && dirs.Count() > 0) { foreach (var dir in dirs) { parent.AddChild(dir); } } #endregion #region 获取子文件 files = TopFileLs(path); #endregion } catch (System.Exception ex) { parent.RemoveAllChildren(); //记录报错路径,并忽略其子目录 sshlog.WriteLog(new Common.Util.SSHLog() { DateTime = DateTime.Now, LogType = Common.Util.SSHLogType.Failure, Message = path }); logger.Error("FTPPath:" + path + Environment.NewLine + MessageUtil.GetExceptionMsg(ex, "")); //更新sftp连接对象 sftp = GetAvailableSFTP(); } #endregion //返回子文件 if (files != null && files.Count() > 0) { foreach (var f in files) { yield return(f); } } #endregion //判断是否有子节点 child = parent.PushChild(); if (child == null) { //如果没有子节点,则回溯至上层,否则跳出(表示遍历结束) again : if (parent.Depth > 0) { parent = parent.Parent; } else { yield break; } child = parent.PushChild(); if (child == null) { goto again; } } //作为父节点 进行循环 parent = child; } #endregion }
/// <summary> /// 复制文件 /// </summary> /// <param name="sf"></param> /// <param name="ms"></param> /// <param name="sshOP">SSH output path</param> /// <param name="sftpProxy"></param> private void SSHCopyFile(SftpFile sf, MonitorServer ms, string sshOP, SFTPProxy sftpProxy) { var sshlog = new Common.Util.SSHLogManager(); //获取本地合法路径 string localPath = GetValidLocalPath(sf.FullName, ms.monitorMacPath, ms.monitorLocalPath); //获取SSH输出路径 暂时不支持网络位置 if (string.IsNullOrWhiteSpace(sshOP.Trim())) { localPath = localPath.Substring(0, localPath.IndexOf(':') + 1) + "\\SSH\\" + ms.monitorServerName + "-SSH"; } else { sshOP.TrimEnd('\\'); localPath = localPath.Replace(ms.monitorLocalPath.TrimEnd('\\'), sshOP + "\\" + ms.monitorServerName + "-SSH"); } #region 制 DateTime dateM = sf.Attributes.LastWriteTime; DateTime dateA = sf.Attributes.LastAccessTime; #region try { Alphaleonis.Win32.Filesystem.FileInfo targetFile = new Alphaleonis.Win32.Filesystem.FileInfo(localPath); //如果文件没有发生改变,则不进行下载 if (targetFile.Exists) { if (targetFile.LastWriteTime == dateM) { return; } } if (!Directory.Exists(targetFile.DirectoryName)) { try { Directory.CreateDirectory(targetFile.DirectoryName); } catch (System.Exception) { } if (!Directory.Exists(targetFile.DirectoryName)) { LongPath.CreateDirectory(targetFile.DirectoryName); } } sftpProxy.GetFile(sf.FullName, targetFile.FullName, true); targetFile.Refresh(); if (targetFile.Exists) { targetFile.CreationTime = dateM; targetFile.LastWriteTime = dateM; targetFile.LastAccessTime = dateA; } else { #region string errorDirToFileNameExtension = ""; if (sf.Name.IndexOf(".") > -1) { errorDirToFileNameExtension = sf.Name.Substring(sf.Name.IndexOf(@".") + 1); } Thread.Sleep(2000); bool copyconfirm = true; int confirmCoount = 0; while (copyconfirm) { #region if (confirmCoount < 3) { targetFile.Refresh(); if (targetFile.Exists) { DateTime fileFirstTime = targetFile.LastWriteTime; Thread.Sleep(2000); DateTime fileSecondTime = targetFile.LastWriteTime; if (fileFirstTime.Equals(fileSecondTime)) { targetFile.CreationTime = dateM; targetFile.LastWriteTime = dateM; targetFile.LastAccessTime = dateA; copyconfirm = false; } } else { Thread.Sleep(2000); } confirmCoount++; } else { break; } #endregion } #endregion } sshlog.WriteLog(new Common.Util.SSHLog() { DateTime = DateTime.Now, LogType = Common.Util.SSHLogType.Success, Message = targetFile.FullName }); } catch (ArgumentException ae) { logger.Error("localpath:" + localPath + Environment.NewLine + MessageUtil.GetExceptionMsg(ae, "")); sshlog.WriteLog(new Common.Util.SSHLog() { DateTime = DateTime.Now, LogType = Common.Util.SSHLogType.Failure, Message = ms.monitorServerIP + ", " + sf.FullName }); } catch (System.Exception ex) { logger.Error("localpath:" + localPath + Environment.NewLine + MessageUtil.GetExceptionMsg(ex, "")); sshlog.WriteLog(new Common.Util.SSHLog() { DateTime = DateTime.Now, LogType = Common.Util.SSHLogType.Failure, Message = ms.monitorServerIP + ", " + sf.FullName }); } #endregion #endregion }