ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ftvalue.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_filetree of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace filetree {
9
10class FTree;
11class FTFile;
12
13/// The entry type which is embedded in each tree node.
14/// Extends class #"FileStatus" of \alib_system mainly by #".DirectorySums", some extended
15/// entry information and a possible custom value.
17 protected:
18 #if !DOXYGEN
19 friend class FTree; ///< Allow access from within class #"%FTree".
20 friend class FTFile; ///< Allow access from within class #"%FTFile".
21 #endif
22
23 public:
24 /// Recursively accumulated values for directories.
26 uinteger Size = 0; ///< The cumulated sizes of all files and directories.
27 std::array<uint32_t, size_t(Types::MARKER_TYPES_END)> TypeCounters= {0,0,0,0,0,0,0,0}; ///< Per-type counters.
28 uint32_t QtyErrsAccess = 0; ///< Number of access errors in the folder and subfolders.
29 uint32_t QtyErrsBrokenLink = 0; ///< Number of broken symbolic links in the directory and its subfolders.
30 uint32_t QtyStopsOnMaxDepth = 0; ///< Number of recursion aborts due to reach of maximum recursion depth.
31 uint32_t QtyStopsOnCircularLinks = 0; ///< Number of recursion aborts due to detected circular links reach of maximum recursion depth.
32
33
34 /// Defaulted default constructor.
35 constexpr DirectorySums() noexcept =default;
36
37 /// Adds the values in the given summary object to this.
38 /// @param rhs The values to add.
39 /// @return A reference to <c>this</c>
40 DirectorySums& operator+= (const DirectorySums& rhs) {
41 Size += rhs.Size ;
42 for (size_t i = 0; i < size_t(Types::MARKER_TYPES_END); ++i)
43 TypeCounters[i]+= rhs.TypeCounters[i];
44 QtyErrsAccess += rhs.QtyErrsAccess ;
45 QtyErrsBrokenLink += rhs.QtyErrsBrokenLink ;
46 QtyStopsOnMaxDepth += rhs.QtyStopsOnMaxDepth;
47 QtyStopsOnCircularLinks+= rhs.QtyErrsBrokenLink ;
48 return *this;
49 }
50
51 /// Subtracts the values in the given summary object from this.
52 /// @param rhs The values to subtract.
53 /// @return A reference to <c>this</c>
55 Size -= rhs.Size ;
56 for (size_t i = 0; i < size_t(Types::MARKER_TYPES_END); ++i)
57 TypeCounters[i]-= rhs.TypeCounters[i];
62 return *this;
63 }
64 /// Returns \c true if the given \p{type} equals either
65 /// #"Types::DIRECTORY" or
66 /// #"Types::SYMBOLIC_LINK_DIR"
67 /// @param type returns \c false if the given type does not represent a directory
68 /// and \c true if \p{type} equals
69 /// #"Types::DIRECTORY" or
70 /// #"Types::SYMBOLIC_LINK_DIR"
71 /// @return \c false if the given type does not represent a directory, \c true otherwise.
72 constexpr bool IsDirType(Types type) const noexcept { return int(type) < 2; }
73
74 /// Adds a file/directory to the counters
75 /// @param finfo The entry to add.
76 /// @return A reference to <c>this</c>
77 constexpr DirectorySums& Add(const FTValue& finfo) noexcept {
78 ++TypeCounters[size_t(finfo.Type())];
79 Size+= finfo.Size();
80 return *this;
81 }
82
83
84 /// Returns the cumulated number of entries (of any type).
85 /// @return The number of entries counted.
86 uint32_t Count() const noexcept {
87 uint32_t result= 0;
88 for (size_t i = 0; i < size_t(Types::MARKER_TYPES_END); ++i)
89 result+= TypeCounters[i];
90 return result;
91 }
92
93 /// Returns the number of entries of the given \p{type}.
94 /// @param type The type to get the number of entries for.
95 /// @return The number of directories or symbolic links to directories.
96 uint32_t Count(Types type) const noexcept {
98 "FILETREE", "Cant get count for file type \"{}\"", type )
99 return TypeCounters[size_t(type)];
100 }
101
102 /// Returns the sum of the number of entries of type
103 /// #"Types::DIRECTORY" and
104 /// #"Types::SYMBOLIC_LINK_DIR"
105 /// @return The number of directories or symbolic links to directories.
106 uint32_t CountDirectories() const noexcept {
107 return TypeCounters[size_t(Types::DIRECTORY)]
109 }
110
111 /// Returns the sum of the number of entries which are \b not of type
112 /// #"Types::DIRECTORY" and
113 /// #"Types::SYMBOLIC_LINK_DIR"
114 /// @return The number of regular files, fifo, sockets, etc.
115 uint32_t CountNonDirectories() const noexcept {
116 uint32_t result= 0;
117 for (size_t i = 2; i < size_t(Types::MARKER_TYPES_END); ++i)
118 result+= TypeCounters[i];
119 return result;
120 }
121
122 }; // struct DirectorySums
123
124 /// Base type to create pointers to different extended entry information structs.
126 {};
127
128 /// Additional information for entries of directory-type. Allocated in the tree's
129 /// #"MonoAllocator" and accessible via #"GetExtendedInfo()" and #".Sums".
131 DirectorySums Sums; ///< The recursive sums evaluated during scan.
132 };
133
134 /// Additional information for entries of symlink-type. Allocated in the tree's
135 /// #"MonoAllocator" and accessible via #"GetExtendedInfo()",
136 /// #"GetLinkTarget" and #"GetRealLinkTarget".
138 CPathString Target; ///< The target path. This is a zero-terminated #"%CString".
139 CPathString RealTarget; ///< The resolved real target path.
140 };
141
142 /// Additional information for entries of the symbolic link type. Allocated in the tree's
143 /// #"MonoAllocator" and accessible via #"GetExtendedInfo",
144 /// #"GetLinkTarget", #"GetRealLinkTarget" and #".Sums".
146 DirectorySums Sums; ///< The recursive sums evaluated during scan.
147 };
148
149 protected:
150 /// Extended information, depending on the entry type.
152
153 /// Pool-allocated custom data.
154 void* custom = nullptr;
155
156 #if ALIB_DEBUG
157 /// The custom type attached. Used for asserting misuse in debug-compilations.
158 const std::type_info* dbgCustomType = nullptr;
159 #endif
160
161
162 public:
163 /// Retrieves the extended info object of this entry.
164 /// @return The extended info object of this entry. If not available \c nullptr is returned.
165 [[nodiscard]] constexpr ExtendedEntryInfo* GetExtendedInfo() const { return extendedInfo; }
166
167 /// Sets the extended information object. As with all set functions, this method should only
168 /// be used from certain code entities, like file scanners. If used, the object passed here
169 /// has to be pool-allocated using public instance #"FTree::Pool;*".
170 /// The object will be freed with the deletion of the corresponding string tree node
171 /// (respectively #"%FTFile" instance).
172 /// @param extInfo A pointer to the information object to use.
173 constexpr void SetExtendedInfo(ExtendedEntryInfo* extInfo) { extendedInfo= extInfo; }
174
175 /// Retrieves the directory sums of this directory or symbolic link to directory.
176 /// @return A reference to the sums.
177 [[nodiscard]] constexpr DirectorySums& Sums() const {
178 #if ALIB_DEBUG && !ALIB_DEBUG_ASSERTION_PRINTABLES
179 ALIB_ASSERT_ERROR( IsDirectory(), "FILETREE",
180 "Requesting sums for FTValue that is not a directory.")
181 ALIB_ASSERT_ERROR( extendedInfo != nullptr, "FILETREE",
182 "Requesting sums for FTValue that has no sums set. ScanState: ", ScanState() )
183 #endif
185 return static_cast<EIDirectory*>(extendedInfo)->Sums;
186 return static_cast<EISymLinkDir*>(extendedInfo)->Sums;
187 }
188
189 /// Sets the sums of the extended info object of this entry.
190 /// @param sums The sums to set.
191 constexpr void SetSums(const DirectorySums& sums) const {
193 static_cast<EIDirectory*>(extendedInfo)->Sums= sums;
194 return;
195 }
196
198 "Given node is not a directory or symbolic link pointing to a directory.")
199
200 static_cast<EISymLinkDir*>(extendedInfo)->Sums= sums;
201 }
202
203 /// Stores the link targets in the extended information object created for symbolic links.
204 /// @param tree The tree that this object belongs to.
205 /// @param target The target as stored in the symlink
206 /// @param realTarget The translated, 'real' target path (if not broken).
207 void SetLinkTarget(FTree& tree, const PathString& target,
208 const PathString& realTarget);
209
210 /// Retrieves the non-translated target of a symbolic link. In debug compilations, the method
211 /// asserts that #"FileStatus::Type" returns either #"Types::SYMBOLIC_LINK"
212 /// or #"Types::SYMBOLIC_LINK_DIR".
213 /// @return A reference to a copy of the zero-terminated string stored in the symbolic link file.
214 [[nodiscard]] CPathString& GetLinkTarget() const noexcept {
217 "FILETREE", "Given node is not a symbolic link." )
218 return static_cast<EISymLinkFile*>(extendedInfo)->Target;
219 }
220
221 /// Retrieves the resolved target of a symbolic link. In debug compilations, the method
222 /// asserts that #"FileStatus::Type" returns either #"Types::SYMBOLIC_LINK" or
223 /// #"Types::SYMBOLIC_LINK_DIR".
224 /// @return A reference to a zero-terminated string giving the translated real path that a
225 /// symbolic link points to.
226 [[nodiscard]] CPathString& GetRealLinkTarget() const noexcept {
229 "FILETREE", "Given node is not a symbolic link." )
230
231 return static_cast<EISymLinkFile*>(extendedInfo)->RealTarget;
232 }
233
234}; // class FTValue
235
236//==================================================================================================
237/// Helper-class to resolve owner and group ids to strings names. The class uses an instance of
238/// #"LRUCacheTable" of size 10 for each value to increase the performance of the
239/// lookup. Because of this and the fact that the returned string value is located in an
240/// internal member buffer, multithreaded invocations of members #".GetOwnerName" and #".GetGroupName"
241/// have to be protected against racing conditions. This is up to the user of the type.
242//==================================================================================================
244 protected:
245
246 #if !defined( _WIN32)
247 /// The owner name cache.
248 mutable
250
251 /// The group name cache.
252 mutable
254 #endif
255
256 public:
257 #if DOXYGEN || !defined( _WIN32)
258 /// Constructor.
259 /// @param poolAllocator The allocator passed to the internal instances of type
260 /// #"LRUCacheTable".
262 : ownerCache(poolAllocator, 6,6)
263 , groupCache(poolAllocator, 6,6) {}
264 #else
266 #endif
267
268
269 #if DOXYGEN
270 /// Changes the capacity of the #"%LRUCacheTable" for owner names, by calling
271 /// #"LRUCacheTable::Reserve;*".<br>
272 /// The default sizes with construction is \b 6 for both values.
273 /// @param numberOfLists The number of LRU-lists to use.
274 /// @param entriesPerList The maximum length of each cache list.
275 void SetOwnerCacheCapacity( integer numberOfLists, integer entriesPerList );
276
277 /// Changes the capacity of the #"%LRUCacheTable" for owner names, by calling
278 /// #"LRUCacheTable::Reserve;*".<br>
279 /// The default sizes with construction is \b 6 for both values.
280 /// @param numberOfLists The number of LRU-lists to use.
281 /// @param entriesPerList The maximum length of each cache list.
282 void SetGroupCacheCapacity( integer numberOfLists, integer entriesPerList );
283 #elif !defined( _WIN32)
284 void SetOwnerCacheCapacity( integer numberOfLists, integer entriesPerList )
285 { ownerCache.Reserve( numberOfLists, entriesPerList ); }
286 void SetGroupCacheCapacity( integer numberOfLists, integer entriesPerList )
287 { groupCache.Reserve( numberOfLists, entriesPerList); }
288 #else //_WIN32:
291 #endif
292
293 /// Retrieves the file's owner's name.
294 /// @see Method #"FileStatus::GetOwnerName;2".
295 /// @param descriptor The file to examine.
296 /// @return The name of the owner of the file.
298 const NString GetOwnerName( const FileStatus& descriptor ) const;
299
300 /// Retrieves the file's group name.
301 /// @see Method #"FileStatus::GetGroupName;2".
302 /// @param descriptor The file to examine.
303 /// @return The name of the group of the file.
305 const NString GetGroupName( const FileStatus& descriptor ) const;
306}; //class OwnerAndGroupResolver
307
308} // namespace alib[::filetree]
309
310
311/// Type alias in namespace #"%alib".
313
314/// Type alias in namespace #"%alib".
316
317} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
#define ALIB_ASSERT_ERROR(cond, domain,...)
void Reserve(integer newQtyLists, integer newQtyEntriesPerList)
CPathString & GetRealLinkTarget() const noexcept
Definition ftvalue.hpp:226
const std::type_info * dbgCustomType
The custom type attached. Used for asserting misuse in debug-compilations.
Definition ftvalue.hpp:158
ExtendedEntryInfo * extendedInfo
Extended information, depending on the entry type.
Definition ftvalue.hpp:151
CPathString & GetLinkTarget() const noexcept
Definition ftvalue.hpp:214
constexpr ExtendedEntryInfo * GetExtendedInfo() const
Definition ftvalue.hpp:165
void * custom
Pool-allocated custom data.
Definition ftvalue.hpp:154
constexpr void SetExtendedInfo(ExtendedEntryInfo *extInfo)
Definition ftvalue.hpp:173
constexpr void SetSums(const DirectorySums &sums) const
Definition ftvalue.hpp:191
void SetLinkTarget(FTree &tree, const PathString &target, const PathString &realTarget)
Definition ftvalue.cpp:5
constexpr DirectorySums & Sums() const
Definition ftvalue.hpp:177
OwnerAndGroupResolver(PoolAllocator &poolAllocator)
Definition ftvalue.hpp:261
void SetOwnerCacheCapacity(integer numberOfLists, integer entriesPerList)
const NString GetGroupName(const FileStatus &descriptor) const
Definition ftvalue.cpp:39
void SetGroupCacheCapacity(integer numberOfLists, integer entriesPerList)
const NString GetOwnerName(const FileStatus &descriptor) const
Definition ftvalue.cpp:24
@ MARKER_TYPES_END
A marker for the last countable type. The rest are unused/errors.
constexpr Types Type() const noexcept
constexpr bool IsDirectory() const noexcept
constexpr ScanStates ScanState() const noexcept
Definition alox.cpp:14
filetree::FTValue FTValue
Type alias in namespace #"%alib".
Definition ftvalue.hpp:312
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
monomem::TPoolAllocator< MonoAllocator > PoolAllocator
strings::TCString< PathCharType > CPathString
The string-type used with this ALib Module.
Definition path.hpp:37
strings::TString< PathCharType > PathString
The string-type used with this ALib Module.
Definition path.hpp:34
containers::LRUCacheTable< TAllocator, containers::TPairDescriptor< TKey, TMapped > > LRUCacheMap
Type alias in namespace #"%alib".
system::FileStatus FileStatus
Type alias in namespace #"%alib".
lang::uinteger uinteger
Type alias in namespace #"%alib".
Definition integers.hpp:152
filetree::OwnerAndGroupResolver OwnerAndGroupResolver
Type alias in namespace #"%alib".
Definition ftvalue.hpp:315
Recursively accumulated values for directories.
Definition ftvalue.hpp:25
uint32_t QtyErrsBrokenLink
Number of broken symbolic links in the directory and its subfolders.
Definition ftvalue.hpp:29
uint32_t QtyStopsOnCircularLinks
Number of recursion aborts due to detected circular links reach of maximum recursion depth.
Definition ftvalue.hpp:31
uint32_t CountNonDirectories() const noexcept
Definition ftvalue.hpp:115
uint32_t Count(Types type) const noexcept
Definition ftvalue.hpp:96
uint32_t QtyStopsOnMaxDepth
Number of recursion aborts due to reach of maximum recursion depth.
Definition ftvalue.hpp:30
std::array< uint32_t, size_t(Types::MARKER_TYPES_END)> TypeCounters
Per-type counters.
Definition ftvalue.hpp:27
constexpr DirectorySums() noexcept=default
Defaulted default constructor.
uinteger Size
The cumulated sizes of all files and directories.
Definition ftvalue.hpp:26
uint32_t CountDirectories() const noexcept
Definition ftvalue.hpp:106
uint32_t Count() const noexcept
Definition ftvalue.hpp:86
constexpr bool IsDirType(Types type) const noexcept
Definition ftvalue.hpp:72
DirectorySums & operator-=(const DirectorySums &rhs)
Definition ftvalue.hpp:54
uint32_t QtyErrsAccess
Number of access errors in the folder and subfolders.
Definition ftvalue.hpp:28
constexpr DirectorySums & Add(const FTValue &finfo) noexcept
Definition ftvalue.hpp:77
DirectorySums Sums
The recursive sums evaluated during scan.
Definition ftvalue.hpp:131
DirectorySums Sums
The recursive sums evaluated during scan.
Definition ftvalue.hpp:146
CPathString RealTarget
The resolved real target path.
Definition ftvalue.hpp:139
CPathString Target
The target path. This is a zero-terminated #"%CString".
Definition ftvalue.hpp:138
Base type to create pointers to different extended entry information structs.
Definition ftvalue.hpp:126