2 * Copyright (C) 2006-2010 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>
19 #include "buffer_tree.h"
21 #include "write_common.h"
24 #include "file_write.cmdline.h"
27 /** Data specific to the file writer. */
28 struct private_file_write_data
{
29 /** The file descriptor of the output file. */
34 * Get a random filename.
36 * This is by no means a secure way to create temporary files in a hostile
37 * directory like \p /tmp. However, we use it only for creating temp files in
38 * ~/.paraslash, for which it is OK. Result must be freed by the caller.
40 __must_check __malloc
static char *random_filename(void)
42 char *result
, *home
= para_homedir();
45 gettimeofday(&tv
, NULL
);
47 result
= make_message("%s/.paraslash/%08lu", home
,
48 para_random(99999999));
53 static void file_write_open(struct writer_node
*wn
)
55 struct private_file_write_data
*pfwd
= para_calloc(sizeof(*pfwd
));
57 wn
->private_data
= pfwd
;
61 static int prepare_output_file(struct writer_node
*wn
)
63 struct file_write_args_info
*conf
= wn
->conf
;
64 struct private_file_write_data
*pfwd
= wn
->private_data
;
68 if (conf
->filename_given
)
69 filename
= conf
->filename_arg
;
71 filename
= random_filename();
72 ret
= para_open(filename
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
);
73 if (!conf
->filename_given
)
78 ret
= mark_fd_blocking(pfwd
->fd
);
87 static void file_write_pre_select(struct sched
*s
, struct task
*t
)
89 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
90 struct private_file_write_data
*pfwd
= wn
->private_data
;
94 ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
95 if (ret
> 0 && pfwd
->fd
>= 0)
96 para_fd_set(pfwd
->fd
, &s
->wfds
, &s
->max_fileno
);
97 else if (ret
!= 0) /* error or bos and fd not yet open */
101 static void file_write_close(struct writer_node
*wn
)
103 struct private_file_write_data
*pfwd
= wn
->private_data
;
110 static void file_write_post_select(__a_unused
struct sched
*s
,
113 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
114 struct private_file_write_data
*pfwd
= wn
->private_data
;
115 struct btr_node
*btrn
= wn
->btrn
;
121 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
125 ret
= prepare_output_file(wn
);
129 if (!FD_ISSET(pfwd
->fd
, &s
->wfds
))
131 bytes
= btr_next_buffer(btrn
, &buf
);
133 //PARA_INFO_LOG("writing %zu\n", bytes);
134 ret
= write(pfwd
->fd
, buf
, bytes
);
137 btr_consume(btrn
, ret
);
140 btr_remove_node(btrn
);
144 __malloc
static void *file_write_parse_config_or_die(const char *options
)
146 struct file_write_args_info
*conf
= para_calloc(sizeof(*conf
));
148 /* exits on errors */
149 file_cmdline_parser_string(options
, conf
, "file_write");
153 static void file_write_free_config(void *conf
)
155 file_cmdline_parser_free(conf
);
158 /** the init function of the file writer */
159 void file_write_init(struct writer
*w
)
161 struct file_write_args_info dummy
;
163 file_cmdline_parser_init(&dummy
);
164 w
->open
= file_write_open
;
165 w
->pre_select
= file_write_pre_select
;
166 w
->post_select
= file_write_post_select
;
167 w
->parse_config_or_die
= file_write_parse_config_or_die
;
168 w
->free_config
= file_write_free_config
;
169 w
->close
= file_write_close
;
170 w
->shutdown
= NULL
; /* nothing to do */
171 w
->help
= (struct ggo_help
) {
172 .short_help
= file_write_args_info_help
,
173 .detailed_help
= file_write_args_info_detailed_help
175 file_cmdline_parser_free(&dummy
);