2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-09-05 16:36:59 adam
10 * Revision 1.4 1995/09/05 11:43:24 adam
11 * Complete version of temporary sets. Not tested yet though.
13 * Revision 1.3 1995/09/04 15:20:40 adam
14 * More work on temp sets. is_open member removed.
16 * Revision 1.2 1995/09/04 09:10:56 adam
19 * Revision 1.1 1994/11/04 13:21:30 quinn
27 #include <sys/types.h>
33 static struct rset_control *r_create(const struct rset_control *sel,
35 static int r_open(struct rset_control *ct, int wflag);
36 static void r_close(struct rset_control *ct);
37 static void r_delete(struct rset_control *ct);
38 static void r_rewind(struct rset_control *ct);
39 static int r_count(struct rset_control *ct);
40 static int r_read (rset_control *ct, void *buf);
41 static int r_write (rset_control *ct, const void *buf);
43 static const rset_control control =
57 const rset_control *rset_kind_temp = &control;
59 struct rset_temp_private {
72 static struct rset_control *r_create(const struct rset_control *sel,
76 rset_temp_parms *temp_parms = parms;
77 struct rset_temp_private *info;
79 logf (LOG_DEBUG, "ritemp_create(%s)", sel->desc);
80 newct = xmalloc(sizeof(*newct));
81 memcpy(newct, sel, sizeof(*sel));
82 newct->buf = xmalloc (sizeof(struct rset_temp_private));
87 info->key_size = temp_parms->key_size;
88 info->buf_size = 1024;
89 info->buf_mem = xmalloc (info->buf_size);
98 static int r_open(struct rset_control *ct, int wflag)
100 struct rset_temp_private *info = ct->buf;
102 assert (info->fd == -1);
106 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
108 info->fd = open (info->fname, O_RDONLY);
111 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
119 static void r_flush (struct rset_control *ct, int mk)
121 struct rset_temp_private *info = ct->buf;
123 if (!info->fname && mk)
125 char *s = (char*) tempnam (NULL, "zrs");
127 info->fname = xmalloc (strlen(s)+1);
128 strcpy (info->fname, s);
130 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
133 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
137 if (info->fname && info->fd != -1 && info->dirty)
141 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
143 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
146 count = info->buf_size;
147 if (count > info->pos_end - info->pos_buf)
148 count = info->pos_end - info->pos_buf;
149 if ((r = write (info->fd, info->buf_mem, count)) < count)
152 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
154 logf (LOG_FATAL, "write of %ld but got %ld",
155 (long) count, (long) r);
162 static void r_close (struct rset_control *ct)
164 struct rset_temp_private *info = ct->buf;
167 if (info->fname && info->fd != -1)
174 static void r_delete (struct rset_control *ct)
176 struct rset_temp_private *info = ct->buf;
180 unlink (info->fname);
181 free (info->buf_mem);
186 static void r_reread (struct rset_control *ct)
188 struct rset_temp_private *info = ct->buf;
194 info->pos_border = info->pos_cur + info->buf_size;
195 if (info->pos_border > info->pos_end)
196 info->pos_border = info->pos_end;
197 count = info->pos_border - info->pos_buf;
199 if ((r = read (info->fd, info->buf_mem, count)) < count)
202 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
204 logf (LOG_FATAL, "read of %ld but got %ld",
205 (long) count, (long) r);
210 info->pos_border = info->pos_end;
213 static void r_rewind (struct rset_control *ct)
215 struct rset_temp_private *info = ct->buf;
223 static int r_count (struct rset_control *ct)
225 struct rset_temp_private *info = ct->buf;
227 return info->pos_end / info->key_size;
230 static int r_read (rset_control *ct, void *buf)
232 struct rset_temp_private *info = ct->buf;
233 size_t nc = info->pos_cur + info->key_size;
235 if (nc > info->pos_border)
237 if (nc > info->pos_end)
240 info->pos_buf = info->pos_cur;
243 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
249 static int r_write (rset_control *ct, const void *buf)
251 struct rset_temp_private *info = ct->buf;
252 size_t nc = info->pos_cur + info->key_size;
254 if (nc > info->pos_buf + info->buf_size)
257 info->pos_buf = info->pos_cur;
260 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
263 info->pos_border = info->pos_cur = nc;