3 * $Id: file.c,v 1.1 1999/07/26 23:33:32 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.1 1999/07/26 23:33:32 mdw
33 * New sources and targets.
37 /*----- Header files ------------------------------------------------------*/
47 #include <sys/types.h>
52 #include <mLib/alloc.h>
53 #include <mLib/dstr.h>
54 #include <mLib/fdflags.h>
67 /*----- Data structures ---------------------------------------------------*/
69 /* --- File specification --- */
71 typedef struct fspec {
79 #define FTYPE_GUESS 0u
84 /* --- File options --- */
86 typedef struct fopts {
90 /* --- File data block --- */
92 typedef struct fdata {
98 /* --- File source block --- */
100 typedef struct fsource {
105 /* --- File target block --- */
107 typedef struct ftarget {
112 /*----- Static variables --------------------------------------------------*/
114 static fopts file_opts = { O_TRUNC };
116 static reffd *rstdin, *rstdout;
119 /*----- File endpoint operations ------------------------------------------*/
121 /* --- @wclose@ --- */
123 static void fept_wclose(endpt *e)
131 /* --- @close@ --- */
133 static void fept_close(endpt *e)
143 /* --- Endpoint operations table --- */
145 static endpt_ops fept_ops = { 0, fept_wclose, fept_close };
147 /*----- General operations on sources and targets -------------------------*/
149 /* --- @file_null@ --- *
151 * Arguments: @void *p@ = an uninteresting argument
155 * Use: Removes the reference to @rnull@ when the file closes.
158 static void file_null(void *p)
163 /* --- @file_nullref@ --- *
167 * Returns: A reference to a file descriptor open on /dev/null.
169 * Use: Obtains a file descriptor for /dev/null. The reference
170 * @rnull@ is `weak', so that I only have a descriptor open when
171 * I actually need one.
174 static reffd *file_nullref(void)
180 if ((n = open("/dev/null", O_RDWR)) < 0) {
181 fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno));
184 fdflags(n, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
185 rnull = reffd_init(n);
186 reffd_handler(rnull, file_null, 0);
191 /* --- @file_fspec@ --- *
193 * Arguments: @fspec *f@ = pointer to filespec to fill in
194 * @scanner *sc@ = pointer to scanner to read from
198 * Use: Reads in a file spec.
201 static void file_fspec(fspec *f, scanner *sc)
204 unsigned type = FTYPE_GUESS;
207 /* --- Set up references to @stdin@ and @stdout@ --- */
210 fdflags(STDIN_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
211 fdflags(STDOUT_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
212 rstdin = reffd_init(STDIN_FILENO);
213 rstdout = reffd_init(STDOUT_FILENO);
216 /* --- Try to get an access type --- */
223 if (sc->t == CTOK_WORD) {
224 int i = conf_enum(sc, "fd,name,null", c ? ENUM_ABBREV : ENUM_NONE,
227 if (type && sc->t == ':')
231 /* --- Check for a valid file descriptor name --- */
233 if (sc->t == CTOK_WORD && type != FTYPE_NAME && type != FTYPE_NULL) {
234 if (strcmp(sc->d.buf, "stdin") == 0) {
237 } else if (strcmp(sc->d.buf, "stdout") == 0) {
243 else if (isdigit((unsigned char)*sc->d.buf)) {
244 int nfd = atoi(sc->d.buf);
246 case STDIN_FILENO: fd = rstdin; REFFD_INC(rstdin); break;
247 case STDOUT_FILENO: fd = rstdout; REFFD_INC(rstdout); break;
249 fd = reffd_init(nfd);
250 fdflags(nfd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
256 /* --- Sort out what to do as a result --- */
258 if (type == FTYPE_NULL)
259 f->type = FTYPE_NULL;
264 } else if (type == FTYPE_FD) {
265 if (sc->t == CTOK_WORD)
266 error(sc, "bad file descriptor `%s'", sc->d.buf);
268 error(sc, "parse error, expected file descriptor");
271 conf_name(sc, '/', &d);
272 f->type = FTYPE_NAME;
273 f->u.name = xstrdup(d.buf);
280 static void file_read(fdata *f, scanner *sc)
282 file_fspec(&f->in, sc);
285 if (f->out.type == FTYPE_FD && f->out.u.fd == rstdin)
286 f->out.u.fd = rstdout;
289 file_fspec(&f->out, sc);
291 f->fa = fattr_global;
295 /* --- @print@ --- */
297 static void file_pfspec(fspec *f, dstr *d)
301 switch (f->u.fd->fd) {
303 dstr_puts(d, "stdin");
306 dstr_puts(d, "stdout");
309 dstr_putf(d, "fd:%i", f->u.fd->fd);
314 dstr_putf(d, "name:%s", f->u.name);
317 dstr_puts(d, "null");
322 static void file_desc(fdata *f, dstr *d)
324 dstr_puts(d, "file ");
325 file_pfspec(&f->in, d);
327 file_pfspec(&f->out, d);
330 /* --- @option@ --- */
332 static int file_option(fdata *f, scanner *sc)
334 fopts *fo = f ? &f->fo : &file_opts;
336 CONF_BEGIN(sc, "file", "file")
338 /* --- Handle file-specific options --- */
340 if (strcmp(sc->d.buf, "create") == 0) {
344 switch (conf_enum(sc, "no,yes", ENUM_ABBREV, "create status")) {
345 case 0: fo->of &= ~O_CREAT; break;
346 case 1: fo->of |= O_CREAT; break;
351 else if (strcmp(sc->d.buf, "open") == 0) {
355 fo->of &= ~(O_TRUNC | O_APPEND | O_EXCL);
356 switch (conf_enum(sc, "no,truncate,append",
357 ENUM_ABBREV, "open status")) {
358 case 0: fo->of |= O_EXCL; break;
359 case 1: fo->of |= O_TRUNC; break;
360 case 2: fo->of |= O_APPEND; break;
365 /* --- See if it's a file attribute --- */
368 fattr *fa = f ? &f->fa : &fattr_global;
369 if (fattr_option(sc, fa))
373 /* --- Nobody understood it --- */
378 /* --- @endpt@ --- */
380 static endpt *file_endpt(fdata *f, const char *desc)
382 reffd *in = 0, *out = 0;
385 /* --- Make the input file first --- */
387 switch (f->in.type) {
397 if ((fd = open(f->in.u.name, O_RDONLY | O_NONBLOCK)) < 0) {
398 fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
399 desc, f->in.u.name, strerror(errno));
406 /* --- Make the output file second --- */
408 switch (f->out.type) {
410 out = file_nullref();
417 int m = f->fo.of | O_WRONLY | O_NONBLOCK;
420 /* --- First try creating the file --- *
422 * This lets me know whether to set the file attributes or not. It
423 * also stands a chance of noticing people playing silly beggars with
428 (fd = open(f->out.u.name, m | O_EXCL, f->fa.mode)) < 0 &&
429 (errno != EEXIST || (m & O_EXCL))) {
430 fw_log(-1, "[%s] couldn't create `%s': %s",
431 desc, f->out.u.name, strerror(errno));
437 if (fattr_apply(f->out.u.name, &f->fa)) {
438 fw_log(-1, "[%s] couldn't apply file attributes: %s",
439 desc, f->out.u.name, strerror(errno));
441 } else if ((fd = open(f->out.u.name, m & ~O_CREAT)) < 0) {
442 fw_log(-1, "[%s] couldn't open `%s': %s",
443 desc, f->out.u.name, strerror(errno));
448 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
449 out = reffd_init(fd);
453 /* --- Set up the endpoint --- */
466 /* --- @destroy@ --- */
468 static void file_destroy(fdata *f)
470 if (f->in.type == FTYPE_NAME)
472 if (f->out.type == FTYPE_NAME)
476 /*----- File source description -------------------------------------------*/
478 /* --- @option@ --- */
480 static int fsource_option(source *s, scanner *sc)
482 fsource *fs = (fsource *)s;
483 return (file_option(fs ? &fs->f : 0, sc));
488 static source *fsource_read(scanner *sc)
492 if (!conf_prefix(sc, "file"))
494 fs = CREATE(fsource);
495 fs->s.ops = &fsource_ops;
497 file_read(&fs->f, sc);
501 /* --- @attach@ --- */
503 static void fsource_destroy(source */*s*/);
505 static void fsource_attach(source *s, scanner *sc, target *t)
507 fsource *fs = (fsource *)s;
510 /* --- Set up the source description string --- */
514 file_desc(&fs->f, &d);
515 dstr_puts(&d, " -> ");
516 dstr_puts(&d, t->desc);
517 fs->s.desc = xstrdup(d.buf);
521 /* --- And then create the endpoints --- */
523 if ((ee = t->ops->create(t, fs->s.desc)) == 0)
525 if ((e = file_endpt(&fs->f, fs->s.desc)) == 0) {
531 /* --- Dispose of the source and target now --- */
535 fsource_destroy(&fs->s);
538 /* --- @destroy@ --- */
540 static void fsource_destroy(source *s)
542 fsource *fs = (fsource *)s;
544 /* free(fs->s.desc); */
545 file_destroy(&fs->f);
549 /* --- File source operations --- */
551 source_ops fsource_ops = {
553 fsource_option, fsource_read, fsource_attach, fsource_destroy
556 /*----- File target description -------------------------------------------*/
558 /* --- @option@ --- */
560 static int ftarget_option(target *t, scanner *sc)
562 ftarget *ft = (ftarget *)t;
563 return (file_option(ft ? &ft->f : 0, sc));
568 static target *ftarget_read(scanner *sc)
573 if (!conf_prefix(sc, "file"))
575 ft = CREATE(ftarget);
576 ft->t.ops = &ftarget_ops;
577 file_read(&ft->f, sc);
578 file_desc(&ft->f, &d);
579 ft->t.desc = xstrdup(d.buf);
584 /* --- @create@ --- */
586 static endpt *ftarget_create(target *t, const char *desc)
588 ftarget *ft = (ftarget *)t;
589 endpt *e = file_endpt(&ft->f, desc);
593 /* --- @destroy@ --- */
595 static void ftarget_destroy(target *t)
597 ftarget *ft = (ftarget *)t;
598 file_destroy(&ft->f);
599 /* free(ft->t.desc); */
603 /* --- File target operations --- */
605 target_ops ftarget_ops = {
607 ftarget_option, ftarget_read, ftarget_create, ftarget_destroy
610 /*----- That's all, folks -------------------------------------------------*/