private static void CleanAnalysisCacheStore(AnalysisCacheIndex cacheIndex) { try { ModuleIntrinsics.Tracer.WriteLine("Entering CleanAnalysisCacheStore.", new object[0]); string str = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\Windows\PowerShell\CommandAnalysis\"); List <string> list = new List <string>(); HashSet <string> set = new HashSet <string>(); foreach (string str2 in cacheIndex.Entries.Keys) { AnalysisCacheIndexEntry entry = cacheIndex.Entries[str2]; string path = Path.Combine(str, entry.Path); ModuleIntrinsics.Tracer.WriteLine("Cache index contains " + path, new object[0]); if (!File.Exists(str2)) { ModuleIntrinsics.Tracer.WriteLine("Module + " + str2 + " no longer exists. Deleting its index entry.", new object[0]); File.Delete(path); list.Add(str2); } else { set.Add(path); } } foreach (string str4 in list) { cacheIndex.Entries.Remove(str4); } ModuleIntrinsics.Tracer.WriteLine("Searching for files with no cache entries.", new object[0]); foreach (string str5 in Directory.EnumerateFiles(str, "PowerShell_AnalysisCacheEntry_*")) { if (!set.Contains(str5)) { ModuleIntrinsics.Tracer.WriteLine("Found stale file: " + str5, new object[0]); File.Delete(str5); } } ModuleIntrinsics.Tracer.WriteLine("Saving cache index.", new object[0]); cacheIndex.LastMaintenance = DateTime.Now; SaveCacheIndex(cacheIndex); } catch (IOException exception) { ModuleIntrinsics.Tracer.WriteLine("Got an IO exception during cache maintenance: " + exception.ToString(), new object[0]); disableDiskBasedCache = true; } catch (UnauthorizedAccessException exception2) { ModuleIntrinsics.Tracer.WriteLine("Got an UnauthorizedAccessException during cache maintenance: " + exception2.ToString(), new object[0]); disableDiskBasedCache = true; } }
public static AnalysisCacheIndex GetCacheIndexFromDisk(string basePath) { AnalysisCacheIndex cacheIndex = null; bool flag = false; try { ModuleIntrinsics.Tracer.WriteLine("Deserializing cache index from " + Path.Combine(basePath, "PowerShell_AnalysisCacheIndex"), new object[0]); cacheIndex = DeserializeFromFile <AnalysisCacheIndex>(basePath, "PowerShell_AnalysisCacheIndex"); } catch (Exception exception) { ModuleIntrinsics.Tracer.WriteLine("Got an exception deserializing index: " + exception.ToString(), new object[0]); CommandProcessorBase.CheckForSevereException(exception); flag = true; } if (cacheIndex == null) { ModuleIntrinsics.Tracer.WriteLine("Creating new index, couldn't get one from disk.", new object[0]); cacheIndex = new AnalysisCacheIndex { LastMaintenance = DateTime.Now }; } if (cacheIndex.Entries == null) { cacheIndex.Entries = new Dictionary <string, AnalysisCacheIndexEntry>(StringComparer.OrdinalIgnoreCase); } if (!flag) { TimeSpan span = (TimeSpan)(DateTime.Now - cacheIndex.LastMaintenance); if (span.TotalDays <= 7.0) { return(cacheIndex); } } if (flag) { ModuleIntrinsics.Tracer.WriteLine("Cleaning analysis store because it was corrupted.", new object[0]); } else { ModuleIntrinsics.Tracer.WriteLine("Cleaning analysis store for its 7-day maintenance window. Last maintenance was " + cacheIndex.LastMaintenance, new object[0]); } if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PSDisableModuleAutoloadingCacheMaintenance"))) { CleanAnalysisCacheStore(cacheIndex); } return(cacheIndex); }
private static void SaveCacheIndex(AnalysisCacheIndex index) { try { ModuleIntrinsics.Tracer.WriteLine("Serializing index to PowerShell_AnalysisCacheIndex", new object[0]); savedCacheIndex = index; SerializeToFile(index, "PowerShell_AnalysisCacheIndex"); } catch (IOException exception) { ModuleIntrinsics.Tracer.WriteLine("Got an IO exception saving cache index: " + exception.ToString(), new object[0]); disableDiskBasedCache = true; } catch (UnauthorizedAccessException exception2) { ModuleIntrinsics.Tracer.WriteLine("Got an unauthorized access exception saving cache index: " + exception2.ToString(), new object[0]); disableDiskBasedCache = true; } }
public static Dictionary <string, List <CommandTypes> > Get(string basePath, string path) { AnalysisCacheIndex cacheIndex = null; ModuleIntrinsics.Tracer.WriteLine("Getting analysis cache entry for " + path + ".", new object[0]); cacheIndex = GetCacheIndex(basePath); if (cacheIndex == null) { ModuleIntrinsics.Tracer.WriteLine("Could not get cache index. Returning.", new object[0]); return(null); } AnalysisCacheIndexEntry entry = null; if (cacheIndex.Entries.TryGetValue(path, out entry)) { ModuleIntrinsics.Tracer.WriteLine("Found cache entry for " + path, new object[0]); if (new FileInfo(path).LastWriteTime == entry.LastWriteTime) { ModuleIntrinsics.Tracer.WriteLine("LastWriteTime is current.", new object[0]); try { ModuleIntrinsics.Tracer.WriteLine("Deserializing from " + Path.Combine(basePath, entry.Path), new object[0]); return(DeserializeFromFile <Dictionary <string, List <CommandTypes> > >(basePath, entry.Path)); } catch (Exception exception) { ModuleIntrinsics.Tracer.WriteLine("Got an exception deserializing: " + exception.ToString(), new object[0]); CommandProcessorBase.CheckForSevereException(exception); if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PSDisableModuleAutoloadingCacheMaintenance"))) { ModuleIntrinsics.Tracer.WriteLine("Cleaning cache store.", new object[0]); CleanAnalysisCacheStore(cacheIndex); } ModuleIntrinsics.Tracer.WriteLine("Returning NULL due to exception.", new object[0]); return(null); } } ModuleIntrinsics.Tracer.WriteLine("Returning NULL - LastWriteTime does not match.", new object[0]); return(null); } ModuleIntrinsics.Tracer.WriteLine("Returning NULL - not cached.", new object[0]); return(null); }
private static AnalysisCacheIndex GetCacheIndex(string basePath) { AnalysisCacheIndex savedCacheIndex = null; if (AnalysisCache.savedCacheIndex != null) { ModuleIntrinsics.Tracer.WriteLine("Found in-memory cache entry.", new object[0]); if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PSDisableModuleAutoLoadingMemoryCache"))) { savedCacheIndex = AnalysisCache.savedCacheIndex; } } if (savedCacheIndex == null) { ModuleIntrinsics.Tracer.WriteLine("No in-memory entry. Getting cache index.", new object[0]); savedCacheIndex = GetCacheIndexFromDisk(basePath); AnalysisCache.savedCacheIndex = savedCacheIndex; } return(savedCacheIndex); }
private static void Cache <T>(string basePath, string path, T value) { bool flag = false; ModuleIntrinsics.Tracer.WriteLine("Caching " + value + " to disk.", new object[0]); AnalysisCacheIndex cacheIndex = GetCacheIndex(basePath); if (cacheIndex == null) { ModuleIntrinsics.Tracer.WriteLine("Could not get cache index. Returning.", new object[0]); } else { AnalysisCacheIndexEntry entry; ModuleIntrinsics.Tracer.WriteLine("Got cache index.", new object[0]); if (!cacheIndex.Entries.TryGetValue(path, out entry)) { entry = new AnalysisCacheIndexEntry(); string str = Guid.NewGuid().ToString(); entry.Path = "PowerShell_AnalysisCacheEntry_" + str; flag = true; ModuleIntrinsics.Tracer.WriteLine("Item not already in cache. Caching to " + entry.Path + ", need to update index.", new object[0]); } DateTime lastWriteTime = new FileInfo(path).LastWriteTime; if (entry.LastWriteTime != lastWriteTime) { ModuleIntrinsics.Tracer.WriteLine(string.Concat(new object[] { "LastWriteTime for ", path, " + has changed. Old: ", entry.LastWriteTime, ", new: ", lastWriteTime, ". Need to update index." }), new object[0]); entry.LastWriteTime = lastWriteTime; flag = true; } string str2 = entry.Path; ModuleIntrinsics.Tracer.WriteLine("Caching to " + str2, new object[0]); if (flag) { cacheIndex = GetCacheIndexFromDisk(basePath); cacheIndex.Entries[path] = entry; } try { if (savedCacheIndex != null) { savedCacheIndex.Entries[path] = entry; } SerializeToFile(value, str2); } catch (IOException exception) { ModuleIntrinsics.Tracer.WriteLine("Couldn't serialize file due to IOException - " + exception.ToString(), new object[0]); disableDiskBasedCache = true; } catch (UnauthorizedAccessException exception2) { ModuleIntrinsics.Tracer.WriteLine("Couldn't serialize file due to UnauthorizedAccessException - " + exception2.ToString(), new object[0]); disableDiskBasedCache = true; } if (flag && !disableDiskBasedCache) { ModuleIntrinsics.Tracer.WriteLine("Serializing index.", new object[0]); SaveCacheIndex(cacheIndex); savedCacheIndex = null; } } }