-
Notifications
You must be signed in to change notification settings - Fork 1
/
makemandb.c
2203 lines (1985 loc) · 54.8 KB
/
makemandb.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
/* $NetBSD: makemandb.c,v 1.16 2012/11/08 19:17:54 christos Exp $ */
/*
* Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: makemandb.c,v 1.16 2012/11/08 19:17:54 christos Exp $");
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <archive.h>
#include <libgen.h>
#include <md5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include "apropos-utils.h"
#include "man.h"
#include "mandoc.h"
#include "mdoc.h"
#include "sqlite3.h"
#define BUFLEN 1024
#define MDOC 0 //If the page is of mdoc(7) type
#define MAN 1 //If the page is of man(7) type
/*
* A data structure for holding section specific data.
*/
typedef struct secbuff {
char *data;
size_t buflen; //Total length of buffer allocated initially
size_t offset; // Current offset in the buffer.
} secbuff;
typedef struct makemandb_flags {
int optimize;
int limit; // limit the indexing to only NAME section
int recreate; // Database was created from scratch
int verbosity; // 0: quiet, 1: default, 2: verbose
} makemandb_flags;
typedef struct mandb_rec {
/* Fields for mandb table */
char *name; // for storing the name of the man page
char *name_desc; // for storing the one line description (.Nd)
secbuff desc; // for storing the DESCRIPTION section
secbuff lib; // for the LIBRARY section
secbuff return_vals; // RETURN VALUES
secbuff env; // ENVIRONMENT
secbuff files; // FILES
secbuff exit_status; // EXIT STATUS
secbuff diagnostics; // DIAGNOSTICS
secbuff errors; // ERRORS
char section[2];
int xr_found;
/* Fields for mandb_meta table */
char *md5_hash;
dev_t device;
ino_t inode;
time_t mtime;
/* Fields for mandb_links table */
char *machine;
char *links; //all the links to a page in a space separated form
char *file_path;
/* Non-db fields */
int page_type; //Indicates the type of page: mdoc or man
} mandb_rec;
static void append(secbuff *sbuff, const char *src);
static void init_secbuffs(mandb_rec *);
static void free_secbuffs(mandb_rec *);
static int check_md5(const char *, sqlite3 *, const char *, char **, void *, size_t);
static void cleanup(mandb_rec *);
static void set_section(const struct mdoc *, const struct man *, mandb_rec *);
static void set_machine(const struct mdoc *, mandb_rec *);
static int insert_into_db(sqlite3 *, mandb_rec *);
static void begin_parse(const char *, struct mparse *, mandb_rec *,
const void *, size_t len);
static void pmdoc_node(const struct mdoc_node *, mandb_rec *);
static void pmdoc_Nm(const struct mdoc_node *, mandb_rec *);
static void pmdoc_Nd(const struct mdoc_node *, mandb_rec *);
static void pmdoc_Sh(const struct mdoc_node *, mandb_rec *);
static void pmdoc_Xr(const struct mdoc_node *, mandb_rec *);
static void pmdoc_Pp(const struct mdoc_node *, mandb_rec *);
static void pmdoc_macro_handler(const struct mdoc_node *, mandb_rec *,
enum mdoct);
static void pman_node(const struct man_node *n, mandb_rec *);
static void pman_parse_node(const struct man_node *, secbuff *);
static void pman_parse_name(const struct man_node *, mandb_rec *);
static void pman_sh(const struct man_node *, mandb_rec *);
static void pman_block(const struct man_node *, mandb_rec *);
static void traversedir(const char *, const char *, sqlite3 *, struct mparse *);
static void mdoc_parse_section(enum mdoc_sec, const char *, mandb_rec *);
static void man_parse_section(enum man_sec, const struct man_node *, mandb_rec *);
static void build_file_cache(sqlite3 *, const char *, const char *,
struct stat *);
static void update_db(sqlite3 *, struct mparse *, mandb_rec *);
__dead static void usage(void);
static void optimize(sqlite3 *);
static char *parse_escape(const char *);
static makemandb_flags mflags = { .verbosity = 1 };
typedef void (*pman_nf)(const struct man_node *n, mandb_rec *);
typedef void (*pmdoc_nf)(const struct mdoc_node *n, mandb_rec *);
static const pmdoc_nf mdocs[MDOC_MAX] = {
NULL, /* Ap */
NULL, /* Dd */
NULL, /* Dt */
NULL, /* Os */
pmdoc_Sh, /* Sh */
NULL, /* Ss */
pmdoc_Pp, /* Pp */
NULL, /* D1 */
NULL, /* Dl */
NULL, /* Bd */
NULL, /* Ed */
NULL, /* Bl */
NULL, /* El */
NULL, /* It */
NULL, /* Ad */
NULL, /* An */
NULL, /* Ar */
NULL, /* Cd */
NULL, /* Cm */
NULL, /* Dv */
NULL, /* Er */
NULL, /* Ev */
NULL, /* Ex */
NULL, /* Fa */
NULL, /* Fd */
NULL, /* Fl */
NULL, /* Fn */
NULL, /* Ft */
NULL, /* Ic */
NULL, /* In */
NULL, /* Li */
pmdoc_Nd, /* Nd */
pmdoc_Nm, /* Nm */
NULL, /* Op */
NULL, /* Ot */
NULL, /* Pa */
NULL, /* Rv */
NULL, /* St */
NULL, /* Va */
NULL, /* Vt */
pmdoc_Xr, /* Xr */
NULL, /* %A */
NULL, /* %B */
NULL, /* %D */
NULL, /* %I */
NULL, /* %J */
NULL, /* %N */
NULL, /* %O */
NULL, /* %P */
NULL, /* %R */
NULL, /* %T */
NULL, /* %V */
NULL, /* Ac */
NULL, /* Ao */
NULL, /* Aq */
NULL, /* At */
NULL, /* Bc */
NULL, /* Bf */
NULL, /* Bo */
NULL, /* Bq */
NULL, /* Bsx */
NULL, /* Bx */
NULL, /* Db */
NULL, /* Dc */
NULL, /* Do */
NULL, /* Dq */
NULL, /* Ec */
NULL, /* Ef */
NULL, /* Em */
NULL, /* Eo */
NULL, /* Fx */
NULL, /* Ms */
NULL, /* No */
NULL, /* Ns */
NULL, /* Nx */
NULL, /* Ox */
NULL, /* Pc */
NULL, /* Pf */
NULL, /* Po */
NULL, /* Pq */
NULL, /* Qc */
NULL, /* Ql */
NULL, /* Qo */
NULL, /* Qq */
NULL, /* Re */
NULL, /* Rs */
NULL, /* Sc */
NULL, /* So */
NULL, /* Sq */
NULL, /* Sm */
NULL, /* Sx */
NULL, /* Sy */
NULL, /* Tn */
NULL, /* Ux */
NULL, /* Xc */
NULL, /* Xo */
NULL, /* Fo */
NULL, /* Fc */
NULL, /* Oo */
NULL, /* Oc */
NULL, /* Bk */
NULL, /* Ek */
NULL, /* Bt */
NULL, /* Hf */
NULL, /* Fr */
NULL, /* Ud */
NULL, /* Lb */
NULL, /* Lp */
NULL, /* Lk */
NULL, /* Mt */
NULL, /* Brq */
NULL, /* Bro */
NULL, /* Brc */
NULL, /* %C */
NULL, /* Es */
NULL, /* En */
NULL, /* Dx */
NULL, /* %Q */
NULL, /* br */
NULL, /* sp */
NULL, /* %U */
NULL, /* Ta */
};
static const pman_nf mans[MAN_MAX] = {
NULL, //br
NULL, //TH
pman_sh, //SH
NULL, //SS
NULL, //TP
NULL, //LP
NULL, //PP
NULL, //P
NULL, //IP
NULL, //HP
NULL, //SM
NULL, //SB
NULL, //BI
NULL, //IB
NULL, //BR
NULL, //RB
NULL, //R
pman_block, //B
NULL, //I
NULL, //IR
NULL, //RI
NULL, //na
NULL, //sp
NULL, //nf
NULL, //fi
NULL, //RE
NULL, //RS
NULL, //DT
NULL, //UC
NULL, //PD
NULL, //AT
NULL, //in
NULL, //ft
};
int
main(int argc, char *argv[])
{
FILE *file;
const char *sqlstr, *manconf = NULL;
char *line, *command, *parent;
char *errmsg;
int ch;
struct mparse *mp;
sqlite3 *db;
ssize_t len;
size_t linesize;
struct mandb_rec rec;
while ((ch = getopt(argc, argv, "C:floQqv")) != -1) {
switch (ch) {
case 'C':
manconf = optarg;
break;
case 'f':
mflags.recreate = 1;
break;
case 'l':
mflags.limit = 1;
break;
case 'o':
mflags.optimize = 1;
break;
case 'Q':
mflags.verbosity = 0;
break;
case 'q':
mflags.verbosity = 1;
break;
case 'v':
mflags.verbosity = 2;
break;
default:
usage();
}
}
memset(&rec, 0, sizeof(rec));
init_secbuffs(&rec);
mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
if (manconf) {
char *arg;
size_t command_len = shquote(manconf, NULL, 0) + 1;
arg = emalloc(command_len);
shquote(manconf, arg, command_len);
easprintf(&command, "man -p -C %s", arg);
free(arg);
} else {
command = estrdup("man -p");
manconf = MANCONF;
}
if (mflags.recreate)
remove(get_dbpath(manconf));
if ((db = init_db(MANDB_CREATE, manconf)) == NULL)
exit(EXIT_FAILURE);
sqlite3_exec(db, "PRAGMA synchronous = 0", NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
close_db(db);
exit(EXIT_FAILURE);
}
sqlite3_exec(db, "ATTACH DATABASE \':memory:\' AS metadb", NULL, NULL,
&errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
close_db(db);
exit(EXIT_FAILURE);
}
/* Call man -p to get the list of man page dirs */
if ((file = popen(command, "r")) == NULL) {
close_db(db);
err(EXIT_FAILURE, "fopen failed");
}
free(command);
/* Begin the transaction for indexing the pages */
sqlite3_exec(db, "BEGIN", NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
exit(EXIT_FAILURE);
}
sqlstr = "CREATE TABLE metadb.file_cache(device, inode,"
" mtime, parent, file PRIMARY KEY);"
"CREATE UNIQUE INDEX metadb.index_file_cache_dev"
" ON file_cache (device, inode); "
"CREATE VIRTUAL TABLE metadb.mandb_dup USING fts4(section, name, "
"name_desc, desc, lib, return_vals, env, files, "
"exit_status, diagnostics, errors, machine "
"); " //mandb_dup
"CREATE VIRTUAL TABLE metadb.mandb_dupaux USING fts4aux(mandb_dup); "; // mandb_dict
sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
close_db(db);
exit(EXIT_FAILURE);
}
if (mflags.verbosity)
printf("Building temporary file cache\n");
line = NULL;
linesize = 0;
while ((len = getline(&line, &linesize, file)) != -1) {
/* Replace the new line character at the end of string with '\0' */
line[len - 1] = '\0';
parent = estrdup(line);
char *pdir = estrdup(dirname(parent));
free(parent);
/* Traverse the man page directories and parse the pages */
traversedir(pdir, line, db, mp);
free(pdir);
}
free(line);
if (pclose(file) == -1) {
close_db(db);
cleanup(&rec);
free_secbuffs(&rec);
err(EXIT_FAILURE, "pclose error");
}
if (mflags.verbosity)
printf("Performing index update\n");
update_db(db, mp, &rec);
mparse_free(mp);
free_secbuffs(&rec);
/* Commit the transaction */
sqlite3_exec(db, "COMMIT", NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
exit(EXIT_FAILURE);
}
sqlstr = "INSERT OR IGNORE INTO mandb_dict SELECT term, occurrences "
"FROM metadb.mandb_dupaux; "
"DROP TABLE metadb.mandb_dup; "
"DROP TABLE metadb.mandb_dupaux;";
sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
}
if (mflags.optimize)
optimize(db);
close_db(db);
return 0;
}
/*
* traversedir --
* Traverses the given directory recursively and passes all the man page files
* in the way to build_file_cache()
*/
static void
traversedir(const char *parent, const char *file, sqlite3 *db,
struct mparse *mp)
{
struct stat sb;
struct dirent *dirp;
DIR *dp;
char *buf;
if (stat(file, &sb) < 0) {
if (mflags.verbosity)
warn("stat failed: %s", file);
return;
}
/* If it is a regular file or a symlink, pass it to build_cache() */
if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode)) {
build_file_cache(db, parent, file, &sb);
return;
}
/* If it is a directory, traverse it recursively */
if (S_ISDIR(sb.st_mode)) {
if ((dp = opendir(file)) == NULL) {
if (mflags.verbosity)
warn("opendir error: %s", file);
return;
}
while ((dirp = readdir(dp)) != NULL) {
/* Avoid . and .. entries in a directory */
if (strncmp(dirp->d_name, ".", 1)) {
easprintf(&buf, "%s/%s", file, dirp->d_name);
traversedir(parent, buf, db, mp);
free(buf);
}
}
closedir(dp);
}
}
/* build_file_cache --
* This function generates an md5 hash of the file passed as it's 2nd parameter
* and stores it in a temporary table file_cache along with the full file path.
* This is done to support incremental updation of the database.
* The temporary table file_cache is dropped thereafter in the function
* update_db(), once the database has been updated.
*/
static void
build_file_cache(sqlite3 *db, const char *parent, const char *file,
struct stat *sb)
{
const char *sqlstr;
sqlite3_stmt *stmt = NULL;
int rc, idx;
assert(file != NULL);
dev_t device_cache = sb->st_dev;
ino_t inode_cache = sb->st_ino;
time_t mtime_cache = sb->st_mtime;
sqlstr = "INSERT INTO metadb.file_cache VALUES (:device, :inode,"
" :mtime, :parent, :file)";
rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
return;
}
idx = sqlite3_bind_parameter_index(stmt, ":device");
rc = sqlite3_bind_int64(stmt, idx, device_cache);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
}
idx = sqlite3_bind_parameter_index(stmt, ":inode");
rc = sqlite3_bind_int64(stmt, idx, inode_cache);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
}
idx = sqlite3_bind_parameter_index(stmt, ":mtime");
rc = sqlite3_bind_int64(stmt, idx, mtime_cache);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
}
idx = sqlite3_bind_parameter_index(stmt, ":parent");
rc = sqlite3_bind_text(stmt, idx, parent, -1, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
}
idx = sqlite3_bind_parameter_index(stmt, ":file");
rc = sqlite3_bind_text(stmt, idx, file, -1, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
}
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
static void
update_existing_entry(sqlite3 *db, const char *file, const char *hash,
mandb_rec *rec, int *new_count, int *link_count, int *err_count)
{
int update_count, rc, idx;
const char *inner_sqlstr;
sqlite3_stmt *inner_stmt;
update_count = sqlite3_total_changes(db);
inner_sqlstr = "UPDATE mandb_meta SET device = :device,"
" inode = :inode, mtime = :mtime WHERE"
" md5_hash = :md5 AND file = :file AND"
" (device <> :device2 OR inode <> "
" :inode2 OR mtime <> :mtime2)";
rc = sqlite3_prepare_v2(db, inner_sqlstr, -1, &inner_stmt, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
return;
}
idx = sqlite3_bind_parameter_index(inner_stmt, ":device");
sqlite3_bind_int64(inner_stmt, idx, rec->device);
idx = sqlite3_bind_parameter_index(inner_stmt, ":inode");
sqlite3_bind_int64(inner_stmt, idx, rec->inode);
idx = sqlite3_bind_parameter_index(inner_stmt, ":mtime");
sqlite3_bind_int64(inner_stmt, idx, rec->mtime);
idx = sqlite3_bind_parameter_index(inner_stmt, ":md5");
sqlite3_bind_text(inner_stmt, idx, hash, -1, NULL);
idx = sqlite3_bind_parameter_index(inner_stmt, ":file");
sqlite3_bind_text(inner_stmt, idx, file, -1, NULL);
idx = sqlite3_bind_parameter_index(inner_stmt, ":device2");
sqlite3_bind_int64(inner_stmt, idx, rec->device);
idx = sqlite3_bind_parameter_index(inner_stmt, ":inode2");
sqlite3_bind_int64(inner_stmt, idx, rec->inode);
idx = sqlite3_bind_parameter_index(inner_stmt, ":mtime2");
sqlite3_bind_int64(inner_stmt, idx, rec->mtime);
rc = sqlite3_step(inner_stmt);
if (rc == SQLITE_DONE) {
/* Check if an update has been performed. */
if (update_count != sqlite3_total_changes(db)) {
if (mflags.verbosity == 2)
printf("Updated %s\n", file);
(*new_count)++;
} else {
/* Otherwise it was a hardlink. */
(*link_count)++;
}
} else {
if (mflags.verbosity == 2)
warnx("Could not update the meta data for %s", file);
(*err_count)++;
}
sqlite3_finalize(inner_stmt);
}
/* read_and_decompress --
* Reads the given file into memory. If it is compressed, decompres
* it before returning to the caller.
*/
static int
read_and_decompress(const char *file, void **buf, size_t *len)
{
size_t off;
ssize_t r;
struct archive *a;
struct archive_entry *ae;
if ((a = archive_read_new()) == NULL)
errx(EXIT_FAILURE, "memory allocation failed");
if (archive_read_support_compression_all(a) != ARCHIVE_OK ||
archive_read_support_format_raw(a) != ARCHIVE_OK ||
archive_read_open_filename(a, file, 65536) != ARCHIVE_OK ||
archive_read_next_header(a, &ae) != ARCHIVE_OK)
goto archive_error;
*len = 65536;
*buf = emalloc(*len);
off = 0;
for (;;) {
r = archive_read_data(a, (char *)*buf + off, *len - off);
if (r == ARCHIVE_OK) {
archive_read_close(a);
*len = off;
return 0;
}
if (r <= 0) {
free(*buf);
break;
}
off += r;
if (off == *len) {
*len *= 2;
if (*len < off) {
if (mflags.verbosity)
warnx("File too large: %s", file);
free(*buf);
archive_read_close(a);
return -1;
}
*buf = erealloc(*buf, *len);
}
}
archive_error:
warnx("Error while reading `%s': %s", file, archive_error_string(a));
archive_read_close(a);
return -1;
}
/* update_db --
* Does an incremental updation of the database by checking the file_cache.
* It parses and adds the pages which are present in file_cache,
* but not in the database.
* It also removes the pages which are present in the databse,
* but not in the file_cache.
*/
static void
update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
{
const char *sqlstr;
sqlite3_stmt *stmt = NULL;
const char *file;
const char *parent;
char *errmsg = NULL;
char *md5sum;
void *buf;
size_t buflen;
int new_count = 0; /* Counter for newly indexed/updated pages */
int total_count = 0; /* Counter for total number of pages */
int err_count = 0; /* Counter for number of failed pages */
int link_count = 0; /* Counter for number of hard/sym links */
int md5_status;
int rc;
sqlstr = "SELECT device, inode, mtime, parent, file"
" FROM metadb.file_cache fc"
" WHERE NOT EXISTS(SELECT 1 FROM mandb_meta WHERE"
" device = fc.device AND inode = fc.inode AND "
" mtime = fc.mtime AND file = fc.file)";
rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
close_db(db);
errx(EXIT_FAILURE, "Could not query file cache");
}
buf = NULL;
while (sqlite3_step(stmt) == SQLITE_ROW) {
free(buf);
total_count++;
rec->device = sqlite3_column_int64(stmt, 0);
rec->inode = sqlite3_column_int64(stmt, 1);
rec->mtime = sqlite3_column_int64(stmt, 2);
parent = (const char *) sqlite3_column_text(stmt, 3);
file = (const char *) sqlite3_column_text(stmt, 4);
if (read_and_decompress(file, &buf, &buflen)) {
err_count++;
buf = NULL;
continue;
}
md5_status = check_md5(file, db, "mandb_meta", &md5sum, buf, buflen);
assert(md5sum != NULL);
if (md5_status == -1) {
if (mflags.verbosity)
warnx("An error occurred in checking md5 value"
" for file %s", file);
err_count++;
continue;
}
if (md5_status == 0) {
/*
* The MD5 hash is already present in the database,
* so simply update the metadata, ignoring symlinks.
*/
struct stat sb;
stat(file, &sb);
if (S_ISLNK(sb.st_mode)) {
free(md5sum);
link_count++;
continue;
}
update_existing_entry(db, file, md5sum, rec,
&new_count, &link_count, &err_count);
free(md5sum);
continue;
}
if (md5_status == 1) {
/*
* The MD5 hash was not present in the database.
* This means is either a new file or an updated file.
* We should go ahead with parsing.
*/
if (mflags.verbosity == 2)
printf("Parsing: %s\n", file);
rec->md5_hash = md5sum;
rec->file_path = estrdup(file);
// file_path is freed by insert_into_db itself.
chdir(parent);
begin_parse(file, mp, rec, buf, buflen);
if (insert_into_db(db, rec) < 0) {
if (mflags.verbosity)
warnx("Error in indexing %s", file);
err_count++;
} else {
new_count++;
}
}
}
free(buf);
sqlite3_finalize(stmt);
if (mflags.verbosity == 2) {
printf("Total Number of new or updated pages encountered = %d\n"
"Total number of (hard or symbolic) links found = %d\n"
"Total number of pages that were successfully"
" indexed/updated = %d\n"
"Total number of pages that could not be indexed"
" due to errors = %d\n",
total_count - link_count, link_count, new_count, err_count);
}
if (mflags.recreate)
return;
if (mflags.verbosity == 2)
printf("Deleting stale index entries\n");
sqlstr = "DELETE FROM mandb_meta WHERE file NOT IN"
" (SELECT file FROM metadb.file_cache);"
"DELETE FROM mandb_links WHERE md5_hash NOT IN"
" (SELECT md5_hash from mandb_meta);"
"DROP TABLE metadb.file_cache;"
"DELETE FROM mandb WHERE rowid NOT IN"
" (SELECT id FROM mandb_meta);";
sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("Removing old entries failed: %s", errmsg);
warnx("Please rebuild database from scratch with -f.");
free(errmsg);
return;
}
}
/*
* begin_parse --
* parses the man page using libmandoc
*/
static void
begin_parse(const char *file, struct mparse *mp, mandb_rec *rec,
const void *buf, size_t len)
{
struct mdoc *mdoc;
struct man *man;
mparse_reset(mp);
rec->xr_found = 0;
if (mparse_readmem(mp, buf, len, file) >= MANDOCLEVEL_FATAL) {
/* Printing this warning at verbosity level 2
* because some packages from pkgsrc might trigger several
* of such warnings.
*/
if (mflags.verbosity == 2)
warnx("%s: Parse failure", file);
return;
}
mparse_result(mp, &mdoc, &man);
if (mdoc == NULL && man == NULL) {
if (mflags.verbosity == 2)
warnx("Not a man(7) or mdoc(7) page");
return;
}
set_machine(mdoc, rec);
set_section(mdoc, man, rec);
if (mdoc) {
rec->page_type = MDOC;
pmdoc_node(mdoc_node(mdoc), rec);
} else {
rec->page_type = MAN;
pman_node(man_node(man), rec);
}
}
/*
* set_section --
* Extracts the section number and normalizes it to only the numeric part
* (Which should be the first character of the string).
*/
static void
set_section(const struct mdoc *md, const struct man *m, mandb_rec *rec)
{
if (md) {
const struct mdoc_meta *md_meta = mdoc_meta(md);
rec->section[0] = md_meta->msec[0];
} else if (m) {
const struct man_meta *m_meta = man_meta(m);
rec->section[0] = m_meta->msec[0];
}
}
/*
* get_machine --
* Extracts the machine architecture information if available.
*/
static void
set_machine(const struct mdoc *md, mandb_rec *rec)
{
if (md == NULL)
return;
const struct mdoc_meta *md_meta = mdoc_meta(md);
if (md_meta->arch)
rec->machine = estrdup(md_meta->arch);
}
static void
pmdoc_node(const struct mdoc_node *n, mandb_rec *rec)
{
if (n == NULL)
return;
switch (n->type) {
case (MDOC_BODY):
/* FALLTHROUGH */
case (MDOC_TAIL):
/* FALLTHROUGH */
case (MDOC_ELEM):
if (mdocs[n->tok] == NULL)
break;
(*mdocs[n->tok])(n, rec);
break;
default:
break;
}
pmdoc_node(n->child, rec);
pmdoc_node(n->next, rec);
}
/*
* pmdoc_Nm --
* Extracts the Name of the manual page from the .Nm macro
*/
static void
pmdoc_Nm(const struct mdoc_node *n, mandb_rec *rec)
{
if (n->sec != SEC_NAME)
return;
for (n = n->child; n; n = n->next) {
if (n->type == MDOC_TEXT) {
concat(&rec->name, n->string);
}
}
}
/*
* pmdoc_Nd --
* Extracts the one line description of the man page from the .Nd macro
*/
static void
pmdoc_Nd(const struct mdoc_node *n, mandb_rec *rec)
{
/*
* A static variable for keeping track of whether a Xr macro was seen
* previously.
*/
char *buf = NULL;
char *temp;
if (n == NULL)
return;
if (n->type == MDOC_TEXT) {
if (rec->xr_found && n->next) {
/*
* An Xr macro was seen previously, so parse this
* and the next node.
*/
temp = estrdup(n->string);
n = n->next;
easprintf(&buf, "%s(%s)", temp, n->string);
concat(&rec->name_desc, buf);
free(buf);
free(temp);
} else {
concat(&rec->name_desc, n->string);
}
rec->xr_found = 0;
} else if (mdocs[n->tok] == pmdoc_Xr) {
/* Remember that we have encountered an Xr macro */
rec->xr_found = 1;
}
if (n->child)
pmdoc_Nd(n->child, rec);
if(n->next)
pmdoc_Nd(n->next, rec);
}
/*
* pmdoc_macro_handler--
* This function is a single point of handling all the special macros that we
* want to handle especially. For example the .Xr macro for properly parsing
* the referenced page name along with the section number, or the .Pp macro
* for adding a new line whenever we encounter it.