Esempio n. 1
0
        public static Hash FileHashCached(this AbsolutePath file, bool nullOnIoError = false)
        {
            if (TryGetHashCache(file, out var foundHash))
            {
                return(foundHash);
            }

            var hash = file.FileHash(nullOnIoError);

            if (hash != Hash.Empty)
            {
                WriteHashCache(file, hash);
            }
            return(hash);
        }
Esempio n. 2
0
        public static async Task <VirtualFile> Analyze(Context context, VirtualFile parent, AbsolutePath absPath,
                                                       IPath relPath, int depth = 0)
        {
            var hash = absPath.FileHash();

            if (!context.UseExtendedHashes && FileExtractor.MightBeArchive(absPath))
            {
                var result = await TryGetContentsFromServer(hash);

                if (result != null)
                {
                    Utils.Log($"Downloaded VFS data for {(string)absPath}");

                    VirtualFile Convert(IndexedVirtualFile file, IPath path, VirtualFile vparent)
                    {
                        var vself = new VirtualFile
                        {
                            Context      = context,
                            Name         = path,
                            Parent       = vparent,
                            Size         = file.Size,
                            LastModified = absPath.LastModifiedUtc.AsUnixTime(),
                            LastAnalyzed = DateTime.Now.AsUnixTime(),
                            Hash         = file.Hash
                        };

                        vself.Children = file.Children.Select(f => Convert(f, f.Name, vself)).ToImmutableList();

                        return(vself);
                    }

                    return(Convert(result, relPath, parent));
                }
            }

            var self = new VirtualFile
            {
                Context      = context,
                Name         = relPath,
                Parent       = parent,
                Size         = absPath.Size,
                LastModified = absPath.LastModifiedUtc.AsUnixTime(),
                LastAnalyzed = DateTime.Now.AsUnixTime(),
                Hash         = hash
            };

            self.FillFullPath(depth);

            if (context.UseExtendedHashes)
            {
                self.ExtendedHashes = ExtendedHashes.FromFile(absPath);
            }

            if (await FileExtractor.CanExtract(absPath))
            {
                await using var tempFolder = Context.GetTemporaryFolder();
                await FileExtractor.ExtractAll(context.Queue, absPath, tempFolder.FullName);

                var list = await tempFolder.FullName.EnumerateFiles()
                           .PMap(context.Queue,
                                 absSrc => Analyze(context, self, absSrc, absSrc.RelativeTo(tempFolder.FullName), depth + 1));

                self.Children = list.ToImmutableList();
            }

            return(self);
        }