1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
Copyright (c) 2010-2011 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include <sys/uio.h>
#include "common-utils.h"
#include "xlator.h"
#include "graph-mem-types.h"
#include "graph-utils.h"
struct gf_printer {
ssize_t (*write) (struct gf_printer *gp, char *buf, size_t len);
void *priv;
};
static ssize_t
gp_write_file (struct gf_printer *gp, char *buf, size_t len)
{
FILE *f = gp->priv;
if (fwrite (buf, len, 1, f) != 1) {
gf_log ("graph-print", GF_LOG_ERROR, "fwrite failed (%s)",
strerror (errno));
return -1;
}
return len;
}
static ssize_t
gp_write_buf (struct gf_printer *gp, char *buf, size_t len)
{
struct iovec *iov = gp->priv;
if (iov->iov_len < len) {
gf_log ("graph-print", GF_LOG_ERROR, "buffer full");
return -1;
}
memcpy (iov->iov_base, buf, len);
iov->iov_base += len;
iov->iov_len -= len;
return len;
}
static int
gpprintf (struct gf_printer *gp, const char *format, ...)
{
va_list arg;
char *str = NULL;
int ret = 0;
va_start (arg, format);
ret = gf_vasprintf (&str, format, arg);
va_end (arg);
if (ret < 0)
return ret;
ret = gp->write (gp, str, ret);
GF_FREE (str);
return ret;
}
static int
glusterfs_graph_print (struct gf_printer *gp, glusterfs_graph_t *graph)
{
#define GPPRINTF(gp, fmt, ...) do { \
ret = gpprintf (gp, fmt, ## __VA_ARGS__); \
if (ret == -1) \
goto out; \
else \
len += ret; \
} while (0)
xlator_t *trav = NULL;
data_pair_t *pair = NULL;
xlator_list_t *xch = NULL;
int ret = 0;
ssize_t len = 0;
if (!graph->first)
return 0;
for (trav = graph->first; trav->next; trav = trav->next);
for (; trav; trav = trav->prev) {
GPPRINTF (gp, "volume %s\n type %s\n", trav->name,
trav->type);
for (pair = trav->options->members_list; pair && pair->next;
pair = pair->next);
for (; pair; pair = pair->prev)
GPPRINTF (gp, " option %s %s\n", pair->key,
pair->value->data);
if (trav->children) {
GPPRINTF (gp, " subvolumes");
for (xch = trav->children; xch; xch = xch->next)
GPPRINTF (gp, " %s", xch->xlator->name);
GPPRINTF (gp, "\n");
}
GPPRINTF (gp, "end-volume\n");
if (trav != graph->first)
GPPRINTF (gp, "\n");
}
out:
if (ret == -1) {
gf_log ("graph-print", GF_LOG_ERROR, "printing failed");
return -1;
}
return len;
#undef GPPRINTF
}
int
glusterfs_graph_print_file (FILE *file, glusterfs_graph_t *graph)
{
struct gf_printer gp = { .write = gp_write_file,
.priv = file
};
return glusterfs_graph_print (&gp, graph);
}
char *
glusterfs_graph_print_buf (glusterfs_graph_t *graph)
{
FILE *f = NULL;
struct iovec iov = {0,};
int len = 0;
char *buf = NULL;
struct gf_printer gp = { .write = gp_write_buf,
.priv = &iov
};
f = fopen ("/dev/null", "a");
if (!f) {
gf_log ("graph-print", GF_LOG_ERROR,
"cannot open /dev/null (%s)", strerror (errno));
return NULL;
}
len = glusterfs_graph_print_file (f, graph);
fclose (f);
if (len == -1)
return NULL;
buf = GF_CALLOC (1, len + 1, gf_graph_mt_buf);
if (!buf) {
return NULL;
}
iov.iov_base = buf;
iov.iov_len = len;
len = glusterfs_graph_print (&gp, graph);
if (len == -1) {
GF_FREE (buf);
return NULL;
}
return buf;
}
|