FileCache.h (2332B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <AppInstallerRuntime.h> 5 #include <AppInstallerSHA256.h> 6 #include <filesystem> 7 #include <istream> 8 #include <sstream> 9 10 namespace AppInstaller::Caching 11 { 12 // A file cache for relatively small files (they are always full loaded into memory due to the hash enforcement). 13 struct FileCache 14 { 15 // The supported file cache types. 16 enum class Type 17 { 18 // Manifests for index V1. 19 IndexV1_Manifest, 20 // Package version data files for index V2. 21 IndexV2_PackageVersionData, 22 // Manifests for index V2. 23 IndexV2_Manifest, 24 // Icon for use during show command when sixel rendering is enabled. 25 Icon, 26 #ifndef AICLI_DISABLE_TEST_HOOKS 27 // The test type. 28 Tests, 29 #endif 30 }; 31 32 // Contains information about a specific file cache instance. 33 struct Details 34 { 35 Details(Type type, std::string identifier); 36 37 Runtime::PathName BasePath; 38 Type Type; 39 std::string Identifier; 40 41 // Gets the full path to the cache directory. 42 std::filesystem::path GetCachePath() const; 43 }; 44 45 // Construct a file cache for the given instance. 46 FileCache(Type type, std::string identifier, std::vector<std::string> sources); 47 48 // Gets the details for this file cache. 49 const Details& GetDetails() const; 50 51 // Gets a stream containing the contents of the requested file. 52 // The hash must match for this function to return successfully. 53 std::unique_ptr<std::istream> GetFile(const std::filesystem::path& relativePath, const Utility::SHA256::HashBuffer& expectedHash) const; 54 55 private: 56 // Gets a stream containing the contents of the requested file from an upstream source. 57 // The hash must match for this function to return successfully. 58 std::unique_ptr<std::stringstream> GetUpstreamFile(std::string relativePath, const Utility::SHA256::HashBuffer& expectedHash) const; 59 60 Details m_details; 61 std::vector<std::string> m_sources; 62 std::filesystem::path m_cacheBase; 63 }; 64 }