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