-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSController.hpp
1412 lines (1276 loc) · 60.8 KB
/
FSController.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
/**
* ============================================================================
* FSController Class - A C++ File System Utility Module
* ============================================================================
*
* DISCLAIMER OF WARRANTY:
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the author(s) be held liable for any damages arising from
* the use of this software, including but not limited to:
*
* - Loss of data
* - Any other type of loss or damage
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would
* be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* LIMITATION OF LIABILITY:
*
* In no event will the author(s) be liable for any damages, including but
* not limited to incidental, consequential, or punitive damages, arising
* out of the use of this software.
*
* COPYING AND DISTRIBUTION:
*
* This software may be copied and distributed free of charge, provided
* that the above copyright notice, disclaimer, and limitations of liability
* are included in all copies.
*
* AUTHORIZATION:
*
* By using this software, you acknowledge that you have read and understood
* the terms and conditions of this license, and agree to be bound by them.
*
* TRADEMARKS:
*
* The names of actual companies and products mentioned in this software
* may be the trademarks of their respective owners.
*
* ACKNOWLEDGMENT:
*
* The authors would like to acknowledge the contributions of the C++
* community, and the many individuals who have helped shape the C++
* standard library.
*
* ============================================================================
*
* Written by [Somorpher], [2024].
*/
/**
*
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
*
*
* This C++ code implements a comprehensive file system utility module that provides a wide
* range of operations for managing files and directories. At the heart of the code is the
* FSController class, a template-based class that allows for flexibility in choosing the
* type of foreign key used to identify file profiler structures.
* The FSController class provides a wide range of functions for interacting with the file system,
* including reading and writing files using memory mapping. The FileRead function uses the mmap
* system call to map a file into memory, allowing for efficient and fast access to file contents.
* The FileWrite function, on the other hand, uses the mmap system call to map a file into memory,
* and then uses the memcpy function to copy data from a buffer into the mapped memory region.
* In addition to reading and writing files, the FSController class also provides functions for
* creating and deleting files, checking if a file exists, is a text file, executable, or a symlink,
* and creating and deleting directories. These functions are implemented using the std::filesystem
* library, which provides a modern and efficient way to interact with the file system.
* The FSController class also provides functions for recursively aggregating directory entries,
* scanning directories for specific file types, and creating backups of directories. These
* functions are implemented using the std::filesystem::directory_iterator class, which provides a
* way to iterate over the entries in a directory. Another key feature of the FSController class is
* its ability to create, register, and manage file profiler structures. A file profiler structure
* is a data structure that contains information about a file, such as its name, size, and contents.
* The FSController class provides functions for creating and registering these structures, as well
* as for performing garbage collection to ensure that unused profiler structures are properly
* cleaned up. The FSController class is designed to be thread-safe, with a mutex used to protect
* access to shared resources. This ensures that the class can be safely used in multi-threaded
* environments without fear of data corruption or other concurrency-related issues. One notable
* feature of the FSController class is its use of attribute macros to specify compiler
* optimizations and other attributes for its member functions. For example, the __0x_attr_gri macro
* is used to specify that a function should be optimized for performance, while the
* __0x_attr_psrsgp macro is used to specify that a function should be optimized for size. The
* FSController class also provides a number of convenience functions for working with files and
* directories. For example, the CreateFile function creates a new file with the specified name and
* contents, while the DeleteFile function deletes a file with the specified name. The
* CreateDirectory function creates a new directory with the specified name, while the
* DeleteDirectory function deletes a directory with the specified name.
*
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
*
*/
/* Headers/Libs *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <algorithm>
#include <array>
#include <deque>
#include <fcntl.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <mutex>
#include <random>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/mman.h>
#include <sys/stat.h>
#include <thread>
#include <type_traits>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <functional>
#include <vector>
/**
* Namespace Pollution Guard, naming-collision prevention
*
*/
#ifndef __MODULE_FS_CONTROLLER__
#define __MODULE_FS_CONTROLLER__
/* Namespace *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
namespace FSControllerModule
{
#define IS_DEBUGGING_CODE 1 // remove this definition for production
/**
* Os/Architecture Detection
*
*/
#if defined(__linux__) || defined(__APPLE__)
/* Global Macro *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#ifdef __GNUC__
#define __SUPPORTED_COMPILER_1__ __GNUC__
#endif
#ifdef __clang__
#define __SUPPORTED_COMPILER_2__ __clang__
#endif
#ifdef IS_DEBUGGING_CODE
#define ATTR_OPTIMIZE_LEVEL "0"
#else
#define ATTR_OPTIMIZE_LEVEL "3"
#endif
#define FS_MAX_FILE_NAME_LENGTH (std::uint16_t)200 /* the max length for individual absolute address */
#define FS_MAX_COLLECTION_STACK_SIZE (std::uint32_t)100000 /* max value for instance aggregation size */
/* FKType is the foreign key type name to use for entity associations */
#define __tm_file_aggregation template <typename _FKType, typename = std::enable_if<!std::is_array_v<_FKType> && !std::is_pointer_v<_FKType>>>
/**
* Compiler Detection, only implement attributes with supported compiler installed
*/
#if defined(__SUPPORTED_COMPILER_1__) || defined(__SUPPORTED_COMPILER_2__)
#define __0x_attr_gri __attribute__((hot, no_icf, warn_unused_result, nothrow, pure, zero_call_used_regs("all"), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_psrsgp __attribute__((no_icf, warn_unused_result, nothrow, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_psrscp __attribute__((no_icf, nothrow, always_inline, access(read_only, 1), zero_call_used_regs("all"), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cc __attribute__((no_icf, nothrow, always_inline, cold, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_mc __attribute__((no_icf, nothrow, always_inline, cold, access(read_write, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_co __attribute__((no_icf, nothrow, always_inline, const, cold, warn_unused_result, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_fe __attribute__((no_icf, nothrow, always_inline, flatten, pure, hot, warn_unused_result, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_itf __attribute__((no_icf, nothrow, always_inline, flatten, pure, warn_unused_result, no_stack_protector, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_iex __attribute__((no_icf, nothrow, always_inline, flatten, pure, warn_unused_result, no_stack_protector, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_isl __attribute__((no_icf, nothrow, always_inline, flatten, pure, warn_unused_result, no_stack_protector, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_id __attribute__((no_icf, nothrow, always_inline, flatten, pure, warn_unused_result, no_stack_protector, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_fr __attribute__((no_icf, warn_unused_result, hot, stack_protect, zero_call_used_regs("used"), access(read_only, 1), access(read_only, 2), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_fw __attribute__((no_icf, stack_protect, hot, zero_call_used_regs("used"), access(read_only, 1), access(read_only, 2), access(read_only, 3), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_rnp __attribute__((no_icf, nothrow, stack_protect, hot, zero_call_used_regs("used"), access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_gss \
__attribute__((no_icf, nothrow, noipa, no_stack_protector, pure, flatten, warn_unused_result, no_sanitize_address, no_sanitize_coverage, no_sanitize_undefined, optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_gci \
__attribute__((no_icf, nothrow, noipa, no_stack_protector, pure, flatten, warn_unused_result, no_sanitize_address, no_sanitize_coverage, no_sanitize_undefined, optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_gsp __attribute__((no_icf, nothrow, stack_protect, warn_unused_result, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_ifc __attribute__((no_icf, nothrow, stack_protect, warn_unused_result, cold, flatten, access(read_write, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cpm \
__attribute__((no_icf, nothrow, stack_protect, warn_unused_result, hot, flatten, pure, access(read_only, 1), access(read_only, 2), access(read_only, 3), zero_call_used_regs("used"), \
optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_ofd __attribute__((no_icf, stack_protect, warn_unused_result, hot, flatten, pure, access(read_only, 1), access(read_only, 2), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cfs __attribute__((no_icf, warn_unused_result, flatten, pure, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_vms __attribute__((no_icf, flatten, stack_protect, zero_call_used_regs("used"), access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_amb __attribute__((no_icf, nothrow, flatten, stack_protect, tainted_args, zero_call_used_regs("used"), access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_dmc __attribute__((no_icf, flatten, stack_protect, zero_call_used_regs("all"), access(read_write, 1), access(read_write, 2), access(read_only, 3), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_drz __attribute__((no_icf, flatten, stack_protect, always_inline, access(read_only, 1), access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cps __attribute__((no_icf, flatten, nothrow, pure, warn_unused_result, no_stack_protector, noipa, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cmb __attribute__((no_icf, flatten, nothrow, stack_protect, zero_call_used_regs("used"), access(write_only, 1), access(read_only, 2), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_gsptr __attribute__((no_icf, cold, nothrow, always_inline, warn_unused_result, optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_dprf __attribute__((no_icf, nothrow, always_inline, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cfl __attribute__((no_icf, always_inline, access(read_only, 1), access(read_only, 2), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cdir __attribute__((no_icf, always_inline, nothrow, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cdire __attribute__((no_icf, warn_unused_result, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_dirprf __attribute__((no_icf, cold, warn_unused_result, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_delfl __attribute__((no_icf, always_inline, no_stack_protector, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_cdbk __attribute__((no_icf, cold, warn_unused_result, stack_protect, zero_call_used_regs("used"), access(read_only, 1), access(read_only, 2), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_dcft __attribute__((cold, warn_unused_result, access(read_only, 1), access(read_only, 2), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_dirwp __attribute__((cold, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_fssh __attribute__((no_icf, always_inline, access(read_only, 1), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_recaggr __attribute__((hot, nothrow, access(read_only, 1), stack_protect, zero_call_used_regs("used"), optimize(ATTR_OPTIMIZE_LEVEL)))
#define __0x_attr_FSC_dirbkv __attribute__((no_icf, warn_unused_result, cold, access(read_only, 1), access(read_only, 2), optimize(ATTR_OPTIMIZE_LEVEL)))
#else
#define __0x_attr_gri [[nodiscard, nothrow]]
#define __0x_attr_psrsgp [[nodiscard, nothrow]]
#define __0x_attr_psrscp [[nothrow]]
#define __0x_attr_FSC_cc [[nothrow, nodiscard]]
#define __0x_attr_FSC_mc [[nothrow, nodiscard]]
#define __0x_attr_FSC_co [[nothrow, nodiscard]]
#define __0x_attr_FSC_fe [[nothrow, nodiscard]]
#define __0x_attr_FSC_itf [[nothrow, nodiscard]]
#define __0x_attr_FSC_iex [[nothrow, nodiscard]]
#define __0x_attr_FSC_isl [[nothrow, nodiscard]]
#define __0x_attr_FSC_id [[nothrow, nodiscard]]
#define __0x_attr_FSC_fr [[nodiscard]]
#define __0x_attr_FSC_fw [[nodiscard]]
#define __0x_attr_FSC_rnp [[nothrow]]
#define __0x_attr_FSC_gss [[nothrow, nodiscard]]
#define __0x_attr_FSC_gci [[nothrow, nodiscard]]
#define __0x_attr_FSC_gsp [[nothrow, nodiscard]]
#define __0x_attr_FSC_ifc [[nothrow, nodiscard]]
#define __0x_attr_FSC_cpm [[nothrow, nodiscard]]
#define __0x_attr_FSC_ofd [[nodiscard]]
#define __0x_attr_FSC_ofs [[nodiscard]]
#define __0x_attr_FSC_vms [[nothrow]]
#define __0x_attr_FSC_amb [[nothrow]]
#define __0x_attr_FSC_dmc [[nothrow]] // ?
#define __0x_attr_FSC_drz [[nothrow]] // ?
#define __0x_attr_FSC_cps [[nothrow, nodiscard]]
#define __0x_attr_FSC_cmb [[nothrow]]
#define __0x_attr_FSC_gsp [[nothrow]]
#define __0x_attr_FSC_dprf [[nothrow]]
#define __0x_attr_FSC_cfl [[]]
#define __0x_attr_FSC_cdir [[nothrow]]
#define __0x_attr_FSC_cdire [[]]
#define __0x_attr_FSC_dirprf [[]]
#define __0x_attr_FSC_delfl [[]]
#define __0x_attr_FSC_cdbk [[]]
#define __0x_attr_FSC_dcft [[]]
#define __0x_attr_FSC_dirwp [[]]
#define __0x_attr_FSC_fssh [[]]
#define __0x_attr_FSC_recaggr [[]]
#define __0x_attr_FSC_dirbkv [[]]
#endif
/**
* Debugging configuration mode, remove this macro for production use!
*/
#ifdef IS_DEBUGGING_CODE
#if !defined(__with_optimize_perform__)
#define __with_optimize_perform__ optimize("0") // 0 optimization for compiler
#endif
#else
#if !defined(__with_optimize_perform__)
#define __with_optimize_perform__ optimize("3") // 3 max optimization
#endif
#endif
/**
* Random Number Generator Static Function(Namespace Encapsulated)
*/
__0x_attr_gri static const std::uint64_t GenerateRandomId(void) noexcept
{
std::random_device _d;
std::mt19937 _eng(_d());
std::uniform_int_distribution<uint64_t> _g(9999999999999, 9999999999999999);
return _g(_eng);
};
/* Type Alias *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
using String_t = std::basic_string<char>;
using StringView_t = std::basic_string_view<char>;
typedef char *fMap_t;
typedef String_t _FKType;
typedef _FKType FKt;
/* Enumeration *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
enum class eFileDescriptorMode : uint8_t
{
READ = 0,
WRITE,
READ_WRITE
};
/* Structure *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma pack(1)
struct alignas(void *) stFileDescriptor
{
String_t file_content{}; /* contains file content */
String_t file_name{}; /* full path(absolute path) to file*/
size_t file_size{}; /* file size in bytes */
/* Clean struct file associated information */
inline void Clean() noexcept
{
if (!file_content.empty())
file_content.clear();
if (!file_name.empty())
file_name.clear();
};
~stFileDescriptor() noexcept
{
Clean();
};
};
/**
*
* file profile register structure, register file profiler structure block within "stack",
* and profile stack register size within "reg_stack_size" member variables
*/
struct alignas(void *) stProfilerStackRegister
{
std::unordered_map<_FKType, struct stFileDescriptor> stack_register{}; /* stack register, allocates file name(abs. address) as key and new file
descriptor instance. */
size_t reg_stack_size{}; /* address register stack size */
bool gc_executed{false};
/**
*
* Helper Utility Function, locate and get fs description at index _i.
* @param _FKType the index to search for
* @returns struct stFileDescriptor, the descriptor at
*/
__0x_attr_psrsgp inline const struct stFileDescriptor getProfile(const _FKType _profile_id) noexcept
{
if (reg_stack_size > 0) [[likely]]
{
if (stack_register.find(_profile_id) != stack_register.end())
{
return stack_register[_profile_id];
}
}
return {};
};
__0x_attr_psrscp inline void createProfile(const struct stFileDescriptor &_new_profile) noexcept
{
if (++reg_stack_size < FS_MAX_COLLECTION_STACK_SIZE - 1) [[likely]]
{
if (stack_register.find(_new_profile.file_name) == stack_register.end())
{
stack_register.insert({_new_profile.file_name, std::move(_new_profile)});
}
return;
}
reg_stack_size--;
};
inline void eraseProfile(const _FKType _fk)
{
if (gc_executed)
return;
if (reg_stack_size > 0 && reg_stack_size-- > 0) [[likely]]
{
auto rsi = stack_register.find(_fk);
if (rsi != stack_register.end()) [[likely]]
{
stack_register.erase(rsi);
}
}
};
inline void GarbageCollect(void)
{
if (!gc_executed)
{
gc_executed = true;
if (reg_stack_size > 0) [[likely]]
{
for (auto &[fKey, profile_description] : stack_register)
{
eraseProfile(profile_description.file_name);
}
}
}
};
stProfilerStackRegister() = default;
stProfilerStackRegister(const stProfilerStackRegister &o) = default;
stProfilerStackRegister(stProfilerStackRegister &&o) = default;
stProfilerStackRegister &operator=(const stProfilerStackRegister &o) = default;
stProfilerStackRegister &operator=(stProfilerStackRegister &&o) = default;
~stProfilerStackRegister(void) noexcept
{
GarbageCollect();
};
};
#pragma pack()
typedef struct alignas(void *)
{
std::vector<String_t> registry{};
String_t largest_file_path{};
std::size_t registry_size{0};
std::size_t max_size{0};
std::size_t min_size{0};
} stDirectoryCollectionStat;
typedef struct alignas(void *)
{
String_t path{};
String_t file{};
bool has_found{false};
} stDirectoryLookup;
/* Template Specialization *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
template <typename _DirLookupType>
struct is_dir_lookup_return_type : std::disjunction<std::is_same<_DirLookupType, stDirectoryLookup>, std::is_same<_DirLookupType, std::vector<stDirectoryLookup>>>
{
};
template <typename _DirLookupType>
inline constexpr bool is_dir_lookup_return_type_v = is_dir_lookup_return_type<_DirLookupType>::value;
/* Class *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/**
* @class FSController
* foreign key type referes to the FK used to allocate/register file descriptor/profilers. will be used to get/set new entries.
*/
template <typename _ForeignKeyType_ = String_t>
class FSController
{
using directoryScanResult_t = std::unordered_map<_ForeignKeyType_, struct stFileDescriptor>;
private:
bool sync_backup_exec_state;
struct stProfilerStackRegister _profile_stack_reg; /* file profile stack register */
std::uint64_t _fs_instance_uid = GenerateRandomId(); /* fs instance unique id, for copy/move semantics, avoid copy */
bool _fs_new_instance = false; /* boolean flag indicating if instance is new or used,
double-free error prevention */
std::mutex _mtx_guard;
public:
/* FS Controller default Constructor */
explicit FSController() noexcept
{
if (!this->_fs_new_instance)
this->_fs_new_instance = true;
};
/* FS Controller Copy Constructor */
__0x_attr_FSC_cc FSController(const FSController &_o) noexcept : _profile_stack_reg(_o._profile_stack_reg), _fs_instance_uid(_o._fs_instance_uid), _fs_new_instance(_o._fs_instance_uid) {};
/* FS Controller Copy Operator Overload */
__0x_attr_FSC_cc FSController &operator=(const FSController &_o) noexcept
{
return *this->__initFsController<const FSController &>(_o);
};
/* FS Controller Move Constructor */
__0x_attr_FSC_mc FSController(FSController &&_o) noexcept
: _profile_stack_reg(std::move(_o._profile_stack_reg)), _fs_instance_uid(std::move(_o._fs_instance_uid)), _fs_new_instance(std::move(_o._fs_instance_uid)) {};
/* FS Controller Move Operator Overload */
__0x_attr_FSC_mc FSController &operator=(FSController &&_o) noexcept
{
return *this->__initFsController<FSController &&>(std::move(_o));
};
/* FS Controller Comparision Operator Overload */
__0x_attr_FSC_co constexpr bool operator==(const FSController &_o) noexcept
{
return this->_fs_instance_uid == _o._fs_instance_uid;
};
__0x_attr_FSC_co constexpr bool operator!=(const FSController &_o) noexcept
{
return !(this->_fs_instance_uid == _o._fs_instance_uid);
};
/**
*
* check if file_name exists or not.
* @param StringView_t absolute path to target file
* @returns bool true if file_name is found, false otherwise
*
*/
__0x_attr_FSC_fe inline static const bool FileExists(const StringView_t &file_name) noexcept
{
if (file_name.empty() || file_name.size() > FS_MAX_FILE_NAME_LENGTH || file_name.find(" ") != std::string::npos) [[unlikely]]
return false;
return std::filesystem::exists(file_name.data());
};
/**
*
* check if every file within file_list exists or not, will return failure if even 1
* is missing.
* @param std::unordered_set<String_t> list with target files
* @returns bool true if every target within file_list is found, false otherwise
*
*/
__0x_attr_FSC_fe inline static const bool FileExists(const std::unordered_set<String_t> &file_list) noexcept
{
if (file_list.empty() && file_list.size() > 500000) [[unlikely]]
return false;
for (const String_t &file_name : file_list)
if (!FileExists(file_name))
return false;
return true;
};
/**
*
* check if file_name is a text file
* @param StringView_t absolute path to target file
* @returns bool true if file_name is a text file, false otherwise
*
*/
__0x_attr_FSC_itf inline static const bool IsTextFile(const StringView_t &file_name) noexcept
{
if (file_name.empty() || !FileExists(file_name))
return false;
return std::filesystem::is_regular_file(file_name.data());
};
/**
*
* check if file_name is a executable
* @param StringView_t absolute path to target file
* @returns bool true if file_name is executable, false otherwise
*
*/
__0x_attr_FSC_iex inline static const bool IsExecutable(const StringView_t &file_name) noexcept
{
if (IsTextFile(file_name.data()))
{
std::filesystem::perms f_perms = std::filesystem::status(file_name.data()).permissions();
if (((f_perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none) || ((f_perms & std::filesystem::perms::group_exec) != std::filesystem::perms::none) ||
((f_perms & std::filesystem::perms::others_exec) != std::filesystem::perms::none))
return true;
}
return false;
};
/**
*
* check if file_name is a symlink
* @param StringView_t absolute path to target symlink
* @returns bool true if file_name is symlink, false otherwise
*
*/
__0x_attr_FSC_isl inline static const bool IsSymlink(const StringView_t &file_name) noexcept
{
return std::filesystem::is_symlink(file_name.data());
};
/**
*
* check if file_name is a directory
* @param StringView_t absolute path to target file
* @returns bool true if file_name is directory, false otherwise
*
*/
__0x_attr_FSC_id inline static const bool IsDirectory(const StringView_t &file_name) noexcept
{
return std::filesystem::is_directory(file_name.data());
};
/**
*
* Read _file_name content and return full profiler structure associated with
* _file_name.
* @param StringView_t the absolute file path to read
* @param bool a const boolean flag dictating if _file_name should be created on
* missing or not
* @returns stFileDescriptor a const read-only access to _file_name associated profile
* structure
*
*/
__0x_attr_FSC_fr const struct stFileDescriptor FileRead(const StringView_t &_file_name, const bool _create_new = false)
{
this->__fileStreamStatusHandle(_file_name, _create_new);
struct stFileDescriptor new_profiler(this->__createEmptyProfilerStructure(_file_name));
int fileDescriptor(this->__openFileDescriptor(_file_name, eFileDescriptorMode::READ));
struct stat file_stat_description(this->__createFileStat(fileDescriptor));
if (file_stat_description.st_size > 0)
{
new_profiler.file_size = file_stat_description.st_size;
fMap_t mapped_data_pointer(this->__createPointerMap(fileDescriptor, file_stat_description.st_size, true, 0));
this->__verifyMemMapState(mapped_data_pointer);
this->__allocMappedBytes(new_profiler.file_content, &mapped_data_pointer, file_stat_description.st_size);
this->__descriptorMapClose(fileDescriptor, &mapped_data_pointer, file_stat_description.st_size);
}
return new_profiler;
};
/**
*
* put _buffer into file _fle_name, create new _file_name if _create_new is true and
* _file_name is not found.
* @param StringView_t read only argument value representing absolute path to
* _file_name
* @param StringView_t read only argument value holding data to be written into
* _file_name
* @param bool const boolean flag dictating if _file_name should be create if not
* found
* @returns void
*
*/
__0x_attr_FSC_fw inline void FileWrite(const StringView_t &_file_name, const StringView_t &_buffer, const bool _create_new = false)
{
this->__fileStreamStatusHandle(_file_name, _create_new, _buffer);
int fileDescriptor(this->__openFileDescriptor(std::move(_file_name), eFileDescriptorMode::WRITE));
const off_t file_size(_buffer.length());
this->__descriptorResize(fileDescriptor, file_size);
fMap_t mapped_data(this->__createPointerMap(fileDescriptor, file_size, false, 0));
this->__verifyMemMapState(mapped_data);
this->__copyMappedMemoryBytes(mapped_data, _buffer);
this->__descriptorMapClose(fileDescriptor, &mapped_data, file_size);
};
/**
*
* Register new Profile, propagate _new_profile into local register.
* @param struct stFileDescriptor new profile to allocate
* @param bool if true, source will be moved into destination
* @returns void
*
*/
__0x_attr_FSC_rnp inline void RegisterNewProfile(struct stFileDescriptor &_new_profile, const bool move_src = false) noexcept
{
if (_new_profile.file_size > 0) [[likely]]
{
std::lock_guard<std::mutex> _lock(this->_mtx_guard);
this->_profile_stack_reg.createProfile(move_src ? std::move(_new_profile) : _new_profile);
if (move_src)
{
_new_profile = {};
}
}
};
/**
*
* Register new Profile(bulk mode), propagate _new_profile entries into local register.
* @param std::unordered_map<struct stFileDescriptor> profile entries to allocate
* @param bool if true, source will be moved into destination
* @returns void
*
*/
__0x_attr_FSC_rnp inline void RegisterNewProfile(std::unordered_map<String_t, struct stFileDescriptor> &_new_profile, const bool move_src = false) noexcept
{
if (_new_profile.size() > 0) [[likely]]
{
std::lock_guard<std::mutex> _lock(this->_mtx_guard);
for (const auto &[fk, descriptor] : _new_profile)
{
if (descriptor.file_size <= 0)
continue;
this->_profile_stack_reg.createProfile(move_src ? std::move(descriptor) : descriptor);
}
if (move_src)
{
for (auto &src : _new_profile)
src.second.Clean();
_new_profile.erase(_new_profile.begin(), _new_profile.end());
}
}
};
/**
*
* Register new Profile(bulk mode), propagate _new_profile entries into local register.
* @param std::vector<struct stFileDescriptor> profile entries to allocate
* @param bool if true, source will be moved into destination
* @returns void
*
*/
__0x_attr_FSC_rnp inline void RegisterNewProfile(std::vector<struct stFileDescriptor> &_new_profile, const bool move_src) noexcept
{
if (_new_profile.size() > 0) [[likely]]
{
std::lock_guard<std::mutex> _lock(this->_mtx_guard);
for (std::size_t profile_counter(0); profile_counter < _new_profile.size(); ++profile_counter)
{
if (_new_profile[profile_counter].file_size <= 0)
continue;
this->_profile_stack_reg.createProfile(move_src ? std::move(_new_profile[profile_counter]) : _new_profile[profile_counter]);
if(move_src) _new_profile[profile_counter].Clean();
}
if (move_src)
{
_new_profile.erase(_new_profile.begin(), _new_profile.end());
}
}
};
/**
*
* Get profiler register stack size
* @returns std::size_t the size of register
*
*/
__0x_attr_FSC_gss constexpr std::size_t &GetRegisterSize(void) noexcept
{
std::lock_guard<std::mutex> _lock(this->_mtx_guard);
return this->_profile_stack_reg.reg_stack_size;
};
/**
*
* Get Instance UID
* @returns std::uint64_t the instance associated uid
*
*/
__0x_attr_FSC_gci constexpr std::uint64_t &GetFsControllerUID(void) noexcept
{
return this->_fs_instance_uid;
};
/**
*
* Get profiler structure data block at _profile_id index.
* @param _ForeignKeyType_& reference to register profiler associated FK assigned at
* allocation
* @returns stFileDescriptor const struct description(profiler) identified by
* _profile_id or empty descriptor if not found
*
*/
__0x_attr_FSC_gsp inline const struct stFileDescriptor GetProfile(const _ForeignKeyType_ &_profile_id) noexcept
{
std::lock_guard<std::mutex> _lock(this->_mtx_guard);
return this->_profile_stack_reg.getProfile(_profile_id);
};
/**
*
* Get read-only internal stack register
* @returns stProfilerStackRegister returns the internal register
*
*/
__0x_attr_FSC_gsptr inline const struct stProfilerStackRegister GetStackPointer(void) noexcept
{
std::lock_guard<std::mutex> _lock(this->_mtx_guard);
return this->_profile_stack_reg;
};
/**
*
* delete a profile identified by _profile_id aka FK
* @param _ForeignKeyType_& reference to register profiler associated FK
* @param bool if true, will unlink globally, meaning the actual file will be removed
* @returns void
*
*/
__0x_attr_FSC_dprf inline void DeleteProfile(const _ForeignKeyType_ &_profile_id) noexcept
{
std::lock_guard<std::mutex> _lock(this->_mtx_guard);
this->_profile_stack_reg.eraseProfile(_profile_id);
};
/**
*
* Create _file_name.
* @param StringView_t the absolute path to _file_name
* @param StringView_t content to write into the file, defaults to nothing
* @returns bool if created, true
*
*/
__0x_attr_FSC_cfl inline const bool CreateFile(const StringView_t &_file_name,
const StringView_t &_content = "")
{
std::ofstream fileCreate(_file_name.data());
if (!fileCreate)
throw std::runtime_error(std::move(String_t("File Create: ") + strerror(errno)));
if (!_content.empty())
{
fileCreate.write(_content.data(), _content.length());
}
fileCreate.close();
return FileExists(_file_name);
};
/**
*
* Create a directory.
* @param StringView_t& directory path to create
* @returns bool true if directory was created
*
*/
__0x_attr_FSC_cdir inline const bool CreateDirectory(const StringView_t &_directory) noexcept
{
std::filesystem::create_directories(_directory);
return std::filesystem::is_directory(_directory);
};
/**
*
* Collect directory entries from _directory, if _recursive is true, it will traverse the entire
* directories within _directory.
* @param StringView_t& directory to collect from
* @param bool optional! if true then recursive traversal
* @returns std::vector<String_t> vector containing entries from _directory scan.
*
*/
__0x_attr_FSC_cdire const std::vector<String_t> CollectDirectoryEntries(const StringView_t &_directory,
const bool _recursive = false)
{
std::vector<String_t> vector_entries;
if (_directory.empty())
return {};
if (_recursive)
{
for (const auto &d_entry : std::filesystem::recursive_directory_iterator(_directory))
{
if (std::filesystem::is_regular_file(d_entry.path()))
{
vector_entries.push_back(d_entry.path().string());
}
}
}
else
{
for (const auto &d_entry : std::filesystem::directory_iterator(_directory))
{
if (std::filesystem::is_regular_file(d_entry.path()))
{
vector_entries.push_back(d_entry.path().string());
}
}
}
return vector_entries;
};
/**
*
* Collect directory entries if any, constructs an object containing min/max collection entry
* size, along with an std::array<string> register containing entry name(path) and a collection
* array size.
* @param StringView_t& directory to collect and profile
* @param bool bit flag mandating recursive iteration or not
* @param std::size_t threshold to use for register allocation, if register size do not exceed
* or touch the threshold, structure will be deleted and empty block returned.
* @returns stDirectoryCollectionStat a structure containing directory profiling information,
* register stack, register size, min/max register entry size.
*
*/
inline const stDirectoryCollectionStat CollectDirectoryEntriesWithProfiling(const StringView_t &_directory, const bool _recursive, const std::size_t _threshold = 1)
{
stDirectoryCollectionStat new_collection_stat{.registry{this->CollectDirectoryEntries(_directory, _recursive)}};
std::size_t max_size_get(0), min_size_get(0), largest_path_index(0), loop_counter(0);
if (new_collection_stat.registry.size() > (std::size_t)0)
{
for (auto ncs_entry = new_collection_stat.registry.begin(); ncs_entry != new_collection_stat.registry.end(); ncs_entry++)
{
++loop_counter;
if (std::filesystem::file_size(*ncs_entry) > max_size_get)
{
max_size_get = std::filesystem::file_size(*ncs_entry);
largest_path_index = loop_counter - 1;
}
if (std::filesystem::file_size(*ncs_entry) < min_size_get)
{
min_size_get = std::filesystem::file_size(*ncs_entry);
}
}
new_collection_stat.min_size = min_size_get;
new_collection_stat.max_size = max_size_get;
new_collection_stat.registry_size = new_collection_stat.registry.size();
new_collection_stat.largest_file_path = new_collection_stat.registry.at(largest_path_index);
}
if (new_collection_stat.registry_size < _threshold)
{
new_collection_stat = {};
}
return new_collection_stat;
};
/**
*
* Scan directory recursively, aggregate every entry which is a regular file type and return the
* aggregation.
* @param StringView_t& path to aggregate from
* @returns directoryScanResult_t the aggregation
*
*/
__0x_attr_FSC_dirprf const directoryScanResult_t DirectoryProfiler(const StringView_t &path)
{
directoryScanResult_t scan_result;
if (path.empty())
return scan_result;
if (path.length() < FS_MAX_FILE_NAME_LENGTH)
{
if (!std::filesystem::path(path).is_absolute())
throw std::runtime_error("Use an absolute path please!");
this->__recursiveAggregation(path, scan_result);
}
return scan_result;
};
/**
*
* Delete a file_name, this action is not reversible.
* @param StringView_t& path to file to remove
* @returns void
*
*/
__0x_attr_FSC_delfl inline static void DeleteFile(const StringView_t &file_name)
{
if (file_name.empty())
return;
if (FileExists(file_name))
{
if (std::filesystem::is_regular_file(file_name))
{
std::filesystem::remove(file_name);
}
}
};
/**
*
* Create a backup copy of dir_source directory as dir_dest, to override existing destination
* files set dest_override to true, to copy empty files set copy_empty_files to true.
* @param StringView_t& the source directory to copy
* @param StringView_t dir_dest the destination backup directory
* @param bool optional! it true will override existing files
* @param bool optional! if true will copy empty files
* @returns bool true if backup was successful
*
*/
__0x_attr_FSC_cdbk const bool CreateDirectoryBackup(const StringView_t &dir_source, const StringView_t &dir_dest, const bool create_backup_dir = false, const bool dest_override = false, const bool copy_empty_files = false)
{
try
{
const bool is_source(IsDirectory(dir_source)), is_destination(IsDirectory(dir_dest));
if (!is_source)
throw std::runtime_error("source directory not valid for backup!");
else if(!is_destination && create_backup_dir)
std::filesystem::create_directories(dir_dest);