3 * $Id: file.c,v 1.3 1999/12/22 15:43:47 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.3 1999/12/22 15:43:47 mdw
35 * Revision 1.2 1999/10/22 22:47:13 mdw
36 * New (empty) endpoint method required.
38 * Revision 1.1 1999/07/26 23:33:32 mdw
39 * New sources and targets.
43 /*----- Header files ------------------------------------------------------*/
53 #include <sys/types.h>
58 #include <mLib/alloc.h>
59 #include <mLib/dstr.h>
60 #include <mLib/fdflags.h>
73 /*----- Data structures ---------------------------------------------------*/
75 /* --- File specification --- */
77 typedef struct fspec {
85 #define FTYPE_GUESS 0u
90 /* --- File options --- */
92 typedef struct fopts {
96 /* --- File data block --- */
98 typedef struct fdata {
104 /* --- File source block --- */
106 typedef struct fsource {
111 /* --- File target block --- */
113 typedef struct ftarget {
118 /*----- Static variables --------------------------------------------------*/
120 static fopts file_opts = { O_TRUNC };
122 static reffd *rstdin, *rstdout;
125 /*----- File endpoint operations ------------------------------------------*/
127 /* --- @wclose@ --- */
129 static void fept_wclose(endpt *e)
137 /* --- @close@ --- */
139 static void fept_close(endpt *e)
149 /* --- Endpoint operations table --- */
151 static endpt_ops fept_ops = { 0, 0, fept_wclose, fept_close };
153 /*----- General operations on sources and targets -------------------------*/
155 /* --- @file_null@ --- *
157 * Arguments: @void *p@ = an uninteresting argument
161 * Use: Removes the reference to @rnull@ when the file closes.
164 static void file_null(void *p)
169 /* --- @file_nullref@ --- *
173 * Returns: A reference to a file descriptor open on /dev/null.
175 * Use: Obtains a file descriptor for /dev/null. The reference
176 * @rnull@ is `weak', so that I only have a descriptor open when
177 * I actually need one.
180 static reffd *file_nullref(void)
186 if ((n = open("/dev/null", O_RDWR)) < 0) {
187 fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno));
190 fdflags(n, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
191 rnull = reffd_init(n);
192 reffd_handler(rnull, file_null, 0);
197 /* --- @file_fspec@ --- *
199 * Arguments: @fspec *f@ = pointer to filespec to fill in
200 * @scanner *sc@ = pointer to scanner to read from
204 * Use: Reads in a file spec.
207 static void file_fspec(fspec *f, scanner *sc)
210 unsigned type = FTYPE_GUESS;
213 /* --- Set up references to @stdin@ and @stdout@ --- */
216 fdflags(STDIN_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
217 fdflags(STDOUT_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
218 rstdin = reffd_init(STDIN_FILENO);
219 rstdout = reffd_init(STDOUT_FILENO);
222 /* --- Try to get an access type --- */
229 if (sc->t == CTOK_WORD) {
230 int i = conf_enum(sc, "fd,name,null", c ? ENUM_ABBREV : ENUM_NONE,
233 if (type && sc->t == ':')
237 /* --- Check for a valid file descriptor name --- */
239 if (sc->t == CTOK_WORD && type != FTYPE_NAME && type != FTYPE_NULL) {
240 if (strcmp(sc->d.buf, "stdin") == 0) {
243 } else if (strcmp(sc->d.buf, "stdout") == 0) {
249 else if (isdigit((unsigned char)*sc->d.buf)) {
250 int nfd = atoi(sc->d.buf);
252 case STDIN_FILENO: fd = rstdin; REFFD_INC(rstdin); break;
253 case STDOUT_FILENO: fd = rstdout; REFFD_INC(rstdout); break;
255 fd = reffd_init(nfd);
256 fdflags(nfd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
262 /* --- Sort out what to do as a result --- */
264 if (type == FTYPE_NULL)
265 f->type = FTYPE_NULL;
270 } else if (type == FTYPE_FD) {
271 if (sc->t == CTOK_WORD)
272 error(sc, "bad file descriptor `%s'", sc->d.buf);
274 error(sc, "parse error, expected file descriptor");
277 conf_name(sc, '/', &d);
278 f->type = FTYPE_NAME;
279 f->u.name = xstrdup(d.buf);
286 static void file_read(fdata *f, scanner *sc)
288 file_fspec(&f->in, sc);
291 if (f->out.type == FTYPE_FD && f->out.u.fd == rstdin)
292 f->out.u.fd = rstdout;
295 file_fspec(&f->out, sc);
297 f->fa = fattr_global;
301 /* --- @print@ --- */
303 static void file_pfspec(fspec *f, dstr *d)
307 switch (f->u.fd->fd) {
309 dstr_puts(d, "stdin");
312 dstr_puts(d, "stdout");
315 dstr_putf(d, "fd:%i", f->u.fd->fd);
320 dstr_putf(d, "name:%s", f->u.name);
323 dstr_puts(d, "null");
328 static void file_desc(fdata *f, dstr *d)
330 dstr_puts(d, "file ");
331 file_pfspec(&f->in, d);
333 file_pfspec(&f->out, d);
336 /* --- @option@ --- */
338 static int file_option(fdata *f, scanner *sc)
340 fopts *fo = f ? &f->fo : &file_opts;
342 CONF_BEGIN(sc, "file", "file")
344 /* --- Handle file-specific options --- */
346 if (strcmp(sc->d.buf, "create") == 0) {
350 switch (conf_enum(sc, "no,yes", ENUM_ABBREV, "create status")) {
351 case 0: fo->of &= ~O_CREAT; break;
352 case 1: fo->of |= O_CREAT; break;
357 else if (strcmp(sc->d.buf, "open") == 0) {
361 fo->of &= ~(O_TRUNC | O_APPEND | O_EXCL);
362 switch (conf_enum(sc, "no,truncate,append",
363 ENUM_ABBREV, "open status")) {
364 case 0: fo->of |= O_EXCL; break;
365 case 1: fo->of |= O_TRUNC; break;
366 case 2: fo->of |= O_APPEND; break;
371 /* --- See if it's a file attribute --- */
374 fattr *fa = f ? &f->fa : &fattr_global;
375 if (fattr_option(sc, fa))
379 /* --- Nobody understood it --- */
384 /* --- @endpt@ --- */
386 static endpt *file_endpt(fdata *f, const char *desc)
388 reffd *in = 0, *out = 0;
391 /* --- Make the input file first --- */
393 switch (f->in.type) {
403 if ((fd = open(f->in.u.name, O_RDONLY | O_NONBLOCK)) < 0) {
404 fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
405 desc, f->in.u.name, strerror(errno));
412 /* --- Make the output file second --- */
414 switch (f->out.type) {
416 out = file_nullref();
423 int m = f->fo.of | O_WRONLY | O_NONBLOCK;
426 /* --- First try creating the file --- *
428 * This lets me know whether to set the file attributes or not. It
429 * also stands a chance of noticing people playing silly beggars with
434 (fd = open(f->out.u.name, m | O_EXCL, f->fa.mode)) < 0 &&
435 (errno != EEXIST || (m & O_EXCL))) {
436 fw_log(-1, "[%s] couldn't create `%s': %s",
437 desc, f->out.u.name, strerror(errno));
443 if (fattr_apply(f->out.u.name, &f->fa)) {
444 fw_log(-1, "[%s] couldn't apply file attributes to `%s': %s",
445 desc, f->out.u.name, strerror(errno));
447 } else if ((fd = open(f->out.u.name, m & ~O_CREAT)) < 0) {
448 fw_log(-1, "[%s] couldn't open `%s': %s",
449 desc, f->out.u.name, strerror(errno));
454 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
455 out = reffd_init(fd);
459 /* --- Set up the endpoint --- */
472 /* --- @destroy@ --- */
474 static void file_destroy(fdata *f)
476 if (f->in.type == FTYPE_NAME)
478 if (f->out.type == FTYPE_NAME)
482 /*----- File source description -------------------------------------------*/
484 /* --- @option@ --- */
486 static int fsource_option(source *s, scanner *sc)
488 fsource *fs = (fsource *)s;
489 return (file_option(fs ? &fs->f : 0, sc));
494 static source *fsource_read(scanner *sc)
498 if (!conf_prefix(sc, "file"))
500 fs = CREATE(fsource);
501 fs->s.ops = &fsource_ops;
503 file_read(&fs->f, sc);
507 /* --- @attach@ --- */
509 static void fsource_destroy(source */*s*/);
511 static void fsource_attach(source *s, scanner *sc, target *t)
513 fsource *fs = (fsource *)s;
516 /* --- Set up the source description string --- */
520 file_desc(&fs->f, &d);
521 dstr_puts(&d, " -> ");
522 dstr_puts(&d, t->desc);
523 fs->s.desc = xstrdup(d.buf);
527 /* --- And then create the endpoints --- */
529 if ((ee = t->ops->create(t, fs->s.desc)) == 0)
531 if ((e = file_endpt(&fs->f, fs->s.desc)) == 0) {
537 /* --- Dispose of the source and target now --- */
541 fsource_destroy(&fs->s);
544 /* --- @destroy@ --- */
546 static void fsource_destroy(source *s)
548 fsource *fs = (fsource *)s;
550 /* free(fs->s.desc); */
551 file_destroy(&fs->f);
555 /* --- File source operations --- */
557 source_ops fsource_ops = {
559 fsource_option, fsource_read, fsource_attach, fsource_destroy
562 /*----- File target description -------------------------------------------*/
564 /* --- @option@ --- */
566 static int ftarget_option(target *t, scanner *sc)
568 ftarget *ft = (ftarget *)t;
569 return (file_option(ft ? &ft->f : 0, sc));
574 static target *ftarget_read(scanner *sc)
579 if (!conf_prefix(sc, "file"))
581 ft = CREATE(ftarget);
582 ft->t.ops = &ftarget_ops;
583 file_read(&ft->f, sc);
584 file_desc(&ft->f, &d);
585 ft->t.desc = xstrdup(d.buf);
590 /* --- @create@ --- */
592 static endpt *ftarget_create(target *t, const char *desc)
594 ftarget *ft = (ftarget *)t;
595 endpt *e = file_endpt(&ft->f, desc);
599 /* --- @destroy@ --- */
601 static void ftarget_destroy(target *t)
603 ftarget *ft = (ftarget *)t;
604 file_destroy(&ft->f);
605 /* free(ft->t.desc); */
609 /* --- File target operations --- */
611 target_ops ftarget_ops = {
613 ftarget_option, ftarget_read, ftarget_create, ftarget_destroy
616 /*----- That's all, folks -------------------------------------------------*/