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 */
10 #include <sys/types.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 */
28 /** non-zero if \a fd was added to the write fd set */
33 * Get a random filename.
35 * This is by no means a secure way to create temporary files in a hostile
36 * directory like \p /tmp. However, we use it only for creating temp files in
37 * ~/.paraslash, for which it is OK. Result must be freed by the caller.
39 __must_check __malloc static char *random_filename(void)
41 char *result, *home = para_homedir();
44 gettimeofday(&tv, NULL);
46 result = make_message("%s/.paraslash/%08lu", home,
47 para_random(99999999));
52 static int file_write_open(struct writer_node *wn)
54 struct private_file_write_data *pfwd = para_calloc(
55 sizeof(struct private_file_write_data));
56 struct file_write_args_info *conf = wn->conf;
59 if (conf->filename_given)
60 filename = conf->filename_arg;
62 filename = random_filename();
63 wn->private_data = pfwd;
64 pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
65 if (!conf->filename_given)
73 static int file_write_pre_select(struct sched *s, struct writer_node *wn)
75 struct private_file_write_data *pfwd = wn->private_data;
76 struct writer_node_group *wng = wn->wng;
83 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
88 static int file_write_post_select(struct sched *s, struct writer_node *wn)
90 struct private_file_write_data *pfwd = wn->private_data;
91 struct writer_node_group *wng = wn->wng;
96 if (*wng->loaded <= wn->written)
98 if (!FD_ISSET(pfwd->fd, &s->wfds))
100 // PARA_INFO_LOG("writing %zd\n", *wng->loaded);
101 ret = write(pfwd->fd, *wng->bufp + wn->written,
102 *wng->loaded - wn->written);
109 static void file_write_close(struct writer_node *wn)
111 struct private_file_write_data *pfwd = wn->private_data;
116 __malloc static void *file_write_parse_config(const char *options)
118 struct file_write_args_info *conf
119 = para_calloc(sizeof(struct file_write_args_info));
120 int ret = file_cmdline_parser_string(options, conf, "file_write");
122 PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
129 /** the init function of the file writer */
130 void file_write_init(struct writer *w)
132 struct file_write_args_info dummy;
134 file_cmdline_parser_init(&dummy);
135 w->open = file_write_open;
136 w->pre_select = file_write_pre_select;
137 w->post_select = file_write_post_select;
138 w->parse_config = file_write_parse_config;
139 w->close = file_write_close;
140 w->shutdown = NULL; /* nothing to do */
141 w->help = (struct ggo_help) {
142 .short_help = file_write_args_info_help,
143 .detailed_help = file_write_args_info_detailed_help
145 file_cmdline_parser_free(&dummy);