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"
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 */
30 /** non-zero if \a fd was added to the write fd set */
35 * Get a random filename.
37 * This is by no means a secure way to create temporary files in a hostile
38 * directory like \p /tmp. However, we use it only for creating temp files in
39 * ~/.paraslash, for which it is OK. Result must be freed by the caller.
41 __must_check __malloc
static char *random_filename(void)
43 char *result
, *home
= para_homedir();
46 gettimeofday(&tv
, NULL
);
48 result
= make_message("%s/.paraslash/%08lu", home
,
49 para_random(99999999));
54 static int file_write_open(struct writer_node
*wn
)
56 struct private_file_write_data
*pfwd
= para_calloc(
57 sizeof(struct private_file_write_data
));
58 struct file_write_args_info
*conf
= wn
->conf
;
61 if (conf
->filename_given
)
62 filename
= conf
->filename_arg
;
64 filename
= random_filename();
65 wn
->private_data
= pfwd
;
66 pfwd
->fd
= open(filename
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
);
67 if (!conf
->filename_given
)
75 static int file_write_pre_select(struct sched
*s
, struct writer_node
*wn
)
77 struct private_file_write_data
*pfwd
= wn
->private_data
;
78 struct writer_node_group
*wng
= wn
->wng
;
85 para_fd_set(pfwd
->fd
, &s
->wfds
, &s
->max_fileno
);
90 static int file_write_post_select(struct sched
*s
, struct writer_node
*wn
)
92 struct private_file_write_data
*pfwd
= wn
->private_data
;
93 struct writer_node_group
*wng
= wn
->wng
;
98 if (*wng
->loaded
<= wn
->written
)
100 if (!FD_ISSET(pfwd
->fd
, &s
->wfds
))
102 // PARA_INFO_LOG("writing %zd\n", *wng->loaded);
103 ret
= write(pfwd
->fd
, *wng
->bufp
+ wn
->written
,
104 *wng
->loaded
- wn
->written
);
111 static void file_write_close(struct writer_node
*wn
)
113 struct private_file_write_data
*pfwd
= wn
->private_data
;
118 __malloc
static void *file_write_parse_config(const char *options
)
120 struct file_write_args_info
*conf
121 = para_calloc(sizeof(struct file_write_args_info
));
122 int ret
= file_cmdline_parser_string(options
, conf
, "file_write");
124 PARA_INFO_LOG("conf->filename_given: %d\n", conf
->filename_given
);
131 /** the init function of the file writer */
132 void file_write_init(struct writer
*w
)
134 struct file_write_args_info dummy
;
136 file_cmdline_parser_init(&dummy
);
137 w
->open
= file_write_open
;
138 w
->pre_select
= file_write_pre_select
;
139 w
->post_select
= file_write_post_select
;
140 w
->parse_config
= file_write_parse_config
;
141 w
->close
= file_write_close
;
142 w
->shutdown
= NULL
; /* nothing to do */
143 w
->help
= (struct ggo_help
) {
144 .short_help
= file_write_args_info_help
,
145 .detailed_help
= file_write_args_info_detailed_help
147 file_cmdline_parser_free(&dummy
);