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 : #ifndef GKFS_TESTS_HELPERS 30 : #define GKFS_TESTS_HELPERS 31 : 32 : #include <filesystem> 33 : #include <fstream> 34 : #include <vector> 35 : 36 : namespace fs = std::filesystem; 37 : 38 : namespace helpers { 39 : 40 : /** 41 : * Generate a random string of length `length`. 42 : * 43 : * @param length The length of the random string. 44 : * @return The generated string. 45 : */ 46 : std::string 47 : random_string(std::size_t length); 48 : 49 : 50 : /** 51 : * Read the contents of `filename` returning them into `str`. 52 : * 53 : * @param filename The filename to read data from. 54 : * @param str The string where data will be copied. 55 : */ 56 : inline void 57 3 : load_string_file(const fs::path& filename, std::string& str) { 58 : 59 6 : std::ifstream file; 60 3 : file.exceptions(std::ios_base::failbit | std::ios_base::badbit); 61 3 : file.open(filename, std::ios_base::binary); 62 : 63 3 : auto sz = static_cast<std::size_t>(fs::file_size(filename)); 64 3 : str.resize(sz, '\0'); 65 3 : file.read(&str[0], static_cast<std::streamsize>(sz)); 66 3 : } 67 : 68 : /** 69 : * A temporary directory with RAII removal 70 : */ 71 : struct temporary_directory { 72 : 73 : /** 74 : * Create a temporary directory with a random dirname. 75 : * The directory is created at a location suitable for temporary files. 76 : */ 77 : temporary_directory(); 78 : 79 : /** 80 : * Remove a temporary directory and all its contents. 81 : */ 82 : ~temporary_directory(); 83 : 84 : /** 85 : * Return the path of the created temporary directory. 86 : * 87 : * @return The path of the created directory. 88 : */ 89 : [[nodiscard]] fs::path 90 : dirname() const; 91 : 92 : fs::path dirname_; 93 : }; 94 : 95 : /** 96 : * A temporary file with RAII removal 97 : */ 98 : struct temporary_file { 99 : 100 : /** 101 : * Create an empty temporary file with `filename` as its name. 102 : * 103 : * @param filename The desired filename for the file. 104 : */ 105 : explicit temporary_file(fs::path filename); 106 : 107 : /** 108 : * Create a temporary file with `filename` as its name and `text` as its 109 : * contents. 110 : * 111 : * @param filename The desired filename for the file. 112 : * @param text The text to be used for contents. 113 : */ 114 : temporary_file(fs::path filename, const std::string_view& text); 115 : 116 : /** 117 : * Destroy and remove a temporary file. 118 : */ 119 : ~temporary_file(); 120 : 121 : /** 122 : * Write text data to the temporary file. 123 : * 124 : * @param text The text to write. 125 : */ 126 : void 127 : write(const std::string_view& text); 128 : 129 : /** 130 : * Write binary data to the temporary file. 131 : * 132 : * @param text The binary data to write. 133 : */ 134 : void 135 : write(const std::vector<char>& data); 136 : 137 : /** 138 : * Return the `filename` for the temporary file. 139 : * 140 : * @return The temporary file's `filename`. 141 : */ 142 : fs::path 143 : filename() const; 144 : 145 : /** 146 : * Return the `size` for the temporary file. 147 : * 148 : * @return The temporary file's `size`. 149 : */ 150 : std::size_t 151 : size() const; 152 : 153 : fs::path filename_; 154 : std::ofstream ofs_; 155 : }; 156 : 157 : } // namespace helpers 158 : 159 : #endif // GKFS_TESTS_HELPERS