LCOV - code coverage report
Current view: top level - model - FileManager.hpp (source / functions) Coverage Total Hit
Test: final-coverage.info Lines: 100.0 % 5 5
Test Date: 2025-11-05 18:54:07 Functions: 100.0 % 3 3

            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              : #pragma once
      37              : 
      38              : #include <nlohmann/json.hpp>
      39              : 
      40              : #include <ert/metrics/Metrics.hpp>
      41              : 
      42              : #include <Map.hpp>
      43              : #include <SafeFile.hpp>
      44              : 
      45              : 
      46              : namespace ert
      47              : {
      48              : namespace metrics
      49              : {
      50              : class Metrics;
      51              : }
      52              : }
      53              : 
      54              : namespace h2agent
      55              : {
      56              : namespace model
      57              : {
      58              : 
      59              : /**
      60              :  * This class stores a list of safe files.
      61              :  */
      62              : class FileManager : public Map<std::string, std::shared_ptr<SafeFile>>
      63              : {
      64              :     boost::asio::io_context *io_context_{};
      65              : 
      66              :     // metrics (will be passed to SafeFile):
      67              :     ert::metrics::Metrics *metrics_{};
      68              : 
      69              :     ert::metrics::counter_t *observed_open_operation_counter_{};
      70              :     ert::metrics::counter_t *observed_close_operation_counter_{};
      71              :     ert::metrics::counter_t *observed_write_operation_counter_{};
      72              :     ert::metrics::counter_t *observed_empty_operation_counter_{};
      73              :     ert::metrics::counter_t *observed_delayed_close_operation_counter_{};
      74              :     ert::metrics::counter_t *observed_instant_close_operation_counter_{};
      75              :     ert::metrics::counter_t *observed_error_open_operation_counter_{};
      76              : 
      77              :     bool read_cache_;
      78              : 
      79              : public:
      80              :     using KeyType = std::string;
      81              :     using ValueType = std::shared_ptr<SafeFile>;
      82              : 
      83              :     /**
      84              :     * File manager class
      85              :     *
      86              :     * @param timersIoContext timers io context needed to schedule delayed close operations.
      87              :     * If you never schedule close operations (@see write) it may be 'nullptr'.
      88              :     * @param metrics underlaying reference for SafeFile in order to compute prometheus metrics
      89              :     * about I/O operations. It may be 'nullptr' if no metrics are enabled.
      90              :     *
      91              :     * @see SafeFile
      92              :     */
      93          161 :     FileManager(boost::asio::io_context *timersIoContext = nullptr, ert::metrics::Metrics *metrics = nullptr) : io_context_(timersIoContext), metrics_(metrics), read_cache_(false) {;}
      94          158 :     ~FileManager() = default;
      95              : 
      96              :     /**
      97              :     * Set metrics reference
      98              :     *
      99              :     * @param metrics Optional metrics object to compute counters
     100              :     * @param source Source label for prometheus metrics
     101              :     */
     102              :     void enableMetrics(ert::metrics::Metrics *metrics, const std::string &source);
     103              : 
     104              :     /** incrementObservedOpenOperationCounter */
     105              :     void incrementObservedOpenOperationCounter();
     106              : 
     107              :     /** incrementObservedCloseOperationCounter */
     108              :     void incrementObservedCloseOperationCounter();
     109              : 
     110              :     /** incrementObservedEmptyOperationCounter */
     111              :     void incrementObservedEmptyOperationCounter();
     112              : 
     113              :     /** incrementObservedWriteOperationCounter */
     114              :     void incrementObservedWriteOperationCounter();
     115              : 
     116              :     /** incrementObservedDelayedCloseOperationCounter */
     117              :     void incrementObservedDelayedCloseOperationCounter();
     118              : 
     119              :     /** incrementObservedInstantCloseOperationCounter */
     120              :     void incrementObservedInstantCloseOperationCounter();
     121              : 
     122              :     /** incrementObservedErrorOpenOperationCounter */
     123              :     void incrementObservedErrorOpenOperationCounter();
     124              : 
     125              :     /**
     126              :      * Write file
     127              :      *
     128              :      * @param path path file to write. Can be relative (to execution directory) or absolute.
     129              :      * @param data data string to write.
     130              :      * @param textOrBinary open file to write text (true) or binary (false) data.
     131              :      * @param closeDelayUs delay after last write operation, to close the file.
     132              :      * Zero value means that no planned close is scheduled, so the file is opened,
     133              :      * written and closed in the same moment.
     134              :      */
     135              :     void write(const std::string &path, const std::string &data, bool textOrBinary, unsigned int closeDelayUs);
     136              : 
     137              :     /**
     138              :     * Read the file content.
     139              :     * There is no close delay. Read transaction is atomic (open, read, close).
     140              :     *
     141              :     * @param path path file to read. Can be relative (to execution directory) or absolute.
     142              :     * @param data data read by reference.
     143              :     * @param textOrBinary open file to read text (true) or binary (false) data.
     144              :     *
     145              :     * @return Boolean about success of the read operation (first read of unexisting file, returns 'false')
     146              :     */
     147              :     bool read(const std::string &path, std::string &data, bool textOrBinary);
     148              : 
     149              :     /**
     150              :     * Enables read cache to speed up content load
     151              :     *
     152              :     * @param enable Boolean to enable or disable cache. File manager disables cache by default.
     153              :     */
     154            4 :     void enableReadCache(bool enable) {
     155            4 :         read_cache_ = enable;
     156            4 :     }
     157              : 
     158              :     /**
     159              :      * Empty file
     160              :      *
     161              :      * @param path path file to empty. Can be relative (to execution directory) or absolute.
     162              :      */
     163              :     void empty(const std::string &path);
     164              : 
     165              :     /**
     166              :      * Builds json document for class configuration
     167              :      *
     168              :      * @return Json object
     169              :      */
     170              :     nlohmann::json getConfigurationJson() const;
     171              : 
     172              :     /**
     173              :      * Json string representation for class configuration (json object)
     174              :      *
     175              :      * @return Json string representation.
     176              :      */
     177              :     std::string configurationAsJsonString() const;
     178              : 
     179              :     /**
     180              :      * Builds json document for class information
     181              :      *
     182              :      * @return Json object
     183              :      */
     184              :     nlohmann::json getJson() const;
     185              : 
     186              :     /**
     187              :      * Json string representation for class information (json object)
     188              :      *
     189              :      * @return Json string representation ('[]' for empty object).
     190              :      */
     191              :     std::string asJsonString() const;
     192              : };
     193              : 
     194              : }
     195              : }
     196              : 
        

Generated by: LCOV version 2.0-1