~ubuntu-branches/ubuntu/saucy/lvm2/saucy-proposed

« back to all changes in this revision

Viewing changes to lib/misc/lvm-string.c

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2012-08-14 14:35:57 UTC
  • mfrom: (3.1.25 sid)
  • Revision ID: package-import@ubuntu.com-20120814143557-93aill2tp3kf3o30
Tags: 2.02.95-4ubuntu1
* Merge from Debian unstable, remaining changes:
  - debian/patches/avoid-dev-block.patch: Prefer any other device name over
    names in /dev/block/ since lvm.conf won't handle this.
  - debian/rules:
    - copy .po file to .pot file for Rosetta (Ubuntu specific).
  - debian/{dmsetup,lvm2}-udeb.install:
    - install initramfs and udev hooks in udebs (Debian bug 504341).
  - auto-start VGs as their PVs are discovered (Ubuntu specific):
    - add debian/tree/lvm2/lib/udev/rules.d/85-lvm2.rules: use watershed plus
      the sledgehammer of vgscan/vgchange to turn on VGs as they come online.
    - debian/tree/lvm2/usr/share/initramfs-tools/scripts/hooks/lvm2:
      - add 85-lvm2.rules to the list of udev rules to copy.
      - depend on udev.
    - debian/control:
      - add versioned Depend on watershed in lvm2 for udev rules.
      - add Depends on watershed-udeb in lvm2-udeb for udev rules.
      - add versioned Depend/Breaks on udev in dmsetup for udev rules.
      - add Depend on initramfs-tools in dmsetup so system is not potentially
        rendered unbootable by out-of-order dpkg configuration.
    - debian/rules:
      - do not install local-top scripts since Ubuntu mounts root using udev.
      - do not install init scripts for lvm2, since udev starts LVM.
    - debian/lvm2.postinst: handle missing lvm2 init script.
    - debian/tree/dmsetup/lib/udev/rules.d/60-persistent-storage-dm.rules:
      watch dm devices for changes with inotify
  - add mountroot failure hooks to help fix bad boots (Debian bug 468115):
    - debian/tree/lvm2/usr/share/initramfs-tools/scripts/init-premount/lvm2
  - remaining changes to upstream event manager packages (Debian bug 514706):
    - debian/rules:
      - enable dmeventd during configure.
    - debian/dmeventd.{8,manpages}: install dmeventd files.
  - rename debian/clvm.defaults to debian/clvm.default so it is installed
    correctly.
  - debian/control: add dmsetup-udeb to libdevmapper1.02.1-udeb recommends.
  - debian/rules: make sure dmsetup and lvm2 initramfs-tools scripts are
    executable.  When the Ubuntu-specific ones are added with a patch,
    they may lose their executable bit.
  - Add and install clvmd resource agent
  - Add dependency on libudev-dev to libdevmapper-dev so that the .pc file
    works.
  - debian/{clvmd.ra,clvm.init}:
    - create /run/lvm if it doesn't exist.
  - debian/clvm.init:
    - exit 3 if not running on status action.
  - Call dh_installman so that our dmeventd manpage actually gets installed
  - Install the missing fsadm manpage.

 * libdevmapper-dev:
  - move .so symlinks and pkgconfig files to multiarched locations.
  - mark libdevmapper-dev M-A: same

 * libdevmapper-event1.02.1:
  - Add Breaks: dmeventd (<< 2.02.95-4ubuntu1) due to debian symbol rename

 * debian/lvm2.{preinst,postinst,postrm}:
  - Implement removal of obsolete /etc/init.d/lvm2 conffile, which
    should not have been re-introduced in Quantal.

 * Dropped Changes, included in Debian:
  - Mostly included packages for upstream event manager (Debian bug 514706).
  - debian/patches/rules-subdir.patch: removed as reordering will cause
    build failure with dmeventd.
  - debian/patches/libdm-event-static.patch: removed as other static libs
    aren't being built anymore either.
  - Update symbols for libdevmapper-event.
  - Update libdevmapper-event, dmeventd descriptions to match Debian
    boilerplate.

 * Disappeared Changes:
  - Don't install documentation in udebs. No diff found, but no docs are
    installed into udebs either.

 * Resurected Changes:
  - corrected dropping the wrong init script. Now clvm.init is shipped
    and lvm2.init is dropped in favor of udev rules as per original
    intention (LP: #1037033).

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
}
45
45
 
46
46
/*
47
 
 * Count occurences of 'c' in 'str' until we reach a null char.
48
 
 *
49
 
 * Returns:
50
 
 *  len - incremented for each char we encounter.
51
 
 *  count - number of occurrences of 'c' and 'c2'.
52
 
 */
53
 
static void _count_chars(const char *str, size_t *len, int *count,
54
 
                         const int c1, const int c2)
55
 
{
56
 
        const char *ptr;
57
 
 
58
 
        for (ptr = str; *ptr; ptr++, (*len)++)
59
 
                if (*ptr == c1 || *ptr == c2)
60
 
                        (*count)++;
61
 
}
62
 
 
63
 
/*
64
 
 * Count occurences of 'c' in 'str' of length 'size'.
65
 
 *
66
 
 * Returns:
67
 
 *   Number of occurrences of 'c'
68
 
 */
69
 
unsigned count_chars(const char *str, size_t len, const int c)
70
 
{
71
 
        size_t i;
72
 
        unsigned count = 0;
73
 
 
74
 
        for (i = 0; i < len; i++)
75
 
                if (str[i] == c)
76
 
                        count++;
77
 
 
78
 
        return count;
79
 
}
80
 
 
81
 
/*
82
 
 * Length of string after escaping double quotes and backslashes.
83
 
 */
84
 
size_t escaped_len(const char *str)
85
 
{
86
 
        size_t len = 1;
87
 
        int count = 0;
88
 
 
89
 
        _count_chars(str, &len, &count, '\"', '\\');
90
 
 
91
 
        return count + len;
92
 
}
93
 
 
94
 
/*
95
 
 * Copies a string, quoting orig_char with quote_char.
96
 
 * Optionally also quote quote_char.
97
 
 */
98
 
static void _quote_characters(char **out, const char *src,
99
 
                              const int orig_char, const int quote_char,
100
 
                              int quote_quote_char)
101
 
{
102
 
        while (*src) {
103
 
                if (*src == orig_char ||
104
 
                    (*src == quote_char && quote_quote_char))
105
 
                        *(*out)++ = quote_char;
106
 
 
107
 
                *(*out)++ = *src++;
108
 
        }
109
 
}
110
 
 
111
 
static void _unquote_one_character(char *src, const char orig_char,
112
 
                                   const char quote_char)
113
 
{
114
 
        char *out;
115
 
        char s, n;
116
 
 
117
 
        /* Optimise for the common case where no changes are needed. */
118
 
        while ((s = *src++)) {
119
 
                if (s == quote_char &&
120
 
                    ((n = *src) == orig_char || n == quote_char)) {
121
 
                        out = src++;
122
 
                        *(out - 1) = n;
123
 
 
124
 
                        while ((s = *src++)) {
125
 
                                if (s == quote_char &&
126
 
                                    ((n = *src) == orig_char || n == quote_char)) {
127
 
                                        s = n;
128
 
                                        src++;
129
 
                                }
130
 
                                *out = s;
131
 
                                out++;
132
 
                        }
133
 
 
134
 
                        *out = '\0';
135
 
                        return;
136
 
                }
137
 
        }
138
 
}
139
 
 
140
 
/*
141
 
 * Unquote each character given in orig_char array and unquote quote_char
142
 
 * as well. Also save the first occurrence of each character from orig_char
143
 
 * that was found unquoted in arr_substr_first_unquoted array. This way we can
144
 
 * process several characters in one go.
145
 
 */
146
 
static void _unquote_characters(char *src, const char *orig_chars,
147
 
                                size_t num_orig_chars,
148
 
                                const char quote_char,
149
 
                                char *arr_substr_first_unquoted[])
150
 
{
151
 
        char *out = src;
152
 
        char c, s, n;
153
 
        unsigned i;
154
 
 
155
 
        while ((s = *src++)) {
156
 
                for (i = 0; i < num_orig_chars; i++) {
157
 
                        c = orig_chars[i];
158
 
                        if (s == quote_char &&
159
 
                            ((n = *src) == c || n == quote_char)) {
160
 
                                s = n;
161
 
                                src++;
162
 
                                break;
163
 
                        }
164
 
                        if (arr_substr_first_unquoted && (s == c) &&
165
 
                            !arr_substr_first_unquoted[i])
166
 
                                arr_substr_first_unquoted[i] = out;
167
 
                };
168
 
                *out++ = s;
169
 
        }
170
 
 
171
 
        *out = '\0';
172
 
}
173
 
 
174
 
/*
175
 
 * Copies a string, quoting hyphens with hyphens.
176
 
 */
177
 
static void _quote_hyphens(char **out, const char *src)
178
 
{
179
 
        _quote_characters(out, src, '-', '-', 0);
180
 
}
181
 
 
182
 
/*
183
 
 * <vg>-<lv>-<layer> or if !layer just <vg>-<lv>.
184
 
 */
185
 
char *build_dm_name(struct dm_pool *mem, const char *vgname,
186
 
                    const char *lvname, const char *layer)
187
 
{
188
 
        size_t len = 1;
189
 
        int hyphens = 1;
190
 
        char *r, *out;
191
 
 
192
 
        _count_chars(vgname, &len, &hyphens, '-', 0);
193
 
        _count_chars(lvname, &len, &hyphens, '-', 0);
194
 
 
195
 
        if (layer && *layer) {
196
 
                _count_chars(layer, &len, &hyphens, '-', 0);
197
 
                hyphens++;
198
 
        }
199
 
 
200
 
        len += hyphens;
201
 
 
202
 
        if (!(r = dm_pool_alloc(mem, len))) {
203
 
                log_error("build_dm_name: Allocation failed for %" PRIsize_t
204
 
                          " for %s %s %s.", len, vgname, lvname, layer);
205
 
                return NULL;
206
 
        }
207
 
 
208
 
        out = r;
209
 
        _quote_hyphens(&out, vgname);
210
 
        *out++ = '-';
211
 
        _quote_hyphens(&out, lvname);
212
 
 
213
 
        if (layer && *layer) {
214
 
                /* No hyphen if the layer begins with _ e.g. _mlog */
215
 
                if (*layer != '_')
216
 
                        *out++ = '-';
217
 
                _quote_hyphens(&out, layer);
218
 
        }
219
 
        *out = '\0';
220
 
 
221
 
        return r;
222
 
}
223
 
 
224
 
char *build_dm_uuid(struct dm_pool *mem, const char *lvid, const char *layer)
225
 
{
226
 
        char *dmuuid;
227
 
        size_t len;
228
 
 
229
 
        if (!layer)
230
 
                layer = "";
231
 
 
232
 
        len = sizeof(UUID_PREFIX) + strlen(lvid) + strlen(layer) + 1;
233
 
 
234
 
        if (!(dmuuid = dm_pool_alloc(mem, len))) {
235
 
                log_error("build_dm_name: Allocation failed for %" PRIsize_t
236
 
                          " %s %s.", len, lvid, layer);
237
 
                return NULL;
238
 
        }
239
 
 
240
 
        sprintf(dmuuid, UUID_PREFIX "%s%s%s", lvid, (*layer) ? "-" : "", layer);
241
 
 
242
 
        return dmuuid;
243
 
}
244
 
 
245
 
/*
246
 
 * Copies a string, quoting double quotes with backslashes.
247
 
 */
248
 
char *escape_double_quotes(char *out, const char *src)
249
 
{
250
 
        char *buf = out;
251
 
 
252
 
        _quote_characters(&buf, src, '\"', '\\', 1);
253
 
        *buf = '\0';
254
 
 
255
 
        return out;
256
 
}
257
 
 
258
 
/*
259
 
 * Undo quoting in situ.
260
 
 */
261
 
void unescape_double_quotes(char *src)
262
 
{
263
 
        _unquote_one_character(src, '\"', '\\');
264
 
}
265
 
 
266
 
/*
267
 
 * Unescape colons and "at" signs in situ and save the substrings
268
 
 * starting at the position of the first unescaped colon and the
269
 
 * first unescaped "at" sign. This is normally used to unescape
270
 
 * device names used as PVs.
271
 
 */
272
 
void unescape_colons_and_at_signs(char *src,
273
 
                                  char **substr_first_unquoted_colon,
274
 
                                  char **substr_first_unquoted_at_sign)
275
 
{
276
 
        const char *orig_chars = ":@";
277
 
        char *arr_substr_first_unquoted[] = {NULL, NULL, NULL};
278
 
 
279
 
        _unquote_characters(src, orig_chars, 2, '\\', arr_substr_first_unquoted);
280
 
 
281
 
        if (substr_first_unquoted_colon)
282
 
                *substr_first_unquoted_colon = arr_substr_first_unquoted[0];
283
 
 
284
 
        if (substr_first_unquoted_at_sign)
285
 
                *substr_first_unquoted_at_sign = arr_substr_first_unquoted[1];
286
 
}
287
 
 
288
 
/*
289
47
 * A-Za-z0-9._-+/=!:&#
290
48
 */
291
49
int validate_tag(const char *n)
292
50
{
293
51
        register char c;
294
 
        register int len = 0;
 
52
        /* int len = 0; */
295
53
 
296
54
        if (!n || !*n)
297
55
                return 0;
298
56
 
299
 
        while ((len++, c = *n++))
 
57
        /* FIXME: Is unlimited tag size support needed ? */
 
58
        while ((/* len++, */ c = *n++))
300
59
                if (!isalnum(c) && c != '.' && c != '_' && c != '-' && c != '+' && c != '/'
301
60
                    && c != '=' && c != '!' && c != ':' && c != '&' && c != '#')
302
61
                        return 0;
337
96
 
338
97
int apply_lvname_restrictions(const char *name)
339
98
{
340
 
        if (!strncmp(name, "snapshot", 8)) {
341
 
                log_error("Names starting \"snapshot\" are reserved. "
342
 
                          "Please choose a different LV name.");
343
 
                return 0;
344
 
        }
345
 
 
346
 
        if (!strncmp(name, "pvmove", 6)) {
347
 
                log_error("Names starting \"pvmove\" are reserved. "
348
 
                          "Please choose a different LV name.");
349
 
                return 0;
350
 
        }
351
 
 
352
 
        if (strstr(name, "_mlog")) {
353
 
                log_error("Names including \"_mlog\" are reserved. "
354
 
                          "Please choose a different LV name.");
355
 
                return 0;
356
 
        }
357
 
 
358
 
        if (strstr(name, "_mimage")) {
359
 
                log_error("Names including \"_mimage\" are reserved. "
360
 
                          "Please choose a different LV name.");
361
 
                return 0;
362
 
        }
363
 
 
364
 
        if (strstr(name, "_rimage")) {
365
 
                log_error("Names including \"_rimage\" are reserved. "
366
 
                          "Please choose a different LV name.");
367
 
                return 0;
368
 
        }
369
 
 
370
 
        if (strstr(name, "_rmeta")) {
371
 
                log_error("Names including \"_rmeta\" are reserved. "
372
 
                          "Please choose a different LV name.");
373
 
                return 0;
374
 
        }
375
 
 
376
 
        if (strstr(name, "_vorigin")) {
377
 
                log_error("Names including \"_vorigin\" are reserved. "
378
 
                          "Please choose a different LV name.");
379
 
                return 0;
 
99
        const char *reserved_prefixes[] = {
 
100
                "snapshot",
 
101
                "pvmove",
 
102
                NULL
 
103
        };
 
104
 
 
105
        const char *reserved_strings[] = {
 
106
                "_mlog",
 
107
                "_mimage",
 
108
                "_rimage",
 
109
                "_rmeta",
 
110
                "_vorigin",
 
111
                "_tdata",
 
112
                "_tmeta",
 
113
                NULL
 
114
        };
 
115
 
 
116
        unsigned i;
 
117
        const char *s;
 
118
 
 
119
        for (i = 0; (s = reserved_prefixes[i]); i++) {
 
120
                if (!strncmp(name, s, strlen(s))) {
 
121
                        log_error("Names starting \"%s\" are reserved. "
 
122
                                  "Please choose a different LV name.", s);
 
123
                        return 0;
 
124
                }
 
125
        }
 
126
 
 
127
        for (i = 0; (s = reserved_strings[i]); i++) {
 
128
                if (strstr(name, s)) {
 
129
                        log_error("Names including \"%s\" are reserved. "
 
130
                                  "Please choose a different LV name.", s);
 
131
                        return 0;
 
132
                }
380
133
        }
381
134
 
382
135
        return 1;
392
145
 
393
146
        return rc;
394
147
}
 
148
 
 
149
char *build_dm_uuid(struct dm_pool *mem, const char *lvid,
 
150
                    const char *layer)
 
151
{
 
152
        return dm_build_dm_uuid(mem, UUID_PREFIX, lvid, layer);
 
153
}