LCOV - code coverage report
Current view: top level - tests/integration/harness/gkfs.io - truncate.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 26 88.5 %
Date: 2024-04-23 00:09:24 Functions: 3 3 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             : /* C++ includes */
      30             : #include <CLI/CLI.hpp>
      31             : #include <nlohmann/json.hpp>
      32             : #include <memory>
      33             : #include <fmt/format.h>
      34             : #include <reflection.hpp>
      35             : #include <serialize.hpp>
      36             : 
      37             : /* C includes */
      38             : #include <sys/types.h>
      39             : #include <unistd.h>
      40             : 
      41             : using json = nlohmann::json;
      42             : 
      43         536 : struct truncate_options {
      44             :     bool verbose{};
      45             :     std::string path{};
      46             :     ::off_t length{};
      47             : 
      48             :     REFL_DECL_STRUCT(truncate_options, REFL_DECL_MEMBER(bool, verbose),
      49             :                      REFL_DECL_MEMBER(std::string, path),
      50             :                      REFL_DECL_MEMBER(::off_t, length));
      51             : };
      52             : 
      53             : struct truncate_output {
      54             :     int retval;
      55             :     int errnum;
      56             : 
      57             :     REFL_DECL_STRUCT(truncate_output, REFL_DECL_MEMBER(int, retval),
      58             :                      REFL_DECL_MEMBER(int, errnum));
      59             : };
      60             : 
      61             : void
      62           6 : to_json(json& record, const truncate_output& out) {
      63           6 :     record = serialize(out);
      64           6 : }
      65             : 
      66             : void
      67           6 : truncate_exec(const truncate_options& opts) {
      68             : 
      69           6 :     auto rv = ::truncate(opts.path.c_str(), opts.length);
      70           6 :     if(rv == -1) {
      71           1 :         if(opts.verbose) {
      72           0 :             fmt::print(
      73             :                     "truncate(path=\"{}\", length={}) = {}, errno: {} [{}]\n",
      74           0 :                     opts.path, opts.length, rv, errno, ::strerror(errno));
      75           0 :             return;
      76             :         }
      77             :     }
      78             : 
      79           6 :     json out = truncate_output{rv, errno};
      80          12 :     fmt::print("{}\n", out.dump(2));
      81             : }
      82             : 
      83             : void
      84         268 : truncate_init(CLI::App& app) {
      85             : 
      86             :     // Create the option and subcommand objects
      87         268 :     auto opts = std::make_shared<truncate_options>();
      88         536 :     auto* cmd = app.add_subcommand("truncate",
      89         268 :                                    "Execute the truncate() system call");
      90             : 
      91             :     // Add options to cmd, binding them to opts
      92         268 :     cmd->add_flag("-v,--verbose", opts->verbose,
      93         536 :                   "Produce human writeable output");
      94             : 
      95         536 :     cmd->add_option("path", opts->path, "Path to file")
      96             :             ->required()
      97         536 :             ->type_name("");
      98             : 
      99         268 :     cmd->add_option("length", opts->length,
     100         536 :                     "Truncate to a size precisely length bytes")
     101             :             ->required()
     102         536 :             ->type_name("");
     103             : 
     104        1078 :     cmd->callback([opts]() { truncate_exec(*opts); });
     105         268 : }

Generated by: LCOV version 1.16