1 /* $Id: rstemp.c,v 1.36 2004-01-16 15:27:35 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 void *r_create(RSET ct, const struct rset_control *sel, void *parms);
38 static RSFD r_open (RSET ct, int flag);
39 static void r_close (RSFD rfd);
40 static void r_delete (RSET ct);
41 static void r_rewind (RSFD rfd);
42 static int r_count (RSET ct);
43 static int r_read (RSFD rfd, void *buf, int *term_index);
44 static int r_write (RSFD rfd, const void *buf);
46 static const struct rset_control control =
60 const struct rset_control *rset_kind_temp = &control;
62 struct rset_temp_info {
65 size_t key_size; /* key size */
66 char *buf_mem; /* window buffer */
67 size_t buf_size; /* size of window */
68 size_t pos_end; /* last position in set */
69 size_t pos_buf; /* position of first byte in window */
70 size_t pos_border; /* position of last byte+1 in window */
71 int dirty; /* window is dirty */
72 int hits; /* no of hits */
74 int (*cmp)(const void *p1, const void *p2);
75 struct rset_temp_rfd *rfd_list;
78 struct rset_temp_rfd {
79 struct rset_temp_info *info;
80 struct rset_temp_rfd *next;
83 size_t pos_cur; /* current position in set */
86 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
88 rset_temp_parms *temp_parms = (rset_temp_parms *) parms;
89 struct rset_temp_info *info;
91 info = (struct rset_temp_info *) xmalloc (sizeof(struct rset_temp_info));
94 info->key_size = temp_parms->key_size;
95 info->buf_size = 4096;
96 info->buf_mem = (char *) xmalloc (info->buf_size);
101 info->cmp = temp_parms->cmp;
102 info->rfd_list = NULL;
104 if (!temp_parms->temp_path)
105 info->temp_path = NULL;
108 info->temp_path = (char *) xmalloc (strlen(temp_parms->temp_path)+1);
109 strcpy (info->temp_path, temp_parms->temp_path);
111 ct->no_rset_terms = 1;
112 ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
113 ct->rset_terms[0] = temp_parms->rset_term;
118 static RSFD r_open (RSET ct, int flag)
120 struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
121 struct rset_temp_rfd *rfd;
123 if (info->fd == -1 && info->fname)
125 if (flag & RSETF_WRITE)
126 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
128 info->fd = open (info->fname, O_BINARY|O_RDONLY);
131 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
135 rfd = (struct rset_temp_rfd *) xmalloc (sizeof(*rfd));
136 rfd->next = info->rfd_list;
137 info->rfd_list = rfd;
141 rfd->countp = &ct->rset_terms[0]->count;
143 rfd->buf = xmalloc (info->key_size);
149 flush current window to file if file is assocated with set
151 static void r_flush (RSFD rfd, int mk)
153 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
155 if (!info->fname && mk)
161 sprintf (template, "%s/zrsXXXXXX", info->temp_path);
163 sprintf (template, "zrsXXXXXX");
165 info->fd = mkstemp (template);
169 logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
172 info->fname = (char *) xmalloc (strlen(template)+1);
173 strcpy (info->fname, template);
175 char *s = (char*) tempnam (info->temp_path, "zrs");
176 info->fname = (char *) xmalloc (strlen(s)+1);
177 strcpy (info->fname, s);
179 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
180 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
183 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
188 if (info->fname && info->fd != -1 && info->dirty)
193 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
195 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
198 count = info->buf_size;
199 if (count > info->pos_end - info->pos_buf)
200 count = info->pos_end - info->pos_buf;
201 if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
204 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
206 logf (LOG_FATAL, "write of %ld but got %ld",
207 (long) count, (long) r);
214 static void r_close (RSFD rfd)
216 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
217 struct rset_temp_rfd **rfdp;
219 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
223 xfree ((*rfdp)->buf);
225 *rfdp = (*rfdp)->next;
228 if (!info->rfd_list && info->fname && info->fd != -1)
235 logf (LOG_FATAL, "r_close but no rfd match!");
239 static void r_delete (RSET ct)
241 struct rset_temp_info *info = (struct rset_temp_info*) ct->buf;
244 unlink (info->fname);
245 xfree (info->buf_mem);
246 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
249 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
250 unlink (info->fname);
254 xfree (info->temp_path);
255 rset_term_destroy (ct->rset_terms[0]);
256 xfree (ct->rset_terms);
261 read from file to window if file is assocated with set -
264 static void r_reread (RSFD rfd)
266 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
273 info->pos_border = ((struct rset_temp_rfd *)rfd)->pos_cur +
275 if (info->pos_border > info->pos_end)
276 info->pos_border = info->pos_end;
277 count = info->pos_border - info->pos_buf;
280 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
282 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
285 if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
288 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
290 logf (LOG_FATAL, "read of %ld but got %ld",
291 (long) count, (long) r);
297 info->pos_border = info->pos_end;
300 static void r_rewind (RSFD rfd)
302 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
305 ((struct rset_temp_rfd *)rfd)->pos_cur = 0;
310 static int r_count (RSET ct)
312 struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
314 return info->pos_end / info->key_size;
317 static int r_read (RSFD rfd, void *buf, int *term_index)
319 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
320 struct rset_temp_info *info = mrfd->info;
322 size_t nc = mrfd->pos_cur + info->key_size;
324 if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
326 if (nc > info->pos_end)
329 info->pos_buf = mrfd->pos_cur;
332 memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
337 if (*mrfd->countp == 0 || (*info->cmp)(buf, mrfd->buf) > 1)
339 memcpy (mrfd->buf, buf, mrfd->info->key_size);
345 static int r_write (RSFD rfd, const 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 (nc > info->pos_buf + info->buf_size)
355 info->pos_buf = mrfd->pos_cur;
356 if (info->pos_buf < info->pos_end)
360 memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
363 if (nc > info->pos_end)
364 info->pos_border = info->pos_end = nc;