Exemple #1
0
        public void Delete(Parameters parameters)
        {
            if (LongDirectory.Exists(Path))
            {
                string[] paths = LongDirectory.GetFiles(Path);
                foreach (string filePath in paths)
                {
                    if (parameters.Verbose)
                    {
                        Console.Out.WriteLine("{0}", filePath);
                    }
                    LongFile.Delete(filePath);
                }
                if (parameters.Recursive)
                {
                    string[] subDirs = LongDirectory.GetDirectories(Path);
                    foreach (string subDir in subDirs)
                    {
                        PFolder pFolder = new PFolder(subDir);
                        pFolder.Delete(parameters);
                    }
                }
                if (parameters.Verbose)
                {
                    Console.Out.WriteLine("{0}", Path);
                }
                LongDirectory.Delete(Path);
            }
            else if (LongFile.Exists(Path))
            {
                if (parameters.Verbose)
                {
                    Console.Out.WriteLine("{0}", Path);
                }
                LongFile.Delete(Path);
            }

            if (Path == parameters.Path)
            {
                if (parameters.RequireEnterToExit)
                {
                    Console.Out.WriteLine("Press enter to exit");
                    Console.In.ReadLine();
                }
            }
        }
Exemple #2
0
        private static void docompressdirectory(ZipStorer zip, string dir, string prefix, bool recursive, Action <string> log)
        {
            if (recursive)
            {
                foreach (var d in LongDirectory.GetDirectories(dir))
                {
                    docompressdirectory(zip, d, prefix, recursive, log);
                }
            }

            foreach (string path in LongDirectory.GetFiles(dir))
            {
                var fn = path.Replace(prefix, "");
                zip.AddFile(ZipStorer.Compression.Deflate, path, fn, "");
                log?.Invoke(fn);
            }
        }
Exemple #3
0
 private static void DeleteDirectories(string[] directories)
 {
     foreach (string directory in directories)
     {
         string[] files = LongDirectory.GetFiles(directory, null, System.IO.SearchOption.TopDirectoryOnly);
         foreach (string file in files)
         {
             LongFile.Delete(file);
         }
         directories = LongDirectory.GetDirectories(directory, null, System.IO.SearchOption.TopDirectoryOnly);
         DeleteDirectories(directories);
         bool ok = NativeMethods.RemoveDirectory(GetWin32LongPath(directory));
         if (!ok)
         {
             ThrowWin32Exception();
         }
     }
 }
Exemple #4
0
        private void AnalyseFolder(string folder1, string folder2)
        {
            string[] files1 = !LongDirectory.Exists(folder1) ? new string[] { } : LongDirectory.GetFiles(folder1).Select(f => LongFile.GetName(f)).ToArray();
            string[] files2 = !LongDirectory.Exists(folder2) ? new string[] { } : LongDirectory.GetFiles(folder2).Select(f => LongFile.GetName(f)).ToArray();
            foreach (string file1 in files1)
            {
                SyncItems.Add(new SyncItem(LongFile.Combine(folder1, file1), LongFile.Combine(folder2, file1), SyncItemType.File));
            }
            foreach (string file2 in files2)
            {
                if (files1.FirstOrDefault(f => f == file2) == null)
                {
                    SyncItems.Add(new SyncItem(LongFile.Combine(folder1, file2), LongFile.Combine(folder2, file2), SyncItemType.File));
                }
            }

            string[] subs1 = !LongDirectory.Exists(folder1) ? new string[] { } : LongDirectory.GetDirectories(folder1).Select(f => LongDirectory.GetName(f)).ToArray();
            string[] subs2 = !LongDirectory.Exists(folder2) ? new string[] { } : LongDirectory.GetDirectories(folder2).Select(f => LongDirectory.GetName(f)).ToArray();
            foreach (string sub1 in subs1)
            {
                string fullSub1 = LongDirectory.Combine(folder1, sub1);
                string fullSub2 = LongDirectory.Combine(folder2, sub1);
                SyncItems.Add(new SyncItem(fullSub1, fullSub2, SyncItemType.Folder));
                AnalyseFolder(fullSub1, fullSub2);
            }
            foreach (string sub2 in subs2)
            {
                if (subs1.FirstOrDefault(f => f == sub2) == null)
                {
                    string fullSub1 = LongDirectory.Combine(folder1, sub2);
                    string fullSub2 = LongDirectory.Combine(folder2, sub2);
                    SyncItems.Add(new SyncItem(fullSub1, fullSub2, SyncItemType.Folder));
                    AnalyseFolder(fullSub1, fullSub2);
                }
            }
        }
Exemple #5
0
        internal void QueueDelta(Delta d)
        {
            string archivefolder = _conn.Path + ".ts" + _S + "old" + _S;

            Directory.CreateDirectory(archivefolder);
            if (d != null)
            {
                // empty old error queue
                _ErrorQue = new ConcurrentQueue <SyncFile>();

                d.FilesDeleted.ForEach(x =>
                {
                    try
                    {
                        if (LongFile.Exists(_conn.Path + x))
                        {
                            _log.Info("Deleting file : " + _conn.Path + x);
                            string fn = archivefolder + x;
                            LongFile.Move(_conn.Path + x, fn);
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex);
                    }
                });
                d.FoldersDeleted.ForEach(x =>
                {
                    _log.Info("Deleting folder : " + _conn.Path + x);
                    if (LongDirectory.Exists(_conn.Path + x))
                    {
                        try
                        {
                            LongDirectory.Move(_conn.Path + x, archivefolder + x);
                        }
                        catch { }
                        try
                        {
                            var f = LongDirectory.GetFiles(_conn.Path + x, "*.*", SearchOption.AllDirectories);
                            if (f.Length == 0)
                            {
                                LongDirectory.Delete(_conn.Path + x, true);
                            }
                        }
                        catch (Exception ex)
                        {
                            _log.Error(ex);
                        }
                    }
                });
                d.FilesAdded.ForEach(x =>
                {
                    _que.Enqueue(x);
                });
                d.FilesChanged.ForEach(x =>
                {
                    _que.Enqueue(x);
                });

                _log.Info("delfolders = " + d.FoldersDeleted.Count);
                _log.Info("delfiles = " + d.FilesDeleted.Count);
                _log.Info("files added = " + d.FilesAdded.Count);
                _log.Info("files changed = " + d.FilesChanged.Count);
                _log.Info("que count = " + _que.Count);

                SaveQueue();
            }
        }
Exemple #6
0
        public void Backup()
        {
            if (FolderBackupStarted != null)
            {
                FolderBackupStarted(this, new FolderEventArgs()
                {
                    Path = Path
                });
            }

            try
            {
                if (Excludes == null)
                {
                    Excludes = MainDbContext.DB.ExcludePaths.ToArray();
                }

                foreach (string filename in LongDirectory.GetFiles(Path).OrderBy(f => f))
                {
                    if (ExcludePath(filename))
                    {
                        continue;
                    }
                    FFile ffile = new FFile(filename);
                    ffile.Excludes            = Excludes;
                    ffile.FileBackupStarted  += FileBackupStarted;
                    ffile.FileBackupFinished += FileBackupFinished;

                    ffile.Backup();

                    if (BackgroundWorker != null && BackgroundWorker.CancellationPending)
                    {
                        return;
                    }
                }

                foreach (string folder in LongDirectory.GetDirectories(Path).OrderBy(f => f))
                {
                    if (ExcludePath(folder))
                    {
                        continue;
                    }

                    FFolder ffolder = new FFolder(folder);
                    ffolder.Excludes              = Excludes;
                    ffolder.BackgroundWorker      = BackgroundWorker;
                    ffolder.FolderBackupStarted  += new FolderBackupStartedHandler(FolderBackupStarted);
                    ffolder.FolderBackupFinished += new FFolder.FolderBackupFinishedHandler(FolderBackupFinished);
                    ffolder.FileBackupStarted    += new FFile.FileBackupStartedHandler(FileBackupStarted);
                    ffolder.FileBackupFinished   += new FFile.FileBackupFinishedHandler(FileBackupFinished);
                    ffolder.Backup();
                    if (BackgroundWorker != null && BackgroundWorker.CancellationPending)
                    {
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
            }

            if (FolderBackupFinished != null)
            {
                FolderBackupFinished(this, new FolderEventArgs()
                {
                    Path = Path, Success = true
                });
            }
        }
Exemple #7
0
        public void Copy(Parameters parameters)
        {
            if (!LongDirectory.Exists(DestPath))
            {
                LongDirectory.CreateDirectory(DestPath);
            }

            if (parameters.Verbose)
            {
                Console.Out.WriteLine("{0}", SourcePath);
            }
            string[] sourceFilePaths = LongDirectory.GetFiles(SourcePath);
            string[] destFilePaths   = LongDirectory.GetFiles(DestPath);

            foreach (string sourceFilePath in sourceFilePaths)
            {
                string copyFileComment = string.Empty;
                bool   copyFile        = false;
                string sourceFilename  = LongFile.GetName(sourceFilePath);
                string destFilePath    = LongFile.Combine(DestPath, sourceFilename);
                if (destFilePaths.FirstOrDefault(f => f.Equals(destFilePath, StringComparison.CurrentCultureIgnoreCase)) == null)
                {
                    copyFile        = true;
                    copyFileComment = "new   ";
                }
                else
                {
                    DateTime sourceModified = LongFile.GetLastWriteTime(sourceFilePath);
                    DateTime destModified   = LongFile.GetLastWriteTime(destFilePath);
                    int      modComp        = sourceModified.CompareTo(destModified);
                    if (modComp > 0)
                    {
                        copyFile        = true;
                        copyFileComment = "newer ";
                    }
                    else if (modComp < 0)
                    {
                        copyFile        = true;
                        copyFileComment = "older ";
                    }
                    else
                    {
                        copyFileComment = "same  ";
                    }
                }

                if (copyFile)
                {
                    if (parameters.Verbose)
                    {
                        Console.Out.WriteLine("     {0} : {1} ", copyFileComment, sourceFilename);
                    }
                    try
                    {
                        LongFile.Copy(sourceFilePath, destFilePath, true);
                    }
                    catch (Exception ex)
                    {
                        Console.Out.WriteLine("{0}", ex.Message);
                    }
                }
            }

            if (parameters.Recursive)
            {
                string[] sourceDirs = LongDirectory.GetDirectories(SourcePath);
                foreach (string sourceDir in sourceDirs)
                {
                    string sourceName = LongFile.GetName(sourceDir);
                    string destDir    = LongFile.Combine(DestPath, sourceName);
                    if (!LongDirectory.Exists(destDir))
                    {
                        LongDirectory.CreateDirectory(destDir);
                    }

                    PFolder pFolder = new PFolder(sourceDir, destDir);
                    pFolder.Copy(parameters);
                }
            }

            if (SourcePath == parameters.SourceDirectory)
            {
                if (parameters.RequireEnterToExit)
                {
                    Console.Out.WriteLine("Press enter to exit");
                    Console.In.ReadLine();
                }
            }
        }