For auth failure, use diagnostic 1011: Init/AC: Bad User/password
[idzebra-moved-to-github.git] / index / retrieve.c
1 /* $Id: retrieve.c,v 1.21.2.1 2005-05-31 19:28:49 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 #else
31 #include <unistd.h>
32 #endif
33
34 #include "index.h"
35 #include <direntz.h>
36
37 int zebra_record_ext_read (void *fh, char *buf, size_t count)
38 {
39     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
40     return read (fc->fd, buf, count);
41 }
42
43 off_t zebra_record_ext_seek (void *fh, off_t offset)
44 {
45     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
46     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
47 }
48
49 off_t zebra_record_ext_tell (void *fh)
50 {
51     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
52     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
53 }
54
55 off_t zebra_record_int_seek (void *fh, off_t offset)
56 {
57     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
58     return (off_t) (fc->record_int_pos = offset);
59 }
60
61 off_t zebra_record_int_tell (void *fh)
62 {
63     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
64     return (off_t) fc->record_int_pos;
65 }
66
67 int zebra_record_int_read (void *fh, char *buf, size_t count)
68 {
69     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
70     int l = fc->record_int_len - fc->record_int_pos;
71     if (l <= 0)
72         return 0;
73     l = (l < (int) count) ? l : count;
74     memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
75     fc->record_int_pos += l;
76     return l;
77 }
78
79 void zebra_record_int_end (void *fh, off_t off)
80 {
81     struct zebra_fetch_control *fc = (struct zebra_fetch_control *) fh;
82     fc->offset_end = off;
83 }
84
85 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
86                         oid_value input_format, Z_RecordComposition *comp,
87                         oid_value *output_format, char **rec_bufp,
88                         int *rec_lenp, char **basenamep)
89 {
90     Record rec;
91     char *fname, *file_type, *basename;
92     RecType rt;
93     struct recRetrieveCtrl retrieveCtrl;
94     char subType[128];
95     struct zebra_fetch_control fc;
96     RecordAttr *recordAttr;
97     void *clientData;
98     int raw_mode = 0;
99
100     if (comp && comp->which == Z_RecordComp_simple &&
101         comp->u.simple->which == Z_ElementSetNames_generic && 
102         !strcmp (comp->u.simple->u.generic, "_sysno_"))
103     {
104         char rec_str[60];
105         sprintf(rec_str, "%d", sysno);
106         *output_format = VAL_SUTRS;
107         *rec_lenp = strlen(rec_str);
108         *rec_bufp = odr_strdup(stream, rec_str);
109         return 0;
110     }
111     rec = rec_get (zh->reg->records, sysno);
112     if (!rec)
113     {
114         logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
115         *basenamep = 0;
116         return 14;
117     }
118     recordAttr = rec_init_attr (zh->reg->zei, rec);
119
120     file_type = rec->info[recInfo_fileType];
121     fname = rec->info[recInfo_filename];
122     basename = rec->info[recInfo_databaseName];
123     *basenamep = (char *) odr_malloc (stream, strlen(basename)+1);
124     strcpy (*basenamep, basename);
125
126     if (comp && comp->which == Z_RecordComp_simple &&
127         comp->u.simple->which == Z_ElementSetNames_generic)
128     {
129         if (!strcmp (comp->u.simple->u.generic, "R"))
130             raw_mode = 1;
131     }
132     if (!(rt = recType_byName (zh->reg->recTypes,
133                                file_type, subType, &clientData)))
134     {
135         logf (LOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
136         return 14;
137     }
138     logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
139     retrieveCtrl.fh = &fc;
140     fc.fd = -1;
141     retrieveCtrl.fname = fname;
142     if (rec->size[recInfo_storeData] > 0)
143     {
144         retrieveCtrl.readf = zebra_record_int_read;
145         retrieveCtrl.seekf = zebra_record_int_seek;
146         retrieveCtrl.tellf = zebra_record_int_tell;
147         fc.record_int_len = rec->size[recInfo_storeData];
148         fc.record_int_buf = rec->info[recInfo_storeData];
149         fc.record_int_pos = 0;
150         logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
151         if (raw_mode)
152         {
153             *output_format = VAL_SUTRS;
154             *rec_lenp = rec->size[recInfo_storeData];
155             *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
156             memcpy(*rec_bufp, rec->info[recInfo_storeData], *rec_lenp);
157             rec_rm (&rec);
158             return 0;
159         }
160     }
161     else
162     {
163         char full_rep[1024];
164
165         if (zh->path_reg && !yaz_is_abspath (fname))
166         {
167             strcpy (full_rep, zh->path_reg);
168             strcat (full_rep, "/");
169             strcat (full_rep, fname);
170         }
171         else
172             strcpy (full_rep, fname);
173
174         if ((fc.fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
175         {
176             logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
177                   full_rep);
178             rec_rm (&rec);
179             return 14;
180         }
181         fc.record_offset = recordAttr->recordOffset;
182
183         retrieveCtrl.readf = zebra_record_ext_read;
184         retrieveCtrl.seekf = zebra_record_ext_seek;
185         retrieveCtrl.tellf = zebra_record_ext_tell;
186
187         zebra_record_ext_seek (retrieveCtrl.fh, 0);
188         if (raw_mode)
189         {
190             *output_format = VAL_SUTRS;
191             *rec_lenp = recordAttr->recordSize;
192             *rec_bufp = (char *) odr_malloc(stream, *rec_lenp);
193             zebra_record_ext_read(&fc, *rec_bufp, *rec_lenp);
194             rec_rm (&rec);
195             close (fc.fd);
196             return 0;
197         }
198     }
199     retrieveCtrl.subType = subType;
200     retrieveCtrl.localno = sysno;
201     retrieveCtrl.score = score;
202     retrieveCtrl.recordSize = recordAttr->recordSize;
203     retrieveCtrl.odr = stream;
204     retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
205     retrieveCtrl.comp = comp;
206     retrieveCtrl.encoding = zh->record_encoding;
207     retrieveCtrl.diagnostic = 0;
208     retrieveCtrl.dh = zh->reg->dh;
209     retrieveCtrl.res = zh->res;
210     retrieveCtrl.rec_buf = 0;
211     retrieveCtrl.rec_len = -1;
212     
213     (*rt->retrieve)(clientData, &retrieveCtrl);
214     *output_format = retrieveCtrl.output_format;
215     *rec_bufp = (char *) retrieveCtrl.rec_buf;
216     *rec_lenp = retrieveCtrl.rec_len;
217     if (fc.fd != -1)
218         close (fc.fd);
219     rec_rm (&rec);
220
221     return retrieveCtrl.diagnostic;
222 }