Example #1
0
        public void DeletePackage(LocalPackageInfo packageInfo)
        {
            if (packageInfo == null)
            {
                throw new ArgumentNullException(nameof(packageInfo));
            }

            logger.LogInformation($"Deleting folder {packageInfo.Reference.FolderPath}.");
            Directory.Delete(packageInfo.Reference.FolderPath, recursive: true);
            logger.LogInformation($"Folder deleted {packageInfo.Reference.FolderPath}.");
        }
Example #2
0
        public async Task <PackageDataValidatorResult> ValidatePackageAsync(LocalPackageInfo packageInfo, MeasureItem measure)
        {
            logger.LogDebug($"Starting validation of package {packageInfo}.");
            var result = await ValidatePackageAsyncInternal(packageInfo);

            if (result.IsValid)
            {
                logger.LogInformation($"Package {packageInfo} is valid.");
            }
            else
            {
                logger.LogWarning($"Package {packageInfo} validation FAILED:\n{string.Join("\n", result.Errors)}");
            }
            return(result);
        }
Example #3
0
        public void ExtractPackage(LocalPackageInfo packageInfo, string targetFolder, bool validate)
        {
            if (packageInfo == null)
            {
                throw new ArgumentNullException(nameof(packageInfo));
            }

            // rent package lock
            if (!packageInfo.LockProvider.TryLock(out object lockToken))
            {
                throw new InvalidOperationException("Package is marked to delete, can't extract it.");
            }
            try
            {
                if (validate)
                {
                    // validate
                    var validator = new PackageDataValidator(app.LoggerFactory, app.Crypto);
                    var result    = validator.ValidatePackageAsync(packageInfo, measure: null).Result;
                    if (!result.IsValid)
                    {
                        throw new InvalidOperationException($"Cannot validate package {packageInfo}:\n{string.Join("\n", result.Errors)}");
                    }
                }

                logger.LogInformation($"Extracting package {packageInfo} to folder: {targetFolder}");

                // read all and extract
                var sequencer = new PackagePartsSequencer();
                IEnumerable <PackageDataStreamPart> allParts = sequencer.GetPartsForPackage(packageInfo.Reference.FolderPath, packageInfo.Sequence);
                using (var readController = new ReadPackageDataStreamController(app.LoggerFactory, packageInfo.Reference, packageInfo.Sequence, allParts))
                    using (var readStream = new PackageDataStream(app.LoggerFactory, readController))
                    {
                        var archive = new PackageArchive(app.CompatibilityChecker, app.MessageSerializer);
                        archive.ReadToFolder(readStream, targetFolder);
                    }

                logger.LogInformation($"Package {packageInfo} has been extracted.");
            }
            finally
            {
                packageInfo.LockProvider.Unlock(lockToken);
            }
        }
Example #4
0
        public LocalPackageInfo RegisterPackage(PackageHashes hashes, PackageMeta metadata)
        {
            if (hashes == null)
            {
                throw new ArgumentNullException(nameof(hashes));
            }

            // create directory
            EnsurePath();
            string packagePath = CreatePackagePath(hashes.PackageId);

            if (Directory.Exists(packagePath))
            {
                logger.LogError("Can't add package with Id {0:s}. This hash already exists in local repository.", hashes.PackageId);
                throw new InvalidOperationException("Package already exists in local repository.");
            }
            Directory.CreateDirectory(packagePath);

            // store data
            var packageSequence = hashes.CreatePackageSequence();
            PackageDownloadInfo downloadStatus = PackageDownloadInfo.CreateForReadyForDownloadPackage(app.Version, hashes.PackageId, packageSequence);

            UpdateDownloadStatus(downloadStatus);
            UpdateHashes(hashes);
            UpdateMetadata(metadata);

            // allocate
            var allocator = new PackageDataAllocator(app.LoggerFactory);

            allocator.Allocate(packagePath, hashes.CreatePackageSequence(), overwrite: false);

            // log and build result
            logger.LogInformation($"New package {hashes.PackageId:s4} added to repository and allocated. Size: {SizeFormatter.ToString(hashes.PackageSize)}");

            var reference = new PackageReference(packagePath, hashes.PackageId);
            var result    = new LocalPackageInfo(reference, downloadStatus, hashes, metadata, packageSequence);

            return(result);
        }
Example #5
0
        private async Task <PackageDataValidatorResult> ValidatePackageAsyncInternal(LocalPackageInfo packageInfo)
        {
            if (packageInfo == null)
            {
                throw new ArgumentNullException(nameof(packageInfo));
            }

            if (!packageInfo.DownloadStatus.IsDownloaded)
            {
                // remark: this can be implemented but don't need it now
                throw new InvalidOperationException("Can't validate integrity of not fully downloaded package.");
            }

            // basic input data integrity validations
            if (packageInfo.Hashes.PackageSize != packageInfo.Sequence.PackageSize)
            {
                return(PackageDataValidatorResult.WithError("Hashes file provided invalid package size that does not match with sequence."));
            }

            if (packageInfo.Metadata.PackageSize != packageInfo.Sequence.PackageSize)
            {
                return(PackageDataValidatorResult.WithError("Metadata file provided invalid package size that does not match with sequence."));
            }

            if (packageInfo.Sequence.SegmentsCount != packageInfo.Hashes.PackageSegmentsHashes.Length)
            {
                return(PackageDataValidatorResult.WithError("Hashes file provided invalid count of segments that does not match with sequence."));
            }

            // validate package hash calculated from segment hashes
            var calculatedPackageHash = cryptoProvider.HashFromHashes(packageInfo.Hashes.PackageSegmentsHashes);

            if (!calculatedPackageHash.Equals(packageInfo.Id))
            {
                return(PackageDataValidatorResult.WithError($"Hash mismatch. Calculated package hash is {calculatedPackageHash:s} but expected is {packageInfo.Id:s}."));
            }

            // before working with files - obtain lock to make sure package is not deleted on check
            if (!packageInfo.LockProvider.TryLock(out object lockToken))
            {
                throw new InvalidOperationException("Can't obtain lock for this package. It is marked for deletion.");
            }
            try
            {
                // start checking files
                var errors    = new List <string>();
                var sequencer = new PackagePartsSequencer();

                // check if data files exists and if correct size
                foreach (var dataFile in sequencer.GetDataFilesForPackage(packageInfo.Reference.FolderPath, packageInfo.Sequence))
                {
                    try
                    {
                        var fileInfo = new FileInfo(dataFile.Path);
                        if (!fileInfo.Exists)
                        {
                            errors.Add($"Expected data file not found. File: {dataFile.Path}");
                            continue;
                        }

                        if (fileInfo.Length != dataFile.DataFileLength)
                        {
                            errors.Add($"Invalid length of data file. Expected is {dataFile.DataFileLength}b but actual is {fileInfo.Length}b. File: {dataFile.Path}");
                            continue;
                        }
                    }
                    catch (Exception e)
                    {
                        errors.Add($"Can't validate file \"{ dataFile.Path }\". Reason: {e.Message}");
                    }
                }

                // don't continue if files are not OK
                if (errors.Any())
                {
                    return(PackageDataValidatorResult.WithErrors(errors));
                }

                // do file hashes check
                IEnumerable <PackageDataStreamPart> allParts = sequencer.GetPartsForPackage(packageInfo.Reference.FolderPath, packageInfo.Sequence);
                try
                {
                    using (var readPackageController = new ReadPackageDataStreamController(loggerFactory, packageInfo.Reference, packageInfo.Sequence, allParts))
                        using (var readPackageStream = new PackageDataStream(loggerFactory, readPackageController))
                            using (var validatePackageController = new ValidatePackageDataStreamController(loggerFactory, cryptoProvider, packageInfo.Sequence, packageInfo.Hashes, allParts, nestedStream: null))
                                using (var validatePackageStream = new PackageDataStream(loggerFactory, validatePackageController))
                                {
                                    await readPackageStream.CopyToAsync(validatePackageStream);
                                }
                }
                catch (HashMismatchException e)
                {
                    errors.Add($"Data file segment hash mismatch: {e.Message}");
                }
                catch (Exception e)
                {
                    errors.Add($"Can't process data files to validation. {e.ToString()}");
                }

                // get result
                if (errors.Any())
                {
                    return(PackageDataValidatorResult.WithErrors(errors));
                }
                return(PackageDataValidatorResult.Valid);
            }
            finally
            {
                packageInfo.LockProvider.Unlock(lockToken);
            }
        }
Example #6
0
        public LocalPackageInfo CreatePackageFromFolder(string folderToProcess, string name, MeasureItem writeMeasure)
        {
            var operationMeasure = Stopwatch.StartNew();

            // storage folder for package
            EnsurePath();
            name = string.IsNullOrWhiteSpace(name) ? FileHelper.GetFileOrDirectoryName(folderToProcess) : name;
            DirectoryInfo buildDirectory = Directory.CreateDirectory(CreateBuildPath());


            logger.LogInformation($"Creating package \"{name}\" from folder: {folderToProcess}");

            // create package archive
            PackageHashes packageHashes;
            int           entriesCount;

            using (var controller = new CreatePackageDataStreamController(app.Version, app.LoggerFactory, app.Crypto, sequenceForNewPackages, buildDirectory.FullName))
            {
                using (var packageStream = new PackageDataStream(app.LoggerFactory, controller)
                {
                    Measure = writeMeasure
                })
                {
                    var archive = new PackageArchive(app.CompatibilityChecker, app.MessageSerializer);
                    archive.WriteFromFolder(folderToProcess, packageStream);
                    entriesCount = archive.EntriesCount;
                }
                packageHashes = controller.PackageId;
            }


            // store package hashes
            UpdateHashes(packageHashes, directoryPath: buildDirectory.FullName);

            // store download status
            PackageSequenceInfo packageSequence = packageHashes.CreatePackageSequence();
            PackageDownloadInfo downloadStatus  = PackageDownloadInfo.CreateForCreatedPackage(app.Version, packageHashes.PackageId, packageSequence);

            UpdateDownloadStatus(downloadStatus, directoryPath: buildDirectory.FullName);

            // store metadata
            var metadata = new PackageMeta()
            {
                Created     = DateTimeOffset.Now,
                Name        = name,
                PackageSize = packageHashes.PackageSize,
                Version     = app.Version,
                PackageId   = packageHashes.PackageId
            };

            UpdateMetadata(metadata, directoryPath: buildDirectory.FullName);

            // rename folder
            string packagePath = CreatePackagePath(packageHashes.PackageId);

            if (Directory.Exists(packagePath))
            {
                throw new InvalidOperationException($"Folder for package {packageHashes.PackageId:s} already exists. {packagePath}");
            }
            Directory.Move(buildDirectory.FullName, packagePath);

            operationMeasure.Stop();
            logger.LogInformation($"Created package \"{packagePath}\":\nHash: {packageHashes.PackageId}\nSize: {SizeFormatter.ToString(packageHashes.PackageSize)}\nFiles and directories: {entriesCount}\nTime: {operationMeasure.Elapsed}");

            var reference = new PackageReference(packagePath, packageHashes.PackageId);
            var result    = new LocalPackageInfo(reference, downloadStatus, packageHashes, metadata, packageSequence);

            return(result);
        }