Ejemplo n.º 1
0
        private void ApplyPatchRecursively(String APatchRootDirectory, String APatchDirectory)
        {
            string[] directories = System.IO.Directory.GetDirectories(APatchDirectory);

            foreach (string dir in directories)
            {
                ApplyPatchRecursively(APatchRootDirectory, dir);
            }

            TPatchTools patch = new TPatchTools();
            string[] files = System.IO.Directory.GetFiles(APatchDirectory);

            foreach (string filename in files)
            {
                // what to do with the file: add, remove, patch
                string action;
                String TargetFile;

                // find a match with the registered File Patterns
                if ((!GetMatch(filename.Substring(APatchRootDirectory.Length + 1), out action, out TargetFile)))
                {
                    throw new Exception("cannot find a destination path for file " + filename.Substring(APatchRootDirectory.Length + 1));
                }

                // make sure that the path exists
                string TargetPath = Path.GetDirectoryName(TargetFile);

                if (!Directory.Exists(TargetPath))
                {
                    Directory.CreateDirectory(TargetPath);
                }

                // Console.WriteLine(filename + ' ' + TargetFile);
                if (action == "new")
                {
                    if (System.IO.File.Exists(TargetFile))
                    {
                        // prepare for undo; make a copy of the original file
                        System.IO.File.Copy(TargetFile, TargetFile + ".bak", true);
                        File.Delete(TargetFile);
                    }

                    // unzip the file
                    BZip2.Decompress(System.IO.File.OpenRead(filename), System.IO.File.OpenWrite(TargetFile), true);
                }
                else if ((action == "patch") && File.Exists(TargetFile))
                {
                    try
                    {
                        // safety copy
                        System.IO.File.Copy(TargetFile, TargetFile + ".bak", true);

                        // apply patch
                        if (System.IO.File.Exists(TargetFile + ".new"))
                        {
                            System.IO.File.Delete(TargetFile + ".new");
                        }

                        patch.ApplyPatch(TargetFile, TargetFile + ".new", filename);

                        // remove original, rename file
                        if (System.IO.File.Exists(TargetFile + ".new"))
                        {
                            if (System.IO.File.Exists(TargetFile))
                            {
                                System.IO.File.Delete(TargetFile);
                            }

                            System.IO.File.Move(TargetFile + ".new", TargetFile);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception("problem patching file " + TargetFile + ": " + e.Message);
                    }
                }
                else if (action == "rem")
                {
                    if (System.IO.File.Exists(TargetFile))
                    {
                        // safety copy
                        System.IO.File.Copy(TargetFile, TargetFile + ".bak");

                        // remove file
                        System.IO.File.Delete(TargetFile);
                    }
                }
                else if (action == "skip")
                {
                    // skip server files on a remote system
                }
            }
        }