2 * Copyright (C) 2006-2012 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>
17 #include "buffer_tree.h"
19 #include "write_common.h"
22 #include "file_write.cmdline.h"
25 /** Data specific to the file writer. */
26 struct private_file_write_data
{
27 /** The file descriptor of the output file. */
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 prepare_output_file(struct writer_node
*wn
)
53 struct file_write_args_info
*conf
= wn
->conf
;
56 struct private_file_write_data
*pfwd
= para_calloc(sizeof(*pfwd
));
58 if (conf
->filename_given
)
59 filename
= conf
->filename_arg
;
61 filename
= random_filename();
62 ret
= para_open(filename
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
);
63 if (!conf
->filename_given
)
68 ret
= mark_fd_blocking(pfwd
->fd
);
71 wn
->private_data
= pfwd
;
80 static void file_write_pre_select(struct sched
*s
, struct task
*t
)
82 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
83 struct private_file_write_data
*pfwd
= wn
->private_data
;
84 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
89 return sched_min_delay(s
);
90 para_fd_set(pfwd
->fd
, &s
->wfds
, &s
->max_fileno
);
93 static void file_write_close(struct writer_node
*wn
)
95 struct private_file_write_data
*pfwd
= wn
->private_data
;
103 static void file_write_post_select(__a_unused
struct sched
*s
,
106 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
107 struct private_file_write_data
*pfwd
= wn
->private_data
;
108 struct btr_node
*btrn
= wn
->btrn
;
114 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
118 ret
= prepare_output_file(wn
);
121 if (!FD_ISSET(pfwd
->fd
, &s
->wfds
))
123 bytes
= btr_next_buffer(btrn
, &buf
);
125 //PARA_INFO_LOG("writing %zu\n", bytes);
126 ret
= write(pfwd
->fd
, buf
, bytes
);
129 btr_consume(btrn
, ret
);
132 btr_remove_node(btrn
);
136 __malloc
static void *file_write_parse_config_or_die(const char *options
)
138 struct file_write_args_info
*conf
= para_calloc(sizeof(*conf
));
140 /* exits on errors */
141 file_cmdline_parser_string(options
, conf
, "file_write");
145 static void file_write_free_config(void *conf
)
147 file_cmdline_parser_free(conf
);
150 /** the init function of the file writer */
151 void file_write_init(struct writer
*w
)
153 struct file_write_args_info dummy
;
155 file_cmdline_parser_init(&dummy
);
156 w
->pre_select
= file_write_pre_select
;
157 w
->post_select
= file_write_post_select
;
158 w
->parse_config_or_die
= file_write_parse_config_or_die
;
159 w
->free_config
= file_write_free_config
;
160 w
->close
= file_write_close
;
161 w
->shutdown
= NULL
; /* nothing to do */
162 w
->help
= (struct ggo_help
) {
163 .short_help
= file_write_args_info_help
,
164 .detailed_help
= file_write_args_info_detailed_help
166 file_cmdline_parser_free(&dummy
);