コード例 #1
0
    public void Dispose_DeletesDirectoryWithContent()
    {
        using (var temporaryDirectory = new TemporaryDirectory(_dirPath))
        {
            temporaryDirectory.PrepareForWriting();
            File.WriteAllText(temporaryDirectory.GetUniquePath(), "a");
        }

        Assert.IsFalse(Directory.Exists(_dirPath));
    }
コード例 #2
0
    public void GetUniquePath_ReturnsUniquePaths()
    {
        using (var temporaryData = new TemporaryDirectory(_dirPath))
        {
            temporaryData.PrepareForWriting();

            for (int i = 0; i < 100; i++)
            {
                string path = temporaryData.GetUniquePath();

                Assert.IsFalse(File.Exists(path));
                Assert.IsFalse(Directory.Exists(path));

                if (i % 2 == 0)
                {
                    File.WriteAllText(temporaryData.GetUniquePath(), "a");
                }
                else
                {
                    Directory.CreateDirectory(path);
                }
            }
        }
    }
コード例 #3
0
        private void PatchFile(
            string fileName, string packageDirPath, string suffix,
            TemporaryDirectory tempDiffDir, CancellationToken cancellationToken)
        {
            _logger.LogDebug(string.Format("Processing patch file entry {0}", fileName));

            var filePath = _localData.Path.PathCombine(fileName);

            _logger.LogTrace("filePath = " + filePath);

            if (!File.Exists(filePath))
            {
                throw new MissingLocalDataFileException(
                          string.Format("Couldn't patch file {0} because it doesn't exists in local data.", fileName));
            }

            var fileVersion = _localMetaData.GetEntryVersionId(fileName);

            _logger.LogTrace("fileVersion = " + fileVersion);

            if (fileVersion != _versionId - 1)
            {
                throw new InvalidLocalDataFileVersionException(string.Format(
                                                                   "Couldn't patch file {0} because expected file version to be ({1}) but it's {2}.",
                                                                   fileName, _versionId - 1, fileVersion));
            }

            _logger.LogDebug("Checking whether patching file content is necessary...");
            if (IsPatchingFileContentNecessary(fileName))
            {
                _logger.LogDebug("Patching is necessary. Generating new file with patched content...");

                var sourceDeltaFilePath = Path.Combine(packageDirPath, fileName + suffix);
                _logger.LogTrace("sourceDeltaFilePath = " + sourceDeltaFilePath);

                if (!File.Exists(sourceDeltaFilePath))
                {
                    throw new MissingFileFromPackageException(string.Format("Cannot find delta file {0} in diff package.",
                                                                            fileName));
                }

                var newFilePath = tempDiffDir.GetUniquePath();
                _logger.LogTrace("newFilePath = " + newFilePath);

                var filePatcher = new FilePatcher(filePath, sourceDeltaFilePath, newFilePath);
                filePatcher.Patch();

                _logger.LogDebug("New file generated. Deleting old file in local data...");
                FileOperations.Delete(filePath, cancellationToken);

                _logger.LogDebug("Old file deleted. Moving new file to local data...");
                FileOperations.Move(newFilePath, filePath, cancellationToken);

                _logger.LogDebug("New file moved.");
            }
            else
            {
                _logger.LogDebug("Patching is not necessary. File content is the same as in previous version.");
            }

            _localMetaData.RegisterEntry(fileName, _versionId);

            _logger.LogDebug("Patch file entry processed.");
        }