private bool ApplyDiffToFile(string filePath, string diffPath, string targetPath, int patchItemIndex)
        {
            PatchItem item = patchInfo.Files[patchItemIndex];

            FileInfo targetFile = new FileInfo(targetPath);

            if (targetFile.Exists && targetFile.MatchesSignature(item.AfterFileSize, item.AfterMd5Hash))
            {
                comms.Log(Localization.Get(StringId.AlreadyUpToDateXthFile, patchItemIndex + 1, patchInfo.Files.Count, item.Path));
                return(true);
            }

            if (item.BeforeFileSize == 0L)
            {
                comms.Log(Localization.Get(StringId.CreatingXthFile, patchItemIndex + 1, patchInfo.Files.Count, item.Path));

                Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
                File.Copy(diffPath, targetPath, true);
                File.Delete(diffPath);

                return(true);
            }

            FileInfo localFile = new FileInfo(filePath);

            if (!localFile.Exists || !localFile.MatchesSignature(item.BeforeFileSize, item.BeforeMd5Hash))
            {
                return(false);
            }

            comms.Log(Localization.Get(StringId.UpdatingXthFile, patchItemIndex + 1, patchInfo.Files.Count, item.Path));

            FilePatchProgress progress       = null;
            string            tempOutputPath = diffPath + "_.tmp";

            if (comms.LogProgress)
            {
                progress = new FilePatchProgress(Path.GetFileName(filePath));
                comms.SetProgress(progress);
            }

            OctoUtils.ApplyDelta(filePath, tempOutputPath, diffPath, progress);

            FileInfo updatedFile = new FileInfo(tempOutputPath);

            if (!updatedFile.Exists || !updatedFile.MatchesSignature(item.AfterFileSize, item.AfterMd5Hash))
            {
                return(false);
            }

            Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
            File.Copy(tempOutputPath, targetPath, true);
            File.Delete(tempOutputPath);
            File.Delete(diffPath);

            return(true);
        }
示例#2
0
        private void TraverseIncrementalPatchRecursively(DirectoryInfo directory, string relativePath)
        {
            FileInfo[] files = directory.GetFiles();
            for (int i = 0; i < files.Length; i++)
            {
                if (cancel)
                {
                    return;
                }

                FileInfo fileInfo         = files[i];
                string   fileRelativePath = relativePath + fileInfo.Name;
                if (!ignoredPathsRegex.PathMatchesPattern(fileRelativePath))
                {
                    string targetAbsolutePath   = previousVersionRoot + fileRelativePath;
                    string diffFileAbsolutePath = incrementalPatchTempPath + fileRelativePath;

                    if (!File.Exists(targetAbsolutePath))
                    {
                        Log(Localization.Get(StringId.CopyingXToPatch, fileRelativePath));

                        PatchUtils.CopyFile(fileInfo.FullName, diffFileAbsolutePath);
                        incrementalPatch.Files.Add(new PatchItem(fileRelativePath, null, fileInfo));
                    }
                    else
                    {
                        FileInfo prevVersion = new FileInfo(targetAbsolutePath);
                        if (!fileInfo.MatchesSignature(prevVersion))
                        {
                            Log(Localization.Get(StringId.CalculatingDiffOfX, fileRelativePath));

                            OctoUtils.CalculateDelta(targetAbsolutePath, fileInfo.FullName, diffFileAbsolutePath, diffQuality);
                            incrementalPatch.Files.Add(new PatchItem(fileRelativePath, prevVersion, fileInfo));
                        }
                    }
                }
            }

            DirectoryInfo[] subDirectories = directory.GetDirectories();
            for (int i = 0; i < subDirectories.Length; i++)
            {
                if (cancel)
                {
                    return;
                }

                string directoryRelativePath = relativePath + subDirectories[i].Name + Path.DirectorySeparatorChar;
                if (!ignoredPathsRegex.PathMatchesPattern(directoryRelativePath))
                {
                    Directory.CreateDirectory(incrementalPatchTempPath + directoryRelativePath);
                    TraverseIncrementalPatchRecursively(subDirectories[i], directoryRelativePath);
                }
            }
        }