3 * $Id: file.c,v 1.2 1999/10/22 22:47:13 mdw Exp $
5 * File source and target
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/10/22 22:47:13 mdw
33 * New (empty) endpoint method required.
35 * Revision 1.1 1999/07/26 23:33:32 mdw
36 * New sources and targets.
40 /*----- Header files ------------------------------------------------------*/
50 #include <sys/types.h>
55 #include <mLib/alloc.h>
56 #include <mLib/dstr.h>
57 #include <mLib/fdflags.h>
70 /*----- Data structures ---------------------------------------------------*/
72 /* --- File specification --- */
74 typedef struct fspec {
82 #define FTYPE_GUESS 0u
87 /* --- File options --- */
89 typedef struct fopts {
93 /* --- File data block --- */
95 typedef struct fdata {
101 /* --- File source block --- */
103 typedef struct fsource {
108 /* --- File target block --- */
110 typedef struct ftarget {
115 /*----- Static variables --------------------------------------------------*/
117 static fopts file_opts = { O_TRUNC };
119 static reffd *rstdin, *rstdout;
122 /*----- File endpoint operations ------------------------------------------*/
124 /* --- @wclose@ --- */
126 static void fept_wclose(endpt *e)
134 /* --- @close@ --- */
136 static void fept_close(endpt *e)
146 /* --- Endpoint operations table --- */
148 static endpt_ops fept_ops = { 0, 0, fept_wclose, fept_close };
150 /*----- General operations on sources and targets -------------------------*/
152 /* --- @file_null@ --- *
154 * Arguments: @void *p@ = an uninteresting argument
158 * Use: Removes the reference to @rnull@ when the file closes.
161 static void file_null(void *p)
166 /* --- @file_nullref@ --- *
170 * Returns: A reference to a file descriptor open on /dev/null.
172 * Use: Obtains a file descriptor for /dev/null. The reference
173 * @rnull@ is `weak', so that I only have a descriptor open when
174 * I actually need one.
177 static reffd *file_nullref(void)
183 if ((n = open("/dev/null", O_RDWR)) < 0) {
184 fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno));
187 fdflags(n, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
188 rnull = reffd_init(n);
189 reffd_handler(rnull, file_null, 0);
194 /* --- @file_fspec@ --- *
196 * Arguments: @fspec *f@ = pointer to filespec to fill in
197 * @scanner *sc@ = pointer to scanner to read from
201 * Use: Reads in a file spec.
204 static void file_fspec(fspec *f, scanner *sc)
207 unsigned type = FTYPE_GUESS;
210 /* --- Set up references to @stdin@ and @stdout@ --- */
213 fdflags(STDIN_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
214 fdflags(STDOUT_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
215 rstdin = reffd_init(STDIN_FILENO);
216 rstdout = reffd_init(STDOUT_FILENO);
219 /* --- Try to get an access type --- */
226 if (sc->t == CTOK_WORD) {
227 int i = conf_enum(sc, "fd,name,null", c ? ENUM_ABBREV : ENUM_NONE,
230 if (type && sc->t == ':')
234 /* --- Check for a valid file descriptor name --- */
236 if (sc->t == CTOK_WORD && type != FTYPE_NAME && type != FTYPE_NULL) {
237 if (strcmp(sc->d.buf, "stdin") == 0) {
240 } else if (strcmp(sc->d.buf, "stdout") == 0) {
246 else if (isdigit((unsigned char)*sc->d.buf)) {
247 int nfd = atoi(sc->d.buf);
249 case STDIN_FILENO: fd = rstdin; REFFD_INC(rstdin); break;
250 case STDOUT_FILENO: fd = rstdout; REFFD_INC(rstdout); break;
252 fd = reffd_init(nfd);
253 fdflags(nfd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
259 /* --- Sort out what to do as a result --- */
261 if (type == FTYPE_NULL)
262 f->type = FTYPE_NULL;
267 } else if (type == FTYPE_FD) {
268 if (sc->t == CTOK_WORD)
269 error(sc, "bad file descriptor `%s'", sc->d.buf);
271 error(sc, "parse error, expected file descriptor");
274 conf_name(sc, '/', &d);
275 f->type = FTYPE_NAME;
276 f->u.name = xstrdup(d.buf);
283 static void file_read(fdata *f, scanner *sc)
285 file_fspec(&f->in, sc);
288 if (f->out.type == FTYPE_FD && f->out.u.fd == rstdin)
289 f->out.u.fd = rstdout;
292 file_fspec(&f->out, sc);
294 f->fa = fattr_global;
298 /* --- @print@ --- */
300 static void file_pfspec(fspec *f, dstr *d)
304 switch (f->u.fd->fd) {
306 dstr_puts(d, "stdin");
309 dstr_puts(d, "stdout");
312 dstr_putf(d, "fd:%i", f->u.fd->fd);
317 dstr_putf(d, "name:%s", f->u.name);
320 dstr_puts(d, "null");
325 static void file_desc(fdata *f, dstr *d)
327 dstr_puts(d, "file ");
328 file_pfspec(&f->in, d);
330 file_pfspec(&f->out, d);
333 /* --- @option@ --- */
335 static int file_option(fdata *f, scanner *sc)
337 fopts *fo = f ? &f->fo : &file_opts;
339 CONF_BEGIN(sc, "file", "file")
341 /* --- Handle file-specific options --- */
343 if (strcmp(sc->d.buf, "create") == 0) {
347 switch (conf_enum(sc, "no,yes", ENUM_ABBREV, "create status")) {
348 case 0: fo->of &= ~O_CREAT; break;
349 case 1: fo->of |= O_CREAT; break;
354 else if (strcmp(sc->d.buf, "open") == 0) {
358 fo->of &= ~(O_TRUNC | O_APPEND | O_EXCL);
359 switch (conf_enum(sc, "no,truncate,append",
360 ENUM_ABBREV, "open status")) {
361 case 0: fo->of |= O_EXCL; break;
362 case 1: fo->of |= O_TRUNC; break;
363 case 2: fo->of |= O_APPEND; break;
368 /* --- See if it's a file attribute --- */
371 fattr *fa = f ? &f->fa : &fattr_global;
372 if (fattr_option(sc, fa))
376 /* --- Nobody understood it --- */
381 /* --- @endpt@ --- */
383 static endpt *file_endpt(fdata *f, const char *desc)
385 reffd *in = 0, *out = 0;
388 /* --- Make the input file first --- */
390 switch (f->in.type) {
400 if ((fd = open(f->in.u.name, O_RDONLY | O_NONBLOCK)) < 0) {
401 fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
402 desc, f->in.u.name, strerror(errno));
409 /* --- Make the output file second --- */
411 switch (f->out.type) {
413 out = file_nullref();
420 int m = f->fo.of | O_WRONLY | O_NONBLOCK;
423 /* --- First try creating the file --- *
425 * This lets me know whether to set the file attributes or not. It
426 * also stands a chance of noticing people playing silly beggars with
431 (fd = open(f->out.u.name, m | O_EXCL, f->fa.mode)) < 0 &&
432 (errno != EEXIST || (m & O_EXCL))) {
433 fw_log(-1, "[%s] couldn't create `%s': %s",
434 desc, f->out.u.name, strerror(errno));
440 if (fattr_apply(f->out.u.name, &f->fa)) {
441 fw_log(-1, "[%s] couldn't apply file attributes: %s",
442 desc, f->out.u.name, strerror(errno));
444 } else if ((fd = open(f->out.u.name, m & ~O_CREAT)) < 0) {
445 fw_log(-1, "[%s] couldn't open `%s': %s",
446 desc, f->out.u.name, strerror(errno));
451 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
452 out = reffd_init(fd);
456 /* --- Set up the endpoint --- */
469 /* --- @destroy@ --- */
471 static void file_destroy(fdata *f)
473 if (f->in.type == FTYPE_NAME)
475 if (f->out.type == FTYPE_NAME)
479 /*----- File source description -------------------------------------------*/
481 /* --- @option@ --- */
483 static int fsource_option(source *s, scanner *sc)
485 fsource *fs = (fsource *)s;
486 return (file_option(fs ? &fs->f : 0, sc));
491 static source *fsource_read(scanner *sc)
495 if (!conf_prefix(sc, "file"))
497 fs = CREATE(fsource);
498 fs->s.ops = &fsource_ops;
500 file_read(&fs->f, sc);
504 /* --- @attach@ --- */
506 static void fsource_destroy(source */*s*/);
508 static void fsource_attach(source *s, scanner *sc, target *t)
510 fsource *fs = (fsource *)s;
513 /* --- Set up the source description string --- */
517 file_desc(&fs->f, &d);
518 dstr_puts(&d, " -> ");
519 dstr_puts(&d, t->desc);
520 fs->s.desc = xstrdup(d.buf);
524 /* --- And then create the endpoints --- */
526 if ((ee = t->ops->create(t, fs->s.desc)) == 0)
528 if ((e = file_endpt(&fs->f, fs->s.desc)) == 0) {
534 /* --- Dispose of the source and target now --- */
538 fsource_destroy(&fs->s);
541 /* --- @destroy@ --- */
543 static void fsource_destroy(source *s)
545 fsource *fs = (fsource *)s;
547 /* free(fs->s.desc); */
548 file_destroy(&fs->f);
552 /* --- File source operations --- */
554 source_ops fsource_ops = {
556 fsource_option, fsource_read, fsource_attach, fsource_destroy
559 /*----- File target description -------------------------------------------*/
561 /* --- @option@ --- */
563 static int ftarget_option(target *t, scanner *sc)
565 ftarget *ft = (ftarget *)t;
566 return (file_option(ft ? &ft->f : 0, sc));
571 static target *ftarget_read(scanner *sc)
576 if (!conf_prefix(sc, "file"))
578 ft = CREATE(ftarget);
579 ft->t.ops = &ftarget_ops;
580 file_read(&ft->f, sc);
581 file_desc(&ft->f, &d);
582 ft->t.desc = xstrdup(d.buf);
587 /* --- @create@ --- */
589 static endpt *ftarget_create(target *t, const char *desc)
591 ftarget *ft = (ftarget *)t;
592 endpt *e = file_endpt(&ft->f, desc);
596 /* --- @destroy@ --- */
598 static void ftarget_destroy(target *t)
600 ftarget *ft = (ftarget *)t;
601 file_destroy(&ft->f);
602 /* free(ft->t.desc); */
606 /* --- File target operations --- */
608 target_ops ftarget_ops = {
610 ftarget_option, ftarget_read, ftarget_create, ftarget_destroy
613 /*----- That's all, folks -------------------------------------------------*/