コード例 #1
0
ファイル: SSHCopyManager.cs プロジェクト: hst-bridge/BBS
        /// <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();
                }
            }
        }
コード例 #2
0
ファイル: LocalSyncManager.cs プロジェクト: hst-bridge/BBS
        /// <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, ""));
            }
        }
コード例 #3
0
ファイル: SSHCopyManager.cs プロジェクト: hst-bridge/BBS
        /// <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
        }