1 /* $Id: rstemp.c,v 1.33 2002-08-02 19:26:57 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
33 #include <sys/types.h>
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);
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_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 */
76 int (*cmp)(const void *p1, const void *p2);
79 struct rset_temp_rfd {
80 struct rset_temp_info *info;
81 struct rset_temp_rfd *next;
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);
102 info->cmp = temp_parms->cmp;
103 if (!temp_parms->temp_path)
104 info->temp_path = NULL;
107 info->temp_path = (char *) xmalloc (strlen(temp_parms->temp_path)+1);
108 strcpy (info->temp_path, temp_parms->temp_path);
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;
117 static RSFD r_open (RSET ct, int flag)
119 struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
120 struct rset_temp_rfd *rfd;
122 assert (info->fd == -1);
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));
139 rfd->countp = &ct->rset_terms[0]->count;
140 rfd->buf = xmalloc (info->key_size);
146 flush current window to file if file is assocated with set
148 static void r_flush (RSFD rfd, int mk)
150 struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info;
152 if (!info->fname && mk)
158 sprintf (template, "%s/zrsXXXXXX", info->temp_path);
160 sprintf (template, "zrsXXXXXX");
162 info->fd = mkstemp (template);
166 logf (LOG_FATAL|LOG_ERRNO, "mkstemp %s", template);
169 info->fname = (char *) xmalloc (strlen(template)+1);
170 strcpy (info->fname, template);
172 char *s = (char*) tempnam (info->temp_path, "zrs");
173 info->fname = (char *) xmalloc (strlen(s)+1);
174 strcpy (info->fname, s);
176 logf (LOG_DEBUG, "creating tempfile %s", info->fname);
177 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
180 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
185 if (info->fname && info->fd != -1 && info->dirty)
190 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
192 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
195 count = info->buf_size;
196 if (count > info->pos_end - info->pos_buf)
197 count = info->pos_end - info->pos_buf;
198 if ((r = write (info->fd, info->buf_mem, count)) < (int) count)
201 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
203 logf (LOG_FATAL, "write of %ld but got %ld",
204 (long) count, (long) r);
211 static void r_close (RSFD rfd)
213 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
216 if (info->fname && info->fd != -1)
221 xfree (((struct rset_temp_rfd *)rfd)->buf);
225 static void r_delete (RSET ct)
227 struct rset_temp_info *info = (struct rset_temp_info*) ct->buf;
230 unlink (info->fname);
231 xfree (info->buf_mem);
232 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
235 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
236 unlink (info->fname);
240 xfree (info->temp_path);
241 rset_term_destroy (ct->rset_terms[0]);
242 xfree (ct->rset_terms);
247 read from file to window if file is assocated with set -
250 static void r_reread (RSFD rfd)
252 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
259 info->pos_border = info->pos_cur + info->buf_size;
260 if (info->pos_border > info->pos_end)
261 info->pos_border = info->pos_end;
262 count = info->pos_border - info->pos_buf;
265 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
267 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
270 if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
273 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
275 logf (LOG_FATAL, "read of %ld but got %ld",
276 (long) count, (long) r);
282 info->pos_border = info->pos_end;
285 static void r_rewind (RSFD rfd)
287 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
295 static int r_count (RSET ct)
297 struct rset_temp_info *info = (struct rset_temp_info *) ct->buf;
299 return info->pos_end / info->key_size;
302 static int r_read (RSFD rfd, void *buf, int *term_index)
304 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd;
305 struct rset_temp_info *info = mrfd->info;
307 size_t nc = info->pos_cur + info->key_size;
309 if (nc > info->pos_border)
311 if (nc > info->pos_end)
314 info->pos_buf = info->pos_cur;
317 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
322 if (*mrfd->countp == 0 || (*info->cmp)(buf, mrfd->buf) > 1)
324 memcpy (mrfd->buf, buf, mrfd->info->key_size);
330 static int r_write (RSFD rfd, const void *buf)
332 struct rset_temp_info *info = ((struct rset_temp_rfd*)rfd)->info;
334 size_t nc = info->pos_cur + info->key_size;
336 if (nc > info->pos_buf + info->buf_size)
339 info->pos_buf = info->pos_cur;
340 if (info->pos_buf < info->pos_end)
344 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
347 if (nc > info->pos_end)
348 info->pos_border = info->pos_end = nc;