Ejemplo n.º 1
0
        private async Task WriteTag([NotNull] IEnumerable <string> dirs)
        {
            myLogger.Info($"[{DateTime.Now:s}] Writing tag file...");
            var fileId = Guid.NewGuid();

            await using var stream = new MemoryStream();
            await TagUtil.WriteTagScriptAsync(new Tag
            {
                ToolId          = myToolId,
                FileId          = fileId.ToString(),
                Product         = myProduct,
                Version         = myVersion,
                CreationUtcTime = DateTime.UtcNow,
                Properties      = myProperties.Select(x => new TagKeyValue
                {
                    Key   = x.Key,
                    Value = x.Value
                }).ToArray(),
                Directories = dirs.OrderBy(x => x, StringComparer.Ordinal).ToArray()
            }, stream);

            var tagFile = Path.Combine(TagUtil.TagDirectory, myProduct, myProduct + '-' + myVersion + '-' + fileId.ToString("N") + TagUtil.TagExtension);
            await myStorage.CreateForWritingAsync(tagFile, AccessMode.Private, stream);
        }
Ejemplo n.º 2
0
        public async Task <Tuple <Statistics, long> > ValidateAsync(
            [NotNull] IEnumerable <KeyValuePair <string, Tag> > items,
            [NotNull] IReadOnlyCollection <string> files,
            StorageFormat storageFormat,
            ValidateMode mode,
            bool verifyAcl = false)
        {
            myLogger.Info($"[{DateTime.Now:s}] Validating storage{myId}...");
            var     fix        = mode == ValidateMode.Fix || mode == ValidateMode.Delete;
            var     statistics = new Statistics();
            ILogger logger     = new LoggerWithStatistics(myLogger, statistics);

            files = await ValidateDataFilesAsync(logger, files, storageFormat, fix);

            var tree = await CreateDirectoryTreeAsync(files);

            foreach (var item in items)
            {
                var tagFile = item.Key;
                var tag     = item.Value;
                logger.Info($"  Validating {tagFile}");
                var isDirty = false;

                if (!tag.Product.ValidateProduct())
                {
                    logger.Error($"Invalid product {tag.Product} in file {tagFile}");
                }

                if (!tag.Version.ValidateVersion())
                {
                    logger.Error($"Invalid version {tag.Version} in file {tagFile}");
                }

                if (tag.CreationUtcTime == DateTime.MinValue)
                {
                    logger.Error($"The empty creation time in {tagFile}");
                    if (fix)
                    {
                        var newCreationUtcTime = TryFixCreationTime(tag);
                        if (newCreationUtcTime != null)
                        {
                            logger.Fix($"The creation time will be assigned to tag {tagFile}");
                            isDirty             = true;
                            tag.CreationUtcTime = newCreationUtcTime.Value;
                        }
                    }
                }

                if (tag.Directories == null || tag.Directories.Length == 0)
                {
                    logger.Error($"The empty directory list in {tagFile}");
                    if (fix)
                    {
                        logger.Fix($"The tag will be deleted {tagFile}");
                        await myStorage.DeleteAsync(tagFile);

                        continue;
                    }
                }
                else
                {
                    for (var index = 0; index < tag.Directories.Length; index++)
                    {
                        var dir = tag.Directories[index];

                        switch (dir.ValidateAndFixDataPath(StorageFormat.Normal, out var fixedDir))
                        {
                        case PathUtil.ValidateAndFixErrors.Ok:
                            break;

                        case PathUtil.ValidateAndFixErrors.Error:
                            logger.Error($"The tag directory {dir} from {tagFile} has invalid format");
                            break;

                        case PathUtil.ValidateAndFixErrors.CanBeFixed:
                            logger.Error($"The tag directory {dir} from {tagFile} has invalid format");
                            if (fix)
                            {
                                isDirty = true;
                                tag.Directories[index] = fixedDir;
                            }

                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        var dstDir = dir.ValidateAndFixDataPath(storageFormat, out fixedDir) == PathUtil.ValidateAndFixErrors.CanBeFixed ? fixedDir : dir;
                        var node   = tree;
                        foreach (var part in dstDir.GetPathComponents())
                        {
                            node = node?.Lookup(part);
                        }

                        if (node == null)
                        {
                            logger.Error($"The directory {dir} from {tagFile} id wasn't found");
                        }
                        else
                        {
                            node.IncrementReferences();
                        }
                    }
                }

                if (isDirty)
                {
                    logger.Info($"The tag file {tagFile} will be overwritten");
                    await using var stream = new MemoryStream();
                    await TagUtil.WriteTagScriptAsync(tag, stream);

                    await myStorage.CreateForWritingAsync(tagFile, AccessMode.Private, stream);
                }
            }

            var deleted = await ValidateUnreachableAsync(logger, tree, mode);

            if (verifyAcl)
            {
                await ValidateAclAsync(logger, files, fix);
            }
            return(Tuple.Create(statistics, deleted));
        }