LCOV - code coverage report
Current view: top level - model - FileManager.cpp (source / functions) Coverage Total Hit
Test: final-coverage.info Lines: 98.8 % 85 84
Test Date: 2025-12-16 15:55:14 Functions: 100.0 % 16 16

            Line data    Source code
       1              : /*
       2              :  ___________________________________________
       3              : |    _     ___                        _     |
       4              : |   | |   |__ \                      | |    |
       5              : |   | |__    ) |__ _  __ _  ___ _ __ | |_   |
       6              : |   | '_ \  / // _` |/ _` |/ _ \ '_ \| __|  |  HTTP/2 AGENT FOR MOCK TESTING
       7              : |   | | | |/ /| (_| | (_| |  __/ | | | |_   |  Version 0.0.z
       8              : |   |_| |_|____\__,_|\__, |\___|_| |_|\__|  |  https://github.com/testillano/h2agent
       9              : |                     __/ |                 |
      10              : |                    |___/                  |
      11              : |___________________________________________|
      12              : 
      13              : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
      14              : SPDX-License-Identifier: MIT
      15              : Copyright (c) 2021 Eduardo Ramos
      16              : 
      17              : Permission is hereby  granted, free of charge, to any  person obtaining a copy
      18              : of this software and associated  documentation files (the "Software"), to deal
      19              : in the Software  without restriction, including without  limitation the rights
      20              : to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
      21              : copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
      22              : furnished to do so, subject to the following conditions:
      23              : 
      24              : The above copyright notice and this permission notice shall be included in all
      25              : copies or substantial portions of the Software.
      26              : 
      27              : THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
      28              : IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
      29              : FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
      30              : AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
      31              : LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      32              : OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
      33              : SOFTWARE.
      34              : */
      35              : 
      36              : #include <memory>
      37              : 
      38              : #include <ert/tracing/Logger.hpp>
      39              : 
      40              : #include <FileManager.hpp>
      41              : 
      42              : namespace h2agent
      43              : {
      44              : namespace model
      45              : {
      46              : 
      47              : 
      48           50 : void FileManager::enableMetrics(ert::metrics::Metrics *metrics, const std::string &source) {
      49              : 
      50           50 :     metrics_ = metrics;
      51              : 
      52           50 :     if (metrics_) {
      53          147 :         ert::metrics::labels_t familyLabels = {{"source", source}};
      54              : 
      55          196 :         ert::metrics::counter_family_t& cf = metrics->addCounterFamily("h2agent_file_manager_operations_counter", "File system operations counter in h2agent_file_manager", familyLabels);
      56          147 :         observed_open_operation_counter_ = &(cf.Add({{"operation", "open"}}));
      57          147 :         observed_close_operation_counter_ = &(cf.Add({{"operation", "close"}}));
      58          147 :         observed_write_operation_counter_ = &(cf.Add({{"operation", "write"}}));
      59          147 :         observed_empty_operation_counter_ = &(cf.Add({{"operation", "empty"}}));
      60          147 :         observed_delayed_close_operation_counter_ = &(cf.Add({{"operation", "delayedClose"}}));
      61          147 :         observed_instant_close_operation_counter_ = &(cf.Add({{"operation", "instantClose"}}));
      62          196 :         observed_error_open_operation_counter_ = &(cf.Add({{"result", "failed"}, {"operation", "open"}}));
      63           49 :     }
      64          442 : }
      65              : 
      66           29 : void FileManager::incrementObservedOpenOperationCounter() {
      67           29 :     if (metrics_) observed_open_operation_counter_->Increment();
      68           29 : }
      69              : 
      70           29 : void FileManager::incrementObservedCloseOperationCounter() {
      71           29 :     if (metrics_) observed_close_operation_counter_->Increment();
      72           29 : }
      73              : 
      74            7 : void FileManager::incrementObservedWriteOperationCounter() {
      75            7 :     if (metrics_) observed_write_operation_counter_->Increment();
      76            7 : }
      77              : 
      78            9 : void FileManager::incrementObservedEmptyOperationCounter() {
      79            9 :     if (metrics_) observed_empty_operation_counter_->Increment();
      80            9 : }
      81              : 
      82            1 : void FileManager::incrementObservedDelayedCloseOperationCounter() {
      83            1 :     if (metrics_) observed_delayed_close_operation_counter_->Increment();
      84            1 : }
      85              : 
      86            6 : void FileManager::incrementObservedInstantCloseOperationCounter() {
      87            6 :     if (metrics_) observed_instant_close_operation_counter_->Increment();
      88            6 : }
      89              : 
      90            5 : void FileManager::incrementObservedErrorOpenOperationCounter() {
      91            5 :     if (metrics_) observed_error_open_operation_counter_->Increment();
      92            5 : }
      93              : 
      94            5 : void FileManager::write(const std::string &path, const std::string &data, bool textOrBinary, unsigned int closeDelayUs) {
      95              : 
      96            5 :     std::shared_ptr<SafeFile> safeFile;
      97              : 
      98            5 :     bool exists{};
      99            5 :     auto result = get(path, exists);
     100            5 :     if (exists) {
     101            1 :         safeFile = result;
     102              :     }
     103              :     else {
     104            4 :         std::ios_base::openmode mode = std::ofstream::out | std::ios_base::app; // for text files
     105            4 :         if (!textOrBinary) mode |= std::ios::binary;
     106              : 
     107            4 :         safeFile = std::make_shared<SafeFile>(this, path, io_context_, mode);
     108            4 :         add(path, safeFile);
     109              :     }
     110              : 
     111            5 :     safeFile->write(data, closeDelayUs);
     112            5 : }
     113              : 
     114           12 : bool FileManager::read(const std::string &path, std::string &data, bool textOrBinary) {
     115              : 
     116              :     bool result;
     117              : 
     118           12 :     std::shared_ptr<SafeFile> safeFile;
     119           12 :     std::ios_base::openmode mode = std::ifstream::in; // for text files
     120              : 
     121           12 :     bool exists{};
     122           12 :     auto value = get(path, exists);
     123           12 :     if (exists) {
     124            7 :         safeFile = value;
     125              :     }
     126              :     else {
     127            5 :         if (!textOrBinary) mode |= std::ios::binary;
     128              : 
     129            5 :         safeFile = std::make_shared<SafeFile>(this, path, io_context_, mode);
     130            5 :         add(path, safeFile);
     131              :     }
     132              : 
     133           12 :     data = safeFile->read(result, mode, read_cache_);
     134              : 
     135           12 :     return result;
     136           12 : }
     137              : 
     138            5 : void FileManager::empty(const std::string &path) {
     139              : 
     140            5 :     std::shared_ptr<SafeFile> safeFile;
     141              : 
     142            5 :     bool exists{};
     143            5 :     auto value = get(path, exists);
     144            5 :     if (exists) {
     145            4 :         safeFile = value;
     146              :     }
     147              :     else {
     148            1 :         safeFile = std::make_shared<SafeFile>(this, path, io_context_);
     149            1 :         add(path, safeFile);
     150              :     }
     151              : 
     152            5 :     safeFile->empty();
     153            5 : }
     154              : 
     155            4 : nlohmann::json FileManager::getConfigurationJson() const {
     156              : 
     157            4 :     nlohmann::json result;
     158            4 :     result["readCache"] = read_cache_ ? "enabled":"disabled";
     159              : 
     160            4 :     return result;
     161              : } // LCOV_EXCL_LINE
     162              : 
     163            2 : std::string FileManager::configurationAsJsonString() const {
     164              : 
     165            2 :     return (getConfigurationJson().dump());
     166              : }
     167              : 
     168            2 : nlohmann::json FileManager::getJson() const {
     169              : 
     170            2 :     nlohmann::json result;
     171              : 
     172            2 :     this->forEach([&](const KeyType& k, const ValueType& value) {
     173            2 :         result.push_back(value->getJson());
     174            2 :     });
     175              : 
     176            2 :     return result;
     177            0 : }
     178              : 
     179            1 : std::string FileManager::asJsonString() const {
     180              : 
     181            3 :     return ((size() != 0) ? getJson().dump() : "[]");
     182              : }
     183              : 
     184              : 
     185              : }
     186              : }
     187              : 
        

Generated by: LCOV version 2.0-1