root/libswish3/trunk/src/libswish3/docinfo.c

Revision 2156, 7.6 kB (checked in by karpet, 2 months ago)

fix NULL free warning

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2007 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /* docinfo.c -- stat and time of files */
21
22 #include <sys/param.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdlib.h>
29
30 extern int errno;
31
32 #include "libswish3.h"
33
34 extern int SWISH_DEBUG;
35
36 /* PUBLIC */
37 swish_DocInfo *
38 swish_init_docinfo(
39 )
40 {
41
42     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
43         SWISH_DEBUG_MSG("init'ing docinfo");
44
45     swish_DocInfo *docinfo = swish_xmalloc(sizeof(swish_DocInfo));
46     docinfo->ref_cnt = 0;
47     docinfo->nwords = 0;
48     docinfo->mtime = 0;
49     docinfo->size = 0;
50     docinfo->encoding = swish_xstrdup((xmlChar *)SWISH_DEFAULT_ENCODING);
51     docinfo->uri = NULL;
52     docinfo->mime = NULL;
53     docinfo->parser = NULL;
54     docinfo->ext = NULL;
55     docinfo->update = NULL;
56
57     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO) {
58         SWISH_DEBUG_MSG("docinfo all ready");
59         swish_debug_docinfo(docinfo);
60     }
61
62     return docinfo;
63 }
64
65 /* PUBLIC */
66 void
67 swish_free_docinfo(
68     swish_DocInfo *ptr
69 )
70 {
71     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
72         SWISH_DEBUG_MSG("freeing swish_DocInfo");
73
74     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
75         swish_debug_docinfo(ptr);
76
77     if (ptr->ref_cnt != 0) {
78         SWISH_WARN("docinfo ref_cnt != 0: %d", ptr->ref_cnt);
79     }
80
81     ptr->nwords = 0;            /* why is this required? */
82     ptr->mtime = 0;
83     ptr->size = 0;
84
85 /* encoding and mime are malloced via xmlstrdup elsewhere */
86     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
87         SWISH_DEBUG_MSG("freeing docinfo->encoding");
88     swish_xfree(ptr->encoding);
89
90     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
91         SWISH_DEBUG_MSG("freeing docinfo->mime");
92     if (ptr->mime != NULL)
93         swish_xfree(ptr->mime);
94
95     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
96         SWISH_DEBUG_MSG("freeing docinfo->uri");
97     if (ptr->uri != NULL)
98         swish_xfree(ptr->uri);
99
100     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
101         SWISH_DEBUG_MSG("freeing docinfo->ext");
102     if (ptr->ext != NULL)
103         swish_xfree(ptr->ext);
104
105     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
106         SWISH_DEBUG_MSG("freeing docinfo->parser");
107     if (ptr->parser != NULL)
108         swish_xfree(ptr->parser);
109
110     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
111         SWISH_DEBUG_MSG("freeing docinfo ptr");
112     swish_xfree(ptr);
113
114     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
115         SWISH_DEBUG_MSG("swish_DocInfo all freed");
116 }
117
118 int
119 swish_check_docinfo(
120     swish_DocInfo *docinfo,
121     swish_Config *config
122 )
123 {
124     int ok;
125     xmlChar *ext;
126
127     ok = 1;
128
129     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
130         swish_debug_docinfo(docinfo);
131
132     if (!docinfo->uri)
133         SWISH_CROAK("Failed to return required header Content-Location:");
134
135     if (docinfo->size == -1)
136         SWISH_CROAK
137             ("Failed to return required header Content-Length: for doc '%s'",
138              docinfo->uri);
139
140 /* might make this conditional on verbose level */
141     if (docinfo->size == 0)
142         SWISH_CROAK("Found zero Content-Length for doc '%s'", docinfo->uri);
143
144     ext = swish_get_file_ext(docinfo->uri);
145 /* this fails with non-filenames like db ids, etc. */
146
147     if (docinfo->ext == NULL) {
148         if (ext != NULL)
149             docinfo->ext = swish_xstrdup(ext);
150         else
151             docinfo->ext = swish_xstrdup((xmlChar *)"none");
152
153     }
154
155     if (ext != NULL)
156         swish_xfree(ext);
157
158     if (!docinfo->mime) {
159         if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
160             SWISH_DEBUG_MSG
161                 ("no MIME known. guessing based on uri extension '%s'",
162                  docinfo->ext);
163         docinfo->mime = swish_get_mime_type(config, docinfo->ext);
164     }
165     else {
166         if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
167             SWISH_DEBUG_MSG("found MIME type in headers: '%s'", docinfo->mime);
168
169     }
170
171     if (!docinfo->parser) {
172         if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
173             SWISH_DEBUG_MSG
174                 ("no parser defined in headers -- deducing from content type '%s'",
175                  docinfo->mime);
176
177         docinfo->parser = swish_get_parser(config, docinfo->mime);
178     }
179     else {
180         if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
181             SWISH_DEBUG_MSG("found parser in headers: '%s'", docinfo->parser);
182
183     }
184
185     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
186         swish_debug_docinfo(docinfo);
187
188     return ok;
189
190 }
191
192 /* PUBLIC */
193 int
194 swish_docinfo_from_filesystem(
195     xmlChar *filename,
196     swish_DocInfo *i,
197     swish_ParserData *parser_data
198 )
199 {
200     struct stat info;
201     int stat_res;
202
203     if (i->ext != NULL)
204         swish_xfree(i->ext);
205
206     i->ext = swish_get_file_ext(filename);
207
208     stat_res = stat((char *)filename, &info);
209
210     if (stat_res == -1) {
211         SWISH_WARN("Can't stat '%s': %s", filename, strerror(errno));
212         return 0;
213     }
214
215     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
216         SWISH_DEBUG_MSG("handling url %s", filename);
217
218     if (i->uri != NULL)
219         swish_xfree(i->uri);
220
221     i->uri = swish_xstrdup(filename);
222     i->mtime = info.st_mtime;
223     i->size = info.st_size;
224
225     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
226         SWISH_DEBUG_MSG("handling mime");
227
228     if (i->mime != NULL)
229         swish_xfree(i->mime);
230
231     i->mime = swish_get_mime_type(parser_data->s3->config, i->ext);
232
233     if (SWISH_DEBUG & SWISH_DEBUG_DOCINFO)
234         SWISH_DEBUG_MSG("handling parser");
235
236     if (i->parser != NULL)
237         swish_xfree(i->parser);
238
239     i->parser = swish_get_parser(parser_data->s3->config, i->mime);
240
241     return 1;
242
243 }
244
245 /* PUBLIC */
246 void
247 swish_debug_docinfo(
248     swish_DocInfo *docinfo
249 )
250 {
251     xmlChar *h_mtime = swish_xmalloc(30);
252     strftime((char *)h_mtime, (unsigned long)30, SWISH_DATE_FORMAT_STRING,
253              (struct tm *)localtime((time_t *) & (docinfo->mtime)));
254
255     SWISH_DEBUG_MSG("DocInfo");
256     SWISH_DEBUG_MSG("  docinfo ptr: %lu", (unsigned long)docinfo);
257 /* SWISH_DEBUG_MSG("  size of swish_DocInfo struct: %d", (int)sizeof(swish_DocInfo)); */
258 /* SWISH_DEBUG_MSG("  size of docinfo ptr: %d",           (int)sizeof(*docinfo)); */
259     SWISH_DEBUG_MSG("  uri: %s (%d)", docinfo->uri, (int)sizeof(docinfo->uri));
260     SWISH_DEBUG_MSG("  doc size: %lu bytes (%d)", (unsigned long)docinfo->size,
261                     (int)sizeof(docinfo->size));
262     SWISH_DEBUG_MSG("  doc mtime: %lu (%d)", (unsigned long)docinfo->mtime,
263                     (int)sizeof(docinfo->mtime));
264 /* SWISH_DEBUG_MSG("  size of mime: %d",                  (int)sizeof(docinfo->mime)); */
265 /* SWISH_DEBUG_MSG("  size of encoding: %d",              (int)sizeof(docinfo->encoding)); */
266     SWISH_DEBUG_MSG("  mtime str: %s", h_mtime);
267     SWISH_DEBUG_MSG("  mime type: %s", docinfo->mime);
268     SWISH_DEBUG_MSG("  encoding: %s", docinfo->encoding);       /* only known after parsing has
269                                                                    started ... */
270     SWISH_DEBUG_MSG("  file ext: %s", docinfo->ext);
271     SWISH_DEBUG_MSG("  parser: %s", docinfo->parser);
272     SWISH_DEBUG_MSG("  nwords: %d", docinfo->nwords);
273
274     swish_xfree(h_mtime);
275
276 }
Note: See TracBrowser for help on using the browser.