chiark / gitweb /
fwd: Improve `source' and `target' lifecycle management.
[fwd] / file.c
diff --git a/file.c b/file.c
index 31e1e07c0fa16c27ebff2f0a9e608d465e0551c8..5da247c0a55dd4779757586fb93a90decff4f75e 100644 (file)
--- a/file.c
+++ b/file.c
@@ -140,7 +140,7 @@ static reffd *file_nullref(void)
   else {
     int n;
     if ((n = open("/dev/null", O_RDWR)) < 0) {
-      fw_log(-1, "couldn't open `/dev/null': %s", strerror(errno));
+      fw_log(NOW, "couldn't open `/dev/null': %s", strerror(errno));
       return (0);
     }
     fdflags(n, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
@@ -242,13 +242,15 @@ static void file_fspec(fspec *f, scanner *sc)
 static void file_read(fdata *f, scanner *sc)
 {
   file_fspec(&f->in, sc);
-  if (sc->t != ',') {
+  if (sc->t == ',') {
+    token(sc);
+    file_fspec(&f->out, sc);
+  } else {
     f->out = f->in;
     if (f->out.type == FTYPE_FD && f->out.u.fd == rstdin)
       f->out.u.fd = rstdout;
-  } else {
-    token(sc);
-    file_fspec(&f->out, sc);
+    else if (f->out.type == FTYPE_NAME)
+      f->out.u.name = xstrdup(f->in.u.name);
   }
   f->fa = fattr_global;
   f->fo = file_opts;
@@ -357,7 +359,7 @@ static endpt *file_endpt(fdata *f, const char *desc)
     case FTYPE_NAME: {
       int fd;
       if ((fd = open(f->in.u.name, O_RDONLY | O_NONBLOCK)) < 0) {
-       fw_log(-1, "[%s] couldn't open `%s' for reading: %s",
+       fw_log(NOW, "[%s] couldn't open `%s' for reading: %s",
               desc, f->in.u.name, strerror(errno));
        return (0);
       }
@@ -389,7 +391,7 @@ static endpt *file_endpt(fdata *f, const char *desc)
       if ((m & O_CREAT) &&
          (fd = open(f->out.u.name, m | O_EXCL, f->fa.mode)) < 0 &&
          (errno != EEXIST || (m & O_EXCL))) {
-       fw_log(-1, "[%s] couldn't create `%s': %s",
+       fw_log(NOW, "[%s] couldn't create `%s': %s",
               desc, f->out.u.name, strerror(errno));
        REFFD_DEC(in);
        return (0);
@@ -397,11 +399,11 @@ static endpt *file_endpt(fdata *f, const char *desc)
 
       if (fd != -1) {
        if (fattr_apply(f->out.u.name, &f->fa)) {
-         fw_log(-1, "[%s] couldn't apply file attributes to `%s': %s",
+         fw_log(NOW, "[%s] couldn't apply file attributes to `%s': %s",
                 desc, f->out.u.name, strerror(errno));
        }
       } else if ((fd = open(f->out.u.name, m & ~O_CREAT)) < 0) {
-       fw_log(-1, "[%s] couldn't open `%s': %s",
+       fw_log(NOW, "[%s] couldn't open `%s': %s",
               desc, f->out.u.name, strerror(errno));
        REFFD_DEC(in);
        return (0);
@@ -455,6 +457,8 @@ static source *fsource_read(scanner *sc)
     return (0);
   fs = CREATE(fsource);
   fs->s.ops = &fsource_ops;
+  fs->s.ref = 1;
+  fs->s.f = 0;
   fs->s.desc = 0;
   file_read(&fs->f, sc);
   return (&fs->s);
@@ -483,18 +487,12 @@ static void fsource_attach(source *s, scanner *sc, target *t)
   /* --- And then create the endpoints --- */
 
   if ((ee = t->ops->create(t, fs->s.desc)) == 0)
-    goto tidy;
+    return;
   if ((e = file_endpt(&fs->f, fs->s.desc)) == 0) {
     ee->ops->close(ee);
-    goto tidy;
+    return;
   }
-  endpt_join(e, ee);
-
-  /* --- Dispose of the source and target now --- */
-
-tidy:
-  t->ops->destroy(t);
-  fsource_destroy(&fs->s);
+  endpt_join(e, ee, fs->s.desc);
 }
 
 /* --- @destroy@ --- */
@@ -511,7 +509,7 @@ static void fsource_destroy(source *s)
 
 source_ops fsource_ops = {
   "file",
-  fsource_option, fsource_read, fsource_attach, fsource_destroy
+  fsource_option, fsource_read, fsource_attach, 0, fsource_destroy
 };
 
 /*----- File target description -------------------------------------------*/
@@ -535,6 +533,7 @@ static target *ftarget_read(scanner *sc)
     return (0);
   ft = CREATE(ftarget);
   ft->t.ops = &ftarget_ops;
+  ft->t.ref = 1;
   file_read(&ft->f, sc);
   file_desc(&ft->f, &d);
   ft->t.desc = xstrdup(d.buf);