LCOV - code coverage report
Current view: top level - tests/integration/harness/gkfs.io - write.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 31 42 73.8 %
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 <commands.hpp>
      35             : #include <reflection.hpp>
      36             : #include <serialize.hpp>
      37             : #include <binary_buffer.hpp>
      38             : 
      39             : /* C includes */
      40             : #include <sys/types.h>
      41             : #include <sys/stat.h>
      42             : #include <fcntl.h>
      43             : #include <unistd.h>
      44             : 
      45             : using json = nlohmann::json;
      46             : 
      47         536 : struct write_options {
      48             :     bool verbose{};
      49             :     std::string pathname;
      50             :     std::string data;
      51             :     ::size_t count;
      52             :     bool append{false};
      53             : 
      54             :     REFL_DECL_STRUCT(write_options, REFL_DECL_MEMBER(bool, verbose),
      55             :                      REFL_DECL_MEMBER(std::string, pathname),
      56             :                      REFL_DECL_MEMBER(std::string, data),
      57             :                      REFL_DECL_MEMBER(::size_t, count));
      58             : };
      59             : 
      60             : struct write_output {
      61             :     ::ssize_t retval;
      62             :     int errnum;
      63             : 
      64             :     REFL_DECL_STRUCT(write_output, REFL_DECL_MEMBER(::size_t, retval),
      65             :                      REFL_DECL_MEMBER(int, errnum));
      66             : };
      67             : 
      68             : void
      69          22 : to_json(json& record, const write_output& out) {
      70          22 :     record = serialize(out);
      71          22 : }
      72             : 
      73             : void
      74          22 : write_exec(const write_options& opts) {
      75          22 :     auto flags = O_WRONLY;
      76          22 :     if(opts.append)
      77           3 :         flags |= O_APPEND;
      78          22 :     auto fd = ::open(opts.pathname.c_str(), flags);
      79             : 
      80          22 :     if(fd == -1) {
      81           0 :         if(opts.verbose) {
      82           0 :             fmt::print(
      83             :                     "open(pathname=\"{}\", buf=\"{}\" count={}) = {}, errno: {} [{}]\n",
      84           0 :                     opts.pathname, opts.data, opts.count, fd, errno,
      85           0 :                     ::strerror(errno));
      86           0 :             return;
      87             :         }
      88             : 
      89           0 :         json out = write_output{fd, errno};
      90           0 :         fmt::print("{}\n", out.dump(2));
      91             : 
      92           0 :         return;
      93             :     }
      94             : 
      95          44 :     io::buffer buf(opts.data);
      96          22 :     auto rv = ::write(fd, buf.data(), opts.count);
      97             : 
      98          22 :     if(opts.verbose) {
      99           0 :         fmt::print("write(pathname=\"{}\", count={}) = {}, errno: {} [{}]\n",
     100           0 :                    opts.pathname, opts.count, rv, errno, ::strerror(errno));
     101           0 :         return;
     102             :     }
     103             : 
     104          44 :     json out = write_output{rv, errno};
     105          44 :     fmt::print("{}\n", out.dump(2));
     106             : }
     107             : 
     108             : void
     109         268 : write_init(CLI::App& app) {
     110             : 
     111             :     // Create the option and subcommand objects
     112         268 :     auto opts = std::make_shared<write_options>();
     113         268 :     auto* cmd = app.add_subcommand("write", "Execute the write() system call");
     114             : 
     115             :     // Add options to cmd, binding them to opts
     116         268 :     cmd->add_flag("-v,--verbose", opts->verbose,
     117         536 :                   "Produce human writeable output");
     118             : 
     119         536 :     cmd->add_option("pathname", opts->pathname, "Directory name")
     120             :             ->required()
     121         536 :             ->type_name("");
     122             : 
     123         536 :     cmd->add_option("data", opts->data, "Data to write")
     124             :             ->required()
     125         536 :             ->type_name("");
     126             : 
     127         536 :     cmd->add_option("count", opts->count, "Number of bytes to write")
     128             :             ->required()
     129         536 :             ->type_name("");
     130             : 
     131         536 :     cmd->add_option("append", opts->append, "Append file")
     132         268 :             ->default_val(false)
     133         536 :             ->type_name("");
     134             : 
     135        1094 :     cmd->callback([opts]() { write_exec(*opts); });
     136         268 : }

Generated by: LCOV version 1.16