ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ftree.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
17#if !defined ( _WIN32 )
18# include <pwd.h>
19# include <grp.h>
20#endif
21
22
23//============================================== Module ============================================
24#if ALIB_C20_MODULES
25 module ALib.Files;
26 import ALib.Lang;
27 import ALib.Characters.Functions;
28 import ALib.Strings;
29# if ALIB_ALOX
30 import ALib.ALox;
31 import ALib.ALox.Impl;
32# endif
33# if ALIB_EXPRESSIONS
34 import ALib.Expressions;
35# endif
36#if ALIB_DEBUG
37# include "ALib.Format.H"
38#endif
39#else
40# include "ALib.Lang.H"
42# include "ALib.Strings.H"
44# include "ALib.ALox.H"
45# include "ALib.ALox.Impl.H"
46# include "ALib.Expressions.H"
47#if ALIB_DEBUG
48# include "ALib.Format.H"
49#endif
50# include "ALib.Files.H"
51#endif
52//========================================== Implementation ========================================
53
54using namespace alib::system;
55namespace alib::files {
56
57//==================================================================================================
58//=== FTreeNodeHandler
59//==================================================================================================
60namespace detail {
61
64 const PathString& symLinkDest,
65 const PathString& symLinkRealPath ) {
67 || node->Type() == FInfo::Types::SYMBOLIC_LINK
68 || node->Type() == FInfo::Types::SYMBOLIC_LINK_DIR,
69 "FILES", "Given node is not a directory or symbolic link." )
70
72 == symLinkDest.IsEmpty(),
73 "FILES", "Error in symbolic link parameter" )
74
75
76 auto& v = *node;
77 ALIB_ASSERT_ERROR( v.GetExtendedInfo() == nullptr, "FILES", "Already set" )
78
79 auto pool= node.Tree<FTree>().Pool();
81 switch (v.Type()) {
83 {
84 v.SetExtendedInfo( pool.New<FInfo::EIDirectory>() );
85 }
86 return;
87
89 {
90 v.SetExtendedInfo( pool.New<FInfo::EISymLinkFile>() );
91 v.SetLinkTarget( node.Tree<FTree>(), symLinkDest, symLinkRealPath);
92 }
93 return;
94
96 {
97 v.SetExtendedInfo( pool.New<FInfo::EISymLinkDir>() );
98 v.SetLinkTarget( node.Tree<FTree>(), symLinkDest, symLinkRealPath);
99 }
100 return;
101
102 default:
103 return;
104 }
106}
107# include "ALib.Lang.CIMethods.H"
108
109} // namespace alib::files[::detail]
110
111//==================================================================================================
112//=== FTree
113//==================================================================================================
115: StringTree( allocator, DIRECTORY_SEPARATOR )
116, Pool ( allocator )
117, ogResolver( Pool )
118, listeners ( allocator ) {
120 numberFormat.FractionalPartWidth= 1;
121
122 DbgSetDCSName("FTree");
123
124 ALIB_DBG( if( alib::FILES.IsBootstrapped())
125 {
126 Log_SetDomain( "ALIB/FILES", Scope::Path)
127 Log_SetDomain( "FTREE" , Scope::Filename)
128 } )
129}
130
132 #if ALIB_DEBUG
133 for( auto& node : nodeTable )
134 if( node.data.custom ) {
135 Path path;
136 createCursor(node).AssemblePath(path);
137 ALIB_ERROR( "FILES",
138 "CustomData not deleted before destruction of class FTree.\n"
139 " First node found: {}.\n"
140 " Attached data type: {}" , path, node.data.dbgCustomType )
141 }
142 #endif
143
144 // we have to delete all nodes before the invocation of the base destructor, because
145 // this would use our pool allocator on existing nodes (which is then destructed already).
146 Clear();
147
148 // delete root value
149 auto* extendedInfo= Root()->GetExtendedInfo();
150 if( extendedInfo )
151 Pool().Delete( reinterpret_cast<FInfo::EIDirectory*>(extendedInfo) );
152
154}
155
157 lang::ContainerOp insertOrRemove,
159 const File* file,
160 const StringTree::Cursor* subTree,
161 const PathString& fileName,
162 const PathString& pathPrefix,
163 const PathString& pathSubstring ) {
164 // checks
165 ALIB_ASSERT_ERROR( file ==nullptr || &file->AsCursor().Tree() == this,"FILES","Given file does not belong to this FTree." )
166 ALIB_ASSERT_ERROR( subTree ==nullptr || subTree->IsValid() ,"FILES","Invalid cursor given." )
167 ALIB_ASSERT_ERROR( subTree ==nullptr || &subTree ->Tree() == this,"FILES","Given cursor does not belong to this FTree." )
168
169 //------------------------------------------ registration ----------------------------------------
170 if( insertOrRemove == lang::ContainerOp::Insert) {
171 listeners.emplace_back( ListenerRecord{ listener,
172 event,
173 (file ? file->AsCursor().Export() : ConstCursorHandle()),
174 (subTree ? subTree-> Export() : ConstCursorHandle()),
177 PathStringPA(Pool) } );
178 listeners.back().fileName << fileName;
179 listeners.back().pathPrefix << pathPrefix;
180 listeners.back().pathSubstring<< pathSubstring;
181
182 return;
183 }
184
185 //----------------------------------------------- de ---------------------------------------------
186 for (auto it= listeners.begin() ; it != listeners.end() ; ++it )
187 if( it->listener == listener
188 && it->event == event
189 && it->file == ( file ? file->AsCursor().Export() : ConstCursorHandle() )
190 && it->subTree == ( subTree ? subTree ->Export() : ConstCursorHandle() )
191 && it->fileName .Equals( fileName )
192 && it->pathPrefix .Equals( pathPrefix )
193 && it->pathSubstring.Equals( pathSubstring ) )
194 {
195 (void) listeners.erase( it );
196 return;
197 }
198
199 ALIB_WARNING("FILES", "Listener with matching set of parameters not found with deregistration.")
200
201} // FTree::registerListener
202
203
205 // checks
206 ALIB_ASSERT_ERROR( listener!=nullptr, "FILES", "Given listener is nullptr." )
207
208 //----------------------------------------------- de ---------------------------------------------
209 int cnt= 0;
210 for (auto it= listeners.begin() ; it != listeners.end() ; )
211 if( it->listener == listener ) {
212 Log_Verbose("Removing listener")
213 it= listeners.erase( it );
214 ++cnt;
215 }
216 else
217 ++it;
218
219 Log_If(cnt==0, Verbosity::Warning, "No listener found to be removed." )
220
221 return cnt;
222} // FTree::registerListener
223
225 File& file
226 IF_ALIB_THREADS( , SharedLock* lock) ,
227 const PathString& filePathGiven ) {
228 Path filePathBuffer;
229 const PathString* filePath= &filePathGiven;
230 for (auto it= listeners.begin() ; it != listeners.end() ; ++it )
231 if( event == it->event ) {
232 // if needed generate file path
233 if( filePath->IsEmpty()
234 && ( it->fileName .IsNotEmpty()
235 || it->pathPrefix .IsNotEmpty()
236 || it->pathSubstring.IsNotEmpty() ) )
237 {
239 (file.AsCursor().IsRoot() ? file.AsCursor()
240 : file.AsCursor().Parent() )
241 .AssemblePath(filePathBuffer);
243 filePath= &filePathBuffer;
244 }
245
246 if( ( it->file .IsValid() && ( it->file == file.AsCursor().Export() ) )
247 || ( it->subTree .IsValid() && ( file.AsCursor().Distance( ImportCursor(it->subTree) ) >= 0 ) )
248 || ( it->fileName .IsNotEmpty() && it->fileName.Equals(file.AsCursor().Name()) )
249 || ( it->pathPrefix .IsNotEmpty() && filePath->StartsWith(it->pathPrefix) )
250 || ( it->pathSubstring.IsNotEmpty() && filePath->IndexOf(it->pathSubstring) >= 0 )
251 )
252 {
253 Log_Verbose("Notifying listener. Event=", event == FTreeListener::Event::CreateNode
254 ? "CreateNode" : "DeleteNode" )
255 it->listener->Notify( file, event );
256 } }
257} // FTree::notifyListeners
258
259
260# include "ALib.Lang.CIFunctions.H"
261void FTree::FixSums( Cursor directory) {
262 ALIB_ASSERT_ERROR( directory->Type() == FInfo::Types::DIRECTORY,
263 "FILES", "Given node is not a directory." )
264
265 FInfo::DirectorySums& sums= directory->Sums();
266 sums= FInfo::DirectorySums();
267 directory.GoToFirstChild();
268 while( directory.IsValid()) {
269 FInfo& v= *directory;
270 sums.TypeCounters[size_t(v.Type())]++;
271 if( v.IsDirectory() )
272 sums+= v.Sums();
273
274 directory.GoToNextSibling();
275} }
276
277//==================================================================================================
278//=== Debug Dump
279//==================================================================================================
280
281#if ALIB_DEBUG && !DOXYGEN
282
284 A_CHAR("{:ta h{2,r} on{10,r} gn{10,r} s(IEC){10,r} dm qqq FxFa (rd{3r}' D' rf{3r}' F' re{2r}' EA' rb{2r}'BL) 'nf l}\n");
285
286AString& DbgDump( AString& target,
287 FTree& tree,
288 EnumBitSet<FInfo::Types> includedTypes,
289 FTree::Cursor startNode ,
290 unsigned depth ) {
291 if( startNode.IsInvalid() )
292 startNode= tree.Root().AsCursor();
293
296 fmt.Reset();
299
300 // loop over all nodes and dump
301 fmt.Format( target, DBG_DUMP_FORMAT, File(startNode) );
302
303 stit.SetMaxDepth( depth );
304 stit.Initialize ( startNode, lang::Inclusion::Exclude );
305 while( stit.IsValid()) {
306 if( includedTypes.Test(stit.Node()->Type()))
307 fmt.Format( target, DBG_DUMP_FORMAT, File(stit.Node()) );
308 stit.Next();
309 }
310
311 return target;
312}
313
314#endif // ALIB_DEBUG && !DOXYGEN (dump methods)
315# include "ALib.Lang.CIMethods.H"
316
317} // namespace alib::files
void SetMaxDepth(unsigned int newMaxDepth=(std::numeric_limits< unsigned >::max)())
void Initialize(CursorType startNode, lang::Inclusion includeStartNode)
void SetPathGeneration(lang::Switch pathGeneration)
StringTree(AllocatorType &allocator, CharacterType pathSeparator)
The entry type which is embedded in each tree node.
Definition finfo.inl:15
constexpr Types Type() const noexcept
Definition finfo.inl:370
constexpr ExtendedEntryInfo * GetExtendedInfo() const
Definition finfo.inl:405
constexpr bool IsDirectory() const noexcept
Definition finfo.inl:372
@ DIRECTORY
Directory/folder.
Definition finfo.inl:26
constexpr DirectorySums & Sums() const
Definition finfo.inl:417
ALIB_DLL FTree(MonoAllocator &allocator)
Definition ftree.cpp:114
PoolAllocator Pool
Definition ftree.inl:162
ALIB_DLL void notifyListeners(FTreeListener::Event event, File &file, SharedLock *lock, const system::PathString &filePath)
Definition ftree.cpp:224
NumberFormat numberFormat
Definition ftree.inl:173
OwnerAndGroupResolver ogResolver
Definition ftree.inl:177
ALIB_DLL int MonitorStop(FTreeListener *listener)
Definition ftree.cpp:204
friend class files::File
Friendship declaration.
Definition ftree.inl:169
ALIB_DLL void registerListener(FTreeListener *listener, lang::ContainerOp insertOrRemove, FTreeListener::Event event, const File *file, const StringTree::Cursor *subTree, const system::PathString &fileName, const system::PathString &pathPrefix, const system::PathString &pathSubstring)
Definition ftree.cpp:156
ListMA< ListenerRecord > listeners
The list of registered listeners.
Definition ftree.inl:194
ALIB_DLL ~FTree()
Destructor.
Definition ftree.cpp:131
static ALIB_DLL void FixSums(Cursor directory)
Definition ftree.cpp:261
Cursor & AsCursor()
Definition ftree.inl:711
static ALIB_DLL threads::RecursiveLock DefaultLock
Formatter & Format(AString &target, TArgs &&... args)
static ALIB_DLL SPFormatter Default
virtual BoxesMA & Reset()
constexpr bool Test(TInterface bit) noexcept
Definition bitset.inl:316
constexpr bool IsEmpty() const
Definition string.inl:365
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.inl:815
bool StartsWith(const TString &needle) const
Definition string.inl:751
ALIB_DLL void ReleaseShared(ALIB_DBG_TAKE_CI)
Definition locks.cpp:381
ALIB_DLL void AcquireShared(ALIB_DBG_TAKE_CI)
Definition locks.cpp:340
#define IF_ALIB_THREADS(...)
Definition alib.inl:401
#define A_CHAR(STR)
#define ALIB_WARNINGS_RESTORE
Definition alib.inl:719
#define ALIB_WARNING(domain,...)
Definition alib.inl:1063
#define Log_If(...)
#define ALIB_ERROR(domain,...)
Definition alib.inl:1062
#define Log_Verbose(...)
#define ALIB_WARNINGS_ALLOW_SPARSE_ENUM_SWITCH
Definition alib.inl:647
#define ALIB_LOCK_RECURSIVE_WITH(lock)
Definition alib.inl:1340
#define Log_SetDomain(...)
#define ALIB_DBG(...)
Definition alib.inl:853
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066
#define ALIB_CALLER_PRUNED
Definition alib.inl:1024
This namespace implements internals of namespace alib::files.
Definition ftree.cpp:60
String DBG_DUMP_FORMAT
ALIB_DLL AString & DbgDump(AString &target, FTree &tree, EnumBitSet< FInfo::Types > includedTypes=EnumBitSet< FInfo::Types >(true), FTree::Cursor startNode=FTree::Cursor(), unsigned depth=(std::numeric_limits< unsigned int >::max)())
ContainerOp
Denotes standard container operations.
@ Insert
Denotes insertions.
@ Off
Switch it off, switched off, etc.
@ Exclude
Chooses exclusion.
strings::TString< PathCharType > PathString
The string-type used with this ALib Module.
Definition path.inl:33
strings::TAString< PathCharType, PoolAllocator > PathStringPA
A pool-allocated string representing a path.
Definition path.inl:46
constexpr PathCharType DIRECTORY_SEPARATOR
The standard path separator character. Defaults to '\' on Windows OS, '/' else.
Definition path.inl:63
threads::SharedLock SharedLock
Type alias in namespace alib.
files::File File
Type alias in namespace alib.
Definition ftree.inl:999
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
lang::TBitSet< TEnum, enumops::IterableTraits< TEnum >::End, enumops::IterableTraits< TEnum >::Begin > EnumBitSet
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
system::Path Path
Type alias in namespace alib.
Definition path.inl:376
format::Formatter Formatter
Type alias in namespace alib.
files::FilesCamp FILES
The singleton instance of ALib Camp class FilesCamp.
Definition filescamp.cpp:47
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
containers::StringTreeIterator< TTree > StringTreeIterator
Type alias in namespace alib.
HashTable< TAllocator, typename NodeKey::ValueDescriptor, typename NodeKey::Hash, typename NodeKey::EqualTo, lang::Caching::Enabled, TRecycling > nodeTable
Recursively accumulated values for directories.
Definition finfo.inl:185
std::array< uint32_t, size_t(Types::MARKER_TYPES_END)> TypeCounters
Per-type counters.
Definition finfo.inl:187
Event
The type of change that imposes the notification of a listener.
Definition ftree.inl:96
@ CreateNode
A file or directory entry was created.
Definition ftree.inl:97
Record used to manage registered listeners.
Definition ftree.inl:181
static ALIB_DLL void AllocateExtendedInfo(StringTree< MonoAllocator, FInfo, detail::FTreeNodeHandler >::Cursor &node, const system::PathString &symLinkDest, const system::PathString &symLinkRealPath)
Definition ftree.cpp:63