Makefile.in: Add afs_command_list.man to the list of server commands.
[paraslash.git] / write_common.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file write_common.c common functions of para_audiod and para_write */
8
9 #include "para.h"
10 #include "string.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "write.h"
14 #include "error.h"
15
16 /** the array containing the names of all supported writers */
17 const char *writer_names[] ={WRITER_NAMES};
18
19 /** the array of supported writers */
20 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
21
22 static void wng_pre_select(__a_unused struct sched *s, struct task *t)
23 {
24         struct writer_node_group *g = t->private_data;
25         int i;
26
27         FOR_EACH_WRITER_NODE(i, g) {
28                 struct writer_node *wn = &g->writer_nodes[i];
29                 t->ret = wn->writer->pre_select(s, wn);
30                 if (t->ret < 0) {
31                         g->eof = 1;
32                         return;
33                 }
34         }
35 }
36
37 static void wng_post_select(struct sched *s, struct task *t)
38 {
39         struct writer_node_group *g = t->private_data;
40         int i;
41         size_t min_written = 0;
42
43         FOR_EACH_WRITER_NODE(i, g) {
44                 struct writer_node *wn = &g->writer_nodes[i];
45                 t->ret = wn->writer->post_select(s, wn);
46                 if (t->ret < 0) {
47                         g->eof = 1;
48                         return;
49                 }
50                 if (!i)
51                         min_written = wn->written;
52                 else
53                         min_written = PARA_MIN(min_written, wn->written);
54         }
55 //      PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
56         if (min_written) {
57                 *g->loaded -= min_written;
58                 FOR_EACH_WRITER_NODE(i, g)
59                         g->writer_nodes[i].written -= min_written;
60         }
61         if (!*g->loaded && *g->input_eof) {
62                 g->eof = 1;
63                 t->ret = -E_WNG_EOF;
64                 return;
65         }
66         t->ret = 1;
67         if (*g->loaded && min_written) {
68 //              PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
69                 memmove(g->buf, g->buf + min_written, *g->loaded);
70         }
71 }
72
73 /**
74  * call the open function of each writer in the group
75  *
76  * \param g the writer node group
77  *
78  * \return If at least one open function returned an error, all successful
79  * writer notes get closed and this error value is returned. Upon success, a
80  * task associated with \a g is registered to the scheduler and the function
81  * returnes a positive value.
82  * */
83 int wng_open(struct writer_node_group *g)
84 {
85         int i, ret = 1;
86
87         PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
88         register_task(&g->task);
89         FOR_EACH_WRITER_NODE(i, g) {
90                 struct writer_node *wn = &g->writer_nodes[i];
91                 wn->wng = g;
92                 ret = wn->writer->open(wn);
93                 if (ret < 0)
94                         goto err_out;
95                 wn->chunk_bytes = ret;
96                 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
97         }
98         sprintf(g->task.status, "%s", "writer node group");
99         g->eof = 0;
100         return 1;
101 err_out:
102         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
103         unregister_task(&g->task);
104         while (i > 0) {
105                 struct writer_node *wn = &g->writer_nodes[--i];
106                 wn->writer->close(wn);
107         }
108         g->num_writers = 0;
109         g->eof = 1;
110         return ret;
111 }
112
113
114 /**
115  * unregister a writer node group task
116  *
117  * \param g the group whose task is to be closed
118  */
119 void wng_unregister(struct writer_node_group *g)
120 {
121         unregister_task(&g->task);
122         g->eof = 1;
123 }
124
125 /**
126  * call the close function of each writer in the given group
127  *
128  * \param g the writer node group to close
129  *
130  * This function also frees all resources of the given group.
131  */
132 void wng_close(struct writer_node_group *g)
133 {
134         int i;
135
136         if (!g)
137                 return;
138         PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
139         FOR_EACH_WRITER_NODE(i, g) {
140                 struct writer_node *wn = &g->writer_nodes[i];
141                 wn->writer->close(wn);
142         }
143         free(g->writer_nodes);
144         free(g);
145 }
146
147 /**
148  * allocate and initialize a new writer_node_group struct
149  *
150  * \param num_writers the number of writer nodes for the new group
151  *
152  * \return Pointer to the new writer node group
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->task.private_data = g;
161         g->task.post_select = wng_post_select;
162         g->task.pre_select = wng_pre_select;
163         return g;
164 }
165
166 /**
167  * call the init function of each supported paraslash writer
168  */
169 void init_supported_writers(void)
170 {
171         int i;
172
173         FOR_EACH_WRITER(i)
174                 writers[i].init(&writers[i]);
175 }
176 /**
177  * check if given string is a valid command line for any writer
178  *
179  * \param \wa string of the form writer_name:options
180  * \param writer_num contains the number of the writer upon success
181  *
182  * This function checks whether \a wa starts with the name of a supported
183  * paraslash writer, optinally followed by a colon and any options for that
184  * writer.  If a valid writer name was found and further are present, the
185  * remaining part of \a wa is passed to that writer's config parser.
186  *
187  * \return On success, a pointer to the gengetopt args info struct is returned
188  * and \a writer_num contains the number of the writer. Otherwise this function
189  * returns \p NULL.
190  */
191 void *check_writer_arg(const char *wa, int *writer_num)
192 {
193         int i;
194
195         *writer_num = -E_WRITE_COMMON_SYNTAX;
196         PARA_INFO_LOG("checking  %s\n", wa);
197         FOR_EACH_WRITER(i) {
198                 const char *name = writer_names[i];
199                 size_t len = strlen(name);
200                 char c;
201                 if (strlen(wa) < len)
202                         continue;
203                 if (strncmp(name, wa, len))
204                         continue;
205                 c = wa[len];
206                 if (c && c != ' ')
207                         continue;
208                 if (c && !writers[i].parse_config)
209                         return NULL;
210                 *writer_num = i;
211                 return writers[i].parse_config(c? wa + len + 1 : "");
212         }
213         PARA_ERROR_LOG("%s", "writer not found\n");
214         return NULL;
215 }
216
217 /**
218  * setup a writer node group with only one writer, the default writer
219  *
220  * The writer which is set up depends on the OS. It defaults to alsa for Linux,
221  * osx_write for OS X, file writer if neither of these is supported.
222  *
223  * \return pointer to the allocated writer node group
224  */
225 struct writer_node_group *setup_default_wng(void)
226 {
227         struct writer_node_group *wng = wng_new(1);
228         wng->writer_nodes[0].writer = &writers[DEFAULT_WRITER];
229         PARA_INFO_LOG("using default writer: %s %p\n",
230                 writer_names[DEFAULT_WRITER], writers[DEFAULT_WRITER].parse_config);
231         wng->writer_nodes[0].conf = writers[DEFAULT_WRITER].parse_config("");
232         return wng;
233 }