]> git.tuebingen.mpg.de Git - paraslash.git/blob - file_write.c
alsa: Use new get_btr_* functions.
[paraslash.git] / file_write.c
1 /*
2  * Copyright (C) 2006-2009 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         /** non-zero if \a fd was added to the write fd set */
32         int check_fd;
33 };
34
35 /*
36  * Get a random filename.
37  *
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.
41  */
42 __must_check __malloc static char *random_filename(void)
43 {
44         char *result, *home = para_homedir();
45         struct timeval tv;
46
47         gettimeofday(&tv, NULL);
48         srandom(tv.tv_usec);
49         result = make_message("%s/.paraslash/%08lu", home,
50                 para_random(99999999));
51         free(home);
52         return result;
53 }
54
55 static int file_write_open(struct writer_node *wn)
56 {
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;
60         char *filename;
61
62         if (conf->filename_given)
63                 filename = conf->filename_arg;
64         else
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)
69                 free(filename);
70         if (pfwd->fd >= 0)
71                 return 1;
72         free(pfwd);
73         return -E_FW_OPEN;
74 }
75
76 static int file_write_pre_select(struct sched *s, struct writer_node *wn)
77 {
78         struct private_file_write_data *pfwd = wn->private_data;
79         struct writer_node_group *wng = wn->wng;
80
81         pfwd->check_fd = 0;
82         if (pfwd->fd <= 0)
83                 return -E_FW_NO_FILE;
84         if (!*wng->loaded)
85                 return 1;
86         para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
87         pfwd->check_fd = 1;
88         return 1;
89 }
90
91 static void file_write_pre_select_btr(struct sched *s, struct task *t)
92 {
93         struct writer_node *wn = container_of(t, struct writer_node, task);
94         struct private_file_write_data *pfwd = wn->private_data;
95         int ret;
96
97         t->error = 0;
98         pfwd->check_fd = 0;
99         ret = btr_node_status(wn->btrn, wn->min_iqs);
100         if (ret >= 0) {
101                 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
102                 pfwd->check_fd = 1;
103         }
104         if (ret != 0) {
105                 s->timeout.tv_sec = 0;
106                 s->timeout.tv_usec = 1;
107         }
108 }
109
110 static int file_write_post_select(struct sched *s, struct writer_node *wn)
111 {
112         struct private_file_write_data *pfwd = wn->private_data;
113         struct writer_node_group *wng = wn->wng;
114         int ret;
115
116         if (!pfwd->check_fd)
117                 return 1;
118         if (*wng->loaded <= wn->written)
119                 return 1;
120         if (!FD_ISSET(pfwd->fd, &s->wfds))
121                 return 1;
122 //      PARA_INFO_LOG("writing %zd\n", *wng->loaded);
123         ret = write(pfwd->fd, *wng->bufp + wn->written,
124                 *wng->loaded - wn->written);
125         if (ret < 0)
126                 return -E_FW_WRITE;
127         wn->written += ret;
128         return 1;
129 }
130
131 static void file_write_close(struct writer_node *wn)
132 {
133         struct private_file_write_data *pfwd = wn->private_data;
134
135         close(pfwd->fd);
136         file_cmdline_parser_free(wn->conf);
137         free(pfwd);
138 }
139
140 static void file_write_post_select_btr(__a_unused struct sched *s,
141                 struct task *t)
142 {
143         struct writer_node *wn = container_of(t, struct writer_node, task);
144         struct private_file_write_data *pfwd = wn->private_data;
145         struct btr_node *btrn = wn->btrn;
146         int ret;
147         char *buf;
148         size_t bytes;
149
150         t->error = 0;
151         ret = btr_node_status(btrn, wn->min_iqs);
152         if (ret == 0)
153                 return;
154         if (ret < 0)
155                 goto err;
156         if (!pfwd->check_fd)
157                 return;
158         if (!FD_ISSET(pfwd->fd, &s->wfds))
159                 return;
160         bytes = btr_next_buffer(btrn, &buf);
161         assert(bytes > 0);
162         //PARA_INFO_LOG("writing %zu\n", bytes);
163         ret = write(pfwd->fd, buf, bytes);
164         if (ret < 0)
165                 goto err;
166         btr_consume(btrn, ret);
167         return;
168 err:
169         assert(ret < 0);
170         t->error = ret;
171 }
172
173 __malloc static void *file_write_parse_config(const char *options)
174 {
175         struct file_write_args_info *conf
176                 = para_calloc(sizeof(struct file_write_args_info));
177         int ret = file_cmdline_parser_string(options, conf, "file_write");
178
179         PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
180         if (!ret)
181                 return conf;
182         free(conf);
183         return NULL;
184 }
185
186 /** the init function of the file writer */
187 void file_write_init(struct writer *w)
188 {
189         struct file_write_args_info dummy;
190
191         file_cmdline_parser_init(&dummy);
192         w->open = file_write_open;
193         w->pre_select = file_write_pre_select;
194         w->pre_select_btr = file_write_pre_select_btr;
195         w->post_select = file_write_post_select;
196         w->post_select_btr = file_write_post_select_btr;
197         w->parse_config = file_write_parse_config;
198         w->close = file_write_close;
199         w->shutdown = NULL; /* nothing to do */
200         w->help = (struct ggo_help) {
201                 .short_help = file_write_args_info_help,
202                 .detailed_help = file_write_args_info_detailed_help
203         };
204         file_cmdline_parser_free(&dummy);
205 }