2 * Copyright (C) 2006-2013 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>
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
, struct task
*t
)
79 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
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
,
103 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
104 struct private_file_write_data
*pfwd
= wn
->private_data
;
105 struct btr_node
*btrn
= wn
->btrn
;
110 ret
= task_get_notification(t
);
113 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
117 ret
= prepare_output_file(wn
);
120 if (!FD_ISSET(pfwd
->fd
, &s
->wfds
))
122 bytes
= btr_next_buffer(btrn
, &buf
);
124 //PARA_INFO_LOG("writing %zu\n", bytes);
125 ret
= xwrite(pfwd
->fd
, buf
, bytes
);
128 btr_consume(btrn
, ret
);
131 btr_remove_node(&wn
->btrn
);
135 __malloc
static void *file_write_parse_config_or_die(int argc
, char **argv
)
137 struct file_write_args_info
*conf
= para_calloc(sizeof(*conf
));
139 /* exits on errors */
140 file_write_cmdline_parser(argc
, argv
, conf
);
144 static void file_write_free_config(void *conf
)
146 file_write_cmdline_parser_free(conf
);
149 /** the init function of the file writer */
150 void file_write_init(struct writer
*w
)
152 struct file_write_args_info dummy
;
154 file_write_cmdline_parser_init(&dummy
);
155 w
->pre_select
= file_write_pre_select
;
156 w
->post_select
= file_write_post_select
;
157 w
->parse_config_or_die
= file_write_parse_config_or_die
;
158 w
->free_config
= file_write_free_config
;
159 w
->close
= file_write_close
;
160 w
->help
= (struct ggo_help
)DEFINE_GGO_HELP(file_write
);
161 file_write_cmdline_parser_free(&dummy
);