1 /* $Id: rstemp.c,v 1.49 2004-09-09 10:08:06 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 {
62 int fd; /* file descriptor for temp file */
63 char *fname; /* name of temp file */
64 char *buf_mem; /* window buffer */
65 size_t buf_size; /* size of window */
66 size_t pos_end; /* last position in set */
67 size_t pos_buf; /* position of first byte in window */
68 size_t pos_border; /* position of last byte+1 in window */
69 int dirty; /* window is dirty */
70 zint hits; /* no of hits */
74 struct rset_temp_rfd {
76 size_t pos_cur; /* current position in set */
77 /* FIXME - term pos or what ?? */
78 zint cur; /* number of the current hit */
81 RSET rstemp_create( NMEM nmem, const struct key_control *kcontrol,
83 const char *temp_path)
85 RSET rnew=rset_create_base(&control, nmem, kcontrol, scope);
86 struct rset_temp_info *info;
88 info = (struct rset_temp_info *) nmem_malloc(rnew->nmem, sizeof(*info));
91 info->buf_size = 4096;
92 info->buf_mem = (char *) nmem_malloc (rnew->nmem, info->buf_size);
99 info->temp_path = NULL;
101 info->temp_path = nmem_strdup(rnew->nmem,temp_path);
104 } /* rstemp_create */
106 static void r_delete (RSET ct)
108 struct rset_temp_info *info = (struct rset_temp_info*) ct->priv;
110 logf (LOG_DEBUG, "r_delete: set size %ld", (long) info->pos_end);
113 logf (LOG_DEBUG, "r_delete: unlink %s", info->fname);
114 unlink (info->fname);
119 static RSFD r_open (RSET ct, int flag)
121 struct rset_temp_info *info = (struct rset_temp_info *) ct->priv;
123 struct rset_temp_rfd *prfd;
125 if (info->fd == -1 && info->fname)
127 if (flag & RSETF_WRITE)
128 info->fd = open (info->fname, O_BINARY|O_RDWR|O_CREAT, 0666);
130 info->fd = open (info->fname, O_BINARY|O_RDONLY);
133 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
137 rfd = rfd_create_base(ct);
139 prfd= (struct rset_temp_rfd *) nmem_malloc(ct->nmem, sizeof(*prfd));
140 rfd->priv=(void *)prfd;
141 prfd->buf = nmem_malloc (ct->nmem,ct->keycontrol->key_size);
148 flush current window to file if file is assocated with set
150 static void r_flush (RSFD rfd, int mk)
152 /* struct rset_temp_info *info = ((struct rset_temp_rfd*) rfd)->info; */
153 struct rset_temp_info *info = rfd->rset->priv;
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= nmem_malloc(rfd->rset->nmem,strlen(template)+1);
173 strcpy (info->fname, template);
175 char *s = (char*) tempnam (info->temp_path, "zrs");
176 info->fname= nmem_malloc(rfd->rset->nmem,strlen(template)+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_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv; */
217 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
219 if (info->fname && info->fd != -1)
223 } /* FIXME - Is this right, don't we risk closing the file too early ?*/
224 rfd_delete_base(rfd);
229 read from file to window if file is assocated with set -
232 static void r_reread (RSFD rfd)
234 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
235 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
242 info->pos_border = mrfd->pos_cur +
244 if (info->pos_border > info->pos_end)
245 info->pos_border = info->pos_end;
246 count = info->pos_border - info->pos_buf;
249 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
251 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
254 if ((r = read (info->fd, info->buf_mem, count)) < (int) count)
257 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
259 logf (LOG_FATAL, "read of %ld but got %ld",
260 (long) count, (long) r);
266 info->pos_border = info->pos_end;
269 static void r_rewind (RSFD rfd)
271 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) (rfd->priv);
272 struct rset_temp_info *info = (struct rset_temp_info *)(rfd->rset->priv);
280 static int r_read (RSFD rfd, void *buf)
282 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
283 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
285 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
287 if (mrfd->pos_cur < info->pos_buf || nc > info->pos_border)
289 if (nc > info->pos_end)
292 info->pos_buf = mrfd->pos_cur;
295 memcpy (buf, info->buf_mem + (mrfd->pos_cur - info->pos_buf),
296 rfd->rset->keycontrol->key_size);
302 static int r_write (RSFD rfd, const void *buf)
304 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
305 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
307 size_t nc = mrfd->pos_cur + rfd->rset->keycontrol->key_size;
309 if (nc > info->pos_buf + info->buf_size)
312 info->pos_buf = mrfd->pos_cur;
313 if (info->pos_buf < info->pos_end)
317 memcpy (info->buf_mem + (mrfd->pos_cur - info->pos_buf), buf,
318 rfd->rset->keycontrol->key_size);
320 if (nc > info->pos_end)
321 info->pos_border = info->pos_end = nc;
326 static void r_pos (RSFD rfd, double *current, double *total)
328 /* struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd; */
329 struct rset_temp_rfd *mrfd = (struct rset_temp_rfd*) rfd->priv;
330 struct rset_temp_info *info = (struct rset_temp_info *)rfd->rset->priv;
332 *current=(double) mrfd->cur;
333 *total=(double) info->hits;