Fix uninit. var
[idzebra-moved-to-github.git] / rset / rstemp.c
1 /* $Id: rstemp.c,v 1.34 2003-03-18 08:53:35 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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
24
25 #include <fcntl.h>
26 #include <assert.h>
27 #ifdef WIN32
28 #include <io.h>
29 #else
30 #include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <sys/types.h>
34 #include <stdio.h>
35
36 #include <zebrautl.h>
37 #include <rstemp.h>
38
39 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
40 static RSFD r_open (RSET ct, int flag);
41 static void r_close (RSFD rfd);
42 static void r_delete (RSET ct);
43 static void r_rewind (RSFD rfd);
44 static int r_count (RSET ct);
45 static int r_read (RSFD rfd, void *buf, int *term_index);
46 static int r_write (RSFD rfd, const void *buf);
47
48 static const struct rset_control control = 
49 {
50     "temp",
51     r_create,
52     r_open,
53     r_close,
54     r_delete,
55     r_rewind,
56     r_count,
57     r_read,
58     r_write,
59 };
60
61 const struct rset_control *rset_kind_temp = &control;
62
63 struct rset_temp_info {
64     int     fd;
65     char   *fname;
66     size_t  key_size;      /* key size */
67     char   *buf_mem;       /* window buffer */
68     size_t  buf_size;      /* size of window */
69     size_t  pos_end;       /* last position in set */
70     size_t  pos_cur;       /* current position in set */
71     size_t  pos_buf;       /* position of first byte in window */
72     size_t  pos_border;    /* position of last byte+1 in window */
73     int     dirty;         /* window is dirty */
74     int     hits;          /* no of hits */
75     char   *temp_path;
76     int     (*cmp)(const void *p1, const void *p2);
77 };
78
79 struct rset_temp_rfd {
80     struct rset_temp_info *info;
81     struct rset_temp_rfd *next;
82     int *countp;
83     void *buf;
84 };
85
86 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
87 {
88     rset_temp_parms *temp_parms = (rset_temp_parms *) parms;
89     struct rset_temp_info *info;
90    
91     info = (struct rset_temp_info *) xmalloc (sizeof(struct rset_temp_info));
92     info->fd = -1;
93     info->fname = NULL;
94     info->key_size = temp_parms->key_size;
95     info->buf_size = 4096;
96     info->buf_mem = (char *) xmalloc (info->buf_size);
97     info->pos_cur = 0;
98     info->pos_end = 0;
99     info->pos_buf = 0;
100     info->dirty = 0;
101     info->hits = -1;
102     info->cmp = temp_parms->cmp;
103     if (!temp_parms->temp_path)
104         info->temp_path = NULL;
105     else
106     {
107         info->temp_path = (char *) xmalloc (strlen(temp_parms->temp_path)+1);
108         strcpy (info->temp_path, temp_parms->temp_path);
109     }
110     ct->no_rset_terms = 1;
111     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
112     ct->rset_terms[0] = temp_parms->rset_term;
113
114     return info;
115 }
116
117 static RSFD r_open (RSET ct, int flag)
118 {
119     struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
120     struct rset_temp_rfd *rfd;
121
122     assert (info->fd == -1);
123     if (info->fname)
124     {
125         if (flag & RSETF_WRITE)
126             info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
127         else
128             info->fd = open (info->fname, O_BINARY|O_RDONLY);
129         if (info->fd == -1)
130         {
131             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
132             exit (1);
133         }
134     }
135     rfd = (struct rset_temp_rfd *) xmalloc (sizeof(*rfd));
136     rfd->info = info;
137     r_rewind (rfd);
138
139     rfd->countp = &ct->rset_terms[0]->count;
140     *rfd->countp = 0;
141     rfd->buf = xmalloc (info->key_size);
142
143     return rfd;
144 }
145
146 /* r_flush:
147       flush current window to file if file is assocated with set
148  */
149 static void r_flush (RSFD rfd, int mk)
150 {
151     struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
152
153     if (!info->fname && mk)
154     {
155 #if HAVE_MKSTEMP
156         char template[1024];
157
158         if (info->temp_path)
159             sprintf (template, "%s/zrsXXXXXX", info->temp_path);
160         else
161             sprintf (template, "zrsXXXXXX");
162
163         info->fd = mkstemp (template);
164
165         if (info->fd == -1)
166         {
167             logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
168             exit (1);
169         }
170         info->fname = (char *) xmalloc (strlen(template)+1);
171         strcpy (info->fname, template);
172 #else
173         char *s = (char*) tempnam (info->temp_path, "zrs");
174         info->fname = (char *) xmalloc (strlen(s)+1);
175         strcpy (info->fname, s);
176
177         logf (LOG_DEBUG, "creating tempfile %s", info->fname);
178         info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
179         if (info->fd == -1)
180         {
181             logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
182             exit (1);
183         }
184 #endif
185     }
186     if (info->fname && info->fd != -1 && info->dirty)
187     {
188         size_t count;
189         int r;
190         
191         if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
192         {
193             logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
194             exit (1);
195         }
196         count = info->buf_size;
197         if (count > info->pos_end - info->pos_buf)
198             count = info->pos_end - info->pos_buf;
199         if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
200         {
201             if (r == -1)
202                 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
203             else
204                 logf (LOG_FATAL, "write of %ld but got %ld",
205                       (long) count, (long) r);
206             exit (1);
207         }
208         info->dirty = 0;
209     }
210 }
211
212 static void r_close (RSFD rfd)
213 {
214     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
215
216     r_flush (rfd, 0);
217     if (info->fname && info->fd != -1)
218     {
219         close (info->fd);
220         info->fd = -1;
221     }
222     xfree (((struct rset_temp_rfd *)rfd)->buf);
223     xfree (rfd);
224 }
225
226 static void r_delete (RSET ct)
227 {
228     struct rset_temp_info *info = (struct rset_temp_info*) ct->buf;
229
230     if (info->fname)
231         unlink (info->fname);        
232     xfree (info->buf_mem);
233     logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
234     if (info->fname)
235     {
236         logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
237         unlink (info->fname);
238         xfree (info->fname);
239     }
240     if (info->temp_path)
241         xfree (info->temp_path);
242     rset_term_destroy (ct->rset_terms[0]);
243     xfree (ct->rset_terms);
244     xfree (info);
245 }
246
247 /* r_reread:
248       read from file to window if file is assocated with set -
249       indicated by fname
250  */
251 static void r_reread (RSFD rfd)
252 {
253     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
254
255     if (info->fname)
256     {
257         size_t count;
258         int r;
259
260         info->pos_border = info->pos_cur + info->buf_size;
261         if (info->pos_border > info->pos_end)
262             info->pos_border = info->pos_end;
263         count = info->pos_border - info->pos_buf;
264         if (count > 0)
265         {
266             if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
267             {
268                 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
269                 exit (1);
270             }
271             if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
272             {
273                 if (r == -1)
274                     logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
275                 else
276                     logf (LOG_FATAL, "read of %ld but got %ld",
277                           (long) count, (long) r);
278                 exit (1);
279             }
280         }
281     }
282     else
283         info->pos_border = info->pos_end;
284 }
285
286 static void r_rewind (RSFD rfd)
287 {
288     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
289
290     r_flush (rfd, 0);
291     info->pos_cur = 0;
292     info->pos_buf = 0;
293     r_reread (rfd);
294 }
295
296 static int r_count (RSET ct)
297 {
298     struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
299
300     return info->pos_end / info->key_size;
301 }
302
303 static int r_read (RSFD rfd, void *buf, int *term_index)
304 {
305     struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
306     struct rset_temp_info *info = mrfd->info;
307
308     size_t nc = info->pos_cur + info->key_size;
309
310     if (nc > info->pos_border)
311     {
312         if (nc > info->pos_end)
313             return 0;
314         r_flush (rfd, 0);
315         info->pos_buf = info->pos_cur;
316         r_reread (rfd);
317     }
318     memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
319             info->key_size);
320     info->pos_cur = nc;
321     *term_index = 0;
322
323     if (*mrfd->countp == 0 || (*info->cmp)(buf, mrfd->buf) > 1)
324     {
325         memcpy (mrfd->buf, buf, mrfd->info->key_size);
326         (*mrfd->countp)++;
327     }
328     return 1;
329 }
330
331 static int r_write (RSFD rfd, const void *buf)
332 {
333     struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
334
335     size_t nc = info->pos_cur + info->key_size;
336
337     if (nc > info->pos_buf + info->buf_size)
338     {
339         r_flush (rfd, 1);
340         info->pos_buf = info->pos_cur;
341         if (info->pos_buf < info->pos_end)
342             r_reread (rfd);
343     }
344     info->dirty = 1;
345     memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
346             info->key_size);
347     info->pos_cur = nc;
348     if (nc > info->pos_end)
349         info->pos_border = info->pos_end = nc;
350     return 1;
351 }