winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

PortableFileEntry.h (2696B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include "AppInstallerSHA256.h"
      5 #include <string>
      6 #include <filesystem>
      7 
      8 namespace AppInstaller::Portable
      9 {
     10     // File type enum of the portable file
     11     enum class PortableFileType
     12     {
     13         Unknown,
     14         File,
     15         Directory,
     16         Symlink
     17     };
     18 
     19     // Metadata representation of a portable file placed down during installation
     20     struct PortableFileEntry
     21     {
     22         // Version 1.0
     23         PortableFileType FileType = PortableFileType::Unknown;
     24         std::string SHA256;
     25         std::string SymlinkTarget;
     26         std::filesystem::path CurrentPath;
     27 
     28         void SetFilePath(const std::filesystem::path& path)
     29         {
     30             if (FileType != PortableFileType::Symlink)
     31             {
     32                 m_filePath = std::filesystem::weakly_canonical(path);
     33             }
     34             else
     35             {
     36                 m_filePath = path;
     37             }
     38         };
     39 
     40         std::filesystem::path GetFilePath() const { return m_filePath; };
     41 
     42         static PortableFileEntry CreateFileEntry(const std::filesystem::path& currentPath, const std::filesystem::path& targetPath, const std::string& sha256)
     43         {
     44             PortableFileEntry fileEntry;
     45             fileEntry.FileType = PortableFileType::File;
     46             fileEntry.CurrentPath = currentPath;
     47             fileEntry.SetFilePath(targetPath);
     48 
     49             if (sha256.empty())
     50             {
     51                 fileEntry.SHA256 = Utility::SHA256::ConvertToString(Utility::SHA256::ComputeHashFromFile(currentPath));
     52             }
     53             else
     54             {
     55                 fileEntry.SHA256 = sha256;
     56             }
     57             return fileEntry;
     58         }
     59 
     60         static PortableFileEntry CreateSymlinkEntry(const std::filesystem::path& symlinkPath, const std::filesystem::path& targetPath)
     61         {
     62             PortableFileEntry symlinkEntry;
     63             symlinkEntry.FileType = PortableFileType::Symlink;
     64             symlinkEntry.SetFilePath(symlinkPath);
     65             symlinkEntry.SymlinkTarget = targetPath.u8string();
     66             return symlinkEntry;
     67         }
     68 
     69         static PortableFileEntry CreateDirectoryEntry(const std::filesystem::path& currentPath, const std::filesystem::path& directoryPath)
     70         {
     71             PortableFileEntry directoryEntry;
     72             directoryEntry.FileType = PortableFileType::Directory;
     73             directoryEntry.CurrentPath = currentPath;
     74             directoryEntry.SetFilePath(directoryPath);
     75             return directoryEntry;
     76         }
     77 
     78     private:
     79         std::filesystem::path m_filePath;
     80     };
     81 }