Ejemplo n.º 1
0
 public LocalPackageInfo(PackageReference reference, PackageDownloadInfo downloadStatus, Dto.PackageHashes hashes, Dto.PackageMeta metadata, PackageSequenceInfo sequence)
 {
     LockProvider    = new PackageLocks();
     DownloadMeasure = new MeasureItem(MeasureType.Throughput);
     UploadMeasure   = new MeasureItem(MeasureType.Throughput);
     Reference       = reference ?? throw new ArgumentNullException(nameof(reference));
     DownloadStatus  = downloadStatus ?? throw new ArgumentNullException(nameof(downloadStatus));
     Hashes          = hashes ?? throw new ArgumentNullException(nameof(hashes));
     Metadata        = metadata ?? throw new ArgumentNullException(nameof(metadata));
     Sequence        = sequence ?? throw new ArgumentNullException(nameof(sequence));
     if (!Reference.Id.Equals(DownloadStatus.PackageId))
     {
         throw new ArgumentException("Invalid hash.", nameof(downloadStatus));
     }
     if (!Reference.Id.Equals(Hashes.PackageId))
     {
         throw new ArgumentException("Invalid hash.", nameof(hashes));
     }
     if (!Reference.Id.Equals(Metadata.PackageId))
     {
         throw new ArgumentException("Invalid hash.", nameof(metadata));
     }
     if (!Metadata.PackageSize.Equals(Sequence.PackageSize))
     {
         throw new ArgumentException("Invalid size of package sequence.", nameof(sequence));
     }
 }
Ejemplo n.º 2
0
        public void Valid()
        {
            var  id             = AppInfo.CreateDefaultCryptoProvider().CreateRandom();
            var  version        = new ClientVersion(1);
            var  baseInfo       = PackageSequenceBaseInfo.Default;
            long size           = baseInfo.SegmentLength * 18; // bitmap length: 8bits + 8bits + 2bits
            var  downloadStatus = PackageDownloadInfo.CreateForReadyForDownloadPackage(version, id, new PackageSequenceInfo(baseInfo, size));

            Assert.Equal(3, downloadStatus.Data.SegmentsBitmap.Length);

            // validate
            downloadStatus.ValidateStatusUpdateFromPeer(new PackageStatusDetail()
            {
                SegmentsBitmap = new byte[3] {
                    0x00, 0x00, 0x00
                },
                BytesDownloaded = 0,
                IsFound         = true
            });

            downloadStatus.ValidateStatusUpdateFromPeer(new PackageStatusDetail()
            {
                SegmentsBitmap = new byte[3] {
                    0x00, 0x00, 0b00000011
                },
Ejemplo n.º 3
0
        public PackageDownloadInfo ReadPackageDownloadStatus(PackageReference reference, PackageSequenceInfo sequenceInfo)
        {
            var dto    = ReadPackageFile <PackageDownload>(reference, PackageDownloadFileName);
            var result = new PackageDownloadInfo(dto, sequenceInfo);

            return(result);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public void UpdateDownloadStatus(PackageDownloadInfo status, string directoryPath = null)
        {
            string path = Path.Combine(directoryPath ?? CreatePackagePath(status.PackageId), PackageDownloadFileName);

            File.WriteAllBytes(path, app.MessageSerializer.Serialize(status.Data));
        }