3 * File source and target
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fw' port forwarder.
12 * `fw' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * `fw' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
42 #include <mLib/alloc.h>
43 #include <mLib/dstr.h>
44 #include <mLib/fdflags.h>
57 /*----- Data structures ---------------------------------------------------*/
59 /* --- File specification --- */
61 typedef struct fspec {
69 #define FTYPE_GUESS 0u
74 /* --- File options --- */
76 typedef struct fopts {
80 /* --- File data block --- */
82 typedef struct fdata {
88 /* --- File source block --- */
90 typedef struct fsource {
95 /* --- File target block --- */
97 typedef struct ftarget {
102 /*----- Static variables --------------------------------------------------*/
104 static fopts file_opts = { O_TRUNC };
106 static reffd *rstdin, *rstdout;
109 /*----- File endpoint operations ------------------------------------------*/
111 /* --- @wclose@ --- */
113 static void fept_wclose(endpt *e)
121 /* --- @close@ --- */
123 static void fept_close(endpt *e)
133 /* --- Endpoint operations table --- */
135 static endpt_ops fept_ops = { 0, 0, fept_wclose, fept_close };
137 /*----- General operations on sources and targets -------------------------*/
139 /* --- @file_null@ --- *
141 * Arguments: @void *p@ = an uninteresting argument
145 * Use: Removes the reference to @rnull@ when the file closes.
148 static void file_null(void *p)
153 /* --- @file_nullref@ --- *
157 * Returns: A reference to a file descriptor open on /dev/null.
159 * Use: Obtains a file descriptor for /dev/null. The reference
160 * @rnull@ is `weak', so that I only have a descriptor open when
161 * I actually need one.
164 static reffd *file_nullref(void)
170 if ((n = open("/dev/null", O_RDWR)) < 0) {
171 fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno));
174 fdflags(n, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
175 rnull = reffd_init(n);
176 reffd_handler(rnull, file_null, 0);
181 /* --- @file_fspec@ --- *
183 * Arguments: @fspec *f@ = pointer to filespec to fill in
184 * @scanner *sc@ = pointer to scanner to read from
188 * Use: Reads in a file spec.
191 static void file_fspec(fspec *f, scanner *sc)
194 unsigned type = FTYPE_GUESS;
197 /* --- Set up references to @stdin@ and @stdout@ --- */
200 fdflags(STDIN_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
201 fdflags(STDOUT_FILENO, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
202 rstdin = reffd_init(STDIN_FILENO);
203 rstdout = reffd_init(STDOUT_FILENO);
206 /* --- Try to get an access type --- */
213 if (sc->t == CTOK_WORD) {
214 int i = conf_enum(sc, "fd,name,null", c ? ENUM_ABBREV : ENUM_NONE,
217 if (type && sc->t == ':')
221 /* --- Check for a valid file descriptor name --- */
223 if (sc->t == CTOK_WORD && type != FTYPE_NAME && type != FTYPE_NULL) {
224 if (strcmp(sc->d.buf, "stdin") == 0) {
227 } else if (strcmp(sc->d.buf, "stdout") == 0) {
233 else if (isdigit((unsigned char)*sc->d.buf)) {
234 int nfd = atoi(sc->d.buf);
236 case STDIN_FILENO: fd = rstdin; REFFD_INC(rstdin); break;
237 case STDOUT_FILENO: fd = rstdout; REFFD_INC(rstdout); break;
239 fd = reffd_init(nfd);
240 fdflags(nfd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
246 /* --- Sort out what to do as a result --- */
248 if (type == FTYPE_NULL)
249 f->type = FTYPE_NULL;
254 } else if (type == FTYPE_FD) {
255 if (sc->t == CTOK_WORD)
256 error(sc, "bad file descriptor `%s'", sc->d.buf);
258 error(sc, "parse error, expected file descriptor");
261 conf_name(sc, '/', &d);
262 f->type = FTYPE_NAME;
263 f->u.name = xstrdup(d.buf);
270 static void file_read(fdata *f, scanner *sc)
272 file_fspec(&f->in, sc);
275 if (f->out.type == FTYPE_FD && f->out.u.fd == rstdin)
276 f->out.u.fd = rstdout;
279 file_fspec(&f->out, sc);
281 f->fa = fattr_global;
285 /* --- @print@ --- */
287 static void file_pfspec(fspec *f, dstr *d)
291 switch (f->u.fd->fd) {
293 dstr_puts(d, "stdin");
296 dstr_puts(d, "stdout");
299 dstr_putf(d, "fd:%i", f->u.fd->fd);
304 dstr_putf(d, "name:%s", f->u.name);
307 dstr_puts(d, "null");
312 static void file_desc(fdata *f, dstr *d)
314 dstr_puts(d, "file ");
315 file_pfspec(&f->in, d);
317 file_pfspec(&f->out, d);
320 /* --- @option@ --- */
322 static int file_option(fdata *f, scanner *sc)
324 fopts *fo = f ? &f->fo : &file_opts;
326 CONF_BEGIN(sc, "file", "file")
328 /* --- Handle file-specific options --- */
330 if (strcmp(sc->d.buf, "create") == 0) {
334 switch (conf_enum(sc, "no,yes", ENUM_ABBREV, "create status")) {
335 case 0: fo->of &= ~O_CREAT; break;
336 case 1: fo->of |= O_CREAT; break;
341 else if (strcmp(sc->d.buf, "open") == 0) {
345 fo->of &= ~(O_TRUNC | O_APPEND | O_EXCL);
346 switch (conf_enum(sc, "no,truncate,append",
347 ENUM_ABBREV, "open status")) {
348 case 0: fo->of |= O_EXCL; break;
349 case 1: fo->of |= O_TRUNC; break;
350 case 2: fo->of |= O_APPEND; break;
355 /* --- See if it's a file attribute --- */
358 fattr *fa = f ? &f->fa : &fattr_global;
359 if (fattr_option(sc, fa))
363 /* --- Nobody understood it --- */
368 /* --- @endpt@ --- */
370 static endpt *file_endpt(fdata *f, const char *desc)
372 reffd *in = 0, *out = 0;
375 /* --- Make the input file first --- */
377 switch (f->in.type) {
387 if ((fd = open(f->in.u.name, O_RDONLY | O_NONBLOCK)) < 0) {
388 fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
389 desc, f->in.u.name, strerror(errno));
396 /* --- Make the output file second --- */
398 switch (f->out.type) {
400 out = file_nullref();
407 int m = f->fo.of | O_WRONLY | O_NONBLOCK;
410 /* --- First try creating the file --- *
412 * This lets me know whether to set the file attributes or not. It
413 * also stands a chance of noticing people playing silly beggars with
418 (fd = open(f->out.u.name, m | O_EXCL, f->fa.mode)) < 0 &&
419 (errno != EEXIST || (m & O_EXCL))) {
420 fw_log(-1, "[%s] couldn't create `%s': %s",
421 desc, f->out.u.name, strerror(errno));
427 if (fattr_apply(f->out.u.name, &f->fa)) {
428 fw_log(-1, "[%s] couldn't apply file attributes to `%s': %s",
429 desc, f->out.u.name, strerror(errno));
431 } else if ((fd = open(f->out.u.name, m & ~O_CREAT)) < 0) {
432 fw_log(-1, "[%s] couldn't open `%s': %s",
433 desc, f->out.u.name, strerror(errno));
438 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
439 out = reffd_init(fd);
443 /* --- Set up the endpoint --- */
456 /* --- @destroy@ --- */
458 static void file_destroy(fdata *f)
460 if (f->in.type == FTYPE_NAME)
462 if (f->out.type == FTYPE_NAME)
463 xfree(f->out.u.name);
466 /*----- File source description -------------------------------------------*/
468 /* --- @option@ --- */
470 static int fsource_option(source *s, scanner *sc)
472 fsource *fs = (fsource *)s;
473 return (file_option(fs ? &fs->f : 0, sc));
478 static source *fsource_read(scanner *sc)
482 if (!conf_prefix(sc, "file"))
484 fs = CREATE(fsource);
485 fs->s.ops = &fsource_ops;
487 file_read(&fs->f, sc);
491 /* --- @attach@ --- */
493 static void fsource_destroy(source */*s*/);
495 static void fsource_attach(source *s, scanner *sc, target *t)
497 fsource *fs = (fsource *)s;
500 /* --- Set up the source description string --- */
504 file_desc(&fs->f, &d);
505 dstr_puts(&d, " -> ");
506 dstr_puts(&d, t->desc);
507 fs->s.desc = xstrdup(d.buf);
511 /* --- And then create the endpoints --- */
513 if ((ee = t->ops->create(t, fs->s.desc)) == 0)
515 if ((e = file_endpt(&fs->f, fs->s.desc)) == 0) {
521 /* --- Dispose of the source and target now --- */
525 fsource_destroy(&fs->s);
528 /* --- @destroy@ --- */
530 static void fsource_destroy(source *s)
532 fsource *fs = (fsource *)s;
534 file_destroy(&fs->f);
538 /* --- File source operations --- */
540 source_ops fsource_ops = {
542 fsource_option, fsource_read, fsource_attach, fsource_destroy
545 /*----- File target description -------------------------------------------*/
547 /* --- @option@ --- */
549 static int ftarget_option(target *t, scanner *sc)
551 ftarget *ft = (ftarget *)t;
552 return (file_option(ft ? &ft->f : 0, sc));
557 static target *ftarget_read(scanner *sc)
562 if (!conf_prefix(sc, "file"))
564 ft = CREATE(ftarget);
565 ft->t.ops = &ftarget_ops;
566 file_read(&ft->f, sc);
567 file_desc(&ft->f, &d);
568 ft->t.desc = xstrdup(d.buf);
573 /* --- @create@ --- */
575 static endpt *ftarget_create(target *t, const char *desc)
577 ftarget *ft = (ftarget *)t;
578 endpt *e = file_endpt(&ft->f, desc);
582 /* --- @destroy@ --- */
584 static void ftarget_destroy(target *t)
586 ftarget *ft = (ftarget *)t;
587 file_destroy(&ft->f);
592 /* --- File target operations --- */
594 target_ops ftarget_ops = {
596 ftarget_option, ftarget_read, 0, ftarget_create, ftarget_destroy
599 /*----- That's all, folks -------------------------------------------------*/