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>
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 */
31 /** non-zero if \a fd was added to the write fd set */
36 * Get a random filename.
38 * This is by no means a secure way to create temporary files in a hostile
39 * directory like \p /tmp. However, we use it only for creating temp files in
40 * ~/.paraslash, for which it is OK. Result must be freed by the caller.
42 __must_check __malloc
static char *random_filename(void)
44 char *result
, *home
= para_homedir();
47 gettimeofday(&tv
, NULL
);
49 result
= make_message("%s/.paraslash/%08lu", home
,
50 para_random(99999999));
55 static int file_write_open(struct writer_node
*wn
)
57 struct private_file_write_data
*pfwd
= para_calloc(
58 sizeof(struct private_file_write_data
));
59 struct file_write_args_info
*conf
= wn
->conf
;
62 if (conf
->filename_given
)
63 filename
= conf
->filename_arg
;
65 filename
= random_filename();
66 wn
->private_data
= pfwd
;
67 pfwd
->fd
= open(filename
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
);
68 if (!conf
->filename_given
)
76 static int file_write_pre_select(struct sched
*s
, struct writer_node
*wn
)
78 struct private_file_write_data
*pfwd
= wn
->private_data
;
79 struct writer_node_group
*wng
= wn
->wng
;
86 para_fd_set(pfwd
->fd
, &s
->wfds
, &s
->max_fileno
);
91 static void file_write_pre_select_btr(struct sched
*s
, struct task
*t
)
93 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
94 struct private_file_write_data
*pfwd
= wn
->private_data
;
99 ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
101 para_fd_set(pfwd
->fd
, &s
->wfds
, &s
->max_fileno
);
103 } else if (ret
< 0) {
104 s
->timeout
.tv_sec
= 0;
105 s
->timeout
.tv_usec
= 1;
109 static int file_write_post_select(struct sched
*s
, struct writer_node
*wn
)
111 struct private_file_write_data
*pfwd
= wn
->private_data
;
112 struct writer_node_group
*wng
= wn
->wng
;
117 if (*wng
->loaded
<= wn
->written
)
119 if (!FD_ISSET(pfwd
->fd
, &s
->wfds
))
121 // PARA_INFO_LOG("writing %zd\n", *wng->loaded);
122 ret
= write(pfwd
->fd
, *wng
->bufp
+ wn
->written
,
123 *wng
->loaded
- wn
->written
);
130 static void file_write_close(struct writer_node
*wn
)
132 struct private_file_write_data
*pfwd
= wn
->private_data
;
135 file_cmdline_parser_free(wn
->conf
);
139 static void file_write_post_select_btr(__a_unused
struct sched
*s
,
142 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
143 struct private_file_write_data
*pfwd
= wn
->private_data
;
144 struct btr_node
*btrn
= wn
->btrn
;
150 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
157 if (!FD_ISSET(pfwd
->fd
, &s
->wfds
))
159 bytes
= btr_next_buffer(btrn
, &buf
);
161 //PARA_INFO_LOG("writing %zu\n", bytes);
162 ret
= write(pfwd
->fd
, buf
, bytes
);
165 btr_consume(btrn
, ret
);
172 __malloc
static void *file_write_parse_config(const char *options
)
174 struct file_write_args_info
*conf
175 = para_calloc(sizeof(struct file_write_args_info
));
176 int ret
= file_cmdline_parser_string(options
, conf
, "file_write");
178 PARA_INFO_LOG("conf->filename_given: %d\n", conf
->filename_given
);
185 /** the init function of the file writer */
186 void file_write_init(struct writer
*w
)
188 struct file_write_args_info dummy
;
190 file_cmdline_parser_init(&dummy
);
191 w
->open
= file_write_open
;
192 w
->pre_select
= file_write_pre_select
;
193 w
->pre_select_btr
= file_write_pre_select_btr
;
194 w
->post_select
= file_write_post_select
;
195 w
->post_select_btr
= file_write_post_select_btr
;
196 w
->parse_config
= file_write_parse_config
;
197 w
->close
= file_write_close
;
198 w
->shutdown
= NULL
; /* nothing to do */
199 w
->help
= (struct ggo_help
) {
200 .short_help
= file_write_args_info_help
,
201 .detailed_help
= file_write_args_info_detailed_help
203 file_cmdline_parser_free(&dummy
);