2 * Copyright (C) 2006-2011 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>
18 #include "buffer_tree.h"
20 #include "write_common.h"
23 #include "file_write.cmdline.h"
26 /** Data specific to the file writer. */
27 struct private_file_write_data
{
28 /** The file descriptor of the output file. */
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 prepare_output_file(struct writer_node
*wn
)
54 struct file_write_args_info
*conf
= wn
->conf
;
57 struct private_file_write_data
*pfwd
= para_calloc(sizeof(*pfwd
));
59 if (conf
->filename_given
)
60 filename
= conf
->filename_arg
;
62 filename
= random_filename();
63 ret
= para_open(filename
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
);
64 if (!conf
->filename_given
)
69 ret
= mark_fd_blocking(pfwd
->fd
);
72 wn
->private_data
= pfwd
;
81 static void file_write_pre_select(struct sched
*s
, struct task
*t
)
83 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
84 struct private_file_write_data
*pfwd
= wn
->private_data
;
85 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
90 return sched_min_delay(s
);
91 para_fd_set(pfwd
->fd
, &s
->wfds
, &s
->max_fileno
);
94 static void file_write_close(struct writer_node
*wn
)
96 struct private_file_write_data
*pfwd
= wn
->private_data
;
104 static void file_write_post_select(__a_unused
struct sched
*s
,
107 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
108 struct private_file_write_data
*pfwd
= wn
->private_data
;
109 struct btr_node
*btrn
= wn
->btrn
;
115 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
119 ret
= prepare_output_file(wn
);
122 if (!FD_ISSET(pfwd
->fd
, &s
->wfds
))
124 bytes
= btr_next_buffer(btrn
, &buf
);
126 //PARA_INFO_LOG("writing %zu\n", bytes);
127 ret
= write(pfwd
->fd
, buf
, bytes
);
130 btr_consume(btrn
, ret
);
133 btr_remove_node(btrn
);
137 __malloc
static void *file_write_parse_config_or_die(const char *options
)
139 struct file_write_args_info
*conf
= para_calloc(sizeof(*conf
));
141 /* exits on errors */
142 file_cmdline_parser_string(options
, conf
, "file_write");
146 static void file_write_free_config(void *conf
)
148 file_cmdline_parser_free(conf
);
151 /** the init function of the file writer */
152 void file_write_init(struct writer
*w
)
154 struct file_write_args_info dummy
;
156 file_cmdline_parser_init(&dummy
);
157 w
->pre_select
= file_write_pre_select
;
158 w
->post_select
= file_write_post_select
;
159 w
->parse_config_or_die
= file_write_parse_config_or_die
;
160 w
->free_config
= file_write_free_config
;
161 w
->close
= file_write_close
;
162 w
->shutdown
= NULL
; /* nothing to do */
163 w
->help
= (struct ggo_help
) {
164 .short_help
= file_write_args_info_help
,
165 .detailed_help
= file_write_args_info_detailed_help
167 file_cmdline_parser_free(&dummy
);