1 /* $Id: rstemp.c,v 1.45 2004-08-24 14:25:16 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
5 This file is part of the Zebra server.
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
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
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
31 #include <sys/types.h>
37 static RSFD r_open (RSET ct, int flag);
38 static void r_close (RSFD rfd);
39 static void r_delete (RSET ct);
40 static void r_rewind (RSFD rfd);
41 /* static int r_count (RSET ct);*/
42 static int r_read (RSFD rfd, void *buf);
43 static int r_write (RSFD rfd, const void *buf);
44 static void r_pos (RSFD rfd, double *current, double *total);
46 /* FIXME - Use the nmem instead of xmalloc all the way through */
48 static const struct rset_control control =
61 const struct rset_control *rset_kind_temp = &control;
63 struct rset_temp_info {
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_buf; /* position of first byte in window */
71 size_t pos_border; /* position of last byte+1 in window */
72 int dirty; /* window is dirty */
73 zint hits; /* no of hits */
75 int (*cmp)(const void *p1, const void *p2);
76 struct rset_temp_rfd *rfd_list;
79 struct rset_temp_rfd {
80 struct rset_temp_info *info;
81 struct rset_temp_rfd *next;
83 size_t pos_cur; /* current position in set */
84 zint cur; /* number of the current hit */
87 RSET rstemp_create( NMEM nmem, int key_size,
88 int (*cmp)(const void *p1, const void *p2),
89 const char *temp_path)
91 RSET rnew=rset_create_base(&control, nmem);
92 struct rset_temp_info *info;
94 info = (struct rset_temp_info *) nmem_malloc ( rnew->nmem, sizeof(*info));
97 info->key_size = key_size;
98 info->buf_size = 4096;
99 info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
105 info->rfd_list = NULL;
108 info->temp_path = NULL;
110 info->temp_path = nmem_strdup(rnew->nmem,temp_path);
113 } /* rstemp_create */
116 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
118 rset_temp_parms *temp_parms = (rset_temp_parms *) parms;
119 struct rset_temp_info *info;
121 info = (struct rset_temp_info *) xmalloc (sizeof(struct rset_temp_info));
124 info->key_size = temp_parms->key_size;
125 info->buf_size = 4096;
126 info->buf_mem = (char *) xmalloc (info->buf_size);
131 info->cmp = temp_parms->cmp;
132 info->rfd_list = NULL;
134 if (!temp_parms->temp_path)
135 info->temp_path = NULL;
138 info->temp_path = (char *) xmalloc (strlen(temp_parms->temp_path)+1);
139 strcpy (info->temp_path, temp_parms->temp_path);
146 static void r_delete (RSET ct)
148 struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
150 xfree (info->buf_mem);
151 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
154 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
155 unlink (info->fname);
156 xfree (info->fname); /* FIXME should be nmem'd, and not freed here */
160 xfree (info->temp_path);
166 static RSFD r_open (RSET ct, int flag)
168 struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
169 struct rset_temp_rfd *rfd;
171 if (info->fd == -1 && info->fname)
173 if (flag & RSETF_WRITE)
174 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
176 info->fd = open (info->fname, O_BINARY|O_RDONLY);
179 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
183 rfd = (struct rset_temp_rfd *) xmalloc (sizeof(*rfd));
184 rfd->next = info->rfd_list;
185 info->rfd_list = rfd;
189 rfd->buf = xmalloc (info->key_size);
195 flush current window to file if file is assocated with set
197 static void r_flush (RSFD rfd, int mk)
199 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
201 if (!info->fname && mk)
207 sprintf (template, "%s/zrsXXXXXX", info->temp_path);
209 sprintf (template, "zrsXXXXXX");
211 info->fd = mkstemp (template);
215 logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
218 info->fname = (char *) xmalloc (strlen(template)+1);
219 strcpy (info->fname, template);
221 char *s = (char*) tempnam (info->temp_path, "zrs");
222 info->fname = (char *) xmalloc (strlen(s)+1);
223 strcpy (info->fname, s);
225 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
226 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
229 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
234 if (info->fname && info->fd != -1 && info->dirty)
239 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
241 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
244 count = info->buf_size;
245 if (count > info->pos_end - info->pos_buf)
246 count = info->pos_end - info->pos_buf;
247 if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
250 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
252 logf (LOG_FATAL, "write of %ld but got %ld",
253 (long) count, (long) r);
260 static void r_close (RSFD rfd)
262 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
263 struct rset_temp_rfd **rfdp;
265 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
269 xfree ((*rfdp)->buf);
271 *rfdp = (*rfdp)->next;
274 if (!info->rfd_list && info->fname && info->fd != -1)
281 logf (LOG_FATAL, "r_close but no rfd match!");
287 read from file to window if file is assocated with set -
290 static void r_reread (RSFD rfd)
292 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
299 info->pos_border = ((struct rset_temp_rfd *)rfd)->pos_cur +
301 if (info->pos_border > info->pos_end)
302 info->pos_border = info->pos_end;
303 count = info->pos_border - info->pos_buf;
306 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
308 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
311 if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
314 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
316 logf (LOG_FATAL, "read of %ld but got %ld",
317 (long) count, (long) r);
323 info->pos_border = info->pos_end;
326 static void r_rewind (RSFD rfd)
328 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
331 ((struct rset_temp_rfd *)rfd)->pos_cur = 0;
334 ((struct rset_temp_rfd *)rfd)->cur=0;
338 static int r_count (RSET ct)
340 struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
342 return info->pos_end / info->key_size;
345 static int r_read (RSFD rfd, void *buf)
347 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
348 struct rset_temp_info *info = mrfd->info;
350 size_t nc = mrfd->pos_cur + info->key_size;
352 if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
354 if (nc > info->pos_end)
357 info->pos_buf = mrfd->pos_cur;
360 memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
367 static int r_write (RSFD rfd, const void *buf)
369 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
370 struct rset_temp_info *info = mrfd->info;
372 size_t nc = mrfd->pos_cur + info->key_size;
374 if (nc > info->pos_buf + info->buf_size)
377 info->pos_buf = mrfd->pos_cur;
378 if (info->pos_buf < info->pos_end)
382 memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
385 if (nc > info->pos_end)
386 info->pos_border = info->pos_end = nc;
391 static void r_pos (RSFD rfd, double *current, double *total)
393 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
394 *current=(double) mrfd->cur;
395 *total=(double) mrfd->info->hits;