1 /* $Id: rstemp.c,v 1.46 2004-08-26 11:11:59 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_read (RSFD rfd, void *buf);
42 static int r_write (RSFD rfd, const void *buf);
43 static void r_pos (RSFD rfd, double *current, double *total);
46 static const struct rset_control control =
59 const struct rset_control *rset_kind_temp = &control;
61 struct rset_temp_info {
64 size_t key_size; /* key size */
65 char *buf_mem; /* window buffer */
66 size_t buf_size; /* size of window */
67 size_t pos_end; /* last position in set */
68 size_t pos_buf; /* position of first byte in window */
69 size_t pos_border; /* position of last byte+1 in window */
70 int dirty; /* window is dirty */
71 zint hits; /* no of hits */
73 int (*cmp)(const void *p1, const void *p2);
74 struct rset_temp_rfd *rfd_list; /* rfds in use */
75 struct rset_temp_rfd *free_list; /* fully alloc'd rfds waiting for reuse*/
78 struct rset_temp_rfd {
79 struct rset_temp_info *info;
80 struct rset_temp_rfd *next;
82 size_t pos_cur; /* current position in set */
83 zint cur; /* number of the current hit */
86 RSET rstemp_create( NMEM nmem, int key_size,
87 int (*cmp)(const void *p1, const void *p2),
88 const char *temp_path)
90 RSET rnew=rset_create_base(&control, nmem);
91 struct rset_temp_info *info;
93 info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
96 info->key_size = key_size;
97 info->buf_size = 4096;
98 info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
104 info->rfd_list = NULL;
105 info->free_list = NULL;
108 info->temp_path = NULL;
110 info->temp_path = nmem_strdup(rnew->nmem,temp_path);
113 } /* rstemp_create */
115 static void r_delete (RSET ct)
117 struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
119 xfree (info->buf_mem);
120 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
123 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
124 unlink (info->fname);
125 xfree (info->fname); /* FIXME should be nmem'd, and not freed here */
129 xfree (info->temp_path);
135 static RSFD r_open (RSET ct, int flag)
137 struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
138 struct rset_temp_rfd *rfd;
140 if (info->fd == -1 && info->fname)
142 if (flag & RSETF_WRITE)
143 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
145 info->fd = open (info->fname, O_BINARY|O_RDONLY);
148 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
152 rfd = info->free_list;
154 info->free_list=rfd->next;
156 rfd = (struct rset_temp_rfd *) xmalloc (sizeof(*rfd));
157 rfd->buf = xmalloc (info->key_size);
159 rfd->next = info->rfd_list;
160 info->rfd_list = rfd;
168 flush current window to file if file is assocated with set
170 static void r_flush (RSFD rfd, int mk)
172 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
174 if (!info->fname && mk)
180 sprintf (template, "%s/zrsXXXXXX", info->temp_path);
182 sprintf (template, "zrsXXXXXX");
184 info->fd = mkstemp (template);
188 logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
191 info->fname = (char *) xmalloc (strlen(template)+1);
192 strcpy (info->fname, template);
194 char *s = (char*) tempnam (info->temp_path, "zrs");
195 info->fname = (char *) xmalloc (strlen(s)+1);
196 strcpy (info->fname, s);
198 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
199 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
202 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
207 if (info->fname && info->fd != -1 && info->dirty)
212 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
214 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
217 count = info->buf_size;
218 if (count > info->pos_end - info->pos_buf)
219 count = info->pos_end - info->pos_buf;
220 if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
223 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
225 logf (LOG_FATAL, "write of %ld but got %ld",
226 (long) count, (long) r);
233 static void r_close (RSFD rfd)
235 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
236 struct rset_temp_rfd **rfdp;
238 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
241 struct rset_temp_rfd *rfd_tmp=*rfdp;
243 *rfdp = (*rfdp)->next;
244 rfd_tmp->next=info->free_list;
245 info->free_list=rfd_tmp;
247 if (!info->rfd_list && info->fname && info->fd != -1)
254 logf (LOG_FATAL, "r_close but no rfd match!");
260 read from file to window if file is assocated with set -
263 static void r_reread (RSFD rfd)
265 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
272 info->pos_border = ((struct rset_temp_rfd *)rfd)->pos_cur +
274 if (info->pos_border > info->pos_end)
275 info->pos_border = info->pos_end;
276 count = info->pos_border - info->pos_buf;
279 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
281 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
284 if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
287 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
289 logf (LOG_FATAL, "read of %ld but got %ld",
290 (long) count, (long) r);
296 info->pos_border = info->pos_end;
299 static void r_rewind (RSFD rfd)
301 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
304 ((struct rset_temp_rfd *)rfd)->pos_cur = 0;
307 ((struct rset_temp_rfd *)rfd)->cur=0;
311 static int r_count (RSET ct)
313 struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
315 return info->pos_end / info->key_size;
318 static int r_read (RSFD rfd, void *buf)
320 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
321 struct rset_temp_info *info = mrfd->info;
323 size_t nc = mrfd->pos_cur + info->key_size;
325 if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
327 if (nc > info->pos_end)
330 info->pos_buf = mrfd->pos_cur;
333 memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
340 static int r_write (RSFD rfd, const void *buf)
342 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
343 struct rset_temp_info *info = mrfd->info;
345 size_t nc = mrfd->pos_cur + info->key_size;
347 if (nc > info->pos_buf + info->buf_size)
350 info->pos_buf = mrfd->pos_cur;
351 if (info->pos_buf < info->pos_end)
355 memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
358 if (nc > info->pos_end)
359 info->pos_border = info->pos_end = nc;
364 static void r_pos (RSFD rfd, double *current, double *total)
366 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
367 *current=(double) mrfd->cur;
368 *total=(double) mrfd->info->hits;