3 * $Id: file.c,v 1.7 2004/04/08 01:36:25 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 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
44 #include <mLib/alloc.h>
45 #include <mLib/dstr.h>
46 #include <mLib/fdflags.h>
59 /*----- Data structures ---------------------------------------------------*/
61 /* --- File specification --- */
63 typedef struct fspec
{
71 #define FTYPE_GUESS 0u
76 /* --- File options --- */
78 typedef struct fopts
{
82 /* --- File data block --- */
84 typedef struct fdata
{
90 /* --- File source block --- */
92 typedef struct fsource
{
97 /* --- File target block --- */
99 typedef struct ftarget
{
104 /*----- Static variables --------------------------------------------------*/
106 static fopts file_opts
= { O_TRUNC
};
108 static reffd
*rstdin
, *rstdout
;
111 /*----- File endpoint operations ------------------------------------------*/
113 /* --- @wclose@ --- */
115 static void fept_wclose(endpt
*e
)
123 /* --- @close@ --- */
125 static void fept_close(endpt
*e
)
135 /* --- Endpoint operations table --- */
137 static endpt_ops fept_ops
= { 0, 0, fept_wclose
, fept_close
};
139 /*----- General operations on sources and targets -------------------------*/
141 /* --- @file_null@ --- *
143 * Arguments: @void *p@ = an uninteresting argument
147 * Use: Removes the reference to @rnull@ when the file closes.
150 static void file_null(void *p
)
155 /* --- @file_nullref@ --- *
159 * Returns: A reference to a file descriptor open on /dev/null.
161 * Use: Obtains a file descriptor for /dev/null. The reference
162 * @rnull@ is `weak', so that I only have a descriptor open when
163 * I actually need one.
166 static reffd
*file_nullref(void)
172 if ((n
= open("/dev/null", O_RDWR
)) < 0) {
173 fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno
));
176 fdflags(n
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
177 rnull
= reffd_init(n
);
178 reffd_handler(rnull
, file_null
, 0);
183 /* --- @file_fspec@ --- *
185 * Arguments: @fspec *f@ = pointer to filespec to fill in
186 * @scanner *sc@ = pointer to scanner to read from
190 * Use: Reads in a file spec.
193 static void file_fspec(fspec
*f
, scanner
*sc
)
196 unsigned type
= FTYPE_GUESS
;
199 /* --- Set up references to @stdin@ and @stdout@ --- */
202 fdflags(STDIN_FILENO
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
203 fdflags(STDOUT_FILENO
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
204 rstdin
= reffd_init(STDIN_FILENO
);
205 rstdout
= reffd_init(STDOUT_FILENO
);
208 /* --- Try to get an access type --- */
215 if (sc
->t
== CTOK_WORD
) {
216 int i
= conf_enum(sc
, "fd,name,null", c ? ENUM_ABBREV
: ENUM_NONE
,
219 if (type
&& sc
->t
== ':')
223 /* --- Check for a valid file descriptor name --- */
225 if (sc
->t
== CTOK_WORD
&& type
!= FTYPE_NAME
&& type
!= FTYPE_NULL
) {
226 if (strcmp(sc
->d
.buf
, "stdin") == 0) {
229 } else if (strcmp(sc
->d
.buf
, "stdout") == 0) {
235 else if (isdigit((unsigned char)*sc
->d
.buf
)) {
236 int nfd
= atoi(sc
->d
.buf
);
238 case STDIN_FILENO
: fd
= rstdin
; REFFD_INC(rstdin
); break;
239 case STDOUT_FILENO
: fd
= rstdout
; REFFD_INC(rstdout
); break;
241 fd
= reffd_init(nfd
);
242 fdflags(nfd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
248 /* --- Sort out what to do as a result --- */
250 if (type
== FTYPE_NULL
)
251 f
->type
= FTYPE_NULL
;
256 } else if (type
== FTYPE_FD
) {
257 if (sc
->t
== CTOK_WORD
)
258 error(sc
, "bad file descriptor `%s'", sc
->d
.buf
);
260 error(sc
, "parse error, expected file descriptor");
263 conf_name(sc
, '/', &d
);
264 f
->type
= FTYPE_NAME
;
265 f
->u
.name
= xstrdup(d
.buf
);
272 static void file_read(fdata
*f
, scanner
*sc
)
274 file_fspec(&f
->in
, sc
);
277 if (f
->out
.type
== FTYPE_FD
&& f
->out
.u
.fd
== rstdin
)
278 f
->out
.u
.fd
= rstdout
;
281 file_fspec(&f
->out
, sc
);
283 f
->fa
= fattr_global
;
287 /* --- @print@ --- */
289 static void file_pfspec(fspec
*f
, dstr
*d
)
293 switch (f
->u
.fd
->fd
) {
295 dstr_puts(d
, "stdin");
298 dstr_puts(d
, "stdout");
301 dstr_putf(d
, "fd:%i", f
->u
.fd
->fd
);
306 dstr_putf(d
, "name:%s", f
->u
.name
);
309 dstr_puts(d
, "null");
314 static void file_desc(fdata
*f
, dstr
*d
)
316 dstr_puts(d
, "file ");
317 file_pfspec(&f
->in
, d
);
319 file_pfspec(&f
->out
, d
);
322 /* --- @option@ --- */
324 static int file_option(fdata
*f
, scanner
*sc
)
326 fopts
*fo
= f ?
&f
->fo
: &file_opts
;
328 CONF_BEGIN(sc
, "file", "file")
330 /* --- Handle file-specific options --- */
332 if (strcmp(sc
->d
.buf
, "create") == 0) {
336 switch (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "create status")) {
337 case 0: fo
->of
&= ~O_CREAT
; break;
338 case 1: fo
->of
|= O_CREAT
; break;
343 else if (strcmp(sc
->d
.buf
, "open") == 0) {
347 fo
->of
&= ~(O_TRUNC
| O_APPEND
| O_EXCL
);
348 switch (conf_enum(sc
, "no,truncate,append",
349 ENUM_ABBREV
, "open status")) {
350 case 0: fo
->of
|= O_EXCL
; break;
351 case 1: fo
->of
|= O_TRUNC
; break;
352 case 2: fo
->of
|= O_APPEND
; break;
357 /* --- See if it's a file attribute --- */
360 fattr
*fa
= f ?
&f
->fa
: &fattr_global
;
361 if (fattr_option(sc
, fa
))
365 /* --- Nobody understood it --- */
370 /* --- @endpt@ --- */
372 static endpt
*file_endpt(fdata
*f
, const char *desc
)
374 reffd
*in
= 0, *out
= 0;
377 /* --- Make the input file first --- */
379 switch (f
->in
.type
) {
389 if ((fd
= open(f
->in
.u
.name
, O_RDONLY
| O_NONBLOCK
)) < 0) {
390 fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
391 desc
, f
->in
.u
.name
, strerror(errno
));
398 /* --- Make the output file second --- */
400 switch (f
->out
.type
) {
402 out
= file_nullref();
409 int m
= f
->fo
.of
| O_WRONLY
| O_NONBLOCK
;
412 /* --- First try creating the file --- *
414 * This lets me know whether to set the file attributes or not. It
415 * also stands a chance of noticing people playing silly beggars with
420 (fd
= open(f
->out
.u
.name
, m
| O_EXCL
, f
->fa
.mode
)) < 0 &&
421 (errno
!= EEXIST
|| (m
& O_EXCL
))) {
422 fw_log(-1, "[%s] couldn't create `%s': %s",
423 desc
, f
->out
.u
.name
, strerror(errno
));
429 if (fattr_apply(f
->out
.u
.name
, &f
->fa
)) {
430 fw_log(-1, "[%s] couldn't apply file attributes to `%s': %s",
431 desc
, f
->out
.u
.name
, strerror(errno
));
433 } else if ((fd
= open(f
->out
.u
.name
, m
& ~O_CREAT
)) < 0) {
434 fw_log(-1, "[%s] couldn't open `%s': %s",
435 desc
, f
->out
.u
.name
, strerror(errno
));
440 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
441 out
= reffd_init(fd
);
445 /* --- Set up the endpoint --- */
458 /* --- @destroy@ --- */
460 static void file_destroy(fdata
*f
)
462 if (f
->in
.type
== FTYPE_NAME
)
464 if (f
->out
.type
== FTYPE_NAME
)
465 xfree(f
->out
.u
.name
);
468 /*----- File source description -------------------------------------------*/
470 /* --- @option@ --- */
472 static int fsource_option(source
*s
, scanner
*sc
)
474 fsource
*fs
= (fsource
*)s
;
475 return (file_option(fs ?
&fs
->f
: 0, sc
));
480 static source
*fsource_read(scanner
*sc
)
484 if (!conf_prefix(sc
, "file"))
486 fs
= CREATE(fsource
);
487 fs
->s
.ops
= &fsource_ops
;
489 file_read(&fs
->f
, sc
);
493 /* --- @attach@ --- */
495 static void fsource_destroy(source */
*s*/
);
497 static void fsource_attach(source
*s
, scanner
*sc
, target
*t
)
499 fsource
*fs
= (fsource
*)s
;
502 /* --- Set up the source description string --- */
506 file_desc(&fs
->f
, &d
);
507 dstr_puts(&d
, " -> ");
508 dstr_puts(&d
, t
->desc
);
509 fs
->s
.desc
= xstrdup(d
.buf
);
513 /* --- And then create the endpoints --- */
515 if ((ee
= t
->ops
->create(t
, fs
->s
.desc
)) == 0)
517 if ((e
= file_endpt(&fs
->f
, fs
->s
.desc
)) == 0) {
523 /* --- Dispose of the source and target now --- */
527 fsource_destroy(&fs
->s
);
530 /* --- @destroy@ --- */
532 static void fsource_destroy(source
*s
)
534 fsource
*fs
= (fsource
*)s
;
536 file_destroy(&fs
->f
);
540 /* --- File source operations --- */
542 source_ops fsource_ops
= {
544 fsource_option
, fsource_read
, fsource_attach
, fsource_destroy
547 /*----- File target description -------------------------------------------*/
549 /* --- @option@ --- */
551 static int ftarget_option(target
*t
, scanner
*sc
)
553 ftarget
*ft
= (ftarget
*)t
;
554 return (file_option(ft ?
&ft
->f
: 0, sc
));
559 static target
*ftarget_read(scanner
*sc
)
564 if (!conf_prefix(sc
, "file"))
566 ft
= CREATE(ftarget
);
567 ft
->t
.ops
= &ftarget_ops
;
568 file_read(&ft
->f
, sc
);
569 file_desc(&ft
->f
, &d
);
570 ft
->t
.desc
= xstrdup(d
.buf
);
575 /* --- @create@ --- */
577 static endpt
*ftarget_create(target
*t
, const char *desc
)
579 ftarget
*ft
= (ftarget
*)t
;
580 endpt
*e
= file_endpt(&ft
->f
, desc
);
584 /* --- @destroy@ --- */
586 static void ftarget_destroy(target
*t
)
588 ftarget
*ft
= (ftarget
*)t
;
589 file_destroy(&ft
->f
);
594 /* --- File target operations --- */
596 target_ops ftarget_ops
= {
598 ftarget_option
, ftarget_read
, 0, ftarget_create
, ftarget_destroy
601 /*----- That's all, folks -------------------------------------------------*/