]> git.tuebingen.mpg.de Git - paraslash.git/blob - write_common.c
6ff778a92d0451ab0500c2dc8b88fbff02e30323
[paraslash.git] / write_common.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file write_common.c common functions of para_audiod and para_write */
20
21 #include "para.h"
22 #include "string.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "write.h"
26 #include "error.h"
27
28 const char *writer_names[] ={WRITER_NAMES};
29 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
30
31 int wng_write(struct writer_node_group *g, char *buf, size_t *loaded)
32 {
33         int ret, i, need_more_writes = 1;
34         size_t min_written = 0;
35
36         while (need_more_writes) {
37                 need_more_writes = 0;
38                 FOR_EACH_WRITER_NODE(i, g) {
39                         size_t w = g->written[i];
40                         int bytes_to_write;
41                         struct writer_node *wn = &g->writer_nodes[i];
42                         if (!i)
43                                 min_written = w;
44                         else
45                                 min_written = PARA_MIN(min_written, w);
46                         if (w == *loaded)
47                                 continue;
48                         if (!g->eof && (*loaded < wn->chunk_bytes + w))
49                                 continue;
50                         bytes_to_write = PARA_MIN(wn->chunk_bytes,
51                                 *loaded - w);
52                         ret = wn->writer->write(buf + w, bytes_to_write, wn);
53                         if (ret < 0)
54                                 goto out;
55                         if (ret != bytes_to_write)
56                                 PARA_WARNING_LOG("short write: %d/%d\n", ret,
57                                         bytes_to_write);
58                         g->written[i] += ret;
59                         need_more_writes = 1;
60                 }
61         }
62         *loaded -= min_written;
63         ret = 0;
64         if (g->eof)
65                 goto out;
66         if (*loaded)
67                 memmove(buf, buf + min_written, *loaded);
68         FOR_EACH_WRITER_NODE(i, g)
69                 g->written[i] -= min_written;
70         ret = 1;
71 out:
72         return ret;
73 }
74
75 static void wng_post_select(struct sched *s, struct task *t)
76 {
77         struct writer_node_group *g = t->private_data;
78         int i;
79         size_t min_written = 0;
80
81         FOR_EACH_WRITER_NODE(i, g) {
82                 struct writer_node *wn = &g->writer_nodes[i];
83                 t->ret = wn->task.ret;
84                 if (t->ret < 0)
85                         return;
86                 if (!i)
87                         min_written = t->ret;
88                 else
89                         min_written = PARA_MIN(min_written, t->ret);
90         }
91         *g->loaded -= min_written;
92         if (!*g->loaded && g->eof)
93                 t->ret = 0;
94         else
95                 t->ret = 1;
96         if (*g->loaded && min_written)
97                 memmove(g->buf, g->buf + min_written, *g->loaded);
98 }
99
100 int wng_open(struct writer_node_group *g)
101 {
102         int i, ret = 1;
103
104         FOR_EACH_WRITER_NODE(i, g) {
105                 struct writer_node *wn = &g->writer_nodes[i];
106                 ret = wn->writer->open(wn);
107                 if (ret < 0)
108                         goto out;
109                 wn->chunk_bytes = ret;
110                 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
111                 wn->wng = g;
112                 PARA_DEBUG_LOG("pre_select: %p\n", &wn->writer->pre_select);
113                 PARA_DEBUG_LOG("post_select: %p\n", &wn->writer->post_select);
114                 wn->task.pre_select = wn->writer->pre_select;
115                 wn->task.post_select = wn->writer->post_select;
116                 wn->task.private_data = wn;
117                 register_task(&wn->task);
118         }
119         register_task(&g->task);
120 out:
121         return ret;
122 }
123
124 void wng_destroy(struct writer_node_group *g)
125 {
126         if (!g)
127                 return;
128         free(g->written);
129         free(g->writer_nodes);
130         free(g);
131 }
132
133 void wng_close(struct writer_node_group *g)
134 {
135         int i;
136
137         FOR_EACH_WRITER_NODE(i, g) {
138                 struct writer_node *wn = &g->writer_nodes[i];
139                 unregister_task(&wn->task);
140                 wn->writer->close(wn);
141         }
142 }
143
144 static void wng_error_handler(struct task *t)
145 {
146         struct writer_node_group *g = t->private_data;
147
148         PARA_INFO_LOG("%p: ret = %d\n", t, t->ret);
149         unregister_task(t);
150         wng_close(g);
151         wng_destroy(g);
152 }
153
154 struct writer_node_group *wng_new(unsigned num_writers)
155 {
156         struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
157         g->num_writers = num_writers;
158         g->writer_nodes = para_calloc(num_writers
159                 * sizeof(struct writer_node));
160         g->written = para_calloc(num_writers * sizeof(size_t));
161         g->task.private_data = g;
162         g->task.post_select = wng_post_select;
163         g->task.error_handler = wng_error_handler;
164         g->task.flags = POST_ADD_TAIL | POST_EOF_IS_ERROR;
165         return g;
166 }
167
168 void init_supported_writers(void)
169 {
170         int i;
171
172         FOR_EACH_WRITER(i)
173                 writers[i].init(&writers[i]);
174 }
175
176 int check_writer_arg(const char *arg)
177 {
178         int i, ret = -E_WRITE_COMMON_SYNTAX;
179         char *a = para_strdup(arg), *p = strchr(a, ':');
180         if (p)
181                 *p = '\0';
182         p++;
183         FOR_EACH_WRITER(i) {
184                 if (strcmp(writer_names[i], a))
185                         continue;
186                 ret = i;
187                 goto out;
188         }
189 out:
190         free(a);
191         return ret;
192 }
193
194 struct writer_node_group *setup_default_wng(void)
195 {
196         struct writer_node_group *wng = wng_new(1);
197         enum writer_enum default_writer;
198
199         if (NUM_SUPPORTED_WRITERS == 1)
200                 default_writer = FILE_WRITE;
201         else
202                 default_writer = 1;
203         wng->writer_nodes[0].writer = &writers[default_writer];
204         PARA_INFO_LOG("using default writer: %s\n",
205                 writer_names[default_writer]);
206         return wng;
207 }