LCOV - code coverage report
Current view: top level - OdbDesignLib/FileModel/Design - FileArchive.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 65.3 % 288 188
Test Date: 2026-11-21 03:51:50 Functions: 78.8 % 33 26
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "FileArchive.h"
       2              : #include <filesystem>
       3              : #include "ArchiveExtractor.h"
       4              : #include "CrossPlatform.h"
       5              : #include "MiscInfoFile.h"
       6              : #include "Logger.h"
       7              : #include "../invalid_odb_error.h"
       8              : #include "StopWatch.h"
       9              : #include "fastmove.h"
      10              : #include <system_error>
      11              : #include <cstdio>
      12              : #include <string>
      13              : #include <memory>
      14              : #include "../../Text/Utf8Sanitizer.h"
      15              : 
      16              : using namespace Utils;
      17              : using namespace std::filesystem;
      18              : 
      19              : namespace Odb::Lib::FileModel::Design
      20              : {
      21            8 :         FileArchive::FileArchive()              
      22            8 :                 : m_filePath()
      23              :         {                       
      24            8 :         }
      25              : 
      26          103 :         FileArchive::FileArchive(const std::string& path)
      27          103 :                 : m_filePath(path)
      28              :         {
      29          103 :         }
      30              : 
      31          113 :         FileArchive::~FileArchive()
      32              :         {
      33          111 :                 m_stepsByName.clear();
      34           93 :                 m_symbolsDirectoriesByName.clear();
      35           95 :         }       
      36              : 
      37            0 :         std::string FileArchive::GetRootDir() const
      38              :         {
      39            0 :                 return m_rootDir;
      40              :         }
      41              : 
      42            4 :         std::string FileArchive::GetProductName() const
      43              :         {
      44            4 :                 return m_productName;
      45              :         }
      46              : 
      47            0 :         std::string FileArchive::GetFilePath() const
      48              :         {
      49            0 :                 return m_filePath;
      50              :         }
      51              : 
      52            0 :         std::string FileArchive::GetFilename() const
      53              :         {
      54              :                 //return m_filename;
      55            0 :                 return path(m_filePath).filename().string();
      56              :         }
      57              : 
      58          440 :         const StepDirectory::StringMap& FileArchive::GetStepsByName() const
      59              :         { 
      60          440 :                 return m_stepsByName;
      61              :         }
      62              : 
      63           86 :         const SymbolsDirectory::StringMap& FileArchive::GetSymbolsDirectoriesByName() const
      64              :         {
      65           86 :                 return m_symbolsDirectoriesByName;
      66              :         }
      67              : 
      68          101 :         bool FileArchive::ParseFileModel()
      69              :         {
      70              :                 //try
      71              :                 {
      72          101 :                         StopWatch timer(true);
      73              : 
      74          195 :                         if (!exists(m_filePath)) return false;          
      75              :                 
      76          101 :                         if (is_regular_file(m_filePath))
      77              :                         {
      78          101 :                                 path extractedPath;
      79          101 :                                 if (!ExtractDesignArchive(m_filePath, extractedPath))
      80              :                                 {
      81           10 :                                         logerror("failed to extract archive: (" + m_filePath + ")");
      82            5 :                                         return false;
      83              :                                 }
      84              : 
      85           96 :                                 m_rootDir = findRootDir(extractedPath);
      86          101 :                         }                       
      87              :                 
      88           96 :                         if (is_directory(m_rootDir))
      89              :                         {
      90          384 :                                 loginfo("Parsing... ");
      91              : 
      92          103 :                                 if (ParseDesignDirectory(m_rootDir))
      93              :                                 {
      94           89 :                                         timer.stop();                                   
      95           89 :                                         auto s = timer.getElapsedSecondsString();
      96          178 :                                         loginfo("Successfully parsed. (" + s + "s)");
      97              : 
      98           89 :                                         return true;
      99           89 :                                 }
     100              :                                 else
     101              :                                 {
     102            0 :                                         logerror("Parsing failed.");
     103            0 :                                         throw std::runtime_error("Parsing failed.");
     104              :                                 }
     105              :                         }
     106              :                         else
     107              :                         {
     108            0 :                                 logerror("Failed to find root directory");
     109              :                         }
     110              :                 }
     111              :                 //catch (std::exception& e)
     112              :                 //{
     113              :                 //      logexception(e);
     114              :                 //      throw e;
     115              :                 //}
     116              : 
     117            0 :                 return false;
     118              :         }
     119              : 
     120            0 :         bool FileArchive::SaveFileModel(const path& directory)
     121              :         {
     122            0 :                 return SaveFileModel(directory, m_productName); 
     123              :         }
     124              : 
     125            0 :         bool FileArchive::SaveFileModel(const path& directory, const std::string& archiveName)
     126              :         {
     127              :                 // create directory in /tmp
     128              :                 // write dir structure and files to it
     129              :                 // gzip with archiveName
     130              :                 // move archive to directory
     131              : 
     132            0 :                 std::string tempName;
     133            0 :                 if (!CrossPlatform::tmpnam_safe(tempName)) return false;
     134              :                 
     135            0 :                 auto tempPath = temp_directory_path() / tempName;
     136            0 :                 if (!create_directory(tempPath)) return false;
     137              : 
     138            0 :                 auto rootPath = tempPath / archiveName;
     139            0 :                 if (!create_directory(rootPath)) return false;
     140            0 :                 if (!Save(rootPath)) return false;
     141              : 
     142              :                 // compress the written file structure
     143            0 :                 std::string createdArchivePath;
     144            0 :                 if (! Utils::ArchiveExtractor::CompressDir(rootPath.string(), tempPath.string(), archiveName, createdArchivePath)) return false;
     145            0 :                 if (createdArchivePath.empty()) return false;
     146              : 
     147              :                 // move the compressed file to the requested save directory
     148            0 :                 path archiveFilename = path(createdArchivePath).filename();
     149            0 :                 path destPath = directory / archiveFilename;
     150            0 :                 std::error_code ec;
     151            0 :                 Utils::fastmove_file(createdArchivePath, destPath, true, ec);
     152            0 :                 if (ec.value() != 0) return false;              
     153              : 
     154            0 :                 return true;
     155            0 :         }       
     156              : 
     157            0 :         bool FileArchive::Save(const path& directory)
     158              :         {
     159              :                 // MiscInfoFile
     160            0 :                 auto miscPath = directory / "misc";
     161            0 :                 if (!create_directory(miscPath)) return false;
     162              : 
     163            0 :                 std::ofstream ofs1(miscPath / "info", std::ios::out);
     164            0 :                 if (!m_miscInfoFile.Save(ofs1)) return false;
     165            0 :                 ofs1.close();
     166              : 
     167              :                 // MiscAttrFile
     168            0 :                 std::ofstream ofs2(miscPath / "sysattr");
     169            0 :                 if (!m_miscAttrListFile.Save(ofs2)) return false;
     170            0 :                 ofs2.close();
     171              : 
     172              :                 // StandardFontsFile
     173            0 :                 auto fontsPath = directory / "fonts";
     174            0 :                 if (!create_directory(fontsPath)) return false;
     175            0 :                 std::ofstream ofs3(fontsPath / "standard");
     176            0 :                 if (!m_standardFontsFile.Save(ofs3)) return false;
     177            0 :                 ofs3.close();
     178              : 
     179              :                 // MatrixFile           
     180            0 :                 auto matrixPath = directory / "matrix";
     181            0 :                 if (!create_directory(matrixPath)) return false;
     182            0 :                 std::ofstream ofs4(matrixPath / "matrix");
     183            0 :                 if (!m_matrixFile.Save(ofs4)) return false;
     184            0 :                 ofs4.close();
     185              : 
     186              :                 // Steps
     187            0 :                 const auto stepsDirectory = directory / "steps";      
     188            0 :                 if (!create_directory(stepsDirectory)) return false;
     189            0 :                 for (auto& kvStepDirectory : m_stepsByName)
     190              :                 {
     191            0 :                         if (!kvStepDirectory.second->Save(stepsDirectory)) return false;
     192              :                 }
     193              : 
     194              :                 // Symbols
     195            0 :                 const auto symbolsDirectory = directory / "symbols";
     196            0 :                 if (!create_directory(symbolsDirectory)) return false;
     197            0 :                 for (auto& kvSymbolsDirectory : m_symbolsDirectoriesByName)
     198              :                 {
     199            0 :                         if (!kvSymbolsDirectory.second->Save(symbolsDirectory)) return false;
     200              :                 }
     201              : 
     202            0 :                 return true;
     203            0 :         }
     204              : 
     205          101 :         bool FileArchive::ExtractDesignArchive(const path& archivePath, path& extractedPath)
     206              :         {
     207          404 :                 loginfo("Extracting... ");
     208              : 
     209          101 :                 if (!Utils::ArchiveExtractor::IsArchiveTypeSupported(archivePath))
     210              :                 {
     211            0 :                         logerror("Unsupported archive type: (" + archivePath.string() + ")");
     212            0 :                         return false;                   
     213              :                 }
     214              : 
     215          101 :                 Utils::ArchiveExtractor extractor(archivePath.string());
     216          101 :                 if (!extractor.Extract()) return false;
     217              : 
     218           96 :                 auto extracted = path(extractor.GetExtractionDirectory());
     219           96 :                 if (!exists(extracted)) return false;
     220              : 
     221           96 :                 extractedPath = extracted;
     222              : 
     223          384 :                 loginfo("Successfully extracted.");
     224              : 
     225           96 :                 return true;
     226              :                                 
     227          101 :         }
     228              : 
     229           96 :         /*static*/ std::string FileArchive::findRootDir(const path& extractedPath)
     230              :         {
     231           96 :                 if (pathContainsTopLevelDesignDirs(extractedPath))
     232              :                 {
     233            0 :                         return extractedPath.string();
     234              :                 }
     235              :                 else
     236              :                 {
     237           96 :                         for (const auto& p : directory_iterator(extractedPath))
     238              :                         {
     239           96 :                                 if (is_directory(p))
     240              :                                 {
     241           96 :                                         if (pathContainsTopLevelDesignDirs(p.path()))
     242              :                                         {
     243           96 :                                                 return p.path().string();
     244              :                                         }
     245              :                                 }
     246          288 :                         }
     247              : 
     248            0 :                         return "";
     249              :                 }
     250              :         }
     251              : 
     252          192 :         /*static*/ bool FileArchive::pathContainsTopLevelDesignDirs(const path& path)
     253              :         {
     254          576 :                 for (const auto& topLevelRootDirName : TOPLEVEL_DESIGN_DIR_NAMES)
     255              :                 {
     256          480 :                         auto rootLevelDirPath = path / topLevelRootDirName;
     257          480 :                         if (!exists(rootLevelDirPath)) return false;
     258          480 :                 }
     259           96 :                 return true;
     260              :         }
     261              : 
     262           79 :         std::unique_ptr<Protobuf::FileArchive> FileArchive::to_protobuf() const
     263              :         {
     264           79 :                 std::unique_ptr<Protobuf::FileArchive> pFileArchiveMessage(new Protobuf::FileArchive);
     265           79 :                 FillProtobufMessage(*pFileArchiveMessage, /*includeSteps=*/true);
     266              : #ifndef NDEBUG
     267              :                 // Debug-only assertion: verify all string fields are valid UTF-8 before serialization
     268           79 :                 Odb::Lib::Text::AssertAllStringFieldsAreValidUtf8(*pFileArchiveMessage, "FileArchive");
     269              : #endif
     270           79 :                 return pFileArchiveMessage;
     271            0 :         }
     272              : 
     273           16 :         std::unique_ptr<Protobuf::FileArchive> FileArchive::to_protobuf_skeleton() const
     274              :         {
     275           16 :                 std::unique_ptr<Protobuf::FileArchive> pFileArchiveMessage(new Protobuf::FileArchive);
     276           16 :                 FillProtobufMessage(*pFileArchiveMessage, /*includeSteps=*/false);
     277           16 :                 return pFileArchiveMessage;
     278            0 :         }
     279              : 
     280           95 :         void FileArchive::FillProtobufMessage(Protobuf::FileArchive& fileArchiveMessage, bool includeSteps) const
     281              :         {
     282          190 :                 fileArchiveMessage.set_productname(Odb::Lib::Text::ToUtf8(m_productName));
     283          190 :                 fileArchiveMessage.set_filename(Odb::Lib::Text::ToUtf8(m_filename));
     284              :                 //fileArchiveMessage.set_filepath(m_filePath);
     285           95 :                 fileArchiveMessage.mutable_matrixfile()->CopyFrom(*m_matrixFile.to_protobuf());
     286           95 :                 fileArchiveMessage.mutable_miscinfofile()->CopyFrom(*m_miscInfoFile.to_protobuf());
     287           95 :                 fileArchiveMessage.mutable_standardfontsfile()->CopyFrom(*m_standardFontsFile.to_protobuf());
     288           95 :                 fileArchiveMessage.mutable_miscattrlistfile()->CopyFrom(*m_miscAttrListFile.to_protobuf());
     289              : 
     290           95 :                 if (includeSteps)
     291              :                 {
     292          159 :                         for (const auto& kvStepDirectoryRecord : m_stepsByName)
     293              :                         {
     294           80 :                                 (*fileArchiveMessage.mutable_stepsbyname())[Odb::Lib::Text::ToUtf8(kvStepDirectoryRecord.first)] = *kvStepDirectoryRecord.second->to_protobuf();
     295              :                         }
     296              :                 }
     297              : 
     298         5083 :                 for (const auto& kvSymbolsDirectory : m_symbolsDirectoriesByName)
     299              :                 {
     300         4988 :                         (*fileArchiveMessage.mutable_symbolsdirectoriesbyname())[Odb::Lib::Text::ToUtf8(kvSymbolsDirectory.first)] = *kvSymbolsDirectory.second->to_protobuf();
     301              :                 }
     302           95 :         }
     303              : 
     304            7 :         void FileArchive::from_protobuf(const Protobuf::FileArchive& message)
     305              :         {
     306            7 :                 m_productName = message.productname();
     307            7 :                 m_filename = message.filename();
     308              :                 //m_filePath = message.filepath();
     309            7 :                 m_matrixFile.from_protobuf(message.matrixfile());
     310            7 :                 m_miscInfoFile.from_protobuf(message.miscinfofile());
     311            7 :                 m_standardFontsFile.from_protobuf(message.standardfontsfile());
     312            7 :                 m_miscAttrListFile.from_protobuf(message.miscattrlistfile());
     313              : 
     314           23 :                 for (const auto& kvStepDirectoryRecord : message.stepsbyname())
     315              :                 {
     316            8 :                         auto pStepDirectory = std::make_shared<StepDirectory>("");
     317            8 :                         pStepDirectory->from_protobuf(kvStepDirectoryRecord.second);
     318            8 :                         m_stepsByName[kvStepDirectoryRecord.first] = pStepDirectory;
     319            8 :                 }
     320              : 
     321          727 :                 for (const auto& kvSymbolsDirectory : message.symbolsdirectoriesbyname())
     322              :                 {
     323          360 :                         auto pSymbolsDirectory = std::make_shared<SymbolsDirectory>("");
     324          360 :                         pSymbolsDirectory->from_protobuf(kvSymbolsDirectory.second);
     325          360 :                         m_symbolsDirectoriesByName[kvSymbolsDirectory.first] = pSymbolsDirectory;
     326          360 :                 }
     327            7 :         }
     328              : 
     329           96 :         bool FileArchive::ParseDesignDirectory(const path& path)
     330              :         {
     331           96 :                 if (!exists(path)) return false;
     332           96 :                 else if (!is_directory(path)) return false;
     333              : 
     334           96 :                 m_productName = path.stem().string();
     335              : 
     336           96 :                 if (! ParseStepDirectories(path)) return false;
     337           93 :         if (! ParseMiscInfoFile(path)) return false;
     338           90 :                 if (! ParseMatrixFile(path)) return false;
     339           90 :                 if (! ParseStandardFontsFile(path)) return false;
     340           89 :                 if (! ParseSymbolsDirectories(path)) return false;
     341           89 :                 if (! ParseMiscAttrListFile(path)) return false;
     342              : 
     343           89 :                 return true;
     344              :         }
     345              : 
     346           96 :         bool FileArchive::ParseStepDirectories(const path& path)
     347              :         {
     348          384 :                 loginfo("Parsing steps...");
     349              : 
     350           96 :                 auto stepsPath = path / "steps";
     351          187 :                 for (auto& d : directory_iterator(stepsPath))
     352              :                 {
     353           94 :                         if (is_directory(d))
     354              :                         {
     355           94 :                                 auto pStep = std::make_shared<StepDirectory>(d.path());
     356           94 :                                 if (pStep->Parse())
     357              :                                 {
     358           91 :                                         m_stepsByName[pStep->GetName()] = pStep;
     359              :                                 }
     360              :                                 else
     361              :                                 {
     362            0 :                                         logwarn("Failed to parse step: " + pStep->GetName());
     363              :                                         //return false;
     364              :                                 }
     365           94 :                         }
     366          102 :                 }
     367              : 
     368          372 :                 loginfo("Parsing steps complete");
     369              : 
     370           93 :                 return true;
     371           96 :         }
     372              : 
     373           93 :     bool FileArchive::ParseMiscInfoFile(const path& path)
     374              :     {
     375          372 :                 loginfo("Parsing misc/info file...");
     376              : 
     377           93 :         auto miscDirectory = path / "misc";
     378           93 :         if (!exists(miscDirectory))
     379              :         {
     380            0 :             auto message = "misc directory does not exist: [" + miscDirectory.string() + "]";
     381            0 :             throw invalid_odb_error(message);
     382            0 :         }
     383           93 :         if (!is_directory(miscDirectory))
     384              :         {
     385            0 :             auto message = "misc path is not a directory: [" + miscDirectory.string() + "]";
     386            0 :             throw invalid_odb_error(message);
     387            0 :         }
     388              : 
     389           96 :         if (!m_miscInfoFile.Parse(miscDirectory)) return false;
     390              : 
     391          360 :                 loginfo("Parsing misc/info file complete");
     392              : 
     393           90 :         return true;
     394           93 :     }
     395              : 
     396           89 :         bool FileArchive::ParseMiscAttrListFile(const path& path)
     397              :         {
     398          356 :                 loginfo("Parsing misc/attrlist file...");
     399              : 
     400           89 :                 auto miscDirectory = path / "misc";
     401           89 :                 if (!exists(miscDirectory))
     402              :                 {
     403            0 :                         auto message = "misc directory does not exist: [" + miscDirectory.string() + "]";
     404            0 :                         throw invalid_odb_error(message);
     405            0 :                 }
     406           89 :                 if (!is_directory(miscDirectory))
     407              :                 {
     408            0 :                         auto message = "misc path is not a directory: [" + miscDirectory.string() + "]";
     409            0 :                         throw invalid_odb_error(message);
     410            0 :                 }
     411              : 
     412           89 :                 if (!m_miscAttrListFile.Parse(miscDirectory)) return false;
     413              : 
     414          356 :                 loginfo("Parsing misc/attrlist file complete");
     415              : 
     416           89 :                 return true;
     417           89 :         }
     418              : 
     419           90 :         bool FileArchive::ParseMatrixFile(const path& path)
     420              :         {
     421          360 :                 loginfo("Parsing matrix/matrix file...");
     422              : 
     423           90 :                 auto matrixDir = path / "matrix";
     424           90 :                 if (!exists(matrixDir))
     425              :                 {
     426            0 :                         auto message = "matrix directory does not exist: [" + matrixDir.string() + "]";
     427            0 :                         throw invalid_odb_error(message);
     428            0 :                 }
     429           90 :                 if (!is_directory(matrixDir))
     430              :                 {
     431            0 :                         auto message = "matrix path is not a directory: [" + matrixDir.string() + "]";
     432            0 :                         throw invalid_odb_error(message);
     433            0 :                 }
     434              : 
     435           90 :                 if (!m_matrixFile.Parse(matrixDir)) return false;
     436              : 
     437          360 :                 loginfo("Parsing matrix/matrix file complete");
     438              : 
     439           90 :                 return true;
     440           90 :         }
     441           90 :         bool FileArchive::ParseStandardFontsFile(const path& path)
     442              :         {
     443          360 :                 loginfo("Parsing fonts/standard file...");
     444              : 
     445           90 :                 auto fontsDir = path / "fonts";
     446           90 :                 if (!exists(fontsDir))
     447              :                 {
     448            0 :                         auto message = "fonts directory does not exist: [" + fontsDir.string() + "]";
     449            0 :                         throw invalid_odb_error(message);
     450            0 :                 }
     451           90 :                 if (!is_directory(fontsDir))
     452              :                 {
     453            0 :                         auto message = "fonts path is not a directory: [" + fontsDir.string() + "]";
     454            0 :                         throw invalid_odb_error(message);
     455            0 :                 }
     456              : 
     457           91 :                 if (!m_standardFontsFile.Parse(fontsDir)) return false;
     458              : 
     459          356 :                 loginfo("Parsing fonts/standard file complete");
     460              : 
     461           89 :                 return true;
     462           90 :         }
     463              : 
     464           89 :         bool FileArchive::ParseSymbolsDirectories(const path& path)
     465              :         {
     466          356 :                 loginfo("Parsing symbols root directory...");
     467              : 
     468           89 :                 auto symbolsDirectory = path / "symbols";
     469              : 
     470           89 :                 if (!exists(symbolsDirectory))
     471              :                 {
     472            0 :                         logwarn("symbols root directory does not exist (" + symbolsDirectory.string() + ")");
     473            0 :                         return true;
     474              :                 }
     475           89 :                 else if (!is_directory(symbolsDirectory))
     476              :                 {
     477            0 :                         logerror("symbols root directory exists but is a regular file/not a directory (" + symbolsDirectory.string() + ")");
     478            0 :                         return false;
     479              :                 }
     480              : 
     481         6106 :                 for (auto& d : directory_iterator(symbolsDirectory))
     482              :                 {
     483         6017 :                         if (is_directory(d))
     484              :                         {
     485         6017 :                                 auto pSymbolsDirectory = std::make_shared<SymbolsDirectory>(d.path());
     486         6017 :                                 if (pSymbolsDirectory->Parse())
     487              :                                 {
     488              :                                         //loginfo("Parsing symbols directory: " + pSymbolsDirectory->GetName() + " complete");
     489              : 
     490         6017 :                                         m_symbolsDirectoriesByName[pSymbolsDirectory->GetName()] = pSymbolsDirectory;
     491              :                                 }
     492              :                                 else
     493              :                                 {
     494            0 :                                         logerror("Parsing symbol directory: " + pSymbolsDirectory->GetName() + " failed");
     495            0 :                                         return false;
     496              :                                 }
     497         6017 :                         }
     498           89 :                 }
     499              : 
     500          356 :                 loginfo("Parsing symbols root directory complete");
     501              : 
     502           89 :                 return true;
     503           89 :         }       
     504              : 
     505            0 :     const MiscInfoFile &FileArchive::GetMiscInfoFile() const
     506              :     {
     507            0 :         return m_miscInfoFile;
     508              :     }
     509              : 
     510            4 :         const MatrixFile& FileArchive::GetMatrixFile() const
     511              :         {
     512            4 :                 return m_matrixFile;
     513              :         }
     514              : 
     515            3 :         const StandardFontsFile& FileArchive::GetStandardFontsFile() const
     516              :         {
     517            3 :                 return m_standardFontsFile;
     518              :         }
     519              : 
     520           82 :         const AttrListFile& FileArchive::GetMiscAttrListFile() const
     521              :         {
     522           82 :                 return m_miscAttrListFile;
     523              :         }
     524              : 
     525           10 :         std::shared_ptr<StepDirectory> FileArchive::GetStepDirectory(const std::string& stepName /*= ""*/) const
     526              :         {
     527           10 :                 std::shared_ptr<FileModel::Design::StepDirectory> pStepDirectory;
     528              : 
     529           10 :                 const auto& steps = GetStepsByName();
     530           10 :                 if (!steps.empty())
     531              :                 {
     532           10 :                         if (stepName.empty())
     533              :                         {
     534              :                                 // return first step
     535            0 :                                 pStepDirectory = steps.begin()->second;
     536              :                         }
     537              :                         else
     538              :                         {
     539           10 :                                 auto findIt = steps.find(stepName);
     540           10 :                                 if (findIt != steps.end())
     541              :                                 {
     542            9 :                                         pStepDirectory = findIt->second;
     543              :                                 }
     544              :                         }
     545              :                 }               
     546              : 
     547           10 :                 return pStepDirectory;
     548            0 :         }
     549              : }
        

Generated by: LCOV version 2.0-1