Bari.Core.Build.Cache.FileBuildCache.Restore C# (CSharp) Method

Restore() public method

Restores the stored files for a given builder to a file system directory

The cache only stores the latest stored results and this is what will be restored to the target directory. To verify if it was generated with the correct dependency fingerprint, use IBuildCache.Contains.

To ensure thread safety, use IBuildCache.LockForBuilder.

public Restore ( BuildKey builder, IFileSystemDirectory targetRoot, bool aggressive, Regex aggressiveExceptions = null ) : ISet
builder BuildKey Builder key
targetRoot IFileSystemDirectory Target file system directory
aggressive bool If true, files in the target directory won't be checked by hash before overriding them
aggressiveExceptions Regex Exceptions to the aggresivve mode. Can be null if not used.
return ISet
        public ISet<TargetRelativePath> Restore(BuildKey builder, IFileSystemDirectory targetRoot, bool aggressive, Regex[] aggressiveExceptions = null)
        {
            var lck = GetOrCreateLock(builder);
            lck.EnterReadLock();
            try
            {
                var dirName = GetCacheDirectoryName(builder);
                if (cacheRoot.Value.ChildDirectories.Contains(dirName))
                {
                    var cacheDir = cacheRoot.Value.GetChildDirectory(dirName);
                    if (cacheDir.Files.Contains(NamesFileName))
                    {
                        using (var reader = cacheDir.ReadTextFile(NamesFileName))
                        {
                            var result = new HashSet<TargetRelativePath>();

                            int idx = 0;
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                var parts = line.Split(';');
                                if (parts.Length == 2)
                                {
                                    var relativeRoot = parts[0];
                                    var relativePath = parts[1];
                                    var fullPath = Path.Combine(relativeRoot, relativePath);

                                    var cacheFileName = idx.ToString(CultureInfo.InvariantCulture);

                                    // It is possible that only a file name (a virtual file) was cached without any contents:
                                    if (cacheDir.Exists(cacheFileName))
                                    {
                                        if (aggressive && BeAggressive(aggressiveExceptions, fullPath))
                                            cacheDir.CopyFile(cacheFileName, targetRoot, fullPath);
                                        else
                                        {
                                            if (aggressive)
                                                log.DebugFormat("Agressive cache restore is disabled for file {0}", fullPath);

                                            CopyIfDifferent(cacheDir, cacheFileName, targetRoot, fullPath);
                                        }
                                    }

                                    result.Add(new TargetRelativePath(relativeRoot, relativePath));
                                }
                                idx++;
                            }

                            return result;
                        }
                    }
                }

                return new HashSet<TargetRelativePath>();
            }
            finally
            {
                lck.ExitReadLock();
            }
        }