2 * Copyright (C) 2006-2012 Red Hat, Inc. All rights reserved.
4 * This file is part of the device-mapper userspace tools.
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU Lesser General Public License v.2.1.
10 * You should have received a copy of the GNU Lesser General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * consume characters while they match the predicate function.
22 static char *_consume(char *buffer
, int (*fn
) (int))
24 while (*buffer
&& fn(*buffer
))
30 static int _isword(int c
)
36 * Split buffer into NULL-separated words in argv.
37 * Returns number of words.
39 int dm_split_words(char *buffer
, unsigned max
,
40 unsigned ignore_comments
__attribute__((unused
)),
45 for (arg
= 0; arg
< max
; arg
++) {
46 buffer
= _consume(buffer
, isspace
);
51 buffer
= _consume(buffer
, _isword
);
63 * Remove hyphen quoting from a component of a name.
64 * NULL-terminates the component and returns start of next component.
66 static char *_unquote(char *component
)
92 int dm_split_lvm_name(struct dm_pool
*mem
, const char *dmname
,
93 char **vgname
, char **lvname
, char **layer
)
95 if (mem
&& !(*vgname
= dm_pool_strdup(mem
, dmname
)))
98 _unquote(*layer
= _unquote(*lvname
= _unquote(*vgname
)));
104 * On error, up to glibc 2.0.6, snprintf returned -1 if buffer was too small;
105 * From glibc 2.1 it returns number of chars (excl. trailing null) that would
106 * have been written had there been room.
108 * dm_snprintf reverts to the old behaviour.
110 int dm_snprintf(char *buf
, size_t bufsize
, const char *format
, ...)
115 va_start(ap
, format
);
116 n
= vsnprintf(buf
, bufsize
, format
, ap
);
119 if (n
< 0 || ((unsigned) n
+ 1 > bufsize
))
125 const char *dm_basename(const char *path
)
127 const char *p
= strrchr(path
, '/');
129 return p
? p
+ 1 : path
;
132 int dm_vasprintf(char **result
, const char *format
, va_list aq
)
136 char *buf
= dm_malloc(size
);
145 n
= vsnprintf(buf
, size
, format
, ap
);
148 if (0 <= n
&& n
< size
)
152 /* Up to glibc 2.0.6 returns -1 */
153 size
= (n
< 0) ? size
* 2 : n
+ 1;
154 if (!(buf
= dm_malloc(size
)))
159 /* Reallocating more then once? */
160 if (!(*result
= dm_strdup(buf
))) {
171 int dm_asprintf(char **result
, const char *format
, ...)
175 va_start(ap
, format
);
176 r
= dm_vasprintf(result
, format
, ap
);
182 * Count occurences of 'c' in 'str' until we reach a null char.
185 * len - incremented for each char we encounter.
186 * count - number of occurrences of 'c' and 'c2'.
188 static void _count_chars(const char *str
, size_t *len
, int *count
,
189 const int c1
, const int c2
)
193 for (ptr
= str
; *ptr
; ptr
++, (*len
)++)
194 if (*ptr
== c1
|| *ptr
== c2
)
199 * Count occurrences of 'c' in 'str' of length 'size'.
202 * Number of occurrences of 'c'
204 unsigned dm_count_chars(const char *str
, size_t len
, const int c
)
209 for (i
= 0; i
< len
; i
++)
217 * Length of string after escaping double quotes and backslashes.
219 size_t dm_escaped_len(const char *str
)
224 _count_chars(str
, &len
, &count
, '\"', '\\');
230 * Copies a string, quoting orig_char with quote_char.
231 * Optionally also quote quote_char.
233 static void _quote_characters(char **out
, const char *src
,
234 const int orig_char
, const int quote_char
,
235 int quote_quote_char
)
238 if (*src
== orig_char
||
239 (*src
== quote_char
&& quote_quote_char
))
240 *(*out
)++ = quote_char
;
246 static void _unquote_one_character(char *src
, const char orig_char
,
247 const char quote_char
)
252 /* Optimise for the common case where no changes are needed. */
253 while ((s
= *src
++)) {
254 if (s
== quote_char
&&
255 ((n
= *src
) == orig_char
|| n
== quote_char
)) {
259 while ((s
= *src
++)) {
260 if (s
== quote_char
&&
261 ((n
= *src
) == orig_char
|| n
== quote_char
)) {
276 * Unquote each character given in orig_char array and unquote quote_char
277 * as well. Also save the first occurrence of each character from orig_char
278 * that was found unquoted in arr_substr_first_unquoted array. This way we can
279 * process several characters in one go.
281 static void _unquote_characters(char *src
, const char *orig_chars
,
282 size_t num_orig_chars
,
283 const char quote_char
,
284 char *arr_substr_first_unquoted
[])
290 while ((s
= *src
++)) {
291 for (i
= 0; i
< num_orig_chars
; i
++) {
293 if (s
== quote_char
&&
294 ((n
= *src
) == c
|| n
== quote_char
)) {
299 if (arr_substr_first_unquoted
&& (s
== c
) &&
300 !arr_substr_first_unquoted
[i
])
301 arr_substr_first_unquoted
[i
] = out
;
310 * Copies a string, quoting hyphens with hyphens.
312 static void _quote_hyphens(char **out
, const char *src
)
314 _quote_characters(out
, src
, '-', '-', 0);
318 * <vg>-<lv>-<layer> or if !layer just <vg>-<lv>.
320 char *dm_build_dm_name(struct dm_pool
*mem
, const char *vgname
,
321 const char *lvname
, const char *layer
)
327 _count_chars(vgname
, &len
, &hyphens
, '-', 0);
328 _count_chars(lvname
, &len
, &hyphens
, '-', 0);
330 if (layer
&& *layer
) {
331 _count_chars(layer
, &len
, &hyphens
, '-', 0);
337 if (!(r
= dm_pool_alloc(mem
, len
))) {
338 log_error("build_dm_name: Allocation failed for %" PRIsize_t
339 " for %s %s %s.", len
, vgname
, lvname
, layer
);
344 _quote_hyphens(&out
, vgname
);
346 _quote_hyphens(&out
, lvname
);
348 if (layer
&& *layer
) {
349 /* No hyphen if the layer begins with _ e.g. _mlog */
352 _quote_hyphens(&out
, layer
);
359 char *dm_build_dm_uuid(struct dm_pool
*mem
, const char *uuid_prefix
, const char *lvid
, const char *layer
)
367 len
= strlen(uuid_prefix
) + strlen(lvid
) + strlen(layer
) + 2;
369 if (!(dmuuid
= dm_pool_alloc(mem
, len
))) {
370 log_error("build_dm_name: Allocation failed for %" PRIsize_t
371 " %s %s.", len
, lvid
, layer
);
375 sprintf(dmuuid
, "%s%s%s%s", uuid_prefix
, lvid
, (*layer
) ? "-" : "", layer
);
381 * Copies a string, quoting double quotes with backslashes.
383 char *dm_escape_double_quotes(char *out
, const char *src
)
387 _quote_characters(&buf
, src
, '\"', '\\', 1);
394 * Undo quoting in situ.
396 void dm_unescape_double_quotes(char *src
)
398 _unquote_one_character(src
, '\"', '\\');
402 * Unescape colons and "at" signs in situ and save the substrings
403 * starting at the position of the first unescaped colon and the
404 * first unescaped "at" sign. This is normally used to unescape
405 * device names used as PVs.
407 void dm_unescape_colons_and_at_signs(char *src
,
408 char **substr_first_unquoted_colon
,
409 char **substr_first_unquoted_at_sign
)
411 const char *orig_chars
= ":@";
412 char *arr_substr_first_unquoted
[] = {NULL
, NULL
, NULL
};
414 _unquote_characters(src
, orig_chars
, 2, '\\', arr_substr_first_unquoted
);
416 if (substr_first_unquoted_colon
)
417 *substr_first_unquoted_colon
= arr_substr_first_unquoted
[0];
419 if (substr_first_unquoted_at_sign
)
420 *substr_first_unquoted_at_sign
= arr_substr_first_unquoted
[1];
423 int dm_strncpy(char *dest
, const char *src
, size_t n
)
425 if (memccpy(dest
, src
, 0, n
))