jaulib v1.3.0
Jau Support Library (C++, Java, ..)
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 <cinttypes>
26#include <cstring>
27#include <memory>
28#include <thread>
29#include <pthread.h>
30
32
33#include <jau/debug.hpp>
34#include <jau/file_util.hpp>
35#include <jau/byte_stream.hpp>
36
37using namespace jau::fractions_i64_literals;
38
39static constexpr const bool _remove_target_test_dir = true;
40
53
55 : topts(topts_),
56 total_real(0),
62 files_real(0),
64 dirs_real(0),
66 {}
67
68 void add(const jau::fs::file_stats& element_stats) {
69 if( element_stats.is_link() ) {
70 if( element_stats.exists() ) {
72 } else {
74 }
75 } else {
76 total_real++;
77 }
78 if( !element_stats.has_access() ) {
80 }
81 if( !element_stats.exists() ) {
83 }
84 if( element_stats.is_file() ) {
85 if( element_stats.is_link() ) {
88 total_file_bytes += element_stats.size();
89 }
90 } else {
91 files_real++;
92 total_file_bytes += element_stats.size();
93 }
94 } else if( element_stats.is_dir() ) {
95 if( element_stats.is_link() ) {
97 } else {
98 dirs_real++;
99 }
100 }
101 }
102
103 std::string to_string() const noexcept {
104 std::string res;
105 res += "- traverse_options "+jau::fs::to_string(topts)+"\n";
106 res += "- total_real "+std::to_string(total_real)+"\n";
107 res += "- total_sym_links_existing "+std::to_string(total_sym_links_existing)+"\n";
108 res += "- total_sym_links_not_existing "+std::to_string(total_sym_links_not_existing)+"\n";
109 res += "- total_no_access "+std::to_string(total_no_access)+"\n";
110 res += "- total_not_existing "+std::to_string(total_not_existing)+"\n";
111 res += "- total_file_bytes "+jau::to_decstring(total_file_bytes)+"\n";
112 res += "- files_real "+std::to_string(files_real)+"\n";
113 res += "- files_sym_link "+std::to_string(files_sym_link)+"\n";
114 res += "- dirs_real "+std::to_string(dirs_real)+"\n";
115 res += "- dirs_sym_link "+std::to_string(dirs_sym_link)+"\n";
116 return res;
117 }
118};
119
120constexpr bool operator ==(const visitor_stats& lhs, const visitor_stats& rhs) noexcept {
121 return lhs.total_file_bytes == rhs.total_file_bytes &&
122 lhs.total_real == rhs.total_real &&
123 lhs.total_sym_links_existing == rhs.total_sym_links_existing &&
124 lhs.total_sym_links_not_existing == rhs.total_sym_links_not_existing &&
125 lhs.total_no_access == rhs.total_no_access &&
126 lhs.total_not_existing == rhs.total_not_existing &&
127 lhs.files_real == rhs.files_real &&
128 lhs.files_sym_link == rhs.files_sym_link &&
129 lhs.dirs_real == rhs.dirs_real &&
130 lhs.dirs_sym_link == rhs.dirs_sym_link;
131}
132constexpr bool operator !=(const visitor_stats& lhs, const visitor_stats& rhs) noexcept {
133 return !( lhs == rhs );
134}
135
137 private:
138 const std::string image_file = "test_data.sqfs";
139 // normal location with jaulib as sole project (a)
140 const std::string project_root1a = "../../test_data";
141 // normal location with jaulib as sole project (b)
142 const std::string project_root1b = "../../../test_data";
143 // submodule location with jaulib directly hosted below main project (a)
144 const std::string project_root2a = "../../../jaulib/test_data";
145 // submodule location with jaulib directly hosted below main project (b)
146 const std::string project_root2b = "../../../../jaulib/test_data";
147
148 public:
149 const std::string temp_root = "test_data_temp";
150
151 jau::fs::file_stats getTestDataDirStats(const std::string& test_exe_path) noexcept {
152 const std::string test_exe_dir = jau::fs::dirname(test_exe_path);
153 std::string path = test_exe_dir + "/" + project_root1a;
154 jau::fs::file_stats path_stats(path);
155 if( path_stats.exists() ) {
156 return path_stats;
157 }
158 path = test_exe_dir + "/" + project_root1b;
159 path_stats = jau::fs::file_stats(path);
160 if( path_stats.exists() ) {
161 return path_stats;
162 }
163 path = test_exe_dir + "/" + project_root2a;
164 path_stats = jau::fs::file_stats(path);
165 if( path_stats.exists() ) {
166 return path_stats;
167 }
168 path = test_exe_dir + "/" + project_root2b;
169 path_stats = jau::fs::file_stats(path);
170 if( path_stats.exists() ) {
171 return path_stats;
172 }
173 return jau::fs::file_stats();
174 }
175 std::string getTestDataRelDir(const std::string& test_exe_path) noexcept {
176 const std::string test_exe_dir = jau::fs::dirname(test_exe_path);
177 std::string path = test_exe_dir + "/" + project_root1a;
178 jau::fs::file_stats path_stats(path);
179 if( path_stats.exists() ) {
180 return project_root1a;
181 }
182 path = test_exe_dir + "/" + project_root1b;
183 path_stats = jau::fs::file_stats(path);
184 if( path_stats.exists() ) {
185 return project_root1b;
186 }
187 path = test_exe_dir + "/" + project_root2a;
188 path_stats = jau::fs::file_stats(path);
189 if( path_stats.exists() ) {
190 return project_root2a;
191 }
192 path = test_exe_dir + "/" + project_root2b;
193 path_stats = jau::fs::file_stats(path);
194 if( path_stats.exists() ) {
195 return project_root2b;
196 }
197 return "";
198 }
199 jau::fs::file_stats getTestDataImageFile(const std::string& test_exe_path) noexcept {
200 const std::string test_exe_dir = jau::fs::dirname(test_exe_path);
201 std::string path = test_exe_dir + "/" + image_file;
202 jau::fs::file_stats path_stats(path);
203 if( path_stats.exists() ) {
204 return path_stats;
205 }
206 return jau::fs::file_stats();
207 }
208
209 // external filesystem source to test ...
210 const std::string project_root_ext = "/mnt/ssd0/data/test_data";
211 // external vfat filesystem destination to test ...
212 const std::string dest_fs_vfat = "/mnt/vfat";
213};
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.
Definition: file_util.hpp:406
constexpr bool exists() const noexcept
Returns true if entity does not exist, exclusive bit.
Definition: file_util.hpp:648
constexpr bool is_link() const noexcept
Returns true if entity is a symbolic link, might be in combination with is_file(),...
Definition: file_util.hpp:642
constexpr bool is_file() const noexcept
Returns true if entity is a file, might be in combination with is_link().
Definition: file_util.hpp:639
constexpr bool is_dir() const noexcept
Returns true if entity is a directory, might be in combination with is_link().
Definition: file_util.hpp:636
uint64_t size() const noexcept
Returns the size in bytes of this element if is_file(), otherwise zero.
Definition: file_util.hpp:597
constexpr bool has_access() const noexcept
Returns true if entity gives no access to user, exclusive bit.
Definition: file_util.hpp:645
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
std::string dirname(const std::string_view &path) noexcept
Return stripped last component from given path separated by /, excluding the trailing separator /.
Definition: file_util.cpp:129
traverse_options
Filesystem traverse options used to visit() path elements.
Definition: file_util.hpp:879
std::string to_string(const fmode_t mask, const bool show_rwx=false) noexcept
Return the string representation of fmode_t.
Definition: file_util.cpp:368
@ 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.
constexpr bool is_set(const cpu_family_t mask, const cpu_family_t bit) noexcept
Definition: cpuid.hpp:122
size_t total_file_bytes
jau::fs::traverse_options topts
std::string to_string() const noexcept
visitor_stats(jau::fs::traverse_options topts_)
int total_sym_links_not_existing
int total_sym_links_existing
void add(const jau::fs::file_stats &element_stats)
constexpr bool operator!=(const visitor_stats &lhs, const visitor_stats &rhs) noexcept
constexpr bool operator==(const visitor_stats &lhs, const visitor_stats &rhs) noexcept
static constexpr const bool _remove_target_test_dir