/// <summary> /// 判断此路径是否发生变化 /// 数据库中路径信息 暂定:有效性为一个月 即 每个月全部清空一次 /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool IsUpdate(Model.Config config, SftpFile sf, MonitorServer ms) { //判断是否更新 bool isUpdate = false; try { SSHPathInfo spi = new SSHPathInfo() { MonitorServerIP = ms.monitorServerIP, MacPath = sf.FullName, LastName = sf.FullName.Substring(sf.FullName.LastIndexOf('/') + 1), depth = GetDepth(sf.FullName), typeflag = sf.IsDirectory ? 0 : 1, updateTime = (Int32)(sf.Attributes.LastWriteTime.Subtract(new DateTime(1970, 1, 1))).TotalSeconds }; SSHPathInfoDAL spidal = new SSHPathInfoDAL(); isUpdate = spidal.IsUpdate(config, spi); } catch (System.Exception ex) { isUpdate = true; BudSSH.Common.Util.LogManager.WriteLog(Common.Util.LogFile.Error, MessageUtil.GetExceptionMsg(ex, "")); } return(isUpdate); }
/// <summary> /// 扫描数据库,判断是否在mac上存在 /// </summary> /// <param name="config"></param> private void DBSync(Config config) { try { /* * 分页获取SSHPathInfo,找出mac端已经删除文件对应的id,删除对应的本地文件并刷新数据库 * * 待确定的想法:如果发现mac端文件已经更新,则更新本地文件 */ List <string> delIDList = new List <string>(); SSHPathInfoDAL spid = new SSHPathInfoDAL(); #region 主要处理 try { FileSystemUtil fsu = new FileSystemUtil(); //获取所有MonitorServer,以便于数据处理 MonitorServerDAL msd = new MonitorServerDAL(); List <MonitorServer> msList = msd.GetAllMonitorServer(config); foreach (SSHPathInfo spi in spid.GetSSHPathInfo(config)) { if (Signal.IsSystemStoping) { break; } #region 判断Mac端文件是否存在,如果不存在,则删除本地SSH输出目录中对应文件 SFTPProxy sftpProxy = null; string localSSHPath = string.Empty; try { //获取对应mac机的信息 MonitorServer ms = msList.Find(x => x.monitorServerIP.Trim().Equals(spi.MonitorServerIP.Trim()) && spi.MacPath.ToLower().Trim().Contains(x.monitorMacPath.ToLower().Trim())); if (ms != null) { sftpProxy = new SFTPProxy(ms.monitorServerIP, ms.account, ms.password); if (!sftpProxy.IsExist(spi.MacPath)) { delIDList.Add(spi.ID); #region 除SSH输出路径的本地文件 //获取本地路径 localSSHPath = GetLocalSSHPath(ms, spi.MacPath, config.Path.OutputPath); if (spi.typeflag == 0) { fsu.DeleteDir(localSSHPath); } else { //如果是文件 fsu.DeleteFile(localSSHPath); //判断目录是否为空:是否有文件 CleanLocalSSHDirectory(fsu, localSSHPath); } #endregion } } } catch (System.Exception ex) { Common.Util.LogManager.WriteLog(Common.Util.LogFile.DBSync, localSSHPath + Environment.NewLine + MessageUtil.GetExceptionMsg(ex, "")); } finally { if (sftpProxy != null) { sftpProxy.Close(); } } #endregion } } catch (System.Exception ex) { Common.Util.LogManager.WriteLog(Common.Util.LogFile.DBSync, MessageUtil.GetExceptionMsg(ex, "")); } finally { spid.Dispose(); } #endregion #region 更新数据库 spid.DeleteEntrys(config, delIDList); #endregion } catch (System.Exception ex) { Common.Util.LogManager.WriteLog(Common.Util.LogFile.DBSync, MessageUtil.GetExceptionMsg(ex, "")); } }