2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file file_write.c simple output plugin for testing purposes */
10 #include <sys/types.h>
16 #include "buffer_tree.h"
18 #include "write_common.h"
21 #include "file_write.cmdline.h"
24 /** Data specific to the file writer. */
25 struct private_file_write_data {
26 /** The file descriptor of the output file. */
31 * Get a random filename.
33 * This is by no means a secure way to create temporary files in a hostile
34 * directory like \p /tmp. However, we use it only for creating temp files in
35 * ~/.paraslash, for which it is OK. Result must be freed by the caller.
37 __must_check __malloc static char *random_filename(void)
39 char *result, *home = para_homedir();
41 srandom(clock_get_realtime(NULL)->tv_usec);
42 result = make_message("%s/.paraslash/%08lu", home,
43 para_random(99999999));
48 static int prepare_output_file(struct writer_node *wn)
50 struct file_write_args_info *conf = wn->conf;
53 struct private_file_write_data *pfwd = para_calloc(sizeof(*pfwd));
55 if (conf->filename_given)
56 filename = conf->filename_arg;
58 filename = random_filename();
59 ret = para_open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
60 if (!conf->filename_given)
65 ret = mark_fd_blocking(pfwd->fd);
68 wn->private_data = pfwd;
77 static void file_write_pre_select(struct sched *s, void *context)
79 struct writer_node *wn = context;
80 struct private_file_write_data *pfwd = wn->private_data;
81 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
86 return sched_min_delay(s);
87 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
90 static void file_write_close(struct writer_node *wn)
92 struct private_file_write_data *pfwd = wn->private_data;
100 static int file_write_post_select(__a_unused struct sched *s, void *context)
102 struct writer_node *wn = context;
103 struct private_file_write_data *pfwd = wn->private_data;
104 struct btr_node *btrn = wn->btrn;
109 ret = task_get_notification(wn->task);
112 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
116 ret = prepare_output_file(wn);
119 if (!FD_ISSET(pfwd->fd, &s->wfds))
121 bytes = btr_next_buffer(btrn, &buf);
123 //PARA_INFO_LOG("writing %zu\n", bytes);
124 ret = xwrite(pfwd->fd, buf, bytes);
127 btr_consume(btrn, ret);
130 btr_remove_node(&wn->btrn);
134 __malloc static void *file_write_parse_config_or_die(int argc, char **argv)
136 struct file_write_args_info *conf = para_calloc(sizeof(*conf));
138 /* exits on errors */
139 file_write_cmdline_parser(argc, argv, conf);
143 static void file_write_free_config(void *conf)
145 file_write_cmdline_parser_free(conf);
148 /** the init function of the file writer */
149 void file_write_init(struct writer *w)
151 struct file_write_args_info dummy;
153 file_write_cmdline_parser_init(&dummy);
154 w->pre_select = file_write_pre_select;
155 w->post_select = file_write_post_select;
156 w->parse_config_or_die = file_write_parse_config_or_die;
157 w->free_config = file_write_free_config;
158 w->close = file_write_close;
159 w->help = (struct ggo_help)DEFINE_GGO_HELP(file_write);
160 file_write_cmdline_parser_free(&dummy);