Fix leak for rset_key_control
[idzebra-moved-to-github.git] / index / retrieve.c
1 /* $Id: retrieve.c,v 1.36 2005-10-28 07:25:30 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25
26 #include <fcntl.h>
27 #ifdef WIN32
28 #include <io.h>
29 #include <process.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include "index.h"
36 #include <direntz.h>
37
38 int zebra_record_ext_read (void *fh, char *buf, size_t count)
39 {
40     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
41     return read (fc->fd, buf, count);
42 }
43
44 off_t zebra_record_ext_seek (void *fh, off_t offset)
45 {
46     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
47     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
48 }
49
50 off_t zebra_record_ext_tell (void *fh)
51 {
52     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
53     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
54 }
55
56 off_t zebra_record_int_seek (void *fh, off_t offset)
57 {
58     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
59     return (off_t) (fc->record_int_pos = offset);
60 }
61
62 off_t zebra_record_int_tell (void *fh)
63 {
64     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
65     return (off_t) fc->record_int_pos;
66 }
67
68 int zebra_record_int_read (void *fh, char *buf, size_t count)
69 {
70     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
71     int l = fc->record_int_len - fc->record_int_pos;
72     if (l <= 0)
73         return 0;
74     l = (l < (int) count) ? l : (int) count;
75     memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
76     fc->record_int_pos += l;
77     return l;
78 }
79
80 void zebra_record_int_end (void *fh, off_t off)
81 {
82     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
83     fc->offset_end = off;
84 }
85
86 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score,
87                         zebra_snippets *hit_snippet, ODR stream,
88                         oid_value input_format, Z_RecordComposition *comp,
89                         oid_value *output_format, char **rec_bufp,
90                         int *rec_lenp, char **basenamep,
91                         char **addinfo)
92 {
93     Record rec;
94     char *fname, *file_type, *basename;
95     RecType rt;
96     struct recRetrieveCtrl retrieveCtrl;
97     struct zebra_fetch_control fc;
98     RecordAttr *recordAttr;
99     void *clientData;
100     int raw_mode = 0;
101
102     *basenamep = 0;
103     *addinfo = 0;
104     if (comp && comp->which == Z_RecordComp_simple &&
105         comp->u.simple->which == Z_ElementSetNames_generic && 
106         !strcmp (comp->u.simple->u.generic, "_sysno_"))
107     {
108         char rec_str[60];
109         sprintf(rec_str, ZINT_FORMAT, sysno);
110         *output_format = VAL_SUTRS;
111         *rec_lenp = strlen(rec_str);
112         *rec_bufp = odr_strdup(stream, rec_str);
113         return 0;
114     }
115     rec = rec_get (zh->reg->records, sysno);
116     if (!rec)
117     {
118         yaz_log (YLOG_DEBUG, "rec_get fail on sysno=" ZINT_FORMAT, sysno);
119         *basenamep = 0;
120         return 14;
121     }
122     recordAttr = rec_init_attr (zh->reg->zei, rec);
123
124     file_type = rec->info[recInfo_fileType];
125     fname = rec->info[recInfo_filename];
126     basename = rec->info[recInfo_databaseName];
127     *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
128     strcpy (*basenamep, basename);
129
130     if (comp && comp->which == Z_RecordComp_simple &&
131         comp->u.simple->which == Z_ElementSetNames_generic && 
132         !strcmp (comp->u.simple->u.generic, "R"))
133     {
134         raw_mode = 1;
135     }
136     if (!(rt = recType_byName (zh->reg->recTypes, zh->res,
137                                file_type, &clientData)))
138     {
139         yaz_log (YLOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
140         return 14;
141     }
142     yaz_log (YLOG_DEBUG, "retrieve localno=" ZINT_FORMAT " score=%d", sysno,score);
143     retrieveCtrl.fh = &fc;
144     fc.fd = -1;
145     retrieveCtrl.fname = fname;
146     if (rec->size[recInfo_storeData] > 0)
147     {
148         retrieveCtrl.readf = zebra_record_int_read;
149         retrieveCtrl.seekf = zebra_record_int_seek;
150         retrieveCtrl.tellf = zebra_record_int_tell;
151         fc.record_int_len = rec->size[recInfo_storeData];
152         fc.record_int_buf = rec->info[recInfo_storeData];
153         fc.record_int_pos = 0;
154         yaz_log (YLOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
155         if (raw_mode)
156         {
157             *output_format = VAL_SUTRS;
158             *rec_lenp = rec->size[recInfo_storeData];
159             *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
160             memcpy(*rec_bufp, rec->info[recInfo_storeData], *rec_lenp);
161             rec_rm (&rec);
162             return 0;
163         }
164     }
165     else
166     {
167         char full_rep[1024];
168
169         if (zh->path_reg && !yaz_is_abspath (fname))
170         {
171             strcpy (full_rep, zh->path_reg);
172             strcat (full_rep, "/");
173             strcat (full_rep, fname);
174         }
175         else
176             strcpy (full_rep, fname);
177
178         if ((fc.fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
179         {
180             yaz_log (YLOG_WARN|YLOG_ERRNO, "Retrieve fail; missing file: %s",
181                   full_rep);
182             rec_rm (&rec);
183             return 14;
184         }
185         fc.record_offset = recordAttr->recordOffset;
186
187         retrieveCtrl.readf = zebra_record_ext_read;
188         retrieveCtrl.seekf = zebra_record_ext_seek;
189         retrieveCtrl.tellf = zebra_record_ext_tell;
190
191         zebra_record_ext_seek (retrieveCtrl.fh, 0);
192         if (raw_mode)
193         {
194             *output_format = VAL_SUTRS;
195             *rec_lenp = recordAttr->recordSize;
196             *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
197             zebra_record_ext_read(&fc, *rec_bufp, *rec_lenp);
198             rec_rm (&rec);
199             close (fc.fd);
200             return 0;
201         }
202     }
203     retrieveCtrl.localno = sysno;
204     retrieveCtrl.score = score;
205     retrieveCtrl.recordSize = recordAttr->recordSize;
206     retrieveCtrl.odr = stream;
207     retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
208     retrieveCtrl.comp = comp;
209     retrieveCtrl.encoding = zh->record_encoding;
210     retrieveCtrl.diagnostic = 0;
211     retrieveCtrl.addinfo = 0;
212     retrieveCtrl.dh = zh->reg->dh;
213     retrieveCtrl.res = zh->res;
214     retrieveCtrl.rec_buf = 0;
215     retrieveCtrl.rec_len = -1;
216     retrieveCtrl.hit_snippet = hit_snippet;
217     retrieveCtrl.doc_snippet = zebra_snippets_create();
218     
219     if (1)
220     {
221         /* snippets code */
222         zebra_snippets *snippet;
223
224 #if NEW_REC_KEYS
225         zebra_rec_keys_t reckeys = zebra_rec_keys_open();
226         
227         zebra_rec_keys_set_buf(reckeys,
228                                rec->info[recInfo_delKeys],
229                                rec->size[recInfo_delKeys], 
230                                0);
231         zebra_snippets_rec_keys(zh, reckeys, retrieveCtrl.doc_snippet);
232         zebra_rec_keys_close(reckeys);
233 #else
234         struct recKeys reckeys;
235         reckeys.buf = rec->info[recInfo_delKeys];
236         reckeys.buf_used = rec->size[recInfo_delKeys];
237         zebra_snippets_rec_keys(zh, &reckeys, retrieveCtrl.doc_snippet);
238 #endif
239
240
241 #if 0
242         /* for debugging purposes */
243         yaz_log(YLOG_LOG, "DOC SNIPPET:");
244         zebra_snippets_log(retrieveCtrl.doc_snippet, YLOG_LOG);
245         yaz_log(YLOG_LOG, "HIT SNIPPET:");
246         zebra_snippets_log(retrieveCtrl.hit_snippet, YLOG_LOG);
247 #endif
248         snippet = zebra_snippets_window(retrieveCtrl.doc_snippet,
249                                         retrieveCtrl.hit_snippet,
250                                         10);
251 #if 0
252         /* for debugging purposes */
253         yaz_log(YLOG_LOG, "WINDOW SNIPPET:");
254         zebra_snippets_log(snippet, YLOG_LOG);
255 #endif
256         (*rt->retrieve)(clientData, &retrieveCtrl);
257
258         zebra_snippets_destroy(snippet);
259     }
260     else
261     {
262         (*rt->retrieve)(clientData, &retrieveCtrl);
263     }
264
265     zebra_snippets_destroy(retrieveCtrl.doc_snippet);
266
267     *output_format = retrieveCtrl.output_format;
268     *rec_bufp = (char *) retrieveCtrl.rec_buf;
269     *rec_lenp = retrieveCtrl.rec_len;
270     if (fc.fd != -1)
271         close (fc.fd);
272     rec_rm (&rec);
273
274     *addinfo = retrieveCtrl.addinfo;
275     return retrieveCtrl.diagnostic;
276 }