Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 : : * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
4 : : *
5 : : * This file is part of LVM2.
6 : : *
7 : : * This copyrighted material is made available to anyone wishing to use,
8 : : * modify, copy, or redistribute it subject to the terms and conditions
9 : : * of the GNU Lesser General Public License v.2.1.
10 : : *
11 : : * You should have received a copy of the GNU Lesser General Public License
12 : : * along with this program; if not, write to the Free Software Foundation,
13 : : * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 : : */
15 : :
16 : : #include "lib.h"
17 : : #include "lvm-string.h"
18 : :
19 : : #include <ctype.h>
20 : :
21 : 0 : int emit_to_buffer(char **buffer, size_t *size, const char *fmt, ...)
22 : : {
23 : : int n;
24 : : va_list ap;
25 : :
26 : 0 : va_start(ap, fmt);
27 : 0 : n = vsnprintf(*buffer, *size, fmt, ap);
28 : 0 : va_end(ap);
29 : :
30 [ # # # # ]: 0 : if (n < 0 || ((size_t)n == *size))
31 : 0 : return 0;
32 : :
33 : 0 : *buffer += n;
34 : 0 : *size -= n;
35 : 0 : return 1;
36 : : }
37 : :
38 : : /*
39 : : * Count occurences of 'c' in 'str' until we reach a null char.
40 : : *
41 : : * Returns:
42 : : * len - incremented for each char we encounter.
43 : : * count - number of occurrences of 'c' and 'c2'.
44 : : */
45 : 0 : static void _count_chars(const char *str, size_t *len, int *count,
46 : : const int c1, const int c2)
47 : : {
48 : : const char *ptr;
49 : :
50 [ # # ]: 0 : for (ptr = str; *ptr; ptr++, (*len)++)
51 [ # # ][ # # ]: 0 : if (*ptr == c1 || *ptr == c2)
52 : 0 : (*count)++;
53 : 0 : }
54 : :
55 : : /*
56 : : * Count occurences of 'c' in 'str' of length 'size'.
57 : : *
58 : : * Returns:
59 : : * Number of occurrences of 'c'
60 : : */
61 : 0 : unsigned count_chars(const char *str, size_t len, const int c)
62 : : {
63 : : size_t i;
64 : 0 : unsigned count = 0;
65 : :
66 [ # # ]: 0 : for (i = 0; i < len; i++)
67 [ # # ]: 0 : if (str[i] == c)
68 : 0 : count++;
69 : :
70 : 0 : return count;
71 : : }
72 : :
73 : : /*
74 : : * Length of string after escaping double quotes and backslashes.
75 : : */
76 : 0 : size_t escaped_len(const char *str)
77 : : {
78 : 0 : size_t len = 1;
79 : 0 : int count = 0;
80 : :
81 : 0 : _count_chars(str, &len, &count, '\"', '\\');
82 : :
83 : 0 : return count + len;
84 : : }
85 : :
86 : : /*
87 : : * Copies a string, quoting orig_char with quote_char.
88 : : * Optionally also quote quote_char.
89 : : */
90 : 0 : static void _quote_characters(char **out, const char *src,
91 : : const int orig_char, const int quote_char,
92 : : int quote_quote_char)
93 : : {
94 [ # # ]: 0 : while (*src) {
95 [ # # ][ # # ]: 0 : if (*src == orig_char ||
[ # # ]
96 : 0 : (*src == quote_char && quote_quote_char))
97 : 0 : *(*out)++ = quote_char;
98 : :
99 : 0 : *(*out)++ = *src++;
100 : : }
101 : 0 : }
102 : :
103 : : /*
104 : : * Unquote orig_char in string.
105 : : * Also unquote quote_char.
106 : : */
107 : 381 : static void _unquote_characters(char *src, const int orig_char,
108 : : const int quote_char)
109 : : {
110 : 381 : char *out = src;
111 : :
112 [ + + ]: 5273 : while (*src) {
113 [ - + ][ # # ]: 4892 : if (*src == quote_char &&
[ # # ]
114 : 0 : (*(src + 1) == orig_char || *(src + 1) == quote_char))
115 : 0 : src++;
116 : :
117 : 4892 : *out++ = *src++;
118 : : }
119 : :
120 : 381 : *out = '\0';
121 : 381 : }
122 : :
123 : : /*
124 : : * Copies a string, quoting hyphens with hyphens.
125 : : */
126 : 0 : static void _quote_hyphens(char **out, const char *src)
127 : : {
128 : 0 : _quote_characters(out, src, '-', '-', 0);
129 : 0 : }
130 : :
131 : : /*
132 : : * <vg>-<lv>-<layer> or if !layer just <vg>-<lv>.
133 : : */
134 : 0 : char *build_dm_name(struct dm_pool *mem, const char *vgname,
135 : : const char *lvname, const char *layer)
136 : : {
137 : 0 : size_t len = 1;
138 : 0 : int hyphens = 1;
139 : : char *r, *out;
140 : :
141 : 0 : _count_chars(vgname, &len, &hyphens, '-', 0);
142 : 0 : _count_chars(lvname, &len, &hyphens, '-', 0);
143 : :
144 [ # # # # ]: 0 : if (layer && *layer) {
145 : 0 : _count_chars(layer, &len, &hyphens, '-', 0);
146 : 0 : hyphens++;
147 : : }
148 : :
149 : 0 : len += hyphens;
150 : :
151 [ # # ]: 0 : if (!(r = dm_pool_alloc(mem, len))) {
152 : 0 : log_error("build_dm_name: Allocation failed for %" PRIsize_t
153 : : " for %s %s %s.", len, vgname, lvname, layer);
154 : 0 : return NULL;
155 : : }
156 : :
157 : 0 : out = r;
158 : 0 : _quote_hyphens(&out, vgname);
159 : 0 : *out++ = '-';
160 : 0 : _quote_hyphens(&out, lvname);
161 : :
162 [ # # # # ]: 0 : if (layer && *layer) {
163 : : /* No hyphen if the layer begins with _ e.g. _mlog */
164 [ # # ]: 0 : if (*layer != '_')
165 : 0 : *out++ = '-';
166 : 0 : _quote_hyphens(&out, layer);
167 : : }
168 : 0 : *out = '\0';
169 : :
170 : 0 : return r;
171 : : }
172 : :
173 : : /*
174 : : * Copies a string, quoting double quotes with backslashes.
175 : : */
176 : 0 : char *escape_double_quotes(char *out, const char *src)
177 : : {
178 : 0 : char *buf = out;
179 : :
180 : 0 : _quote_characters(&buf, src, '\"', '\\', 1);
181 : 0 : *buf = '\0';
182 : :
183 : 0 : return out;
184 : : }
185 : :
186 : : /*
187 : : * Undo quoting in situ.
188 : : */
189 : 381 : void unescape_double_quotes(char *src)
190 : : {
191 : 381 : _unquote_characters(src, '\"', '\\');
192 : 381 : }
193 : :
194 : : /*
195 : : * Device layer names are all of the form <vg>-<lv>-<layer>, any
196 : : * other hyphens that appear in these names are quoted with yet
197 : : * another hyphen. The top layer of any device has no layer
198 : : * name. eg, vg0-lvol0.
199 : : */
200 : 0 : int validate_name(const char *n)
201 : : {
202 : : register char c;
203 : 0 : register int len = 0;
204 : :
205 [ # # ][ # # ]: 0 : if (!n || !*n)
206 : 0 : return 0;
207 : :
208 : : /* Hyphen used as VG-LV separator - ambiguity if LV starts with it */
209 [ # # ]: 0 : if (*n == '-')
210 : 0 : return 0;
211 : :
212 [ # # ][ # # ]: 0 : if (!strcmp(n, ".") || !strcmp(n, ".."))
213 : 0 : return 0;
214 : :
215 [ # # ]: 0 : while ((len++, c = *n++))
216 [ # # ][ # # ]: 0 : if (!isalnum(c) && c != '.' && c != '_' && c != '-' && c != '+')
[ # # ][ # # ]
[ # # ]
217 : 0 : return 0;
218 : :
219 [ # # ]: 0 : if (len > NAME_LEN)
220 : 0 : return 0;
221 : :
222 : 0 : return 1;
223 : : }
|