-
Notifications
You must be signed in to change notification settings - Fork 1
/
mfreq-index.c
2773 lines (2206 loc) · 66.5 KB
/
mfreq-index.c
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
/* ************************************************************************
*
* mfreq-index
*
* (c) 1994-2018 by Markus Reschke
*
* ************************************************************************ */
/*
* local constants
*/
#define MFREQ_INDEX_C
/* defaults for this program */
#define NAME "mfreq-index"
#define CFG_FILENAME "index.cfg"
/*
* include header files
*/
/* local header files */
#include "common.h" /* common stuff */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
/* files */
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
/*
* more local constants
*/
/* update configuration path */
#ifndef CFG_PATH
#define CFG_PATH DEFAULT_CFG_PATH
#endif
/* build configuration filepath */
#define CFG_FILEPATH CFG_PATH"/"CFG_FILENAME
/*
* local functions
*/
_Bool ReadConfig(char *Filepath);
/* ************************************************************************
* data sorting
* ************************************************************************ */
/*
* sort file data by name
* - algorithm: merge sort
* - lower case > upper case
*/
IndexData_Type *MergeSort(IndexData_Type *List, IndexData_Type **Last)
{
IndexData_Type *NewList = NULL; /* return value */
IndexData_Type *LeftSub; /* left sublist */
IndexData_Type *RightSub; /* right sublist */
IndexData_Type *Element = NULL; /* single element */
IndexData_Type *MergedList; /* merged list */
unsigned int LeftSize; /* number of elements in left sublist */
unsigned int RightSize; /* number of elements in right sublist */
unsigned int StepSize; /* number of elements to process */
unsigned int Merges; /* number of sublist merges */
_Bool Run = True;
/* sanity check */
if (List == NULL) return NewList;
/*
* master loop
*/
StepSize = 1; /* start with sublists with one element each */
/* an 1 element sublist is sorted by definition :-) */
NewList = List; /* initialize new list for first loop run */
while (Run)
{
/* prepare this run */
LeftSub = NewList; /* start with new list */
NewList = NULL; /* reset new list */
MergedList = NULL; /* reset merged list */
Merges = 0; /* reset counter */
/*
* process list using sublists with StepSize elements
*/
/* as long as we haven't reached the lists end */
while (LeftSub)
{
/*
* create virtual left and right sublists with StepSize elements
*/
RightSub = LeftSub; /* starting point */
LeftSize = 0; /* reset size */
/* as long as we don't reach the list's end move SubSize elements to the right */
while (RightSub && (LeftSize < StepSize))
{
LeftSize++; /* increase size of left sublist */
RightSub = RightSub ->Next; /* move to next element */
}
RightSize = StepSize; /* assume size of right sublist to be StepSize */
/* might be larger than real size */
/*
* merge both sub lists as long as elements are left
* also prevent overrun of right sublist
*/
while ((LeftSize > 0) || ((RightSize > 0) && RightSub))
{
/*
* select element to merge
*/
/* no elements left in left sublist */
if (LeftSize == 0)
{
/* so take next element of right sublist */
Element = RightSub; /* take element */
RightSize--; /* one element less in sublist */
RightSub = RightSub->Next; /* move to next element in sublist */
}
/* no elements left in right sublist */
/* or end of right sublist reached */
else if ((RightSize == 0) || (RightSub == NULL))
{
/* so take next element of left sublist */
Element = LeftSub; /* take element */
LeftSize--; /* one element less in sublist */
LeftSub = LeftSub->Next; /* move to next element in sublist */
}
/* otherwise we have to compare the next elements of both sublists */
else
{
if (LeftSub->Name && RightSub->Name) /* sanity check */
{
if (strcmp(LeftSub->Name, RightSub->Name) <= 0)
{
/* element of left sublist is smaller or equal */
/* take that one */
Element = LeftSub; /* take element */
LeftSize--; /* one element less in sublist */
LeftSub = LeftSub->Next; /* move to next element in sublist */
}
else
{
/* element of right sublist is smaller */
/* take that one */
Element = RightSub; /* take element */
RightSize--; /* one element less in sublist */
RightSub = RightSub->Next; /* move to next element in sublist */
}
}
}
/*
* merge selected element
* by moving it to the merged list
*/
if (MergedList) /* if merged list exists */
{
MergedList->Next = Element; /* link element */
}
else /* otherwise */
{
/* it's the first element for the merged list */
/* and the starting point for the next loop run */
NewList = Element; /* set first element of new list */
}
MergedList = Element; /* Element is the new end of merged list */
}
Merges++; /* another merge done */
LeftSub = RightSub; /* move to next sublist pair */
}
MergedList->Next = NULL; /* end merged list */
/*
* loop control/feedback
*/
/* if only one merge is done we sorted the complete list */
if (Merges <= 1) /* if job done */
{
Run = False; /* end master loop */
/* if requested update pointer to last element */
if (Last) *Last = MergedList;
}
else /* another run required */
{
StepSize = StepSize * 2; /* double stepsize of sublist for next run */
}
}
return NewList;
}
/* ************************************************************************
* file handling
* ************************************************************************ */
/*
* check if file exists and is a regular file
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool CheckFile(char *Filepath)
{
_Bool Flag = False; /* return value */
struct stat FileData;
/* sanity check */
if (Filepath == NULL) return Flag;
/* check file type */
if (lstat(Filepath, &FileData) == 0)
{
if (S_ISREG(FileData.st_mode)) /* regular file */
{
Flag = True;
}
else
{
Log(L_WARN, "No regular file (%s)!", Filepath);
}
}
else
{
Log(L_WARN, "Can't access file (%s)!", Filepath);
}
return Flag;
}
/*
* open logfile
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool OpenLogfile()
{
_Bool Flag = False; /* return value */
/* sanity check */
if (Env->LogFilepath == NULL) return Flag;
Env->Log = fopen(Env->LogFilepath, "a"); /* append mode */
if (Env->Log != NULL) /* success */
{
/*
* We try to lock the logfile to ensure that only one
* instance of this tool is running.
*/
Flag = LockFile(Env->Log, Env->LogFilepath); /* lock file */
if (Flag == False)
{
Log(L_WARN, "Couldn't lock logfile!");
Log(L_WARN, "Another instance of " NAME " might be running.");
}
}
else /* error */
{
Log(L_WARN, "Couldn't open logfile (%s)!", Env->LogFilepath);
}
Log(L_INFO, NAME" "VERSION" started."); /* log: start */
return Flag;
}
/*
* read magic file and add filepaths to fileindex
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool ReadMagicFile(char *Filename)
{
_Bool Flag = False; /* return value */
_Bool Run = True; /* control flag */
FILE *File; /* filestream */
size_t Length;
File = fopen(Filename, "r"); /* read mode */
if (File)
{
while (Run)
{
/* read line-wise */
if (fgets(TempBuffer, DEFAULT_BUFFER_SIZE, File) != NULL)
{
Length = strlen(TempBuffer);
if (Length == 0) /* sanity check */
{
Run = False; /* end loop */
}
else if (Length == (DEFAULT_BUFFER_SIZE - 1)) /* maximum size reached */
{
/* now check if line matches buffer size exacly or exceeds it */
/* exact matches should have a LF as last character in front of the trailing 0 */
if (TempBuffer[Length - 1] != 10) /* pre-last char is not LF */
{
Run = False; /* end loop */
Log(L_WARN, "Input overflow for magic file (%s)!", Filename);
}
}
if (Run) /* if still in business */
{
/* remove LF at end of line */
if (TempBuffer[Length - 1] == 10)
{
TempBuffer[Length - 1] = 0;
Length--;
}
/* check if filepath is a regular file */
if (CheckFile(TempBuffer))
{
/* add to file index */
Run = AddDataElement(Filename, TempBuffer, NULL);
if (Run) Env->Files++; /* increase file counter */
}
}
}
else /* EOF or error */
{
Run = False; /* end loop */
/* check for error */
if (ferror(File) != 0)
{
Log(L_WARN, "Read error for magic file (%s)!", Filename);
}
}
}
Flag = True; /* signal success */
fclose(File); /* close file */
}
else
{
Log(L_WARN, "Couldn't open magic file (%s)!", Filename);
}
return Flag;
}
/*
* get magic files from directory
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool ProcessMagicPath(char *Path)
{
_Bool Flag = False; /* return value */
_Bool Run = True; /* control flag */
DIR *Directory;
struct dirent *File;
struct stat FileData;
/* sanity check */
if (Path == NULL) return Flag;
Directory = opendir(Path); /* open directory */
if (Directory == NULL) /* error */
{
Log(L_WARN, "Can't access directory (%s)!", Path);
}
else /* success */
{
if (chdir(Path) != 0) /* change current directory to path */
{ /* to simplify lstat later */
Run = False;
}
/* get all directory entries and check each name */
while (Run)
{
File = readdir(Directory); /* get next file */
if (File) /* got it */
{
/* check file type */
if (lstat(File->d_name, &FileData) == 0)
{
if (S_ISREG(FileData.st_mode)) /* regular file */
{
/* let's read the magic file if it's not excluded */
if (! MatchExcludeList(File->d_name))
Flag = ReadMagicFile(File->d_name);
}
}
}
else /* error or no more entries */
{
Run = False; /* end loop */
}
}
chdir(Env->CWD); /* change current directory back */
closedir(Directory); /* close directory */
}
return Flag;
}
/*
* get files from a directory and add them to the fileindex
* - supports recursive directory processing
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool ProcessPath(char *Path, char *PW, int Depth, _Bool AutoMagic)
{
_Bool Flag = False; /* return value */
_Bool Run = True; /* control flag */
_Bool AliasFlag = False; /* path aliases */
_Bool AnyCase = False; /* case-insensive search */
DIR *Directory;
struct dirent *File;
struct stat FileData;
char *LocalPath = NULL; /* absolute path */
char *SubPath = NULL; /* path of sub-directory */
char *Help, *LastDot;
size_t Length; /* string length */
unsigned int AliasNumber = 0; /* alias counter */
off_t AliasOffset = -1; /* alias file offset */
/* sanity check */
if ((Path == NULL) || (Depth < 0)) return Flag;
/* case-insensive search */
if (Env->CfgSwitches & SW_ANY_CASE) AnyCase = True;
/* build absolute path if necessary */
if (Path[0] != '/') /* relative path */
{
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s/%s", Env->CWD, Path);
LocalPath = CopyString(TempBuffer);
Path = LocalPath;
}
Directory = opendir(Path); /* open directory */
if (Directory != NULL) /* dir opened */
{
if (chdir(Path) != 0) /* change current directory to path */
{ /* to simplify lstat later */
Run = False;
}
/*
* path aliasing
*/
if (Env->CfgSwitches & SW_PATH_ALIASES) /* if enabled */
{
/* get and set top number */
if (Env->LastAlias) AliasNumber = Env->LastAlias->Number;
AliasNumber++;
/* check for overflow */
if (AliasNumber <= 10000) /* limit to 10000 */
{
/* add alias to list */
if (AddAliasElement(AliasNumber, Path))
{
AliasOffset = Env->LastAlias->Offset;
AliasFlag = True; /* perform aliasing */
}
else
{
Run = False;
}
}
/* else: AliasFlag is still false */
}
/* intermediate check */
if (Run)
{
Flag = True; /* now true by default */
}
else
{
Log(L_WARN, "Processing error (%s)!", Path);
}
/*
* get all directory entries and process each one
*/
while (Run)
{
File = readdir(Directory); /* get next file */
if (File) /* got it */
{
/* copy file name (File isn't re-entrant) */
snprintf(TempBuffer2, DEFAULT_BUFFER_SIZE - 1, "%s", File->d_name);
/* check file type */
if (lstat(TempBuffer2, &FileData) == 0)
{
if (S_ISREG(FileData.st_mode)) /* regular file */
{
/* add file to index if not excluded */
if (! MatchExcludeList(TempBuffer2))
{
/* build filepath */
/* omit filename (automatic filepath) */
if (AliasFlag) /* path alias enabled */
{
/* create aliased path without filename */
/* format: %<alias offset>%/ */
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%%%ld%%/", AliasOffset);
}
else /* path alias disabled */
{
/* create full path whithout filename */
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s/", Path);
}
if (AnyCase) /* case-insensitive search */
{
/*
* For AnyCase we have to convert the filename to uppper case
* later on. So we need to add the original filename to the path.
*/
/* add filename to path */
Length = strlen(TempBuffer);
Help = &TempBuffer[Length]; /* end of path */
snprintf(Help, DEFAULT_BUFFER_SIZE - 1 - Length,
"%s", TempBuffer2);
}
Flag = AddDataElement(TempBuffer2, TempBuffer, PW);
if (Flag) Env->Files++; /* increase file counter */
if (AutoMagic) /* auto magic enabled */
{
/* find last "." in filename */
Help = TempBuffer2;
LastDot = NULL;
while (Help[0] != 0) /* scan string */
{
if (Help[0] == '.') LastDot = Help;
Help++; /* next char */
}
/* create magic */
if (LastDot) /* got extension */
{
/* add filename to path */
Length = strlen(TempBuffer);
Help = &TempBuffer[Length]; /* end of path */
snprintf(Help, DEFAULT_BUFFER_SIZE - 1 - Length,
"%s", TempBuffer2);
LastDot[0] = 0; /* create sub-string */
Flag = AddDataElement(TempBuffer2, TempBuffer, PW);
}
}
}
}
else if (S_ISDIR(FileData.st_mode)) /* directory */
{
/* enter recursion but skip "." and ".." */
if ((Depth > 0) &&
(strcmp(TempBuffer2, ".") != 0) &&
(strcmp(TempBuffer2, "..") != 0))
{
/* create sub path */
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s/%s", Path, TempBuffer2);
SubPath = CopyString(TempBuffer);
/* call me resursively with a decreased depth */
Flag = ProcessPath(SubPath, PW, Depth - 1, AutoMagic);
if (SubPath) /* free path */
{
free(SubPath);
SubPath = NULL;
}
chdir(Path); /* chdir back to current directory */
}
}
}
}
else /* error or no more entries */
{
Run = False; /* end loop */
}
}
chdir(Env->CWD); /* move back to start directory */
closedir(Directory); /* close directory */
}
else /* error */
{
Log(L_WARN, "Can't access directory (%s)!", Path);
}
/* clean up */
if (LocalPath) free(LocalPath);
return Flag;
}
/*
* get files from a directory and add them to global filelist
* - supports filename pattern
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool ScanPath(char *Path, char *Pattern)
{
_Bool Flag = False; /* return value */
_Bool Run = True; /* loop control flag */
_Bool Add; /* data control flag */
DIR *Directory;
struct dirent *File;
struct stat FileData;
/* sanity check */
if (Path == NULL) return Flag;
Directory = opendir(Path); /* open directory */
if (Directory != NULL) /* dir opened */
{
if (chdir(Path) != 0) /* change current directory to path */
{ /* to simplify lstat later */
Run = False;
}
Flag = Run; /* update flag */
/*
* get all directory entries and process each one
*/
while (Run)
{
File = readdir(Directory); /* get next file */
if (File) /* got it */
{
/* copy file name (File isn't re-entrant) */
snprintf(TempBuffer2, DEFAULT_BUFFER_SIZE - 1, "%s", File->d_name);
/* get file details */
if (lstat(TempBuffer2, &FileData) == 0)
{
/* only process regular files */
if (S_ISREG(FileData.st_mode)) /* regular file */
{
/* if name is not excluded */
if (! MatchExcludeList(TempBuffer2))
{
Add = True; /* add file by default */
/* check if name matches pattern */
if (Pattern && !MatchPattern(TempBuffer2, Pattern)) Add = False;
if (Add) /* add file to list */
{
Run = AddFileElement(TempBuffer2, FileData.st_mtime);
Flag = Run; /* update flag */
}
}
}
}
}
else /* error or no more entries */
{
Run = False; /* end loop */
}
}
chdir(Env->CWD); /* move back to start directory */
closedir(Directory); /* close directory */
}
else /* error */
{
Log(L_WARN, "Can't access directory (%s)!", Path);
}
return Flag;
}
/*
* process directory for smart magics
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool ProcessSmartMagic(char *Name, char *Path, char *Pattern, char *Password,
_Bool Latest)
{
_Bool Flag = False; /* return value */
_Bool Run = False; /* control flag */
char *LocalPath = NULL; /* absolute path */
File_Type *File; /* file list */
File_Type *Next; /* file element */
time_t Time; /* time (seconds) */
/* sanity checks */
if ((Name == NULL) || (Path == NULL)) return Flag;
/*
* process directory
*/
/* build absolute path if necessary */
if (Path[0] != '/') /* relative path */
{
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s/%s", Env->CWD, Path);
LocalPath = CopyString(TempBuffer); /* create copy */
Path = LocalPath; /* and use copy */
}
/* scan directory (considering excludes and pattern) */
if (ScanPath(Path, Pattern))
{
if (Env->FileList) /* got some files */
{
Run = True; /* ok to proceed */
}
else /* no matching files */
{
Flag = True; /* job done */
}
}
/*
* process filedate
*/
if (Run && Latest) /* select latest file */
{
Time = 0; /* start with 0 */
/* find latest time */
File = Env->FileList; /* start of list */
while (File) /* follow list */
{
if (File->Time > Time) /* newer */
{
Time = File->Time; /* update time */
}
File = File->Next; /* next element */
}
/* mark all older files */
File = Env->FileList; /* start of list */
while (File) /* follow list */
{
if (File->Time < Time) /* older */
{
File->Flags |= FILE_SKIP; /* mark file */
}
File = File->Next; /* next element */
}
}
/*
* process matching files
*/
if (Run)
{
File = Env->FileList; /* start of list */
while (File) /* follow list */
{
Next = File->Next; /* get next element */
/* add match to global index */
if (!(File->Flags & FILE_SKIP))
{
/* create full path */
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s/%s", Path, File->Name);
Flag = AddDataElement(Name, TempBuffer, Password);
if (Flag) Env->Files++; /* increase file counter */
else Next = NULL; /* end loop */
}
File = Next; /* next element */
}
}
/*
* clean up
*/
if (Env->FileList) /* global file list */
{
FreeFileList(Env->FileList); /* free list */
Env->FileList = NULL; /* reset global variables */
Env->LastFile = NULL;
}
if (LocalPath) free(LocalPath); /* local path */
return Flag;
}
/* ************************************************************************
* file index
* ************************************************************************ */
/*
* write index
*
* returns:
* - 1 on success
* - 0 on error
*/
_Bool WriteIndex(char *Filepath)
{
_Bool Flag = False; /* return value */
_Bool Run = True; /* control flag */
IndexData_Type *IndexData = NULL; /* data list */
IndexData_Type *LastData; /* last element in data list */
IndexLookup_Type *IndexLookup = NULL; /* lookup list */
IndexAlias_Type *IndexAlias = NULL; /* alias list */
FILE *DataFile = NULL; /* index data file */
FILE *LookupFile = NULL; /* index lookup file */
FILE *AliasFile = NULL; /* index alias file */
FILE *OffsetFile = NULL; /* index offset file */
_Bool DataLock = False; /* data file locked */
_Bool LookupLock = False; /* lookup file locked */
_Bool AliasLock = False; /* alias file locked */
_Bool OffsetLock = False; /* offset file locked */
char FirstChar = 0; /* first char of filename */
unsigned int Counter = 0; /* filename/line counter */
off_t Offset; /* file offset */
char *Help;
_Bool OffsetFlag = False; /* flag for binary search mode */
/* sanity check */
if (Filepath == NULL) return False;
/* update flag for binary search (create offset file) */
if (Env->CfgSwitches & SW_BINARY_SEARCH) OffsetFlag = True;
/*
* sort data
*/
if (Run)
{
Run = False; /* reset flag */
LastData = NULL; /* reset pointer */
IndexData = MergeSort(Env->DataList, &LastData); /* sort */
Env->DataList = IndexData; /* update list start */
Env->LastData = LastData; /* update list end */
if (IndexData != NULL) Run = True; /* ok for next part */
}
/*
* open index files
*/
if (Run)
{
Run = False; /* reset flag */
/* data file */
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s."SUFFIX_DATA, Filepath);
DataFile = fopen(TempBuffer, "w"); /* truncate & write mode */
/* lookup file (binary search) */
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s."SUFFIX_LOOKUP, Filepath);
LookupFile = fopen(TempBuffer, "w"); /* truncate & write mode */
/* alias file */
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s."SUFFIX_ALIAS, Filepath);
AliasFile = fopen(TempBuffer, "w"); /* truncate & write mode */
/* offset file */
snprintf(TempBuffer, DEFAULT_BUFFER_SIZE - 1,
"%s."SUFFIX_OFFSET, Filepath);
OffsetFile = fopen(TempBuffer, "w"); /* truncate & write mode */
/* check */
if (DataFile && LookupFile && AliasFile && OffsetFile)
{
Run = True; /* ok for next part */
}
else
{
Log(L_WARN, "Can't open index files (%s)!", Filepath);
}
}