NEWS update.
[paraslash.git] / file_write.c
index 8991034..8cafbb6 100644 (file)
 /*
- * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file file_write.c simple output plugin for testing purposes */
 
+#include <regex.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <sys/time.h>
+#include <stdbool.h>
+
 #include "para.h"
 #include "list.h"
 #include "sched.h"
+#include "ggo.h"
+#include "buffer_tree.h"
 #include "write.h"
+#include "write_common.h"
 #include "string.h"
 #include "fd.h"
 #include "file_write.cmdline.h"
 #include "error.h"
 
-/** data specific to the file writer */
+/** Data specific to the file writer. */
 struct private_file_write_data {
-       /** the file descriptor of the output file */
+       /** The file descriptor of the output file. */
        int fd;
-       /** non-zero if \a fd was added to the write fd set */
-       int check_fd;
 };
 
-static int file_write_open(struct writer_node *wn)
+/*
+ * Get a random filename.
+ *
+ * This is by no means a secure way to create temporary files in a hostile
+ * directory like \p /tmp. However, we use it only for creating temp files in
+ * ~/.paraslash, for which it is OK. Result must be freed by the caller.
+ */
+__must_check __malloc static char *random_filename(void)
+{
+       char *result, *home = para_homedir();
+       struct timeval tv;
+
+       gettimeofday(&tv, NULL);
+       srandom(tv.tv_usec);
+       result = make_message("%s/.paraslash/%08lu", home,
+               para_random(99999999));
+       free(home);
+       return result;
+}
+
+static int prepare_output_file(struct writer_node *wn)
 {
-       struct private_file_write_data *pfwd = para_calloc(
-               sizeof(struct private_file_write_data));
        struct file_write_args_info *conf = wn->conf;
        char *filename;
+       int ret;
+       struct private_file_write_data *pfwd = para_calloc(sizeof(*pfwd));
+
        if (conf->filename_given)
                filename = conf->filename_arg;
-       else {
-               char *tmp = para_tmpname(), *home = para_homedir();
-               filename = make_message("%s/.paraslash/%s", home, tmp);
-               free(home);
-               free(tmp);
-       }
-       wn->private_data = pfwd;
-       pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+       else
+               filename = random_filename();
+       ret = para_open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
        if (!conf->filename_given)
                free(filename);
-       if (pfwd->fd >= 0)
-               return 8192;
+       if (ret < 0)
+               goto out;
+       pfwd->fd = ret;
+       ret = mark_fd_blocking(pfwd->fd);
+       if (ret < 0)
+               goto out_close;
+       wn->private_data = pfwd;
+       return 1;
+out_close:
+       close(pfwd->fd);
+out:
        free(pfwd);
-       return -E_FW_OPEN;
+       return ret;
 }
 
-static int file_write_pre_select(struct sched *s, struct writer_node *wn)
+static void file_write_pre_select(struct sched *s, struct task *t)
 {
+       struct writer_node *wn = container_of(t, struct writer_node, task);
        struct private_file_write_data *pfwd = wn->private_data;
-       struct writer_node_group *wng = wn->wng;
+       int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
 
-       pfwd->check_fd = 0;
-       if (pfwd->fd <= 0)
-               return -E_FW_NO_FILE;
-       if (!*wng->loaded)
-               return 1;
+       if (ret == 0)
+               return;
+       if (ret < 0 || !pfwd)
+               return sched_min_delay(s);
        para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
-       pfwd->check_fd = 1;
-       return 1;
 }
 
-static int file_write_post_select(struct sched *s, struct writer_node *wn)
+static void file_write_close(struct writer_node *wn)
 {
        struct private_file_write_data *pfwd = wn->private_data;
-       struct writer_node_group *wng = wn->wng;
+
+       if (!pfwd)
+               return;
+       close(pfwd->fd);
+       free(pfwd);
+}
+
+static void file_write_post_select(__a_unused struct sched *s,
+               struct task *t)
+{
+       struct writer_node *wn = container_of(t, struct writer_node, task);
+       struct private_file_write_data *pfwd = wn->private_data;
+       struct btr_node *btrn = wn->btrn;
        int ret;
+       char *buf;
+       size_t bytes;
 
-       if (!pfwd->check_fd)
-               return 1;
-       if (*wng->loaded <= wn->written)
-               return 1;
+       t->error = 0;
+       ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
+       if (ret <= 0)
+               goto out;
+       if (!pfwd) {
+               ret = prepare_output_file(wn);
+               goto out;
+       }
        if (!FD_ISSET(pfwd->fd, &s->wfds))
-               return 1;
-//     PARA_INFO_LOG("writing %zd\n", *wng->loaded);
-       ret = write(pfwd->fd, wng->buf + wn->written,
-               *wng->loaded - wn->written);
+               return;
+       bytes = btr_next_buffer(btrn, &buf);
+       assert(bytes > 0);
+       //PARA_INFO_LOG("writing %zu\n", bytes);
+       ret = write(pfwd->fd, buf, bytes);
        if (ret < 0)
-               return -E_FW_WRITE;
-       wn->written += ret;
-       return 1;
+               goto out;
+       btr_consume(btrn, ret);
+out:
+       if (ret < 0)
+               btr_remove_node(btrn);
+       t->error = ret;
 }
 
-static void file_write_close(struct writer_node *wn)
+__malloc static void *file_write_parse_config_or_die(const char *options)
 {
-       struct private_file_write_data *pfwd = wn->private_data;
-       close(pfwd->fd);
-       free(pfwd);
+       struct file_write_args_info *conf = para_calloc(sizeof(*conf));
+
+       /* exits on errors */
+       file_cmdline_parser_string(options, conf, "file_write");
+       return conf;
 }
 
-__malloc static void *file_write_parse_config(const char *options)
+static void file_write_free_config(void *conf)
 {
-       PARA_INFO_LOG("options: %s\n", options);
-       struct file_write_args_info *conf
-               = para_calloc(sizeof(struct file_write_args_info));
-       int ret = file_cmdline_parser_string(options, conf, "file_write");
-       PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
-       if (!ret)
-               return conf;
-       free(conf);
-       return NULL;
+       file_cmdline_parser_free(conf);
 }
 
 /** the init function of the file writer */
 void file_write_init(struct writer *w)
 {
-       w->open = file_write_open;
+       struct file_write_args_info dummy;
+
+       file_cmdline_parser_init(&dummy);
        w->pre_select = file_write_pre_select;
        w->post_select = file_write_post_select;
-       w->parse_config = file_write_parse_config;
+       w->parse_config_or_die = file_write_parse_config_or_die;
+       w->free_config = file_write_free_config;
        w->close = file_write_close;
        w->shutdown = NULL; /* nothing to do */
+       w->help = (struct ggo_help) {
+               .short_help = file_write_args_info_help,
+               .detailed_help = file_write_args_info_detailed_help
+       };
+       file_cmdline_parser_free(&dummy);
 }