private ChunkedFileStream OpenFileStream(CancellationToken cancellationToken) { var parentDirectory = Path.GetDirectoryName(_destinationFilePath); if (!string.IsNullOrEmpty(parentDirectory)) { DirectoryOperations.CreateDirectory(parentDirectory, cancellationToken); } var chunksRange = CalculateContainingChunksRange(_range); int startChunk = (int)(chunksRange.Start / _chunksData.ChunkSize); int endChunk = (int)_range.End; if (_range.End != -1) { endChunk = (int)(chunksRange.End / _chunksData.ChunkSize); if (chunksRange.End % _chunksData.ChunkSize != 0) { endChunk += 1; } } _logger.LogTrace(string.Format("Opening chunked file stream for chunks {0}-{1}", startChunk, endChunk)); return(new ChunkedFileStream(_destinationFilePath, _size, _chunksData, HashFunction, ChunkedFileStream.WorkFlags.PreservePreviousFile, startChunk, endChunk)); }
private void AddFile(string fileName, string packageDirPath, string suffix, CancellationToken cancellationToken) { _logger.LogDebug(string.Format("Processing add file entry {0}", fileName)); var filePath = _localData.Path.PathCombine(fileName); _logger.LogTrace("filePath = " + filePath); var sourceFilePath = Path.Combine(packageDirPath, fileName + suffix); _logger.LogTrace("sourceFilePath = " + sourceFilePath); if (!File.Exists(sourceFilePath)) { throw new MissingFileFromPackageException(string.Format("Cannot find file {0} in diff package.", fileName)); } _logger.LogDebug("Creating file parent directories in local data..."); var fileParentDirPath = Path.GetDirectoryName(filePath); _logger.LogTrace("fileParentDirPath = " + fileParentDirPath); //TODO: Assert that fileParentDirPath is not null // ReSharper disable once AssignNullToNotNullAttribute DirectoryOperations.CreateDirectory(fileParentDirPath, cancellationToken); _logger.LogDebug("File parent directories created in local data."); _logger.LogDebug("Copying file to local data (overwriting if needed)..."); FileOperations.Copy(sourceFilePath, filePath, true, cancellationToken); _logger.LogDebug("File copied to local data."); _localMetaData.RegisterEntry(fileName, _versionId); _logger.LogDebug("Add file entry processed."); }
private static string GenerateOrRead() { var filePath = GetFilePath(); if (File.Exists(filePath)) { string savedSenderId = File.ReadAllText(filePath); if (!string.IsNullOrEmpty(savedSenderId)) { UnityEngine.Debug.Log("SenderId: " + savedSenderId + " (loaded from " + filePath + ")"); return(savedSenderId); } } string senderId = Guid.NewGuid().ToString().Replace("-", ""); string parentDirPath = Path.GetDirectoryName(filePath); if (parentDirPath != null) { DirectoryOperations.CreateDirectory(parentDirPath, CancellationToken.Empty); } File.WriteAllText(filePath, senderId); UnityEngine.Debug.Log("SenderId: " + senderId + " (saved in " + filePath + ")"); return(senderId); }
public override void Execute(CancellationToken cancellationToken) { base.Execute(cancellationToken); Checks.FileExists(_packagePath); if (_versionDiffSummary.CompressionMethod == "pack1") { Assert.IsTrue(File.Exists(_packageMetaPath), "Compression method is pack1, but meta file does not exist"); DebugLogger.Log("Parsing package meta file"); _pack1Meta = Pack1Meta.ParseFromFile(_packageMetaPath); DebugLogger.Log("Package meta file parsed succesfully"); } DebugLogger.Log("Installing diff."); var packageDirPath = _temporaryData.GetUniquePath(); DebugLogger.LogVariable(packageDirPath, "packageDirPath"); DebugLogger.Log("Creating package directory."); DirectoryOperations.CreateDirectory(packageDirPath); try { DebugLogger.Log("Unarchiving files."); string usedSuffix; IUnarchiver unarchiver = CreateUnrachiver(packageDirPath, out usedSuffix); _unarchivePackageStatusReporter.OnProgressChanged(0.0, "Unarchiving package..."); unarchiver.UnarchiveProgressChanged += (name, isFile, entry, amount, entryProgress) => { var entryMinProgress = Mathf.Max(0, entry - 1) / (double)amount; // entry could be zero var entryMaxProgress = entry / (double)amount; var progress = entryMinProgress + (entryMaxProgress - entryMinProgress) * entryProgress; _unarchivePackageStatusReporter.OnProgressChanged(progress, "Unarchiving package..."); }; unarchiver.Unarchive(cancellationToken); _unarchivePackageStatusReporter.OnProgressChanged(1.0, string.Empty); ProcessAddedFiles(packageDirPath, usedSuffix, cancellationToken); ProcessRemovedFiles(cancellationToken); ProcessModifiedFiles(packageDirPath, usedSuffix, cancellationToken); DeleteEmptyMacAppDirectories(); } finally { DebugLogger.Log("Deleting package directory."); if (Directory.Exists(packageDirPath)) { DirectoryOperations.Delete(packageDirPath, true); } } }
private void UnpackDirectory(Pack1Meta.FileEntry file, CancellationToken cancellationToken) { string destPath = Path.Combine(_destinationDirPath, file.Name); DebugLogger.Log("Creating directory " + destPath); DirectoryOperations.CreateDirectory(destPath, cancellationToken); DebugLogger.Log("Directory " + destPath + " created successfully!"); }
private void CreateDataDir() { string dirPath = Path.GetDirectoryName(_filePath); if (dirPath != null) { DirectoryOperations.CreateDirectory(dirPath, CancellationToken.Empty); } }
public static void CreateParents(string path) { var dirName = Path.GetDirectoryName(path); if (dirName != null) { DirectoryOperations.CreateDirectory(dirName, CancellationToken.Empty); } }
public void Clear() { DebugLogger.Log("Clearing download data."); if (Directory.Exists(Path)) { DirectoryOperations.Delete(Path, true); DirectoryOperations.CreateDirectory(Path); } }
public virtual void PrepareForWriting() { DebugLogger.Log("Preparing directory for writing."); DirectoryOperations.CreateDirectory(_path, CancellationToken.Empty); _hasWriteAccess = true; DebugLogger.Log("Directory prepared for writing."); }
private FileStream OpenFileStream(CancellationToken cancellationToken) { var parentDirectory = Path.GetDirectoryName(_destinationFilePath); if (!string.IsNullOrEmpty(parentDirectory)) { DirectoryOperations.CreateDirectory(parentDirectory, cancellationToken); } return(new FileStream(_destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None)); }
public virtual void PrepareForWriting() { DebugLogger.Log("Preparing directory for writing."); if (!_hasWriteAccess) { DebugLogger.Log("Creating directory."); DirectoryOperations.CreateDirectory(_path); _hasWriteAccess = true; } }
public override void Execute(CancellationToken cancellationToken) { base.Execute(cancellationToken); Checks.FileExists(_packagePath); if (_versionDiffSummary.CompressionMethod == "pack1") { Assert.IsTrue(File.Exists(_packageMetaPath), "Compression method is pack1, but meta file does not exist"); DebugLogger.Log("Parsing package meta file"); _pack1Meta = Pack1Meta.ParseFromFile(_packageMetaPath); DebugLogger.Log("Package meta file parsed succesfully"); } DebugLogger.Log("Installing diff."); var packageDirPath = _temporaryData.GetUniquePath(); DebugLogger.LogVariable(packageDirPath, "packageDirPath"); DebugLogger.Log("Creating package directory."); DirectoryOperations.CreateDirectory(packageDirPath); try { DebugLogger.Log("Unarchiving files."); IUnarchiver unarchiver = CreateUnrachiver(packageDirPath); unarchiver.UnarchiveProgressChanged += (name, isFile, entry, amount) => { _unarchivePackageStatusReporter.OnProgressChanged(entry / (double)amount); }; unarchiver.Unarchive(cancellationToken); _unarchivePackageStatusReporter.OnProgressChanged(1.0); ProcessAddedFiles(packageDirPath, cancellationToken); ProcessRemovedFiles(cancellationToken); ProcessModifiedFiles(packageDirPath, cancellationToken); } finally { DebugLogger.Log("Deleting package directory."); if (Directory.Exists(packageDirPath)) { DirectoryOperations.Delete(packageDirPath, true); } } }
private void AddDirectory(string dirName, CancellationToken cancellationToken) { _logger.LogDebug(string.Format("Processing add directory entry {0}", dirName)); var dirPath = _localData.Path.PathCombine(dirName); _logger.LogTrace("dirPath = " + dirPath); _logger.LogDebug("Creating directory in local data..."); DirectoryOperations.CreateDirectory(dirPath, cancellationToken); _logger.LogDebug("Directory created."); _logger.LogDebug("Add directory entry processed."); }
private TemporaryDirectory([NotNull] string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentException("Value cannot be null or empty.", "path"); } Path = path; if (Directory.Exists(Path)) { DirectoryOperations.Delete(Path, CancellationToken.Empty, true); } DirectoryOperations.CreateDirectory(Path, CancellationToken.Empty); }
private void ProcessAddedFiles(string packageDirPath, string suffix, CancellationToken cancellationToken) { DebugLogger.Log("Processing added files."); _addFilesStatusReporter.OnProgressChanged(0.0, "Installing package..."); for (int i = 0; i < _versionDiffSummary.AddedFiles.Length; i++) { cancellationToken.ThrowIfCancellationRequested(); var entryName = _versionDiffSummary.AddedFiles[i]; string entryPath = _localData.Path.PathCombine(entryName); if (entryName.EndsWith("/")) { DirectoryOperations.CreateDirectory(entryPath); // TODO: Uncomment this after fixing directory registration in install content command //_localMetaData.RegisterEntry(entryName, _versionId); } else { string sourceFilePath = Path.Combine(packageDirPath, entryName + suffix); if (!File.Exists(sourceFilePath)) { throw new InstallerException(string.Format("Cannot find file <{0}> in content package.", entryName)); } DebugLogger.LogFormat("Copying {0} -> {1}", sourceFilePath, entryName); DirectoryOperations.CreateParentDirectory(entryPath); FileOperations.Copy(sourceFilePath, entryPath, true); _localMetaData.RegisterEntry(entryName, _versionId); } _addFilesStatusReporter.OnProgressChanged((i + 1) / (double)_versionDiffSummary.AddedFiles.Length, "Installing package..."); } _addFilesStatusReporter.OnProgressChanged(1.0, "Installing package..."); }
public override void Execute(CancellationToken cancellationToken) { base.Execute(cancellationToken); foreach (var entry in _entries) { var tempDirName = _packagePath + string.Format("{0}_{1}_{2}", entry.Name, entry.Offset, entry.Size); TemporaryDirectory.ExecuteIn(tempDirName, (tempDir) => { _logger.LogDebug(string.Format("Repairing the file {0}", entry.Name)); string packagePath = Path.Combine(tempDir.Path, ".pack" + Path.GetRandomFileName()); string unarchivePath = Path.Combine(tempDir.Path, Path.GetRandomFileName()); if (!Directory.Exists(unarchivePath)) { DirectoryOperations.CreateDirectory(unarchivePath, cancellationToken); } var downloader = new ChunkedHttpDownloader(packagePath, _resource.ResourceUrls, _resource.ChunksData, _resource.Size); long start = entry.Offset.GetValueOrDefault(); long end = (start + entry.Size.GetValueOrDefault()) - 1; // Offset by 1 to denote a byte index var range = new BytesRange(start, end); downloader.SetRange(range); var effectiveRange = downloader.CalculateContainingChunksRange(range); long totalData = effectiveRange.End == -1 ? _resource.Size - effectiveRange.Start : effectiveRange.End - effectiveRange.Start; var downloadStatus = _entryStatus[entry].DownloadStatus; var repairStatus = _entryStatus[entry].RepairStatus; downloadStatus.IsActive.Value = true; downloadStatus.TotalBytes.Value = totalData; downloadStatus.Description.Value = "Downloading broken file..."; downloadStatus.Bytes.Value = 0; downloader.DownloadProgressChanged += downloadedBytes => { downloadStatus.Bytes.Value = downloadedBytes; }; _logger.LogDebug(string.Format("Downloading the partial package with range {0}-{1}", start, end)); downloader.Download(cancellationToken); downloadStatus.IsActive.Value = false; repairStatus.IsActive.Value = true; repairStatus.Description.Value = "Reparing broken file..."; repairStatus.Progress.Value = 0.0; _logger.LogDebug("Unarchiving the package."); var unarchiver = new Pack1Unarchiver(packagePath, _meta, unarchivePath, _packagePassword, _unpackingSuffix, effectiveRange); unarchiver.UnarchiveProgressChanged += (name, isFile, unarchiveEntry, amount, entryProgress) => { repairStatus.Progress.Value = entryProgress; }; unarchiver.UnarchiveSingleFile(entry, cancellationToken); EmplaceFile(Path.Combine(unarchivePath, entry.Name + _unpackingSuffix), Path.Combine(_localData.Path, entry.Name), cancellationToken); repairStatus.IsActive.Value = false; }); } }
public override void Execute(CancellationToken cancellationToken) { base.Execute(cancellationToken); Checks.FileExists(_packagePath); Assert.IsTrue(_localMetaData.GetRegisteredEntries().Length == 0, "Cannot install content if previous version is still present."); if (_versionContentSummary.CompressionMethod == "pack1") { Assert.IsTrue(File.Exists(_packageMetaPath), "Compression method is pack1, but meta file does not exist"); DebugLogger.Log("Parsing package meta file"); _pack1Meta = Pack1Meta.ParseFromFile(_packageMetaPath); DebugLogger.Log("Package meta file parsed succesfully"); } DebugLogger.Log("Installing content."); var packageDirPath = _temporaryData.GetUniquePath(); DebugLogger.LogVariable(packageDirPath, "destinationDir"); DebugLogger.Log("Creating package directory."); DirectoryOperations.CreateDirectory(packageDirPath); try { DebugLogger.Log("Unarchiving package."); IUnarchiver unarchiver = CreateUnrachiver(packageDirPath); unarchiver.UnarchiveProgressChanged += (name, isFile, entry, amount) => { _unarchivePackageStatusReporter.OnProgressChanged(entry / (double)amount); }; unarchiver.Unarchive(cancellationToken); _unarchivePackageStatusReporter.OnProgressChanged(1.0); DebugLogger.Log("Copying files."); for (int i = 0; i < _versionContentSummary.Files.Length; i++) { cancellationToken.ThrowIfCancellationRequested(); InstallFile(_versionContentSummary.Files[i].Path, packageDirPath); _copyFilesStatusReporter.OnProgressChanged((i + 1) / (double)_versionContentSummary.Files.Length); } _copyFilesStatusReporter.OnProgressChanged(1.0); } finally { DebugLogger.Log("Deleting package directory."); if (Directory.Exists(packageDirPath)) { DirectoryOperations.Delete(packageDirPath, true); } } }