jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_fileutils.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cassert>
25#include <cstring>
26#include <pthread.h>
27
28#include <jau/test/catch2_ext.hpp>
29
30#include <jau/debug.hpp>
31#include <jau/file_util.hpp>
32#include <jau/byte_stream.hpp>
33
34using namespace jau::enums;
35using namespace jau::fractions_i64_literals;
36
37static constexpr const bool _remove_target_test_dir = true;
38
51
65
66 void add(const jau::fs::file_stats& element_stats) {
67 if( element_stats.is_link() ) {
68 if( element_stats.exists() ) {
70 } else {
72 }
73 } else {
74 total_real++;
75 }
76 if( !element_stats.has_access() ) {
78 }
79 if( !element_stats.exists() ) {
81 }
82 if( element_stats.is_file() ) {
83 if( element_stats.is_link() ) {
86 total_file_bytes += element_stats.size();
87 }
88 } else {
89 files_real++;
90 total_file_bytes += element_stats.size();
91 }
92 } else if( element_stats.is_dir() ) {
93 if( element_stats.is_link() ) {
95 } else {
96 dirs_real++;
97 }
98 }
99 }
100
101 std::string to_string() const noexcept {
102 std::string res;
103 res += "- traverse_options "+jau::fs::to_string(topts)+"\n";
104 res += "- total_real "+std::to_string(total_real)+"\n";
105 res += "- total_sym_links_existing "+std::to_string(total_sym_links_existing)+"\n";
106 res += "- total_sym_links_not_existing "+std::to_string(total_sym_links_not_existing)+"\n";
107 res += "- total_no_access "+std::to_string(total_no_access)+"\n";
108 res += "- total_not_existing "+std::to_string(total_not_existing)+"\n";
109 res += "- total_file_bytes "+jau::to_decstring(total_file_bytes)+"\n";
110 res += "- files_real "+std::to_string(files_real)+"\n";
111 res += "- files_sym_link "+std::to_string(files_sym_link)+"\n";
112 res += "- dirs_real "+std::to_string(dirs_real)+"\n";
113 res += "- dirs_sym_link "+std::to_string(dirs_sym_link)+"\n";
114 return res;
115 }
116};
117
118constexpr bool operator ==(const visitor_stats& lhs, const visitor_stats& rhs) noexcept {
119 return lhs.total_file_bytes == rhs.total_file_bytes &&
120 lhs.total_real == rhs.total_real &&
121 lhs.total_sym_links_existing == rhs.total_sym_links_existing &&
122 lhs.total_sym_links_not_existing == rhs.total_sym_links_not_existing &&
123 lhs.total_no_access == rhs.total_no_access &&
124 lhs.total_not_existing == rhs.total_not_existing &&
125 lhs.files_real == rhs.files_real &&
126 lhs.files_sym_link == rhs.files_sym_link &&
127 lhs.dirs_real == rhs.dirs_real &&
128 lhs.dirs_sym_link == rhs.dirs_sym_link;
129}
130constexpr bool operator !=(const visitor_stats& lhs, const visitor_stats& rhs) noexcept {
131 return !( lhs == rhs );
132}
133
135 private:
136 const std::string image_file = "test_data.sqfs";
137 // normal location with jaulib as sole project (a)
138 const std::string project_root1a = "../../test_data";
139 // normal location with jaulib as sole project (b)
140 const std::string project_root1b = "../../../test_data";
141 // submodule location with jaulib directly hosted below main project (a)
142 const std::string project_root2a = "../../../jaulib/test_data";
143 // submodule location with jaulib directly hosted below main project (b)
144 const std::string project_root2b = "../../../../jaulib/test_data";
145
146 public:
147 const std::string temp_root = "test_data_temp";
148
149 jau::fs::file_stats getTestDataDirStats(const std::string& test_exe_path) noexcept {
150 const std::string test_exe_dir = jau::fs::dirname(test_exe_path);
151 std::string path = test_exe_dir + "/" + project_root1a;
152 jau::fs::file_stats path_stats(path);
153 if( path_stats.exists() ) {
154 return path_stats;
155 }
156 path = test_exe_dir + "/" + project_root1b;
157 path_stats = jau::fs::file_stats(path);
158 if( path_stats.exists() ) {
159 return path_stats;
160 }
161 path = test_exe_dir + "/" + project_root2a;
162 path_stats = jau::fs::file_stats(path);
163 if( path_stats.exists() ) {
164 return path_stats;
165 }
166 path = test_exe_dir + "/" + project_root2b;
167 path_stats = jau::fs::file_stats(path);
168 if( path_stats.exists() ) {
169 return path_stats;
170 }
171 return jau::fs::file_stats();
172 }
173 std::string getTestDataRelDir(const std::string& test_exe_path) noexcept {
174 const std::string test_exe_dir = jau::fs::dirname(test_exe_path);
175 std::string path = test_exe_dir + "/" + project_root1a;
176 jau::fs::file_stats path_stats(path);
177 if( path_stats.exists() ) {
178 return project_root1a;
179 }
180 path = test_exe_dir + "/" + project_root1b;
181 path_stats = jau::fs::file_stats(path);
182 if( path_stats.exists() ) {
183 return project_root1b;
184 }
185 path = test_exe_dir + "/" + project_root2a;
186 path_stats = jau::fs::file_stats(path);
187 if( path_stats.exists() ) {
188 return project_root2a;
189 }
190 path = test_exe_dir + "/" + project_root2b;
191 path_stats = jau::fs::file_stats(path);
192 if( path_stats.exists() ) {
193 return project_root2b;
194 }
195 return "";
196 }
197 jau::fs::file_stats getTestDataImageFile(const std::string& test_exe_path) noexcept {
198 const std::string test_exe_dir = jau::fs::dirname(test_exe_path);
199 std::string path = test_exe_dir + "/" + image_file;
200 jau::fs::file_stats path_stats(path);
201 if( path_stats.exists() ) {
202 return path_stats;
203 }
204 return jau::fs::file_stats();
205 }
206
207 // external filesystem source to test ...
208 const std::string project_root_ext = "/mnt/ssd0/data/test_data";
209 // external vfat filesystem destination to test ...
210 const std::string dest_fs_vfat = "/mnt/vfat";
211};
jau::fs::file_stats getTestDataImageFile(const std::string &test_exe_path) noexcept
const std::string temp_root
const std::string dest_fs_vfat
std::string getTestDataRelDir(const std::string &test_exe_path) noexcept
jau::fs::file_stats getTestDataDirStats(const std::string &test_exe_path) noexcept
const std::string project_root_ext
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
constexpr bool exists() const noexcept
Returns true if entity does not exist, exclusive bit.
constexpr bool is_link() const noexcept
Returns true if entity is a symbolic link, might be in combination with is_file(),...
constexpr bool is_file() const noexcept
Returns true if entity is a file, might be in combination with is_link().
constexpr bool is_dir() const noexcept
Returns true if entity is a directory, might be in combination with is_link().
uint64_t size() const noexcept
Returns the size in bytes of this element if is_file(), otherwise zero.
constexpr bool has_access() const noexcept
Returns true if entity gives no access to user, exclusive bit.
constexpr bool operator!=(const E lhs, const E rhs) noexcept
constexpr bool operator==(const E lhs, const E rhs) noexcept
constexpr bool is_set(const E mask, const E bits) noexcept
std::string to_string(const fmode_t mask, const bool show_rwx) noexcept
Return the string representation of fmode_t.
std::string dirname(const std::string_view &path) noexcept
Return stripped last component from given path separated by /, excluding the trailing separator /.
traverse_options
Filesystem traverse options used to visit() path elements.
@ follow_symlinks
Traverse through symbolic linked directories if traverse_options::recursive is set,...
std::string to_decstring(const value_type &v, const char separator=',', const nsize_t width=0) noexcept
Produce a decimal string representation of an integral integer value.
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
Definition enum_util.hpp:65
jau::fs::traverse_options topts
std::string to_string() const noexcept
visitor_stats(jau::fs::traverse_options topts_)
int total_sym_links_not_existing
void add(const jau::fs::file_stats &element_stats)
static constexpr const bool _remove_target_test_dir