Makefile.in: kill sample_conf variable
[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 #include "para.h"
20 #include "string.h"
21 #include "write.h"
22 #include "error.h"
23
24 const char *writer_names[] ={WRITER_NAMES};
25 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
26
27 int wng_write(struct writer_node_group *g, char *buf, size_t *loaded)
28 {
29         int ret, i, need_more_writes = 1;
30         size_t min_written = 0;
31
32         while (need_more_writes) {
33                 need_more_writes = 0;
34                 FOR_EACH_WRITER_NODE(i, g) {
35                         size_t w = g->written[i];
36                         unsigned char *p = buf + w;
37                         int bytes_to_write;
38                         struct writer_node *wn = &g->writer_nodes[i];
39                         if (!i)
40                                 min_written = w;
41                         else
42                                 min_written = PARA_MIN(min_written, w);
43                         if (w == *loaded)
44                                 continue;
45                         if (!g->eof && (*loaded < wn->chunk_bytes + w))
46                                 continue;
47                         bytes_to_write = PARA_MIN(wn->chunk_bytes,
48                                 *loaded - w);
49                         ret = wn->writer->write(p, bytes_to_write, wn);
50                         if (ret < 0)
51                                 goto out;
52                         if (ret != bytes_to_write)
53                                 PARA_WARNING_LOG("short write: %d/%d\n", ret,
54                                         bytes_to_write);
55                         g->written[i] += ret;
56                         need_more_writes = 1;
57                 }
58         }
59         *loaded -= min_written;
60         ret = 0;
61         if (g->eof)
62                 goto out;
63         if (*loaded)
64                 memmove(buf, buf + min_written, *loaded);
65         FOR_EACH_WRITER_NODE(i, g)
66                 g->written[i] -= min_written;
67         ret = 1;
68 out:
69         return ret;
70 }
71
72 int wng_open(struct writer_node_group *g)
73 {
74         int i, ret = 1;
75
76         FOR_EACH_WRITER_NODE(i, g) {
77                 struct writer_node *wn = &g->writer_nodes[i];
78                 ret = wn->writer->open(wn);
79                 if (ret < 0)
80                         goto out;
81                 wn->chunk_bytes = ret;
82                 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
83         }
84 out:
85         return ret;
86 }
87
88 void wng_close(struct writer_node_group *g)
89 {
90         int i;
91
92         FOR_EACH_WRITER_NODE(i, g) {
93                 struct writer_node *wn = &g->writer_nodes[i];
94                 wn->writer->close(wn);
95         }
96 }
97
98 struct writer_node_group *wng_new(unsigned num_writers)
99 {
100         struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
101         g->num_writers = num_writers;
102         g->writer_nodes = para_calloc(num_writers
103                 * sizeof(struct writer_node));
104         g->written = para_calloc(num_writers * sizeof(size_t));
105         return g;
106 }
107
108 void wng_destroy(struct writer_node_group *g)
109 {
110         if (!g)
111                 return;
112         free(g->written);
113         free(g->writer_nodes);
114         free(g);
115 }
116
117 void init_supported_writers(void)
118 {
119         int i;
120
121         FOR_EACH_WRITER(i)
122                 writers[i].init(&writers[i]);
123 }
124
125 int check_writer_arg(const char *arg)
126 {
127         int i, ret = -E_WRITE_COMMON_SYNTAX;
128         char *a = para_strdup(arg), *p = strchr(a, ':');
129         if (p)
130                 *p = '\0';
131         p++;
132         FOR_EACH_WRITER(i) {
133                 if (strcmp(writer_names[i], a))
134                         continue;
135                 ret = i;
136                 goto out;
137         }
138 out:
139         free(a);
140         return ret;
141 }
142
143 struct writer_node_group *setup_default_wng(void)
144 {
145         struct writer_node_group *wng = wng_new(1);
146         enum writer_enum default_writer;
147
148         if (NUM_SUPPORTED_WRITERS == 1)
149                 default_writer = FILE_WRITE;
150         else
151                 default_writer = 1;
152         wng->writer_nodes[0].writer = &writers[default_writer];
153         PARA_INFO_LOG("using default writer: %s\n",
154                 writer_names[default_writer]);
155         return wng;
156 }