summaryrefslogtreecommitdiffstats
path: root/src/descriptions_store.c
blob: 8276973718562287bd9abe4997386c067df01c41 (plain)
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
/*
 * descriptions_store.c: store man page descriptions in database
 *
 * Copyright (C) 2002, 2003, 2006, 2007, 2008, 2011 Colin Watson.
 *
 * This file is part of man-db.
 *
 * man-db is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * man-db is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with man-db; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif /* HAVE_CONFIG_H */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "gettext.h"
#define _(String) gettext (String)

#include "error.h"
#include "gl_array_list.h"
#include "gl_hash_map.h"
#include "gl_xlist.h"
#include "gl_xmap.h"
#include "stat-time.h"
#include "xalloc.h"

#include "manconfig.h"

#include "debug.h"
#include "filenames.h"
#include "glcontainers.h"

#include "db_storage.h"

#include "ult_src.h"
#include "descriptions.h"

static void gripe_bad_store (const char *name, const char *ext)
{
	if (quiet < 2)
		error (0, 0, _("warning: failed to store entry for %s(%s)"),
		       name, ext);
}

/* Is PARENT a prefix of CHILD, such that CHILD is in the manual hierarchy
 * PARENT?  This requires that the part of CHILD following PARENT start with
 * "/man".
 */
static int is_prefix (const char *parent, const char *child)
{
	return (STRNEQ (child, parent, strlen (parent)) &&
		STRNEQ (child + strlen (parent), "/man", 4));
}

/* Take a list of descriptions returned by parse_descriptions() and store
 * it into the database.
 */
void store_descriptions (MYDBM_FILE dbf, gl_list_t descs, struct mandata *info,
			 const char *path, const char *base, gl_list_t trace)
{
	const struct page_description *desc;
	const char *trace_name;
	gl_map_t trace_infos;
	gl_list_t whatis_infos;
	struct mandata *whatis_info;
	const struct mandata *pointer_info;

	assert (trace);
	assert (gl_list_size (trace) > 0);

	if (gl_list_size (descs)) {
		GL_LIST_FOREACH (trace, trace_name)
			debug ("trace: '%s'\n", trace_name);
	}

	trace_infos = new_string_map (GL_HASH_MAP,
				      (gl_mapvalue_dispose_fn)
				      free_mandata_struct);
	whatis_infos = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL,
					     (gl_listelement_dispose_fn)
					     free_mandata_struct,
					     true);

	GL_LIST_FOREACH (trace, trace_name)
		gl_map_put (trace_infos, xstrdup (trace_name),
			    filename_info (trace_name, quiet < 2));

	GL_LIST_FOREACH (descs, desc) {
		/* Either it's the real thing or merely a reference. Get the
		 * id and pointer right in either case.
		 */
		bool found_real_page = false;
		bool found_external = false;

		whatis_info = XZALLOC (struct mandata);
		whatis_info->ext = xstrdup (info->ext);
		whatis_info->sec = xstrdup (info->sec);
		whatis_info->id = info->id;
		if (info->comp)
			whatis_info->comp = xstrdup (info->comp);
		if (info->filter)
			whatis_info->filter = xstrdup (info->filter);
		if (desc->whatis)
			whatis_info->whatis = xstrdup (desc->whatis);
		whatis_info->mtime = info->mtime;

		if (STREQ (base, desc->name))
			found_real_page = true;
		else {
			GL_LIST_FOREACH (trace, trace_name) {
				const struct mandata *trace_info;
				struct stat st;

				trace_info = gl_map_get (trace_infos,
							 trace_name);
				if (!trace_info ||
				    !STREQ (trace_info->name, desc->name))
					continue;

				if (path && !is_prefix (path, trace_name)) {
					/* Link outside this manual
					 * hierarchy; skip this description.
					 */
					found_external = true;
					break;
				}
				free (whatis_info->ext);
				whatis_info->ext = xstrdup (trace_info->ext);
				free (whatis_info->sec);
				whatis_info->sec = xstrdup (trace_info->sec);
				if (!gl_list_next_node (trace, trace_node)) {
					if (info->id == SO_MAN)
						whatis_info->id = ULT_MAN;
				} else {
					if (info->id == ULT_MAN)
						whatis_info->id = SO_MAN;
				}
				free (whatis_info->comp);
				if (trace_info->comp)
					whatis_info->comp = xstrdup
						(trace_info->comp);
				else
					whatis_info->comp = NULL;
				if (lstat (trace_name, &st) == 0)
					whatis_info->mtime = get_stat_mtime
						(&st);
				else
					whatis_info->mtime = info->mtime;
				found_real_page = true;
			}
		}

		if (found_external) {
			debug ("skipping '%s'; link outside manual "
			       "hierarchy\n", desc->name);
			free_mandata_struct (whatis_info);
			continue;
		}

		if (!found_real_page) {
			whatis_info->name = xstrdup (desc->name);
			if (info->id < STRAY_CAT)
				whatis_info->id = WHATIS_MAN;
			else
				whatis_info->id = WHATIS_CAT;
			/* Don't waste space storing the whatis in the db
			 * more than once.
			 */
			free (whatis_info->whatis);
			whatis_info->whatis = NULL;
			gl_list_add_last (whatis_infos, whatis_info);
			continue;
		}

		debug ("name = '%s', ext = '%s', id = %c\n",
		       desc->name, whatis_info->ext, whatis_info->id);
		if (dbstore (dbf, whatis_info, desc->name) > 0) {
			gripe_bad_store (base, whatis_info->ext);
			free_mandata_struct (whatis_info);
			goto out;
		}

		free_mandata_struct (whatis_info);
	}

	/* The pointer for a WHATIS_MAN or WHATIS_CAT entry should be the
	 * last entry in the trace that has the same section and extension
	 * as the starting page (which is always the first entry in the
	 * trace).  If we were to add WHATIS_* entries for different
	 * extensions, then try_db -> add_candidate -> make_filename in
	 * man(1) would end up constructing a path that doesn't exist and is
	 * thus unusable.
	 */
	pointer_info = NULL;
	GL_LIST_FOREACH (trace, trace_name) {
		const struct mandata *trace_info;

		trace_info = gl_map_get (trace_infos, trace_name);
		if (trace_info &&
		    STREQ (trace_info->sec, info->sec) &&
		    STREQ (trace_info->ext, info->ext))
			pointer_info = trace_info;
	}
	assert (pointer_info);

	GL_LIST_FOREACH (whatis_infos, whatis_info) {
		char *name;

		name = whatis_info->name;
		whatis_info->name = NULL;

		whatis_info->pointer = xstrdup (pointer_info->name);

		debug ("name = '%s', ext = '%s', id = %c, pointer = '%s'\n",
		       name, whatis_info->ext, whatis_info->id,
		       whatis_info->pointer);
		if (dbstore (dbf, whatis_info, name) > 0) {
			gripe_bad_store (base, whatis_info->ext);
			free (name);
			goto out;
		}

		free (name);
	}

out:
	gl_list_free (whatis_infos);
	gl_map_free (trace_infos);
}