ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
finfo.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_files of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_FILES_FINFO
9#define HPP_ALIB_FILES_FINFO 1
10#pragma once
18
19namespace alib { namespace files {
20
21class FTree;
22class File;
23
24/// The entry type which is embedded in each tree node.
25class FInfo
26{
27 protected:
28 friend class FTree; ///< Allow access from within class File.
29 friend class File; ///< Allow access from within class File.
30
31 public:
32 /// Enumeration of the possible file types. This is compatible with the posix list of types,
33 /// with the exception that symbolic links are differentiated between those linking to
34 /// a directory and those linking to any other file type.
35 enum class Types : uint8_t
36 {
37 DIRECTORY = 0, ///< Directory/folder.
38 SYMBOLIC_LINK_DIR = 1, ///< Symbolic link targeting a directory.
39 ///< In case scanning does not resolve links, this is never set.
40 REGULAR = 2, ///< Regular file.
41 SYMBOLIC_LINK = 3, ///< Symbolic link targeting a non-directory file.
42 ///< In case scanning does not resolve links, possibly to a directory.
43 BLOCK = 4, ///< A block special file.
44 CHARACTER = 5, ///< A character special file.
45 FIFO = 6, ///< A FIFO (also known as pipe) file.
46 SOCKET = 7, ///< A socket file.
47
48 UNKNOWN_OR_ERROR = 8, ///< Filetype (should never or seldom happen).
49 ///< Maybe filesystem changed during scan or similar strange thing.
50 MARKER_TYPES_END = 9, ///< A marker for the last countable type. The rest are unused/errors
51 };
52
53 /// This is a resourced enumeration that is equivalent to enum class #Types but uses
54 /// a 1-Letter code when serialized. The symbols are taken from GNU/Linux command
55 /// <c>'ls -l'</c>, except special type \b SYMBOLIC_LINK_DIR which uses an upper case <c>'L'</c>
56 /// in contrast to the lower case <c>'l'</c> used with links to files.
57 enum class TypeNames1Letter : uint8_t
58 {
59 DIRECTORY = 0, ///< d: Directory/folder.
60 SYMBOLIC_LINK_DIR = 1, ///< L: Symbolic link targeting a directory.
61 REGULAR = 2, ///< -: Regular file.
62 SYMBOLIC_LINK = 3, ///< l: Symbolic link targeting a non-directory file.
63 BLOCK = 4, ///< b: A block special file.
64 CHARACTER = 5, ///< c: A character special file.
65 FIFO = 6, ///< p: A FIFO (also known as pipe) file.
66 SOCKET = 7, ///< s: A socket file.
67 };
68
69 /// This is a resourced enumeration that is equivalent to enum class #Types but uses
70 /// a 2-Letter code when serialized.
71 enum class TypeNames2Letters : uint8_t
72 {
73 DIRECTORY = 0, ///< DR: Directory/folder.
74 SYMBOLIC_LINK_DIR = 1, ///< LD: Symbolic link targeting a directory.
75 REGULAR = 2, ///< RF: Regular file.
76 SYMBOLIC_LINK = 3, ///< LF: Symbolic link targeting a non-directory file.
77 BLOCK = 4, ///< BL: A block special file.
78 CHARACTER = 5, ///< CH: A character special file.
79 FIFO = 6, ///< FF: A FIFO (also known as pipe) file.
80 SOCKET = 7, ///< SO: A socket file.
81 };
82
83 /// This is a resourced enumeration that is equivalent to enum class #Types but uses
84 /// a 3-Letter code when serialized.
85 enum class TypeNames3Letters : uint8_t
86 {
87 DIRECTORY = 0, ///< DIR: Directory/folder.
88 SYMBOLIC_LINK_DIR = 1, ///< SLD: Symbolic link targeting a directory.
89 REGULAR = 2, ///< REG: Regular file.
90 SYMBOLIC_LINK = 3, ///< SLF: Symbolic link targeting a non-directory file.
91 BLOCK = 4, ///< BLK: A block special file.
92 CHARACTER = 5, ///< CHR: A character special file.
93 FIFO = 6, ///< FFO: A FIFO (also known as pipe) file.
94 SOCKET = 7, ///< SCK: A socket file.
95 };
96
97
98 /// Permission flags. Compatible with posix* definition.
99 enum class Permissions : uint32_t
100 {
101 NONE = 0, ///< no permission bits are set
102 UNKNOWN = 010000, ///< Unknown permissions (e.g., when not read, or filesystem does not support permissions)
103 MASK = 07777, ///< All valid permission bits. Equivalent to all | set_uid | set_gid | sticky_bit
104 ALL = 0777, ///< All users have read, write, and execute/search permissions Equivalent to owner_all | group_all | others_all
105
106 OWNER_READ = 0400, /// < S_IRUSR File owner has read permission
107 OWNER_WRITE = 0200, /// < S_IWUSR File owner has write permission
108 OWNER_EXEC = 0100, /// < S_IXUSR File owner has execute/search permission
109 OWNER_ALL = 0700, /// < S_IRWXU File owner has read, write, and execute/search permissions Equivalent to owner_read | owner_write | owner_exec
110
111 GROUP_READ = 040, /// < S_IRGRP The file's user group has read permission
112 GROUP_WRITE = 020, /// < S_IWGRP The file's user group has write permission
113 GROUP_EXEC = 010, /// < S_IXGRP The file's user group has execute/search permission
114 GROUP_ALL = 070, /// < S_IRWXG The file's user group has read, write, and execute/search permissions Equivalent to group_read | group_write | group_exec
115
116 OTHERS_READ = 04, ///< S_IROTH Other users have read permission
117 OTHERS_WRITE = 02, ///< S_IWOTH Other users have write permission
118 OTHERS_EXEC = 01, ///< S_IXOTH Other users have execute/search permission
119 OTHERS_ALL = 07, ///< S_IRWXO Other users have read, write, and execute/search permissions Equivalent to others_read | others_write | others_exec
120
121 SET_UID = 04000, ///< S_ISUID Set user ID to file owner user ID on execution
122 SET_GID = 02000, ///< S_ISGID Set group ID to file's user group ID on execution
123 STICKY_BIT = 01000, ///< S_ISVTX Implementation-defined meaning, but POSIX XSI specifies that when set on a directory, only file owners may delete files even if the directory is writeable to others (used with /tmp)
124 };
125
126 /// Type definition for owner and group ids
127 using TOwnerAndGroupID = uint32_t;
128
129 /// Constant value for owner and group IDs to denote that the field was not determined.
130 static constexpr TOwnerAndGroupID UnknownID= (std::numeric_limits<uint32_t>::max)();
131
132 /// Per-entry information about how a node was scanned.
133 enum class Qualities : uint8_t
134 {
135 NONE = 0, ///< Node created only from given (real) start path.
136 STATS = 1, ///< Only stats (size, date, owner, etc.) read.
137
138 RESOLVED = 2, ///< Read symlink target strings.
139
140 MAX_DEPTH_REACHED = 3, ///< Scanner stopped, because maximum depth was reached.
141 NOT_FOLLOWED = 4, ///< A symbolic link that targets a directory, but scan parameters specify not to follow.
142 NOT_CROSSING_FS = 5, ///< A directory that represented a mounted filesystem was not followed due to field
143 NO_AFS = 6, ///< A directory that represented a mounted filesystem was not followed due to field
144 ///< \alib{files;ScanParameters::CrossFileSystems} being set to \c false.
145 RECURSIVE = 7, ///< Follow symlink target strings.
146
147 NO_ACCESS = 8, ///< Scanner failure due to limited access rights.
148 NO_ACCESS_SL = 9, ///< Scanner failure due to limited access rights.
149 NO_ACCESS_SL_TARGET=10, ///< Scanner failure due to limited access rights.
150 NO_ACCESS_DIR =11, ///< Scanner failure due to limited access rights.
151 BROKEN_LINK =12, ///< A symbolic link targets a non-existent file or directory.
152 CIRCULAR_LINK =13, ///< Set if recursion stopped on a symbolic link which was identified by the
153 ///< operating system as a circular link.
154 DUPLICATE =14, ///< Set if recursion stopped on a symbolic link who's target had been scanned already.
155 ///< This might happen either because the path was already scanned by another search, or
156 ///< because a symbolic link is circular or because of a mixture of both.
157 ///< Thus, this can indicate a circular link, but does not have to.
158 NOT_EXISTENT =15, ///< Set if a given start path does not exist.
159 UNKNOWN_ERROR =16, ///< Unknown scanner failure
160 };
161
162 /// This is a resourced enumeration that is equivalent to enum class #Qualities but uses
163 /// a 3-Letter code when serialized.
164 enum class Qualities3Letters : uint8_t
165 {
166 NONE = 0, ///< NON: Node created only from given (real) start path.
167 STATS = 1, ///< STA: Only stats (size, date, owner, etc.) read.
168
169 RESOLVED = 2, ///< RES: Read symlink target strings.
170
171 MAX_DEPTH_REACHED = 3, ///< MDR:Scanner stopped, because maximum depth was reached.
172 NOT_FOLLOWED = 4, ///< NFO:A symbolic link that targets a directory, but scan parameters specify not to follow.
173 NOT_CROSSING_FS = 5, ///< NCF:A directory that represented a mounted filesystem was not followed due to field
174 NO_AFS = 6, ///< NAF:A directory that represented a mounted filesystem was not followed due to field
175 ///< \alib{files;ScanParameters::CrossFileSystems} being set to \c false.
176 RECURSIVE = 7, ///< REC:Follow symlink target strings.
177
178 NO_ACCESS = 8, ///< NAC: Scanner failure due to limited access rights.
179 NO_ACCESS_SL = 9, ///< NSL: Scanner failure due to limited access rights.
180 NO_ACCESS_SL_TARGET=10, ///< NAT: Scanner failure due to limited access rights.
181 NO_ACCESS_DIR =11, ///< NAD: Scanner failure due to limited access rights.
182 BROKEN_LINK =12, ///< BRL: A symbolic link targets a non-existent file or directory.
183 CIRCULAR_LINK =13, ///< CIL: Set if recursion stopped on a symbolic link which was identified by the
184 ///< operating system as a circular link.
185 DUPLICATE =14, ///< DUP: Set if recursion stopped on a symbolic link who's target had been scanned already.
186 ///< This might happen either because the path was already scanned by another search, or
187 ///< because a symbolic link is circular or because of a mixture of both.
188 ///< Thus, this can indicate a circular link, but does not have to.
189 NOT_EXISTENT =15, ///< NEX: Set if a given start path does not exist.
190 UNKNOWN_ERROR =16, ///< UKE: Unknown scanner failure
191 };
192
193
194 /// Recursively accumulated values for directories.
196 {
198 uinteger Size = 0; ///< The cumulated sizes of all files and directories.
199 uint32_t TypeCounters[int(Types::MARKER_TYPES_END)]= {0,0,0,0,0,0,0,0}; ///< Per-type counters.
200 uint32_t QtyErrsAccess = 0; ///< Number of access errors in the folder and subfolders.
201 uint32_t QtyErrsBrokenLink = 0; ///< Number of broken symbolic links in the directory and its subfolders.
202 uint32_t QtyStopsOnMaxDepth = 0; ///< Number of recursion aborts due to reach of maximum recursion depth.
203 uint32_t QtyStopsOnCircularLinks = 0; ///< Number of recursion aborts due to detected circular links reach of maximum recursion depth.
204
205
206 /// Defaulted default constructor.
207 constexpr DirectorySums() noexcept = default;
208
209 /// Adds the values in the given summary object to this.
210 /// @param rhs The values to add.
211 /// @return A reference to <c>this</c>
212 DirectorySums& operator+= (const DirectorySums& rhs)
213 {
214 Size += rhs.Size ;
215 for (int i = 0; i < int(Types::MARKER_TYPES_END); ++i)
216 TypeCounters[i]+= rhs.TypeCounters[i];
217 QtyErrsAccess += rhs.QtyErrsAccess ;
218 QtyErrsBrokenLink += rhs.QtyErrsBrokenLink ;
219 QtyStopsOnMaxDepth += rhs.QtyStopsOnMaxDepth;
220 QtyStopsOnCircularLinks+= rhs.QtyErrsBrokenLink ;
221 return *this;
222 }
223
224 /// Subtracts the values in the given summary object from this.
225 /// @param rhs The values to subtract.
226 /// @return A reference to <c>this</c>
228 {
229 Size -= rhs.Size ;
230 for (int i = 0; i < int(Types::MARKER_TYPES_END); ++i)
231 TypeCounters[i]-= rhs.TypeCounters[i];
236 return *this;
237 }
238 /// Returns \c true if the given \p{type} equals either
239 /// \alib{files::FInfo;Types::DIRECTORY} or
240 /// \alib{files::FInfo;Types::SYMBOLIC_LINK_DIR}
241 /// @param type returns \c false if the given type does not represent a directory
242 /// and \c true if \p{type} equals
243 /// \alib{files::FInfo;Types::DIRECTORY} or
244 /// \alib{files::FInfo;Types::SYMBOLIC_LINK_DIR}
245 /// @return \c false if the given type does not represent a directory, \c true otherwise.
246 constexpr bool IsDirType(Types type) const noexcept
247 {
248 return int(type) < 2;
249 }
250
251 /// Adds a file/directory to the counters
252 /// @param finfo The entry to add.
253 /// @return A reference to <c>this</c>
254 constexpr DirectorySums& Add(const FInfo& finfo) noexcept
255 {
256 ++TypeCounters[int(finfo.Type())];
257 Size+= finfo.Size();
258 return *this;
259 }
260
261
262 /// Returns the cumulated number of entries (of any type).
263 /// @return The number of entries counted.
264 uint32_t Count() const noexcept
265 {
266 uint32_t result= 0;
267 for (int i = 0; i < int(Types::MARKER_TYPES_END); ++i)
268 result+= TypeCounters[i];
269 return result;
270 }
271
272 /// Returns the number of entries of the given \p{type}.
273 /// @param type The type to get the number of entries for.
274 /// @return The number of directories or symbolic links to directories.
275 uint32_t Count(Types type) const noexcept
276 {
278 "FILES", "Cant get count for file type {!Q}", type )
279 return TypeCounters[int(type)];
280 }
281
282 /// Returns the sum of the number of entries of type
283 /// \alib{files::FInfo;Types::DIRECTORY} and
284 /// \alib{files::FInfo;Types::SYMBOLIC_LINK_DIR}
285 /// @return The number of directories or symbolic links to directories.
286 uint32_t CountDirectories() const noexcept
287 {
288 return TypeCounters[int(Types::DIRECTORY)]
290 }
291
292 /// Returns the sum of the number of entries which are \b not of type
293 /// \alib{files::FInfo;Types::DIRECTORY} and
294 /// \alib{files::FInfo;Types::SYMBOLIC_LINK_DIR}
295 /// @return The number of regular files, fifo, sockets, etc.
296 uint32_t CountNonDirectories() const noexcept
297 {
298 uint32_t result= 0;
299 for (int i = 2; i < int(Types::MARKER_TYPES_END); ++i)
300 result+= TypeCounters[i];
301 return result;
302 }
304 }; // struct DirectorySums
305
306 /// Base type to create pointers to different extended entry information structs.
308 {};
309
310 /// Additional information for entries of directory-type. Allocated in the tree's
311 /// \alib{MonoAllocator} and accessible via #GetExtendedInfo() and #Sums.
313 {
314 DirectorySums Sums; ///< The recursive sums evaluated during scan.
315 };
316
317 /// Additional information for entries of symlink-type. Allocated in the tree's
318 /// \alib{MonoAllocator} and accessible via #GetExtendedInfo(),
319 /// #GetLinkTarget and #GetRealLinkTarget.
321 {
322 lang::system::CPathString Target; ///< The target path.
323 ///< This is a zero-terminated \b CString.
324 lang::system::CPathString RealTarget; ///< The resolved real target path.
325 };
326
327 /// Additional information for entries of symbolic link type. Allocated in the tree's
328 /// \alib{MonoAllocator} and accessible via #GetExtendedInfo(), #GetLinkTarget,
329 /// #GetRealLinkTarget and #Sums.
331 {
332 DirectorySums Sums; ///< The recursive sums evaluated during scan.
333 };
334
335 protected:
336 DateTime mDate ; ///< The last date and time the contents of the file was modified.
337 ///< This timestamp is should be correct on all platforms/filesystems.
338 DateTime bDate ; ///< The date this file was created. This timestamp is correctly set only
339 ///< with certain filesystems under GNU/Linux, e.g., 'EXT4'.
340 ///< If not available, the smallest value of the other three timestamps
341 ///< is used.
342 DateTime cDate ; ///< The date of the last change of the files' meta-information (e.g., owner).
343 ///< This is not correctly set on all platforms/filesystems.
344 ///< If not available, it will be set to #mDate.
345 DateTime aDate ; ///< The date of last read or write access.
346 ///< This is not correctly set on all platforms/filesystems.
347 ///< If not available, it will be set to #mDate.
348 uinteger size ; ///< The file size. In case of a directory, this is \c 0.
349 uint32_t owner ; ///< The user id that owns the file.
350 uint32_t group ; ///< The group id that owns the file.
351 uint32_t qtyHardLinks ; ///< The number of hard links to the file.
352
353
354
355 /// A bitfield encoding various information.
356 struct Bits {
357 Types Type : 4; ///< The file type.
358 bool IsArtificialFS : 1; ///< Denotes whether the file resides in an artificial filesystem.
359 bool TargetIsArtificialFS : 1; ///< Denotes whether a link points into an artificial filesystem.
360 bool IsCrossingFS : 1; ///< Denotes whether the file is a mount point.
361 bool TargetIsCrossingFS : 1; ///< Denotes whether a link points to a mount point.
362 Permissions Permission : 13; ///< The unix file-permissions.
363 Qualities Quality : 5; ///< The scan quality.
364
365 /// Default constructor. Sets all members to \b %NONE, \c false or \b %Error.
368 , IsArtificialFS {false}
369 , TargetIsArtificialFS{false}
370 , IsCrossingFS {false}
371 , TargetIsCrossingFS {false}
374 {}
375 };
376
377 Bits bits; ///< A bitfield encoding various information.
378
379 /// Extended information, depending on the entry type.
381
382 /// Pool-allocated custom data.
383 void* custom = nullptr;
384 #if ALIB_DEBUG
385 /// The custom type attached. Used for asserting misuse in debug-compilations.
386 const std::type_info* dbgCustomType = nullptr;
387 #endif
388
389
390public:
391
392 /// @return Retrieves the permissions of the entry.
393 [[nodiscard]] constexpr Permissions Perms () const noexcept { return bits.Permission; }
394 /// @return Retrieves the type of the entry
395 [[nodiscard]] constexpr Types Type () const noexcept { return bits.Type; }
396 /// @return Checks type for being either directory or symbolic link pointing to one.
397 [[nodiscard]] constexpr bool IsDirectory () const noexcept { return int(bits.Type) < 2; }
398 /// @return Checks type for being a symbolic link (to normal file or to a directory).
399 [[nodiscard]] constexpr bool IsSymbolicLink () const noexcept { return Type() == Types::SYMBOLIC_LINK
401 /// @return Retrieves the scan quality of the entry.
402 [[nodiscard]] constexpr Qualities Quality () const noexcept { return bits.Quality; }
403 /// @return Returns true if the entry resides on an artificial filesystem.
404 [[nodiscard]] constexpr bool IsArtificialFS () const noexcept { return bits.IsArtificialFS; }
405 /// @return Returns true if the entry is a symlink and its target resides on an artificial filesystem.
406 [[nodiscard]] constexpr bool TargetIsArtificialFS() const noexcept { return bits.TargetIsArtificialFS; }
407 /// @return Returns true if the entry resides on a different filesystem than it's parent.
408 [[nodiscard]] constexpr bool IsCrossingFS () const noexcept { return bits.IsCrossingFS; }
409 /// @return Returns true if the entry is a symlink and resides on a different filesystem than the link.
410 [[nodiscard]] constexpr bool TargetIsCrossingFS () const noexcept { return bits.TargetIsCrossingFS; }
411 /// @return Retrieves the file size.
412 [[nodiscard]] constexpr uinteger Size () const noexcept { return size; }
413 /// @return Retrieves the \ref mDate "last modification date" of this file/folder.
414 [[nodiscard]] constexpr DateTime MDate () const noexcept { return mDate; }
415 /// @return Retrieves the \ref bDate "birth date" of this file/folder.
416 [[nodiscard]] constexpr DateTime BDate () const noexcept { return bDate; }
417 /// @return Retrieves the \ref cDate "change date" of this file/folder. If unavailable, same as #MDate.
418 [[nodiscard]] constexpr DateTime CDate () const noexcept { return cDate; }
419 /// @return Retrieves the \ref aDate "timestamp of last access" to this file/folder. If unavailable, same as #MDate.
420 [[nodiscard]] constexpr DateTime ADate () const noexcept { return aDate; }
421 /// @return Retrieves the ID of the owner of the file/folder if available. Otherwise set to #UnknownID.
422 [[nodiscard]] constexpr uint32_t Owner () const noexcept { return owner; }
423 /// @return Retrieves the ID of the group of the file/folder if available. Otherwise set to #UnknownID.
424 [[nodiscard]] constexpr uint32_t Group () const noexcept { return group; }
425 /// @return Retrieves the number of hard links pointing to this file if available. Otherwise set to #UnknownID.
426 [[nodiscard]] constexpr uint32_t QtyHardLinks () const noexcept { return qtyHardLinks; }
427
428 /// Retrieves the extended info object of this entry.
429 /// @return The extended info object of this entry. If not available \c nullptr is returned.
430 [[nodiscard]] constexpr ExtendedEntryInfo* GetExtendedInfo() const
431 { return extendedInfo; }
432
433 /// Sets the extended information object. As with all set functions, this method should only
434 /// be used from certain code entities, like file scanners. If used, the object passed here
435 /// has to be pool-allocated using public instance \alib{files;FTree::Pool}.
436 /// The object will be freed with the deletion of the corresponding string tree node
437 /// (respectively \b File instance).
438 /// @param extInfo A pointer to the information object to use.
439 constexpr void SetExtendedInfo(ExtendedEntryInfo* extInfo)
440 { extendedInfo= extInfo; }
441
442 /// Retrieves the directory sums of this directory or symbolic link to directory.
443 /// @return A reference to the sums.
444 [[nodiscard]] constexpr DirectorySums& Sums() const
445 {
446 ALIB_ASSERT_ERROR( IsDirectory(), "FILES",
447 "Requesting sums for FInfo that is not a directory.")
448 ALIB_ASSERT_ERROR( extendedInfo != nullptr, "FILES",
449 "Requesting sums for FInfo that has no sums set. Quality: ", Quality() )
451 return static_cast<EIDirectory*>(extendedInfo)->Sums;
452 return static_cast<EISymLinkDir*>(extendedInfo)->Sums;
453 }
454
455 /// Sets the sums of the extended info object of this entry.
456 /// @param sums The sums to set.
457 constexpr void SetSums(const DirectorySums& sums) const
458 {
460 {
461 static_cast<EIDirectory*>(extendedInfo)->Sums= sums;
462 return;
463 }
464
466 "Given node is not a directory or symbolic link pointing to a directory.")
467
468 static_cast<EISymLinkDir*>(extendedInfo)->Sums= sums;
469 }
470
471 /// Stores the link targets in the extended information object created for symbolic links.
472 /// @param tree The tree that this object belongs to.
473 /// @param target The target as stored in the symlink
474 /// @param realTarget The translated, 'real' target path (if not broken).
475 void SetLinkTarget(FTree& tree, const lang::system::PathString& target,
476 const lang::system::PathString& realTarget);
477
478 /// Retrieves the non-translated target of a symbolic link. In debug compilations, the method
479 /// asserts that #Type returns either \alib{files::FInfo;Types::SYMBOLIC_LINK}
480 /// or \alib{files::FInfo;Types::SYMBOLIC_LINK_DIR}.
481 /// @return A reference to a copy of the zero-terminated string stored in the symbolic link file.
482 [[nodiscard]] lang::system::CPathString& GetLinkTarget() const noexcept
483 {
485 || Type() == FInfo::Types::SYMBOLIC_LINK_DIR, "FILES",
486 "Given node is not a symbolic link.")
487
488 return static_cast<EISymLinkFile*>(extendedInfo)->Target;
489 }
490
491 /// Retrieves the resolved target of a symbolic link. In debug compilations, the method
492 /// asserts that #Type returns either \alib{files::FInfo;Types::SYMBOLIC_LINK} or
493 /// \alib{files::FInfo;Types::SYMBOLIC_LINK_DIR}.
494 /// @return A reference to a zero-terminated string giving the translated real path that a
495 /// symbolic link points to.
496 [[nodiscard]] lang::system::CPathString& GetRealLinkTarget() const noexcept
497 {
499 || Type() == FInfo::Types::SYMBOLIC_LINK_DIR, "FILES",
500 "Given node is not a symbolic link.")
501
502 return static_cast<EISymLinkFile*>(extendedInfo)->RealTarget;
503 }
504
505 /// Sets the permissions of the entry. \param v The value to set.
506 void SetPerms (Permissions v) noexcept { bits.Permission= v; }
507 /// Sets the type of the entry. \param v The value to set.
508 void SetType (Types v) noexcept { bits.Type= v; }
509 /// Sets the quality of scan of the entry. \param v The value to set.
510 void SetQuality (Qualities v) noexcept { bits.Quality= v; }
511 /// Mark the entry as residing on an artificial filesystem.
512 void SetArtificialFS() noexcept { bits.IsArtificialFS= true; }
513 /// Mark the entry as a symlink who's target is residing on an artificial filesystem.
515 /// Mark the entry as residing on a different filesystem than its parent.
516 void SetCrossingFS() noexcept { bits.IsCrossingFS= true; }
517 /// Mark the entry as a symlink who's target is residing on a different filesystem than the symlink.
518 void SetTargetCrossingFS() noexcept { bits.TargetIsCrossingFS= true; }
519 /// Sets the file size. \param v The value to set.
520 void SetSize (uinteger v) noexcept { size = v; }
521 /// Sets the \ref mDate "last modification date" of this file/folder. \param v The value to set.
522 void SetMDate (DateTime v) noexcept { mDate= v; }
523 /// Sets the \ref bDate "birth date" of this file/folder. \param v The value to set.
524 void SetBDate (DateTime v) noexcept { bDate= v; }
525 /// Sets the \ref cDate "change date" of this file/folder. \param v The value to set.
526 void SetCDate (DateTime v) noexcept { cDate= v; }
527 /// Sets the \ref aDate "time of last access" of this file/folder. \param v The value to set.
528 void SetADate (DateTime v) noexcept { aDate= v; }
529 /// Sets the ID of the owner of the file/folder. \param v The value to set.
530 void SetOwner (uint32_t v) noexcept { owner= v; }
531 /// Sets the ID of the group of the file/folder. \param v The value to set.
532 void SetGroup (uint32_t v) noexcept { group= v; }
533 /// Sets the number of hard links that point to this file. \param v The value to set.
534 void SetQtyHardlinks(uint32_t v) noexcept { qtyHardLinks= v; }
535}; // class FInfo
536
537
538//==================================================================================================
539/// Helper-class to resolve owner and group ids to strings names. The class uses an instance of
540/// \alib{containers;LRUCacheTable} of size 10 for each value to increase the performance of the
541/// lookup. Because of this and the fact that the returned string value is located in an internal
542/// member buffer, multithreaded invocations of members #GetOwnerName and #GetGroupName have to be
543/// protected against racing conditions. This is up to the user of the type.
544//==================================================================================================
546{
547 protected:
548
549 #if !defined( _WIN32)
550 /// The owner name cache.
552
553 /// The group name cache.
555 #endif
556
557 public:
558 #if DOXYGEN || !defined( _WIN32)
559 /// Constructor.
560 /// @param poolAllocator The allocator passed to the internal instances of type
561 /// \alib{containers;LRUCacheTable}.
563 : ownerCache(poolAllocator, 6,6)
564 , groupCache(poolAllocator, 6,6) {}
565 #else
567 #endif
568
569
570 #if DOXYGEN
571 /// Changes the capacity of the \b %LRUCacheTable for owner names, by calling
572 /// \alib{containers;LRUCacheTable::Reserve}.<br>
573 /// The default sizes with construction is \b 6 for both values.
574 /// @param numberOfLists The number of LRU-lists to use.
575 /// @param entriesPerList The maximum length of each cache list.
576 void SetOwnerCacheCapacity( integer numberOfLists, integer entriesPerList );
577
578 /// Changes the capacity of the \b %LRUCacheTable for owner names, by calling
579 /// \alib{containers;LRUCacheTable::Reserve}.<br>
580 /// The default sizes with construction is \b 6 for both values.
581 /// @param numberOfLists The number of LRU-lists to use.
582 /// @param entriesPerList The maximum length of each cache list.
583 void SetGroupCacheCapacity( integer numberOfLists, integer entriesPerList );
584 #elif !defined( _WIN32)
585 void SetOwnerCacheCapacity( integer numberOfLists, integer entriesPerList )
586 { ownerCache.Reserve( numberOfLists, entriesPerList ); }
587 void SetGroupCacheCapacity( integer numberOfLists, integer entriesPerList )
588 { groupCache.Reserve( numberOfLists, entriesPerList); }
589 #else //_WIN32:
592 #endif
593
594 /// Retrieves the file's owner's name.
595 /// @param fInfo The file to examine.
596 /// @return The name of the owner of the file.
598 const NString& GetOwnerName( const FInfo& fInfo );
599
600 /// Retrieves the file's group name.
601 /// @param fInfo The file to examine.
602 /// @return The name of the group of the file.
604 const NString& GetGroupName( const FInfo& fInfo );
605}; //class OwnerAndGroupResolver
606
607} // namespace alib[::files]
608
609
610/// Type alias in namespace \b alib.
612
613/// Type alias in namespace \b alib.
615
616} // namespace [alib]
617
628ALIB_RESOURCED_IN_MODULE(alib::files::FInfo::TypeNames1Letter , alib::FILES, "FT1" )
629ALIB_RESOURCED_IN_MODULE(alib::files::FInfo::TypeNames2Letters, alib::FILES, "FT2" )
630ALIB_RESOURCED_IN_MODULE(alib::files::FInfo::TypeNames3Letters, alib::FILES, "FT3" )
631ALIB_RESOURCED_IN_MODULE(alib::files::FInfo::Qualities , alib::FILES, "FQ" )
632ALIB_RESOURCED_IN_MODULE(alib::files::FInfo::Qualities3Letters, alib::FILES, "FQ3" )
633
634
635ALIB_BOXING_VTABLE_DECLARE( alib::files::FInfo::Permissions , vt_files_perms )
636ALIB_BOXING_VTABLE_DECLARE( alib::files::FInfo::Types , vt_files_type )
637ALIB_BOXING_VTABLE_DECLARE( alib::files::FInfo::TypeNames1Letter , vt_files_type1 )
638ALIB_BOXING_VTABLE_DECLARE( alib::files::FInfo::TypeNames2Letters , vt_files_type2 )
639ALIB_BOXING_VTABLE_DECLARE( alib::files::FInfo::TypeNames3Letters , vt_files_type3 )
640ALIB_BOXING_VTABLE_DECLARE( alib::files::FInfo::Qualities , vt_files_qual )
641ALIB_BOXING_VTABLE_DECLARE( alib::files::FInfo::Qualities3Letters , vt_files_qual3 )
642
643#endif // HPP_ALIB_FILES_FINFO
644
void Reserve(integer newQtyLists, integer newQtyEntriesPerList)
The entry type which is embedded in each tree node.
Definition finfo.hpp:26
lang::system::CPathString & GetLinkTarget() const noexcept
Definition finfo.hpp:482
uint32_t group
The group id that owns the file.
Definition finfo.hpp:350
uint32_t owner
The user id that owns the file.
Definition finfo.hpp:349
void SetOwner(uint32_t v) noexcept
Sets the ID of the owner of the file/folder.
Definition finfo.hpp:530
constexpr bool IsSymbolicLink() const noexcept
Definition finfo.hpp:399
static constexpr TOwnerAndGroupID UnknownID
Constant value for owner and group IDs to denote that the field was not determined.
Definition finfo.hpp:130
void SetCDate(DateTime v) noexcept
Sets the change date of this file/folder.
Definition finfo.hpp:526
Bits bits
A bitfield encoding various information.
Definition finfo.hpp:377
constexpr bool TargetIsArtificialFS() const noexcept
Definition finfo.hpp:406
constexpr Permissions Perms() const noexcept
Definition finfo.hpp:393
constexpr uint32_t Group() const noexcept
Definition finfo.hpp:424
constexpr void SetSums(const DirectorySums &sums) const
Definition finfo.hpp:457
void SetBDate(DateTime v) noexcept
Sets the birth date of this file/folder.
Definition finfo.hpp:524
void SetTargetArtificialFS() noexcept
Mark the entry as a symlink who's target is residing on an artificial filesystem.
Definition finfo.hpp:514
constexpr DateTime MDate() const noexcept
Definition finfo.hpp:414
uint32_t TOwnerAndGroupID
Type definition for owner and group ids.
Definition finfo.hpp:127
uint32_t qtyHardLinks
The number of hard links to the file.
Definition finfo.hpp:351
constexpr void SetExtendedInfo(ExtendedEntryInfo *extInfo)
Definition finfo.hpp:439
constexpr DirectorySums & Sums() const
Definition finfo.hpp:444
void SetLinkTarget(FTree &tree, const lang::system::PathString &target, const lang::system::PathString &realTarget)
Definition finfo.cpp:23
void SetQtyHardlinks(uint32_t v) noexcept
Sets the number of hard links that point to this file.
Definition finfo.hpp:534
void SetTargetCrossingFS() noexcept
Mark the entry as a symlink who's target is residing on a different filesystem than the symlink.
Definition finfo.hpp:518
constexpr uinteger Size() const noexcept
Definition finfo.hpp:412
constexpr uint32_t QtyHardLinks() const noexcept
Definition finfo.hpp:426
ExtendedEntryInfo * extendedInfo
Extended information, depending on the entry type.
Definition finfo.hpp:380
Permissions
Permission flags. Compatible with posix* definition.
Definition finfo.hpp:100
@ MASK
All valid permission bits. Equivalent to all | set_uid | set_gid | sticky_bit.
@ GROUP_READ
< S_IRWXU File owner has read, write, and execute/search permissions Equivalent to owner_read | owner...
@ OTHERS_ALL
S_IRWXO Other users have read, write, and execute/search permissions Equivalent to others_read | othe...
@ GROUP_EXEC
< S_IWGRP The file's user group has write permission
@ OTHERS_EXEC
S_IXOTH Other users have execute/search permission.
@ GROUP_WRITE
< S_IRGRP The file's user group has read permission
@ ALL
All users have read, write, and execute/search permissions Equivalent to owner_all | group_all | othe...
@ UNKNOWN
Unknown permissions (e.g., when not read, or filesystem does not support permissions)
@ OWNER_ALL
< S_IXUSR File owner has execute/search permission
@ STICKY_BIT
S_ISVTX Implementation-defined meaning, but POSIX XSI specifies that when set on a directory,...
@ SET_UID
S_ISUID Set user ID to file owner user ID on execution.
@ OWNER_EXEC
< S_IWUSR File owner has write permission
@ SET_GID
S_ISGID Set group ID to file's user group ID on execution.
@ NONE
no permission bits are set
@ OWNER_WRITE
< S_IRUSR File owner has read permission
@ OTHERS_READ
< S_IRWXG The file's user group has read, write, and execute/search permissions Equivalent to group_r...
@ OTHERS_WRITE
S_IWOTH Other users have write permission.
@ GROUP_ALL
< S_IXGRP The file's user group has execute/search permission
constexpr ExtendedEntryInfo * GetExtendedInfo() const
Definition finfo.hpp:430
void SetADate(DateTime v) noexcept
Sets the time of last access of this file/folder.
Definition finfo.hpp:528
void SetQuality(Qualities v) noexcept
Sets the quality of scan of the entry.
Definition finfo.hpp:510
constexpr bool IsCrossingFS() const noexcept
Definition finfo.hpp:408
Qualities
Per-entry information about how a node was scanned.
Definition finfo.hpp:134
@ RECURSIVE
Follow symlink target strings.
@ STATS
Only stats (size, date, owner, etc.) read.
@ NO_ACCESS_DIR
Scanner failure due to limited access rights.
@ MAX_DEPTH_REACHED
Scanner stopped, because maximum depth was reached.
@ NOT_CROSSING_FS
A directory that represented a mounted filesystem was not followed due to field.
@ RESOLVED
Read symlink target strings.
@ NO_ACCESS_SL
Scanner failure due to limited access rights.
@ UNKNOWN_ERROR
Unknown scanner failure.
@ BROKEN_LINK
A symbolic link targets a non-existent file or directory.
@ NO_ACCESS_SL_TARGET
Scanner failure due to limited access rights.
@ NOT_FOLLOWED
A symbolic link that targets a directory, but scan parameters specify not to follow.
@ NO_ACCESS
Scanner failure due to limited access rights.
@ NOT_EXISTENT
Set if a given start path does not exist.
void SetPerms(Permissions v) noexcept
Sets the permissions of the entry.
Definition finfo.hpp:506
void SetType(Types v) noexcept
Sets the type of the entry.
Definition finfo.hpp:508
constexpr DateTime CDate() const noexcept
Definition finfo.hpp:418
constexpr DateTime ADate() const noexcept
Definition finfo.hpp:420
constexpr Qualities Quality() const noexcept
Definition finfo.hpp:402
constexpr bool IsDirectory() const noexcept
Definition finfo.hpp:397
constexpr bool TargetIsCrossingFS() const noexcept
Definition finfo.hpp:410
void SetMDate(DateTime v) noexcept
Sets the last modification date of this file/folder.
Definition finfo.hpp:522
void * custom
Pool-allocated custom data.
Definition finfo.hpp:383
constexpr uint32_t Owner() const noexcept
Definition finfo.hpp:422
void SetSize(uinteger v) noexcept
Sets the file size.
Definition finfo.hpp:520
constexpr Types Type() const noexcept
Definition finfo.hpp:395
@ MARKER_TYPES_END
A marker for the last countable type. The rest are unused/errors.
@ DIRECTORY
Directory/folder.
@ CHARACTER
A character special file.
@ BLOCK
A block special file.
@ SOCKET
A socket file.
@ FIFO
A FIFO (also known as pipe) file.
void SetGroup(uint32_t v) noexcept
Sets the ID of the group of the file/folder.
Definition finfo.hpp:532
const std::type_info * dbgCustomType
The custom type attached. Used for asserting misuse in debug-compilations.
Definition finfo.hpp:386
uinteger size
The file size. In case of a directory, this is 0.
Definition finfo.hpp:348
lang::system::CPathString & GetRealLinkTarget() const noexcept
Definition finfo.hpp:496
constexpr bool IsArtificialFS() const noexcept
Definition finfo.hpp:404
void SetArtificialFS() noexcept
Mark the entry as residing on an artificial filesystem.
Definition finfo.hpp:512
constexpr DateTime BDate() const noexcept
Definition finfo.hpp:416
void SetCrossingFS() noexcept
Mark the entry as residing on a different filesystem than its parent.
Definition finfo.hpp:516
OwnerAndGroupResolver(PoolAllocator &poolAllocator)
Definition finfo.hpp:562
ALIB_API const NString & GetGroupName(const FInfo &fInfo)
Definition finfo.cpp:69
ALIB_API const NString & GetOwnerName(const FInfo &fInfo)
Definition finfo.cpp:52
void SetGroupCacheCapacity(integer numberOfLists, integer entriesPerList)
void SetOwnerCacheCapacity(integer numberOfLists, integer entriesPerList)
#define ALIB_ENUMS_MAKE_BITWISE(TEnum)
Definition bitwise.hpp:109
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:849
#define ALIB_ENUMS_ASSIGN_RECORD(TEnum, TRecord)
Definition records.hpp:712
#define ALIB_API
Definition alib.hpp:639
#define ALIB_ENUMS_MAKE_ITERABLE(TEnum, StopElement)
Definition iterable.hpp:100
#define ALIB_BOXING_VTABLE_DECLARE(TMapped, Identifier)
Definition vtable.inl:460
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:760
#define ALIB_RESOURCED_IN_MODULE(T, Camp, ResName)
Definition alib.cpp:69
files::File File
Type alias in namespace alib.
Definition ftree.hpp:1046
files::OwnerAndGroupResolver OwnerAndGroupResolver
Type alias in namespace alib.
Definition finfo.hpp:614
lang::uinteger uinteger
Type alias in namespace alib.
Definition integers.hpp:276
strings::TString< nchar > NString
Type alias in namespace alib.
files::FilesCamp FILES
The singleton instance of ALib Camp class FilesCamp.
Definition filescamp.cpp:23
files::FInfo FInfo
Type alias in namespace alib.
Definition finfo.hpp:611
files::FTree FTree
Type alias in namespace alib.
Definition ftree.hpp:1034
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273
A bitfield encoding various information.
Definition finfo.hpp:356
bool TargetIsArtificialFS
Denotes whether a link points into an artificial filesystem.
Definition finfo.hpp:359
Qualities Quality
The scan quality.
Definition finfo.hpp:363
Permissions Permission
The unix file-permissions.
Definition finfo.hpp:362
bool IsCrossingFS
Denotes whether the file is a mount point.
Definition finfo.hpp:360
Bits()
Default constructor. Sets all members to NONE, false or Error.
Definition finfo.hpp:366
bool TargetIsCrossingFS
Denotes whether a link points to a mount point.
Definition finfo.hpp:361
bool IsArtificialFS
Denotes whether the file resides in an artificial filesystem.
Definition finfo.hpp:358
Types Type
The file type.
Definition finfo.hpp:357
Recursively accumulated values for directories.
Definition finfo.hpp:196
uint32_t QtyErrsAccess
Number of access errors in the folder and subfolders.
Definition finfo.hpp:200
uint32_t QtyStopsOnCircularLinks
Number of recursion aborts due to detected circular links reach of maximum recursion depth.
Definition finfo.hpp:203
constexpr bool IsDirType(Types type) const noexcept
Definition finfo.hpp:246
ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE uinteger Size
The cumulated sizes of all files and directories.
Definition finfo.hpp:198
constexpr DirectorySums & Add(const FInfo &finfo) noexcept
Definition finfo.hpp:254
uint32_t Count() const noexcept
Definition finfo.hpp:264
constexpr DirectorySums() noexcept=default
Defaulted default constructor.
uint32_t Count(Types type) const noexcept
Definition finfo.hpp:275
uint32_t CountNonDirectories() const noexcept
Definition finfo.hpp:296
uint32_t TypeCounters[int(Types::MARKER_TYPES_END)]
Per-type counters.
Definition finfo.hpp:199
DirectorySums & operator-=(const DirectorySums &rhs)
Definition finfo.hpp:227
uint32_t QtyStopsOnMaxDepth
Number of recursion aborts due to reach of maximum recursion depth.
Definition finfo.hpp:202
uint32_t QtyErrsBrokenLink
Number of broken symbolic links in the directory and its subfolders.
Definition finfo.hpp:201
uint32_t CountDirectories() const noexcept
Definition finfo.hpp:286
DirectorySums Sums
The recursive sums evaluated during scan.
Definition finfo.hpp:314
DirectorySums Sums
The recursive sums evaluated during scan.
Definition finfo.hpp:332
lang::system::CPathString RealTarget
The resolved real target path.
Definition finfo.hpp:324
lang::system::CPathString Target
Definition finfo.hpp:322
Base type to create pointers to different extended entry information structs.
Definition finfo.hpp:308