LCOV - code coverage report
Current view: top level - include/daemon/backend/data - file_handle.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 14 17 82.4 %
Date: 2024-04-30 13:21:35 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
       3             :   Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
       4             : 
       5             :   This software was partially supported by the
       6             :   EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
       7             : 
       8             :   This software was partially supported by the
       9             :   ADA-FS project under the SPPEXA project funded by the DFG.
      10             : 
      11             :   This file is part of GekkoFS.
      12             : 
      13             :   GekkoFS is free software: you can redistribute it and/or modify
      14             :   it under the terms of the GNU General Public License as published by
      15             :   the Free Software Foundation, either version 3 of the License, or
      16             :   (at your option) any later version.
      17             : 
      18             :   GekkoFS is distributed in the hope that it will be useful,
      19             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21             :   GNU General Public License for more details.
      22             : 
      23             :   You should have received a copy of the GNU General Public License
      24             :   along with GekkoFS.  If not, see <https://www.gnu.org/licenses/>.
      25             : 
      26             :   SPDX-License-Identifier: GPL-3.0-or-later
      27             : */
      28             : /**
      29             :  * @brief Definitions for the file handle abstraction layer used for the backend
      30             :  * file system.
      31             :  */
      32             : #ifndef GEKKOFS_DAEMON_FILE_HANDLE_HPP
      33             : #define GEKKOFS_DAEMON_FILE_HANDLE_HPP
      34             : 
      35             : #include <daemon/backend/data/data_module.hpp>
      36             : 
      37             : extern "C" {
      38             : #include <unistd.h>
      39             : }
      40             : 
      41             : namespace gkfs::data {
      42             : 
      43             : /**
      44             :  * @brief File handle class to encapsulate a file descriptor, allowing RAII
      45             :  * closing of the file descriptor.
      46             :  */
      47             : class FileHandle {
      48             : 
      49             : private:
      50             :     constexpr static const int init_value{-1}; ///< initial file descriptor
      51             : 
      52             :     int fd_{init_value}; ///< file descriptor
      53             :     std::string path_{}; ///< chunk file path
      54             : 
      55             : public:
      56             :     FileHandle() = default;
      57             : 
      58         174 :     explicit FileHandle(int fd, const std::string& path) noexcept : fd_(fd) {}
      59             : 
      60             :     FileHandle(FileHandle&& rhs) = default;
      61             : 
      62             :     FileHandle(const FileHandle& other) = delete;
      63             : 
      64             :     FileHandle&
      65             :     operator=(FileHandle&& rhs) = default;
      66             : 
      67             :     FileHandle&
      68             :     operator=(const FileHandle& other) = delete;
      69             : 
      70             :     explicit operator bool() const noexcept {
      71             :         return valid();
      72             :     }
      73             : 
      74             :     bool
      75             :     operator!() const noexcept {
      76             :         return !valid();
      77             :     }
      78             : 
      79             :     /**
      80             :      * @brief Checks for valid file descriptor value.
      81             :      * @return boolean if valid file descriptor
      82             :      */
      83             :     [[nodiscard]] bool
      84         174 :     valid() const noexcept {
      85         174 :         return fd_ != init_value;
      86             :     }
      87             : 
      88             :     /**
      89             :      * @brief Retusn the file descriptor value used in this file handle
      90             :      * operation.
      91             :      * @return file descriptor value
      92             :      */
      93             :     [[nodiscard]] int
      94         161 :     native() const noexcept {
      95         161 :         return fd_;
      96             :     }
      97             : 
      98             :     /**
      99             :      * @brief Closes file descriptor and resets it to initial value
     100             :      * @return boolean if file descriptor was successfully closed
     101             :      */
     102             :     bool
     103         160 :     close() noexcept {
     104         160 :         if(fd_ != init_value) {
     105         160 :             if(::close(fd_) < 0) {
     106           0 :                 GKFS_DATA_MOD->log()->warn(
     107             :                         "{}() Failed to close file descriptor '{}' path '{}' errno '{}'",
     108           0 :                         __func__, fd_, path_, ::strerror(errno));
     109           0 :                 return false;
     110             :             }
     111             :         }
     112         160 :         fd_ = init_value;
     113         160 :         return true;
     114             :     }
     115             : 
     116             :     /**
     117             :      * @brief Destructor implicitly closes the internal file descriptor.
     118             :      */
     119         174 :     ~FileHandle() {
     120         174 :         if(fd_ != init_value)
     121         160 :             close();
     122         174 :     }
     123             : };
     124             : 
     125             : } // namespace gkfs::data
     126             : 
     127             : 
     128             : #endif // GEKKOFS_DAEMON_FILE_HANDLE_HPP

Generated by: LCOV version 1.16