2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file file_write.c simple output plugin for testing purposes */
20 #include "file_write.cmdline.h"
23 /** data specific to the file writer */
24 struct private_file_write_data {
25 /** the file descriptor of the output file */
27 /** non-zero if \a fd was added to the write fd set */
32 * Get a random filename.
34 * This is by no means a secure way to create temporary files in a hostile
35 * directory like \p /tmp. However, we use it only for creating temp files in
36 * ~/.paraslash, for which it is OK. Result must be freed by the caller.
38 __must_check __malloc static char *random_filename(void)
40 char *result, *home = para_homedir();
43 gettimeofday(&tv, NULL);
45 result = make_message("%s/.paraslash/%08lu", home,
46 para_random(99999999));
51 static int file_write_open(struct writer_node *wn)
53 struct private_file_write_data *pfwd = para_calloc(
54 sizeof(struct private_file_write_data));
55 struct file_write_args_info *conf = wn->conf;
58 if (conf->filename_given)
59 filename = conf->filename_arg;
61 filename = random_filename();
62 wn->private_data = pfwd;
63 pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
64 if (!conf->filename_given)
72 static int file_write_pre_select(struct sched *s, struct writer_node *wn)
74 struct private_file_write_data *pfwd = wn->private_data;
75 struct writer_node_group *wng = wn->wng;
82 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
87 static int file_write_post_select(struct sched *s, struct writer_node *wn)
89 struct private_file_write_data *pfwd = wn->private_data;
90 struct writer_node_group *wng = wn->wng;
95 if (*wng->loaded <= wn->written)
97 if (!FD_ISSET(pfwd->fd, &s->wfds))
99 // PARA_INFO_LOG("writing %zd\n", *wng->loaded);
100 ret = write(pfwd->fd, *wng->bufp + wn->written,
101 *wng->loaded - wn->written);
108 static void file_write_close(struct writer_node *wn)
110 struct private_file_write_data *pfwd = wn->private_data;
115 __malloc static void *file_write_parse_config(const char *options)
117 struct file_write_args_info *conf
118 = para_calloc(sizeof(struct file_write_args_info));
119 int ret = file_cmdline_parser_string(options, conf, "file_write");
121 PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
128 /** the init function of the file writer */
129 void file_write_init(struct writer *w)
131 struct file_write_args_info dummy;
133 file_cmdline_parser_init(&dummy);
134 w->open = file_write_open;
135 w->pre_select = file_write_pre_select;
136 w->post_select = file_write_post_select;
137 w->parse_config = file_write_parse_config;
138 w->close = file_write_close;
139 w->shutdown = NULL; /* nothing to do */
140 w->help = (struct ggo_help) {
141 .short_help = file_write_args_info_help,
142 .detailed_help = file_write_args_info_detailed_help