Ejemplo n.º 1
0
        private object copy(DirectoryInfo rootFrom, DirectoryInfo fromDir, DirectoryInfo toDir, IStringFilter nf, IStringFilter df)
        {
            bool isRoot       = (rootFrom == fromDir);
            bool isVisible    = (isRoot || CheckHidden(fromDir));
            bool processFiles = isVisible;

            if (processFiles && (df != null && !df.IsMatch(fromDir.FullName)))
            {
                processFiles = false;
                VerboseMessage("{0} did not pass directory filter", fromDir.FullName);
            }

            var from = new FileOrDirectoryInfo(fromDir);
            var to   = new FileOrDirectoryInfo(toDir);

            return(ProcessPrepare(from, to,
                                  delegate
            {
                if (processFiles)
                {
                    if (EmptyDirectories && !to.Exists)
                    {
                        bool created;
                        object r = createDir(fromDir, toDir, out created);
                        if (r != null || !created)
                        {
                            return r;
                        }
                    }

                    foreach (FileInfo f in fromDir.GetFiles())
                    {
                        FileInfo toFile = new FileInfo(Path.Combine(to.FullName, f.Name));
                        object r = copySingleFile(nf, f, toFile);
                        if (r != null)
                        {
                            return r;
                        }
                    }
                }
                if (Recursive)
                {
                    foreach (DirectoryInfo d in fromDir.GetDirectories())
                    {
                        object r = copy(rootFrom, d, new DirectoryInfo(Path.Combine(to.FullName, d.Name)), nf, df);
                        if (r != null)
                        {
                            return r;
                        }
                    }
                }
                return null;
            }));
        }
Ejemplo n.º 2
0
        private object createDir(DirectoryInfo fromDir, DirectoryInfo toDir, out bool created)
        {
            var    from       = new FileOrDirectoryInfo(fromDir);
            var    to         = new FileOrDirectoryInfo(toDir);
            bool   createdTmp = false;
            object ret        = ProcessComplete(from, to, false, delegate(bool skip)
            {
                if (!skip)
                {
                    toDir.Create();
                    toDir.Attributes       = fromDir.Attributes;
                    toDir.LastWriteTimeUtc = fromDir.LastWriteTimeUtc;
                    createdTmp             = true;
                }
                return(null);
            });

            created = createdTmp;
            return(ret);
        }
Ejemplo n.º 3
0
        private object copySingleFile(IStringFilter nf, FileInfo f, FileInfo toFile)
        {
            if (nf != null && (!nf.IsMatch(f.FullName) || !CheckHidden(f)))
            {
                VerboseMessage("{0} did not pass filter", f.FullName);
                return(null);
            }
            var    from = new FileOrDirectoryInfo(f);
            var    to   = new FileOrDirectoryInfo(toFile);
            bool   skip = false;
            object ret  = ProcessPrepare(from, to,
                                         delegate
            {
                if (toFile.Directory != null && !toFile.Directory.Exists)
                {
                    bool created;
                    object r = createDir(f.Directory, toFile.Directory, out created);
                    if (!created || r != null)
                    {
                        return(r);
                    }
                }


                bool overwrite = (Overwrite == OverwriteMode.Always);
                if (Overwrite == OverwriteMode.IfNewer)
                {
                    if (toFile.Exists && toFile.LastWriteTimeUtc >= f.LastWriteTimeUtc)
                    {
                        VerboseMessage("Ignoring never file {0} ", toFile.FullName);
                        return(null);
                    }
                    overwrite = true;
                }

                skip = (toFile.Exists && !overwrite);
                return(null);
            });

            if (ret != null)
            {
                return(ret);
            }
            if (skip && Overwrite != OverwriteMode.Confirm)
            {
                VerboseMessage("Ignoring existing file {0} ", toFile.FullName);
                return(null);
            }
            ret = ProcessComplete(from, to, skip, delegate(bool skip1) {
                if (!skip1)
                {
                    // Continue with copy
                    if (toFile.Directory != null && !toFile.Directory.Exists)
                    {
                        toFile.Directory.Create();
                    }

                    if (toFile.Exists && (toFile.Attributes & (FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System)) != 0)
                    {
                        toFile.Attributes &= ~(FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System);
                    }

                    if (Move)
                    {
                        if (toFile.FullName != f.FullName)
                        {
                            toFile.Delete();
                        }
                        VerboseMessage("Move {0} => {1}", f.FullName, toFile.FullName);
                        Context.MoveFile(f.FullName, toFile.FullName, true);
                    }
                    else
                    {
                        VerboseMessage("Copy {0} => {1}", f.FullName, toFile.FullName);
                        Context.CopyFile(f.FullName, toFile.FullName, true);
                    }
                }
                return(null);
            });
            return(ret);
        }
Ejemplo n.º 4
0
        private object downloadSingleFile(IStringFilter nf, Uri single, FileInfo toFile)
        {
            var from = new UriFileInfo(single);
            var ff   = from.Name;

            if (string.IsNullOrEmpty(ff))
            {
                ff = toFile.Name;
            }
            if (nf != null && (!nf.IsMatch(ff)))
            {
                VerboseMessage("{0} did not pass filter", single);
                return(null);
            }

            var    to   = new FileOrDirectoryInfo(toFile);
            bool   skip = false;
            object ret  = ProcessPrepare(from, to,
                                         delegate
            {
                if (toFile.Directory != null && !toFile.Directory.Exists)
                {
                    bool created;
                    object r = createDir(new DirectoryInfo(Path.GetTempPath()), toFile.Directory, out created);
                    if (!created || r != null)
                    {
                        return(r);
                    }
                }


                bool overwrite = (Overwrite == OverwriteMode.Always);
                if (Overwrite == OverwriteMode.IfNewer)
                {
                    if (toFile.Exists && toFile.LastWriteTimeUtc >= from.LastWriteTimeUtc)
                    {
                        VerboseMessage("Ignoring never file {0} ", toFile.FullName);
                        return(null);
                    }
                    overwrite = true;
                }

                skip = (toFile.Exists && !overwrite);
                return(null);
            });

            if (ret != null)
            {
                return(ret);
            }
            if (skip && Overwrite != OverwriteMode.Confirm)
            {
                VerboseMessage("Ignoring existing file {0} ", toFile.FullName);
                return(null);
            }
            ret = ProcessComplete(from, to, skip, delegate(bool skip1)
            {
                if (!skip1)
                {
                    // Continue with copy
                    if (toFile.Directory != null && !toFile.Directory.Exists)
                    {
                        toFile.Directory.Create();
                    }
                    VerboseMessage("Downloading {0} => {1}", from.FullName, toFile.FullName);
                    Download dn = new Download
                    {
                        From      = single.OriginalString,
                        To        = toFile.FullName,
                        Transform = TransformRules.None
                    };
                    return(Context.Execute(dn));
                }
                return(null);
            });
            return(ret);
        }
Ejemplo n.º 5
0
        object extract(ZipFile zip, string rootDirectory, string targetName, ZipEntry entry, Dictionary <string, bool> dirs)
        {
            string dirName;

            if (UsePath)
            {
                dirName = (entry.IsFile) ? Path.GetDirectoryName(Path.GetFullPath(targetName)) : targetName;
            }
            else
            {
                dirName = rootDirectory;
            }

            // Get date time from entry
            DateTime?entryTimeUtc = getEntryTimeUtc(entry);

            // Create directory
            bool     skip        = false;
            string   zipDirName  = string.Empty;
            ZipEntry dirZipEntry = null;

            if (entry.IsFile)
            {
                zipDirName  = Path.GetDirectoryName(entry.Name);
                dirZipEntry = zip.GetEntry(zipDirName);
                if (dirZipEntry == null)
                {
                    dirZipEntry = zip.GetEntry(zipDirName + "/");
                }
            }
            else
            {
                zipDirName  = entry.Name;
                dirZipEntry = entry;
            }

            if (dirs.TryGetValue(zipDirName, out skip) && skip)
            {
                return(null);
            }


            if (entry.IsDirectory)
            {
                DirectoryInfo dir = new DirectoryInfo(dirName);
                var           zz  = new ZipFSEntry(entry, ZipTime);
                var           to  = new FileOrDirectoryInfo(dir);
                object        r   = ProcessPrepare(zz, to, () => null);
                if (r == null)
                {
                    r = ProcessComplete(new ZipFSEntry(entry, ZipTime), new FileOrDirectoryInfo(dir), skip, skp => {
                        dirs[zipDirName] = skip = skp;
                        if (Extract && !skp)
                        {
                            DirectoryInfo di = Directory.CreateDirectory(dirName);
                            setAttributes(di, entry);
                        }
                        return(null);
                    });
                }
                if (r != null || skip)
                {
                    return(r);
                }
            }
            if (entry.IsFile)
            {
                var pfrom = new ZipFSEntry(entry, ZipTime);
                var fi    = new FileInfo(targetName);
                var pto   = new FileOrDirectoryInfo(fi);
                if (fi.Exists)
                {
                    if (Overwrite == OverwriteMode.Never)
                    {
                        skip = true;
                    }
                    if (Overwrite == OverwriteMode.IfNewer)
                    {
                        if (entryTimeUtc == null || entryTimeUtc <= File.GetLastWriteTimeUtc(targetName))
                        {
                            VerboseMessage("Ignoring never file {0}", targetName);
                            skip = true;
                        }
                    }
                }

                if ((skip && Overwrite != OverwriteMode.Confirm))
                {
                    return(null);
                }
                object r = ProcessPrepare(pfrom, pto, () => null);
                if (r != null)
                {
                    return(r);
                }

                return(ProcessComplete(pfrom, pto, skip,
                                       sk =>
                {
                    if (sk || !Extract)
                    {
                        return null;
                    }

                    if (!fi.Directory.Exists)
                    {
                        DirectoryInfo di = Directory.CreateDirectory(dirName);
                        if (dirZipEntry != null)
                        {
                            setAttributes(di, dirZipEntry);
                        }
                    }

                    const FileAttributes mask = (FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System);
                    if (fi.Exists && (fi.Attributes & mask) != 0)
                    {
                        fi.Attributes = fi.Attributes & ~mask;
                    }

                    try
                    {
                        using (Stream outputStream = Context.CreateStream(targetName))
                        {
                            StreamUtils.Copy(zip.GetInputStream(entry), outputStream, new byte[16384],
                                             delegate(object x, ProgressEventArgs y) { Context.OnProgress(1, y.Name); }, ProgressInterval, this, entry.Name, entry.Size);
                        }
                    }
                    catch
                    {
                        File.Delete(targetName);
                        throw;
                    }

                    setAttributes(fi, entry);
                    return null;
                }));
            }
            return(null);
        }
Ejemplo n.º 6
0
        private object downloadSingleFile(IStringFilter nf, Uri single, FileInfo toFile)
        {
            var from = new UriFileInfo(single);
            var ff = from.Name;
            if (string.IsNullOrEmpty(ff))
                ff = toFile.Name;
            if (nf != null && (!nf.IsMatch(ff)))
            {
                VerboseMessage("{0} did not pass filter", single);
                return null;
            }

            var to = new FileOrDirectoryInfo(toFile);
            bool skip = false;
            object ret = ProcessPrepare(from, to,
                delegate
                {
                    if (toFile.Directory != null && !toFile.Directory.Exists)
                    {
                        bool created;
                        object r = createDir(new DirectoryInfo(Path.GetTempPath()), toFile.Directory, out created);
                        if (!created || r != null)
                            return r;
                    }

                    bool overwrite = (Overwrite == OverwriteMode.Always);
                    if (Overwrite == OverwriteMode.IfNewer)
                    {
                        if (toFile.Exists && toFile.LastWriteTimeUtc >= from.LastWriteTimeUtc)
                        {
                            VerboseMessage("Ignoring never file {0} ", toFile.FullName);
                            return null;
                        }
                        overwrite = true;
                    }

                    skip = (toFile.Exists && !overwrite);
                    return null;
                });
            if (ret != null)
                return ret;
            if (skip && Overwrite != OverwriteMode.Confirm)
            {
                VerboseMessage("Ignoring existing file {0} ", toFile.FullName);
                return null;
            }
            ret = ProcessComplete(from, to, skip, delegate(bool skip1)
            {
                if (!skip1)
                {
                    // Continue with copy
                    if (toFile.Directory != null && !toFile.Directory.Exists)
                        toFile.Directory.Create();
                    VerboseMessage("Downloading {0} => {1}", from.FullName, toFile.FullName);
                    Download dn = new Download
                    {
                        From = single.OriginalString,
                        To = toFile.FullName,
                        Transform = TransformRules.None
                    };
                    return Context.Execute(dn);
                }
                return null;
            });
            return ret;
        }
Ejemplo n.º 7
0
 private object createDir(DirectoryInfo fromDir, DirectoryInfo toDir, out bool created)
 {
     var from = new FileOrDirectoryInfo(fromDir);
     var to = new FileOrDirectoryInfo(toDir);
     bool createdTmp=false;
     object ret = ProcessComplete(from, to, false, delegate(bool skip)
            {
                if (!skip)
                {
                    toDir.Create();
                    toDir.Attributes = fromDir.Attributes;
                    toDir.LastWriteTimeUtc = fromDir.LastWriteTimeUtc;
                    createdTmp = true;
                }
                return null;
            });
     created = createdTmp;
     return ret;
 }
Ejemplo n.º 8
0
        private object copySingleFile(IStringFilter nf, FileInfo f, FileInfo toFile)
        {
            if (nf != null && (!nf.IsMatch(f.FullName) || !CheckHidden(f)))
            {
                VerboseMessage("{0} did not pass filter", f.FullName);
                return null;
            }
            var from=new FileOrDirectoryInfo(f);
            var to = new FileOrDirectoryInfo(toFile);
            bool skip = false;
            object ret = ProcessPrepare(from, to,
                delegate
                    {
                        if (toFile.Directory != null && !toFile.Directory.Exists)
                        {
                            bool created;
                            object r = createDir(f.Directory, toFile.Directory, out created);
                            if (!created || r != null)
                                return r;
                        }

                        bool overwrite = (Overwrite == OverwriteMode.Always);
                        if (Overwrite == OverwriteMode.IfNewer)
                        {
                            if (toFile.Exists && toFile.LastWriteTimeUtc >= f.LastWriteTimeUtc)
                            {
                                VerboseMessage("Ignoring never file {0} ", toFile.FullName);
                                return null;
                            }
                            overwrite = true;
                        }

                        skip = (toFile.Exists && !overwrite);
                        return null;
                    });
            if (ret!=null)
                return ret;
            if (skip && Overwrite != OverwriteMode.Confirm)
            {
                VerboseMessage("Ignoring existing file {0} ", toFile.FullName);
                return null;
            }
            ret = ProcessComplete(from,to, skip, delegate(bool skip1) {
                                      if (!skip1)
                                      {
                                          // Continue with copy
                                          if (toFile.Directory != null && !toFile.Directory.Exists)
                                              toFile.Directory.Create();

                                          if (toFile.Exists && (toFile.Attributes & (FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System)) != 0)
                                              toFile.Attributes &= ~(FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System);

                                          if (Move)
                                          {
                                              if (toFile.FullName != f.FullName)
                                                  toFile.Delete();
                                              VerboseMessage("Move {0} => {1}", f.FullName, toFile.FullName);
                                              Context.MoveFile(f.FullName, toFile.FullName, true);
                                          }
                                          else
                                          {
                                              VerboseMessage("Copy {0} => {1}", f.FullName, toFile.FullName);
                                              Context.CopyFile(f.FullName, toFile.FullName, true);
                                          }
                                      }
                                      return null;
                                  });
            return ret;
        }
Ejemplo n.º 9
0
        private object copy(DirectoryInfo rootFrom, DirectoryInfo fromDir, DirectoryInfo toDir,IStringFilter nf, IStringFilter df)
        {
            bool isRoot = (rootFrom == fromDir);
            bool isVisible = (isRoot || CheckHidden(fromDir));
            bool processFiles = isVisible;

            if (processFiles && (df != null && !df.IsMatch(fromDir.FullName)))
            {
                processFiles = false;
                VerboseMessage("{0} did not pass directory filter", fromDir.FullName);
            }

            var from = new FileOrDirectoryInfo(fromDir);
            var to = new FileOrDirectoryInfo(toDir);

            return ProcessPrepare(from, to,
                delegate
                    {
                        if (processFiles)
                        {
                            if (EmptyDirectories && !to.Exists)
                            {
                                bool created;
                                object r = createDir(fromDir, toDir, out created);
                                if (r != null || !created)
                                    return r;
                            }

                            foreach (FileInfo f in fromDir.GetFiles())
                            {
                                FileInfo toFile = new FileInfo(Path.Combine(to.FullName, f.Name));
                                object r = copySingleFile(nf, f, toFile);
                                if (r != null)
                                    return r;
                            }
                        }
                        if (Recursive)
                        {
                            foreach (DirectoryInfo d in fromDir.GetDirectories())
                            {
                                object r = copy(rootFrom, d, new DirectoryInfo(Path.Combine(to.FullName, d.Name)), nf, df);
                                if (r != null)
                                    return r;
                            }
                        }
                        return null;
                    });
        }
Ejemplo n.º 10
0
        object extract(ZipFile zip, string rootDirectory,string targetName, ZipEntry entry, Dictionary<string, bool> dirs)
        {
            string dirName;
            if (UsePath)
                dirName = (entry.IsFile) ? Path.GetDirectoryName(Path.GetFullPath(targetName)) : targetName;
            else
                dirName = rootDirectory;

            // Get date time from entry
            DateTime? entryTimeUtc = getEntryTimeUtc(entry);

            // Create directory
            bool skip = false;
            string zipDirName = string.Empty;
            ZipEntry dirZipEntry = null;

            if (entry.IsFile)
            {
                zipDirName = Path.GetDirectoryName(entry.Name);
                dirZipEntry = zip.GetEntry(zipDirName);
                if (dirZipEntry == null)
                    dirZipEntry = zip.GetEntry(zipDirName + "/");
            }
            else
            {
                zipDirName = entry.Name;
                dirZipEntry = entry;
            }

            if (dirs.TryGetValue(zipDirName, out skip) && skip)
                return null;

            if (entry.IsDirectory)
            {
                DirectoryInfo dir = new DirectoryInfo(dirName);
                var zz = new ZipFSEntry(entry, ZipTime);
                var to=new FileOrDirectoryInfo(dir);
                object r = ProcessPrepare(zz, to, () => null);
                if (r==null)
                    r = ProcessComplete(new ZipFSEntry(entry, ZipTime), new FileOrDirectoryInfo(dir), skip, skp=>{
                        dirs[zipDirName] = skip = skp;
                        if (Extract && !skp)
                        {
                            DirectoryInfo di = Directory.CreateDirectory(dirName);
                            setAttributes(di,entry);
                        }
                        return null;
                    });
                if (r != null || skip)
                    return r;
            }
            if (entry.IsFile)
            {

                var pfrom = new ZipFSEntry(entry, ZipTime);
                var fi = new FileInfo(targetName);
                var pto = new FileOrDirectoryInfo(fi);
                if (fi.Exists)
                {
                    if (Overwrite == OverwriteMode.Never)
                        skip = true;
                    if (Overwrite == OverwriteMode.IfNewer)
                    {
                        if (entryTimeUtc == null || entryTimeUtc <= File.GetLastWriteTimeUtc(targetName))
                        {
                            VerboseMessage("Ignoring never file {0}", targetName);
                            skip = true;
                        }
                    }
                }

                if ((skip && Overwrite != OverwriteMode.Confirm))
                    return null;
                object r = ProcessPrepare(pfrom, pto, () => null);
                if (r != null)
                    return r;

                return ProcessComplete(pfrom, pto, skip,
                    sk =>
                        {
                            if (sk || !Extract)
                                return null;

                            if (!fi.Directory.Exists)
                            {
                                DirectoryInfo di = Directory.CreateDirectory(dirName);
                                if (dirZipEntry != null)
                                    setAttributes(di,dirZipEntry);
                            }

                            const FileAttributes mask = (FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System);
                            if (fi.Exists && (fi.Attributes & mask)!=0)
                                fi.Attributes = fi.Attributes & ~mask;

                            try
                            {
                                using (Stream outputStream = Context.CreateStream(targetName))
                                {
                                    StreamUtils.Copy(zip.GetInputStream(entry), outputStream, new byte[16384],
                                                     delegate(object x, ProgressEventArgs y) { Context.OnProgress(1, y.Name); }, ProgressInterval, this, entry.Name, entry.Size);
                                }
                            }
                            catch
                            {
                                File.Delete(targetName);
                                throw;
                            }

                            setAttributes(fi, entry);
                            return null;
                        });
            }
            return null;
        }