writers: Unify ->pre_select().
[paraslash.git] / file_write.c
1 /*
2 * Copyright (C) 2006-2010 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file file_write.c simple output plugin for testing purposes */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <sys/time.h>
13 #include <stdbool.h>
14
15 #include "para.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "string.h"
23 #include "fd.h"
24 #include "file_write.cmdline.h"
25 #include "error.h"
26
27 /** Data specific to the file writer. */
28 struct private_file_write_data {
29 /** The file descriptor of the output file. */
30 int fd;
31 };
32
33 /*
34 * Get a random filename.
35 *
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.
39 */
40 __must_check __malloc static char *random_filename(void)
41 {
42 char *result, *home = para_homedir();
43 struct timeval tv;
44
45 gettimeofday(&tv, NULL);
46 srandom(tv.tv_usec);
47 result = make_message("%s/.paraslash/%08lu", home,
48 para_random(99999999));
49 free(home);
50 return result;
51 }
52
53 static void file_write_open(struct writer_node *wn)
54 {
55 struct private_file_write_data *pfwd = para_calloc(sizeof(*pfwd));
56
57 wn->private_data = pfwd;
58 pfwd->fd = -1;
59 }
60
61 static int prepare_output_file(struct writer_node *wn)
62 {
63 struct file_write_args_info *conf = wn->conf;
64 struct private_file_write_data *pfwd = wn->private_data;
65 char *filename;
66 int ret;
67
68 if (conf->filename_given)
69 filename = conf->filename_arg;
70 else
71 filename = random_filename();
72 ret = para_open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
73 if (!conf->filename_given)
74 free(filename);
75 if (ret < 0)
76 goto out;
77 pfwd->fd = ret;
78 ret = mark_fd_blocking(pfwd->fd);
79 if (ret >= 0)
80 return 1;
81 close(pfwd->fd);
82 out:
83 free(pfwd);
84 return ret;
85 }
86
87 static void file_write_pre_select(struct sched *s, struct task *t)
88 {
89 struct writer_node *wn = container_of(t, struct writer_node, task);
90 struct private_file_write_data *pfwd = wn->private_data;
91 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
92
93 if (ret == 0)
94 return;
95 if (ret < 0 || pfwd->fd < 0)
96 return sched_min_delay(s);
97 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
98 }
99
100 static void file_write_close(struct writer_node *wn)
101 {
102 struct private_file_write_data *pfwd = wn->private_data;
103
104 if (pfwd->fd >= 0)
105 close(pfwd->fd);
106 free(pfwd);
107 }
108
109 static void file_write_post_select(__a_unused struct sched *s,
110 struct task *t)
111 {
112 struct writer_node *wn = container_of(t, struct writer_node, task);
113 struct private_file_write_data *pfwd = wn->private_data;
114 struct btr_node *btrn = wn->btrn;
115 int ret;
116 char *buf;
117 size_t bytes;
118
119 t->error = 0;
120 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
121 if (ret <= 0)
122 goto out;
123 if (pfwd->fd < 0) {
124 ret = prepare_output_file(wn);
125 if (ret < 0)
126 goto out;
127 }
128 if (!FD_ISSET(pfwd->fd, &s->wfds))
129 return;
130 bytes = btr_next_buffer(btrn, &buf);
131 assert(bytes > 0);
132 //PARA_INFO_LOG("writing %zu\n", bytes);
133 ret = write(pfwd->fd, buf, bytes);
134 if (ret < 0)
135 goto out;
136 btr_consume(btrn, ret);
137 out:
138 if (ret < 0)
139 btr_remove_node(btrn);
140 t->error = ret;
141 }
142
143 __malloc static void *file_write_parse_config_or_die(const char *options)
144 {
145 struct file_write_args_info *conf = para_calloc(sizeof(*conf));
146
147 /* exits on errors */
148 file_cmdline_parser_string(options, conf, "file_write");
149 return conf;
150 }
151
152 static void file_write_free_config(void *conf)
153 {
154 file_cmdline_parser_free(conf);
155 }
156
157 /** the init function of the file writer */
158 void file_write_init(struct writer *w)
159 {
160 struct file_write_args_info dummy;
161
162 file_cmdline_parser_init(&dummy);
163 w->open = file_write_open;
164 w->pre_select = file_write_pre_select;
165 w->post_select = file_write_post_select;
166 w->parse_config_or_die = file_write_parse_config_or_die;
167 w->free_config = file_write_free_config;
168 w->close = file_write_close;
169 w->shutdown = NULL; /* nothing to do */
170 w->help = (struct ggo_help) {
171 .short_help = file_write_args_info_help,
172 .detailed_help = file_write_args_info_detailed_help
173 };
174 file_cmdline_parser_free(&dummy);
175 }