Ejemplo n.º 1
0
        private void ShowMod(FTPSyncModification mod)
        {
            string LocalFile  = mod.LocalFile.Replace(Locations.LocalFolderName_Minecraft + "\\", "");
            string RemoteFile = mod.RemoteFile.Replace(Config.ftpsettings.FtpServerFolder + "/", "");

            switch (mod.Action)
            {
            case FTPAction.Copy:
                if (mod.Upload)
                {
                    ShowStatus(1, string.Format("--> {0} ({1} {2})", LocalFile, (mod.Exists ? "Updated" : "Added"), mod.ModifiedTime.ToShortDateString()));
                }
                else
                {
                    ShowStatus(1, string.Format("<-- {0} ({1} {2})", RemoteFile, (mod.Exists ? "Updated" : "Added"), mod.ModifiedTime.ToShortDateString()));
                }
                break;

            case FTPAction.Delete:
                if (mod.Upload)
                {
                    ShowStatus(1, string.Format(" X {0}", LocalFile));
                }
                else
                {
                    ShowStatus(1, string.Format(" X {0}", RemoteFile));
                }
                break;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Compare local directory with remote directory
        /// </summary>
        /// <param name="localFolder">folder @ client</param>
        /// <param name="remoteFolder">folder @ server</param>
        /// <param name="recursive">Will we go into directories?</param>
        /// <returns>List of modifications to make</returns>
        public List <FTPSyncModification> CompareDirectory(string localFolder, string remoteFolder, FTPSyncSettings settings)
        {
            BytesTotal = 0;
            BytesDone  = 0;
            BytesLast  = 0;
            LastFile   = "";

            // make list of local files
            List <string> locals = new List <string>();

            GetLocalFiles(ref locals, localFolder, settings.Recurse);

            // check remote files
            List <FTPSyncModification> mods = new List <FTPSyncModification>();

            CheckRemoteFiles(ref locals, ref mods, localFolder, remoteFolder, settings);

            if (settings.WhenMissingRemote != FTPAction.Noop)
            {
                // local files still in list have been added
                FileInfo fi;
                foreach (string LocalFile in locals)
                {
                    fi = new FileInfo(LocalFile);
                    FTPSyncModification m = new FTPSyncModification();
                    m.Exists       = false;
                    m.Upload       = true;
                    m.Action       = settings.WhenMissingRemote;
                    m.LocalFile    = LocalFile;
                    m.ModifiedTime = fi.LastWriteTime;
                    m.RemoteFile   = LocalFile.Replace(localFolder, remoteFolder).Replace('\\', '/');
                    if (settings.WhenMissingRemote == FTPAction.Copy)
                    {
                        BytesTotal += fi.Length;
                    }
                    mods.Add(m);
                    ShowMod(m);
                }
            }
            return(mods);
        }
Ejemplo n.º 3
0
        private bool ConfirmModification(FTPSyncModification mod)
        {
            while (true)
            {
                switch (mod.Action)
                {
                case FTPAction.Copy:
                    if (mod.Upload)
                    {
                        Console.WriteLine("Overwrite remote file: " + mod.LocalFile + "? Y/N");
                    }
                    else
                    {
                        Console.WriteLine("Overwrite local file: " + mod.RemoteFile + "? Y/N");
                    }
                    break;

                case FTPAction.Delete:
                    if (mod.Upload)
                    {
                        Console.WriteLine("Delete local file: " + mod.LocalFile + "? Y/N");
                    }
                    else
                    {
                        Console.WriteLine("Delete remote file: " + mod.RemoteFile + "? Y/N");
                    }
                    break;
                }

                string overwrite = Console.ReadLine();
                if (overwrite.ToUpper().Equals("Y"))
                {
                    return(true);
                }
                else if (overwrite.ToUpper().Equals("N"))
                {
                    return(false);
                }
            }
        }
Ejemplo n.º 4
0
        private void DownloadModification(FTPSyncModification mod)
        {
            FileInfo fi;

            // delete old file
            if (mod.Exists)
            {
                File.Delete(mod.LocalFile);
            }

            // check directory
            string dir = Path.GetDirectoryName(mod.LocalFile);

            CreateDirectoryLocal(new DirectoryInfo(dir));

            // download file
            this.FtpServer.DownloadFile(mod.LocalFile, mod.RemoteFile);

            // update file properties
            fi = new FileInfo(mod.LocalFile);
            fi.CreationTime   = mod.ModifiedTime;
            fi.LastAccessTime = mod.ModifiedTime;
            fi.LastWriteTime  = mod.ModifiedTime;
        }
Ejemplo n.º 5
0
        private void UploadModification(FTPSyncModification mod)
        {
            if (mod.Exists)
            {
                this.FtpServer.DeleteFile(mod.RemoteFile);
            }

            // check directory
            string dir = mod.RemoteFile.Substring(0, mod.RemoteFile.LastIndexOf('/'));

            CreateDirectoryRemote(dir);

            // upload file
            this.FtpServer.UploadFile(mod.LocalFile, mod.RemoteFile);

            // update file properties
            try
            {
                this.FtpServer.SetLastWriteTime(mod.RemoteFile, mod.ModifiedTime);
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 6
0
 private void DeleteRemoteMod(FTPSyncModification mod)
 {
     this.FtpServer.DeleteFile(mod.RemoteFile);
 }
Ejemplo n.º 7
0
 private void DeleteLocalMod(FTPSyncModification mod)
 {
     File.Delete(mod.LocalFile);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Check all remote files for:
        /// </summary>
        /// <param name="locals"></param>
        /// <param name="mods">List of modifications. Items are added when syncing is required</param>
        /// <param name="localFolder">folder @ client</param>
        /// <param name="remoteFolder">folder @ server</param>
        /// <param name="recursive">if true, directories are also checked by recalling this function </param>
        private void CheckRemoteFiles(ref List <string> locals, ref List <FTPSyncModification> mods, string localFolder, string remoteFolder, FTPSyncSettings settings)
        {
            this.FtpServer.ChangeWorkingDirectory(remoteFolder);

            FTPFile[] FtpServerFileInfo = this.FtpServer.GetFileInfos();

            string   LocalTargetFolder = null;
            string   FtpTargetFolder   = null;
            bool     AddModification   = false;
            FileInfo fi = default(FileInfo);

            foreach (FTPFile CurrentFileOrDirectory in FtpServerFileInfo)
            {
                if (settings.Recurse)
                {
                    if (CurrentFileOrDirectory.Dir && CurrentFileOrDirectory.Name != "." && CurrentFileOrDirectory.Name != "..")
                    {
                        LocalTargetFolder = Path.Combine(localFolder, CurrentFileOrDirectory.Name);
                        FtpTargetFolder   = string.Format("{0}/{1}", remoteFolder, CurrentFileOrDirectory.Name);

                        CheckRemoteFiles(ref locals, ref mods, LocalTargetFolder, FtpTargetFolder, settings);

                        // set the ftp working folder back to the correct value
                        this.FtpServer.ChangeWorkingDirectory(remoteFolder);
                    }
                }

                if (!CurrentFileOrDirectory.Dir)
                {
                    FTPSyncModification m = new FTPSyncModification();
                    AddModification = false;
                    DateTime RemoteStamp = TimeZone.CurrentTimeZone.ToLocalTime(CurrentFileOrDirectory.LastModified);

                    m.LocalFile  = Path.Combine(localFolder, CurrentFileOrDirectory.Name);
                    m.RemoteFile = string.Format("{0}/{1}", remoteFolder, CurrentFileOrDirectory.Name);

                    // check file existence
                    if (!File.Exists(m.LocalFile))
                    {
                        if (settings.WhenMissingLocal != FTPAction.Noop)
                        {
                            AddModification = true;
                            m.Exists        = false;
                            m.ModifiedTime  = RemoteStamp;
                            m.Upload        = false;
                            m.Action        = settings.WhenMissingLocal;
                            if (settings.WhenMissingLocal == FTPAction.Copy)
                            {
                                BytesTotal += CurrentFileOrDirectory.Size;
                            }
                        }
                    }
                    else
                    {
                        // read file info
                        fi = new FileInfo(m.LocalFile);
                        DateTime LocalStamp = TimeZone.CurrentTimeZone.ToLocalTime(fi.LastWriteTime);

                        // check file size
                        if (CurrentFileOrDirectory.Size != fi.Length)
                        {
                            m.Exists = true;
                            if (CurrentFileOrDirectory.Size < fi.Length)
                            {
                                if (settings.WhenLargerLocal == FTPAction.Copy)
                                {
                                    AddModification = true;
                                    m.ModifiedTime  = LocalStamp;
                                    m.Upload        = true;
                                    m.Action        = settings.WhenLargerLocal;
                                    BytesTotal     += fi.Length;
                                }
                            }
                            else
                            {
                                if (settings.WhenLargerRemote == FTPAction.Copy)
                                {
                                    AddModification = true;
                                    m.ModifiedTime  = RemoteStamp;
                                    m.Upload        = false;
                                    m.Action        = settings.WhenLargerRemote;
                                    BytesTotal     += CurrentFileOrDirectory.Size;
                                }
                            }
                        }

                        // check modification time
                        if (RemoteStamp != LocalStamp)
                        {
                            m.Exists = true;
                            if (RemoteStamp < LocalStamp)
                            {
                                if (settings.WhenNewerLocal == FTPAction.Copy)
                                {
                                    AddModification = true;
                                    m.ModifiedTime  = LocalStamp;
                                    m.Upload        = true;
                                    m.Action        = settings.WhenNewerLocal;
                                    BytesTotal     += fi.Length;
                                }
                            }
                            else
                            {
                                if (settings.WhenNewerRemote == FTPAction.Copy)
                                {
                                    AddModification = true;
                                    m.ModifiedTime  = RemoteStamp;
                                    m.Upload        = false;
                                    m.Action        = settings.WhenNewerRemote;
                                    BytesTotal     += CurrentFileOrDirectory.Size;
                                }
                            }
                        }

                        // remove from local list
                        locals.Remove(m.LocalFile);
                    }

                    if (AddModification)
                    {
                        mods.Add(m);
                        ShowMod(m);
                    }
                }
            }
        }