Beispiel #1
0
        public LocalDirectory(FileOrDirectory parent, Uri url, SyncJob job)
        {
            Job    = job;
            Parent = parent;
            if (!url.ToString().Contains(':'))
            {
                if (url.ToString() == ".")
                {
                    url = new Uri(Environment.CurrentDirectory);
                }
                else
                {
                    url = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, url.ToString()));
                }
            }
            if (!url.IsFile)
            {
                throw new NotSupportedException("url is no local file.");
            }
            Url = url;
            var info = new DirectoryInfo(Path);

            Name       = info.Name;
            Class      = ObjectClass.Directory;
            ChangedUtc = info.LastWriteTimeUtc;
        }
Beispiel #2
0
 public void DeleteDirectory(FileOrDirectory dest)
 {
     try {
         System.IO.Directory.Delete(((LocalDirectory)dest).Path, true);
     } catch (Exception ex) {
         Job.Failure(dest, ex);
     }
 }
Beispiel #3
0
 public Stream ReadFile(FileOrDirectory src)
 {
     try {
         var path = System.IO.Path.Combine(Path, src.Name);
         return(File.OpenRead(path));
     } catch (Exception ex) {
         Job.Failure(src, ex);
     }
     return(null);
 }
Beispiel #4
0
 public void Delete(FileOrDirectory dest)
 {
     if (dest.Class == ObjectClass.File)
     {
         DeleteFile(dest);
     }
     else
     {
         DeleteDirectory(dest);
     }
 }
Beispiel #5
0
 public IDirectory Root(FileOrDirectory fd)
 {
     if (fd.Parent == null)
     {
         return((IDirectory)fd);
     }
     else
     {
         return(Root(fd.Parent));
     }
 }
Beispiel #6
0
        public void DeleteDirectory(FileOrDirectory dest, EventHandler onFinished = null)
        {
            var dir = (FtpDirectory)dest;
            int con = Job.Connections.Count(dir.url);

            if (con == 0)
            {
                con = 1;
            }
            var list    = dir.List();
            var subdirs = list.OfType <FtpDirectory>();

            var n = new SyncJob.Counter();

            n.N = subdirs.Count();

            var finished = (EventHandler)((sender, args) => {
                lock (n) {
                    n.N--;
                    if (n.N <= 0)
                    {
                        var ftp = Job.Connections.Open(ref dir.url);
                        try {
                            foreach (var file in list.Where(f => f.Class == ObjectClass.File))
                            {
                                ftp.DeleteFile(file.Name);
                            }
                            ftp.ChangeDirectoryUp();
                            ftp.DeleteDirectory(dest.Name);
                        } catch (Exception ex) {
                            Job.Failure(dest, ex);
                        } finally {
                            Job.Connections.Pass(ftp);
                        }
                        if (onFinished != null)
                        {
                            onFinished(this, EventArgs.Empty);
                        }
                    }
                }
            });

            foreach (var item in subdirs)
            {
                var d = item;
                var t = new Task(() => { d.DeleteDirectory(d, finished); });
                //t.Finished += finished;
                Job.Threads.Do(t);
            }
            if (subdirs.Count() == 0)
            {
                finished(this, EventArgs.Empty);
            }
        }
Beispiel #7
0
        public void DeleteFile(FileOrDirectory dest)
        {
            var ftp = Job.Connections.Open(ref url);

            try {
                ftp.DeleteFile(dest.Name);
            } catch (Exception ex) {
                Job.Failure(dest, ex, ftp);
            } finally {
                Job.Connections.Pass(ftp);
            }
        }
Beispiel #8
0
 public void DeleteFile(FileOrDirectory dest)
 {
     try {
         var path = System.IO.Path.Combine(Path, dest.Name);
         if (new FileInfo(path).FullName != new FileInfo(Job.LogFile).FullName)
         {
             System.IO.File.Delete(path);
         }
     } catch (Exception ex) {
         Job.Failure(dest, ex);
     }
 }
Beispiel #9
0
 public FtpDirectory(FileOrDirectory parent, Uri url, SyncJob job)
 {
     Job    = job;
     Parent = parent;
     if (url.Scheme != "ftp" && url.Scheme != "ftps")
     {
         throw new NotSupportedException();
     }
     Url     = url;
     Name    = url.File();
     Class   = ObjectClass.Directory;
     Changed = DateTime.Now.AddDays(2);
     if (parent is FtpDirectory)
     {
         TransferProgress = ((FtpDirectory)parent).TransferProgress;
     }
 }
Beispiel #10
0
 public IDirectory CreateDirectory(FileOrDirectory src)
 {
     try {
         string path;
         if (src == null)
         {
             path = Path;
         }
         else
         {
             path = System.IO.Path.Combine(Path, src.Name);
         }
         System.IO.Directory.CreateDirectory(path);
         return(new LocalDirectory(this, new Uri(path), Job));
     } catch (Exception ex) {
         Job.Failure(src, ex);
     }
     return(null);
 }
Beispiel #11
0
        public IDirectory CreateDirectory(FileOrDirectory dest)
        {
            var ftp = Job.Connections.Open(ref url);

            try {
                var path = ftp.CorrectPath(Url.Path());
                if (dest != null)
                {
                    path = path + "/" + dest.Name;
                }
                //var curpath = ftp.CurrentDirectory;
                //var ps = path.Split('/');
                //var cs = curpath.Split('/');
                //var j = cs.Length-1;
                //var i = Math.Min(ps.Length, j+1);
                //while (j > i-1) { ftp.ChangeDirectoryUp(); j--; }
                //while (j > 0 && ps[j] != cs[j]) { ftp.ChangeDirectoryUp(); j--; i = j+1; }

                //while (i < ps.Length) { str.Append("/"); str.Append(ps[i++]); }

                //var dir = str.ToString();
                ftp.MakeDirectory(path);

                //if (url.Query()["old"] != null) ftp.ChangeDirectoryMultiPath(path);
                //else ftp.ChangeDirectory(path);

                if (dest != null)
                {
                    return(new FtpDirectory(this, Url.Relative(dest.Name), Job));
                }
                else
                {
                    return(this);
                }
            } catch (Exception ex) {
                Job.Failure(dest, ex, ftp);
            } finally {
                Job.Connections.Pass(ftp);
            }
            return(null);
        }
Beispiel #12
0
        public void WriteFile(System.IO.Stream file, FileOrDirectory src)
        {
            if (file == null)
            {
                return;
            }

            var ftp = Job.Connections.Open(ref url);

            try {
                if (ftp.FileTransferType != TransferType.Binary)
                {
                    ftp.FileTransferType = TransferType.Binary;
                }
                var path = Url.Path() + "/" + src.Name;
                if (TransferProgress)
                {
                    progress.Add(ftp, new ProgressData {
                        ElapsedTime = new TimeSpan(0), Path = path, Size = src.Size
                    });
                    ftp.TransferProgress += ShowProgress;
                }
                var start = DateTime.Now;
                ftp.PutFile(file, src.Name, FileAction.Create);
                ftp.SetDateTime(src.Name, src.ChangedUtc);

                Log.Upload(path, src.Size, DateTime.Now - start);
            } catch (Exception e) {
                Job.Failure(src, e, ftp);
            } finally {
                if (TransferProgress)
                {
                    ftp.TransferProgress -= ShowProgress;
                    progress.Remove(ftp);
                }
                Job.Connections.Pass(ftp);
            }
        }
Beispiel #13
0
 public void WriteFile(Stream sstream, FileOrDirectory src)
 {
     if (sstream == null)
     {
         return;
     }
     try {
         var path = System.IO.Path.Combine(Path, src.Name);
         using (var dstream = File.Create(path)) {
             if (sstream is PipeStream)
             {
                 ((PipeStream)sstream).Read(dstream);
             }
             else
             {
                 Streams.Copy(sstream, dstream);
             }
         }
         File.SetLastAccessTimeUtc(path, src.ChangedUtc);
     } catch (Exception ex) {
         Job.Failure(src, ex);
     }
 }
Beispiel #14
0
        public System.IO.Stream ReadFile(FileOrDirectory src)
        {
            var file = new FtpStream(Job);

            Job.Threads.DoAsync(() => {
                var ftp = Job.Connections.Open(ref url);
                try {
                    if (ftp.FileTransferType != TransferType.Binary)
                    {
                        ftp.FileTransferType = TransferType.Binary;
                    }
                    file.Client = ftp;
                    file.Path   = Url.Path() + "/" + src.Name;
                    file.Size   = src.Size;
                    if (TransferProgress)
                    {
                        progress.Add(ftp, new ProgressData {
                            ElapsedTime = new TimeSpan(0), Path = file.Path, Size = src.Size
                        });
                        ftp.TransferProgress += ShowProgress;
                    }
                    file.Client.GetFile(src.Name, file, false);
                } catch (Exception ex) {
                    Job.Failure(src, ex, ftp);
                    file.Exception(ex);
                } finally {
                    file.Close();
                    if (TransferProgress)
                    {
                        file.Client.TransferProgress -= ShowProgress;
                        progress.Remove(file.Client);
                    }
                    Job.Connections.Pass(file.Client);
                }
            });
            return(file);
        }
Beispiel #15
0
        public void Directory(IDirectory sdir, IDirectory ddir)
        {
            if (ddir == null || sdir == null)
            {
                return;
            }

            sdir.Source      = ddir.Source = sdir;
            sdir.Destination = ddir.Destination = ddir;

            if (sdir is FtpDirectory)
            {
                ((FtpDirectory)sdir).TransferProgress = true;
            }
            if (ddir is FtpDirectory)
            {
                ((FtpDirectory)ddir).TransferProgress = true;
            }
            var slist = sdir.List().Where(file => !johnshope.Sync.Paths.Match(ExcludePatterns, file.RelativePath)).ToList();
            var dlist = ddir.List();
            //ddir.CreateDirectory(null);

            //Parallel.ForEach<FileOrDirectory>(list, new ParallelOptions { MaxDegreeOfParallelism = con },
            var tasks = new List <Task>();
            var n     = new Counter();

            n.N = slist.Count;
            var finished = (EventHandler)((sender, args) => {
                lock (n) {
                    n.N--;
                    if (n.N <= 0)
                    {
                        if (Mode != CopyMode.Add)
                        {
                            foreach (var dest in dlist)
                            {
                                ddir.Delete(dest);
                            }
                        }
                    }
                }
            });

            foreach (var source in slist)
            {
                var src = source;
                var t   = new Task(() => {
                    FileOrDirectory dest = null;
                    lock (dlist) { if (dlist.Contains(src.Name))
                                   {
                                       dest = dlist[src.Name];
                                   }
                    }
                    if (dest != null && dest.Class != src.Class && (src.Changed > dest.Changed || Mode == CopyMode.Clone))
                    {
                        ddir.Delete(dest);
                    }
                    if (src.Class == ObjectClass.File)
                    {
                        /*if (Verbose && dest != null) {
                         *      johnshope.Sync.Log.CyanText(src.Name + ":    " + src.Changed.ToShortDateString() + "-" + src.Changed.ToShortTimeString() + " => " +
                         *              dest.Changed.ToShortDateString() + "-" + dest.Changed.ToShortTimeString());
                         * }*/
                        if (dest == null || ((Mode == CopyMode.Update || Mode == CopyMode.Add) && src.Changed > dest.Changed) || (Mode == CopyMode.Clone && (src.Changed > dest.Changed + dt)))
                        {
                            using (var s = sdir.ReadFile(src)) {
                                ddir.WriteFile(s, src);
                            }
                        }
                    }
                    else
                    {
                        if (dest == null)
                        {
                            Directory((IDirectory)src, ddir.CreateDirectory(src));
                        }
                        else
                        {
                            Directory((IDirectory)src, (IDirectory)dest);
                        }
                    }
                    lock (dlist) { dlist.Remove(src.Name); }
                });
                tasks.Add(t);
                t.Finished += finished;
                Threads.Do(t);
            }
            if (slist.Count == 0)
            {
                finished(this, EventArgs.Empty);
            }
        }
Beispiel #16
0
 public void Failure(FileOrDirectory file, Exception ex, FtpClient ftp)
 {
     lock (Failures) Failures.Enqueue(new FailureInfo {
             File = file, Exception = ex
         }); Log.Exception(ftp, ex);
 }
Beispiel #17
0
 public void DeleteDirectory(FileOrDirectory dest)
 {
     DeleteDirectory(dest, null);
 }