~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to shlibs/blkid/src/superblocks/superblocks.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * superblocks.c - reads information from filesystem and raid superblocks
3
 
 *
4
 
 * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
5
 
 *
6
 
 * This file may be redistributed under the terms of the
7
 
 * GNU Lesser General Public License.
8
 
 */
9
 
 
10
 
#include <stdio.h>
11
 
#include <string.h>
12
 
#include <stdlib.h>
13
 
#include <unistd.h>
14
 
#include <fcntl.h>
15
 
#include <ctype.h>
16
 
#include <sys/types.h>
17
 
#include <sys/stat.h>
18
 
#include <errno.h>
19
 
#include <stdint.h>
20
 
#include <stdarg.h>
21
 
 
22
 
#include "superblocks.h"
23
 
 
24
 
/**
25
 
 * SECTION:superblocks
26
 
 * @title: Superblocks probing
27
 
 * @short_description: filesystems and raids superblocks probing.
28
 
 *
29
 
 * The library API has been originaly designed for superblocks probing only.
30
 
 * This is reason why some *deprecated* superblock specific functions don't use
31
 
 * '_superblocks_' namespace in the function name. Please, don't use these
32
 
 * functions in new code.
33
 
 *
34
 
 * The 'superblocks' probers support NAME=value (tags) interface only. The
35
 
 * superblocks probing is enabled by default (and controled by
36
 
 * blkid_probe_enable_superblocks()).
37
 
 *
38
 
 * Currently supported tags:
39
 
 *
40
 
 * @TYPE: filesystem type
41
 
 *
42
 
 * @SEC_TYPE: secondary filesystem type
43
 
 *
44
 
 * @LABEL: filesystem label
45
 
 *
46
 
 * @LABEL_RAW: raw label from FS superblock
47
 
 *
48
 
 * @UUID: filesystem UUID (lower case)
49
 
 *
50
 
 * @UUID_SUB: subvolume uuid (e.g. btrfs)
51
 
 *
52
 
 * @UUID_RAW: raw UUID from FS superblock
53
 
 *
54
 
 * @EXT_JOURNAL: external journal UUID
55
 
 *
56
 
 * @USAGE:  usage string: "raid", "filesystem", ...
57
 
 *
58
 
 * @VERSION: filesystem version
59
 
 *
60
 
 * @MOUNT: cluster mount name (?) -- ocfs only
61
 
 *
62
 
 * @SBMAGIC: super block magic string
63
 
 *
64
 
 * @SBMAGIC_OFFSET: offset of SBMAGIC
65
 
 *
66
 
 * @FSSIZE: size of filessystem [not-implemented yet]
67
 
 */
68
 
 
69
 
static int superblocks_probe(blkid_probe pr, struct blkid_chain *chn);
70
 
static int superblocks_safeprobe(blkid_probe pr, struct blkid_chain *chn);
71
 
 
72
 
static int blkid_probe_set_usage(blkid_probe pr, int usage);
73
 
 
74
 
 
75
 
/*
76
 
 * Superblocks chains probing functions
77
 
 */
78
 
static const struct blkid_idinfo *idinfos[] =
79
 
{
80
 
        /* RAIDs */
81
 
        &linuxraid_idinfo,
82
 
        &ddfraid_idinfo,
83
 
        &iswraid_idinfo,
84
 
        &lsiraid_idinfo,
85
 
        &viaraid_idinfo,
86
 
        &silraid_idinfo,
87
 
        &nvraid_idinfo,
88
 
        &pdcraid_idinfo,
89
 
        &highpoint45x_idinfo,
90
 
        &highpoint37x_idinfo,
91
 
        &adraid_idinfo,
92
 
        &jmraid_idinfo,
93
 
 
94
 
        &drbd_idinfo,
95
 
        &lvm2_idinfo,
96
 
        &lvm1_idinfo,
97
 
        &snapcow_idinfo,
98
 
        &luks_idinfo,
99
 
        &vmfs_volume_idinfo,
100
 
 
101
 
        /* Filesystems */
102
 
        &vfat_idinfo,
103
 
        &swsuspend_idinfo,
104
 
        &swap_idinfo,
105
 
        &xfs_idinfo,
106
 
        &ext4dev_idinfo,
107
 
        &ext4_idinfo,
108
 
        &ext3_idinfo,
109
 
        &ext2_idinfo,
110
 
        &jbd_idinfo,
111
 
        &reiser_idinfo,
112
 
        &reiser4_idinfo,
113
 
        &jfs_idinfo,
114
 
        &udf_idinfo,
115
 
        &iso9660_idinfo,
116
 
        &zfs_idinfo,
117
 
        &hfsplus_idinfo,
118
 
        &hfs_idinfo,
119
 
        &ufs_idinfo,
120
 
        &hpfs_idinfo,
121
 
        &sysv_idinfo,
122
 
        &xenix_idinfo,
123
 
        &ntfs_idinfo,
124
 
        &cramfs_idinfo,
125
 
        &romfs_idinfo,
126
 
        &minix_idinfo,
127
 
        &gfs_idinfo,
128
 
        &gfs2_idinfo,
129
 
        &ocfs_idinfo,
130
 
        &ocfs2_idinfo,
131
 
        &oracleasm_idinfo,
132
 
        &vxfs_idinfo,
133
 
        &squashfs_idinfo,
134
 
        &netware_idinfo,
135
 
        &btrfs_idinfo,
136
 
        &ubifs_idinfo,
137
 
        &bfs_idinfo,
138
 
        &vmfs_fs_idinfo,
139
 
        &befs_idinfo,
140
 
        &nilfs2_idinfo,
141
 
        &exfat_idinfo
142
 
};
143
 
 
144
 
/*
145
 
 * Driver definition
146
 
 */
147
 
const struct blkid_chaindrv superblocks_drv = {
148
 
        .id           = BLKID_CHAIN_SUBLKS,
149
 
        .name         = "superblocks",
150
 
        .dflt_enabled = TRUE,
151
 
        .dflt_flags   = BLKID_SUBLKS_DEFAULT,
152
 
        .idinfos      = idinfos,
153
 
        .nidinfos     = ARRAY_SIZE(idinfos),
154
 
        .has_fltr     = TRUE,
155
 
        .probe        = superblocks_probe,
156
 
        .safeprobe    = superblocks_safeprobe,
157
 
};
158
 
 
159
 
/**
160
 
 * blkid_probe_enable_superblocks:
161
 
 * @pr: probe
162
 
 * @enable: TRUE/FALSE
163
 
 *
164
 
 * Enables/disables the superblocks probing for non-binary interface.
165
 
 *
166
 
 * Returns: 0 on success, or -1 in case of error.
167
 
 */
168
 
int blkid_probe_enable_superblocks(blkid_probe pr, int enable)
169
 
{
170
 
        if (!pr)
171
 
                return -1;
172
 
        pr->chains[BLKID_CHAIN_SUBLKS].enabled = enable;
173
 
        return 0;
174
 
}
175
 
 
176
 
/**
177
 
 * blkid_probe_set_superblocks_flags:
178
 
 * @pr: prober
179
 
 * @flags: BLKID_SUBLKS_* flags
180
 
 *
181
 
 * Sets probing flags to the superblocks prober. This function is optional, the
182
 
 * default are BLKID_SUBLKS_DEFAULTS flags.
183
 
 *
184
 
 * Returns: 0 on success, or -1 in case of error.
185
 
 */
186
 
int blkid_probe_set_superblocks_flags(blkid_probe pr, int flags)
187
 
{
188
 
        if (!pr)
189
 
                return -1;
190
 
 
191
 
        pr->chains[BLKID_CHAIN_SUBLKS].flags = flags;
192
 
        return 0;
193
 
}
194
 
 
195
 
/**
196
 
 * blkid_probe_reset_superblocks_filter:
197
 
 * @pr: prober
198
 
 *
199
 
 * Resets superblocks probing filter
200
 
 *
201
 
 * Returns: 0 on success, or -1 in case of error.
202
 
 */
203
 
int blkid_probe_reset_superblocks_filter(blkid_probe pr)
204
 
{
205
 
        return __blkid_probe_reset_filter(pr, BLKID_CHAIN_SUBLKS);
206
 
}
207
 
 
208
 
/**
209
 
 * blkid_probe_invert_superblocks_filter:
210
 
 * @pr: prober
211
 
 *
212
 
 * Inverts superblocks probing filter
213
 
 *
214
 
 * Returns: 0 on success, or -1 in case of error.
215
 
 */
216
 
int blkid_probe_invert_superblocks_filter(blkid_probe pr)
217
 
{
218
 
        return __blkid_probe_invert_filter(pr, BLKID_CHAIN_SUBLKS);
219
 
}
220
 
 
221
 
/**
222
 
 * blkid_probe_filter_superblocks_type:
223
 
 * @pr: prober
224
 
 * @flag: filter BLKID_FLTR_{NOTIN,ONLYIN} flag
225
 
 * @names: NULL terminated array of probing function names (e.g. "vfat").
226
 
 *
227
 
 *  BLKID_FLTR_NOTIN  - probe for all items which are NOT IN @names
228
 
 *  BLKID_FLTR_ONLYIN - probe for items which are IN @names
229
 
 *
230
 
 * Returns: 0 on success, or -1 in case of error.
231
 
 */
232
 
int blkid_probe_filter_superblocks_type(blkid_probe pr, int flag, char *names[])
233
 
{
234
 
        return __blkid_probe_filter_types(pr, BLKID_CHAIN_SUBLKS, flag, names);
235
 
}
236
 
 
237
 
/**
238
 
 * blkid_probe_filter_superblocks_usage:
239
 
 * @pr: prober
240
 
 * @flag: filter BLKID_FLTR_{NOTIN,ONLYIN} flag
241
 
 * @usage: BLKID_USAGE_* flags
242
 
 *
243
 
 *  BLKID_FLTR_NOTIN  - probe for all items which are NOT IN @usage
244
 
 *  BLKID_FLTR_ONLYIN - probe for items which are IN @usage
245
 
 *
246
 
 * Returns: 0 on success, or -1 in case of error.
247
 
 */
248
 
int blkid_probe_filter_superblocks_usage(blkid_probe pr, int flag, int usage)
249
 
{
250
 
        unsigned long *fltr;
251
 
        struct blkid_chain *chn;
252
 
        int i;
253
 
 
254
 
        if (!pr)
255
 
                return -1;
256
 
 
257
 
        fltr = blkid_probe_get_filter(pr, BLKID_CHAIN_SUBLKS, TRUE);
258
 
        if (!fltr)
259
 
                return -1;
260
 
 
261
 
        chn = &pr->chains[BLKID_CHAIN_SUBLKS];
262
 
 
263
 
        for (i = 0; i < chn->driver->nidinfos; i++) {
264
 
                const struct blkid_idinfo *id = chn->driver->idinfos[i];
265
 
 
266
 
                if (id->usage & usage) {
267
 
                        if (flag & BLKID_FLTR_NOTIN)
268
 
                                blkid_bmp_set_item(chn->fltr, i);
269
 
                } else if (flag & BLKID_FLTR_ONLYIN)
270
 
                        blkid_bmp_set_item(chn->fltr, i);
271
 
        }
272
 
        DBG(DEBUG_LOWPROBE, printf("a new probing usage-filter initialized\n"));
273
 
        return 0;
274
 
}
275
 
 
276
 
/**
277
 
 * blkid_known_fstype:
278
 
 * @fstype: filesystem name
279
 
 *
280
 
 * Returns: 1 for known filesytems, or 0 for unknown filesystem.
281
 
 */
282
 
int blkid_known_fstype(const char *fstype)
283
 
{
284
 
        int i;
285
 
 
286
 
        if (!fstype)
287
 
                return 0;
288
 
 
289
 
        for (i = 0; i < ARRAY_SIZE(idinfos); i++) {
290
 
                const struct blkid_idinfo *id = idinfos[i];
291
 
                if (strcmp(id->name, fstype) == 0)
292
 
                        return 1;
293
 
        }
294
 
        return 0;
295
 
}
296
 
 
297
 
/*
298
 
 * The blkid_do_probe() backend.
299
 
 */
300
 
static int superblocks_probe(blkid_probe pr, struct blkid_chain *chn)
301
 
{
302
 
        int i = 0;
303
 
 
304
 
        if (!pr || chn->idx < -1)
305
 
                return -1;
306
 
        blkid_probe_chain_reset_vals(pr, chn);
307
 
 
308
 
        DBG(DEBUG_LOWPROBE,
309
 
                printf("--> starting probing loop [SUBLKS idx=%d]\n",
310
 
                chn->idx));
311
 
 
312
 
        if (pr->size <= 0 || (pr->size <= 1024 && !S_ISCHR(pr->mode)))
313
 
                /* Ignore very very small block devices or regular files (e.g.
314
 
                 * extended partitions). Note that size of the UBI char devices
315
 
                 * is 1 byte */
316
 
                goto nothing;
317
 
 
318
 
        i = chn->idx + 1;
319
 
 
320
 
        for ( ; i < ARRAY_SIZE(idinfos); i++) {
321
 
                const struct blkid_idinfo *id;
322
 
                const struct blkid_idmag *mag = NULL;
323
 
                blkid_loff_t off = 0;
324
 
 
325
 
                chn->idx = i;
326
 
 
327
 
                if (chn->fltr && blkid_bmp_get_item(chn->fltr, i))
328
 
                        continue;
329
 
 
330
 
                id = idinfos[i];
331
 
 
332
 
                if (id->minsz && id->minsz > pr->size)
333
 
                        continue;       /* the device is too small */
334
 
 
335
 
                mag = id->magics ? &id->magics[0] : NULL;
336
 
 
337
 
                /* don't probe for RAIDs, swap or journal on CD/DVDs */
338
 
                if ((id->usage & (BLKID_USAGE_RAID | BLKID_USAGE_OTHER)) &&
339
 
                    blkid_probe_is_cdrom(pr))
340
 
                        continue;
341
 
 
342
 
                /* don't probe for RAIDs on floppies */
343
 
                if ((id->usage & BLKID_USAGE_RAID) && blkid_probe_is_tiny(pr))
344
 
                        continue;
345
 
 
346
 
                DBG(DEBUG_LOWPROBE, printf("[%d] %s:\n", i, id->name));
347
 
 
348
 
                if (blkid_probe_get_idmag(pr, id, &off, &mag))
349
 
                        continue;
350
 
 
351
 
                /* final check by probing function */
352
 
                if (id->probefunc) {
353
 
                        DBG(DEBUG_LOWPROBE, printf("\tcall probefunc()\n"));
354
 
                        if (id->probefunc(pr, mag) != 0) {
355
 
                                blkid_probe_chain_reset_vals(pr, chn);
356
 
                                continue;
357
 
                        }
358
 
                }
359
 
 
360
 
                /* all cheks passed */
361
 
                if (chn->flags & BLKID_SUBLKS_TYPE)
362
 
                        blkid_probe_set_value(pr, "TYPE",
363
 
                                (unsigned char *) id->name,
364
 
                                strlen(id->name) + 1);
365
 
 
366
 
                blkid_probe_set_usage(pr, id->usage);
367
 
 
368
 
                if (mag)
369
 
                        blkid_probe_set_magic(pr, off, mag->len,
370
 
                                        (unsigned char *) mag->magic);
371
 
 
372
 
                DBG(DEBUG_LOWPROBE,
373
 
                        printf("<-- leaving probing loop (type=%s) [SUBLKS idx=%d]\n",
374
 
                        id->name, chn->idx));
375
 
                return 0;
376
 
        }
377
 
 
378
 
nothing:
379
 
        DBG(DEBUG_LOWPROBE,
380
 
                printf("<-- leaving probing loop (failed) [SUBLKS idx=%d]\n",
381
 
                chn->idx));
382
 
        return 1;
383
 
}
384
 
 
385
 
/*
386
 
 * This is the same function as blkid_do_probe(), but returns only one result
387
 
 * (cannot be used in while()) and checks for ambivalen results (more
388
 
 * filesystems on the device) -- in such case returns -2.
389
 
 *
390
 
 * The function does not check for filesystems when a RAID or crypto signature
391
 
 * is detected.  The function also does not check for collision between RAIDs
392
 
 * and crypto devices. The first detected RAID or crypto device is returned.
393
 
 *
394
 
 * The function does not probe for ambivalent results on very small devices
395
 
 * (e.g. floppies), on small devices the first detected filesystem is returned.
396
 
 */
397
 
static int superblocks_safeprobe(blkid_probe pr, struct blkid_chain *chn)
398
 
{
399
 
        struct blkid_prval vals[BLKID_NVALS_SUBLKS];
400
 
        int nvals = BLKID_NVALS_SUBLKS;
401
 
        int idx = -1;
402
 
        int count = 0;
403
 
        int intol = 0;
404
 
        int rc;
405
 
 
406
 
        while ((rc = superblocks_probe(pr, chn)) == 0) {
407
 
 
408
 
                if (blkid_probe_is_tiny(pr) && !count)
409
 
                        /* floppy or so -- returns the first result. */
410
 
                        return 0;
411
 
 
412
 
                count++;
413
 
 
414
 
                if (idinfos[chn->idx]->usage & (BLKID_USAGE_RAID | BLKID_USAGE_CRYPTO))
415
 
                        break;
416
 
 
417
 
                if (!(idinfos[chn->idx]->flags & BLKID_IDINFO_TOLERANT))
418
 
                        intol++;
419
 
 
420
 
                if (count == 1) {
421
 
                        /* save the first result */
422
 
                        nvals = blkid_probe_chain_copy_vals(pr, chn, vals, nvals);
423
 
                        idx = chn->idx;
424
 
                }
425
 
        }
426
 
 
427
 
        if (rc < 0)
428
 
                return rc;              /* error */
429
 
 
430
 
        if (count > 1 && intol) {
431
 
                DBG(DEBUG_LOWPROBE,
432
 
                        printf("ERROR: superblocks chain: "
433
 
                               "ambivalent result detected (%d filesystems)!\n",
434
 
                               count));
435
 
                return -2;              /* error, ambivalent result (more FS) */
436
 
        }
437
 
        if (!count)
438
 
                return 1;               /* nothing detected */
439
 
 
440
 
        if (idx != -1) {
441
 
                /* restore the first result */
442
 
                blkid_probe_chain_reset_vals(pr, chn);
443
 
                blkid_probe_append_vals(pr, vals, nvals);
444
 
                chn->idx = idx;
445
 
        }
446
 
 
447
 
        /*
448
 
         * The RAID device could be partitioned. The problem are RAID1 devices
449
 
         * where the partition table is visible from underlaying devices. We
450
 
         * have to ignore such partition tables.
451
 
         */
452
 
        if (chn->idx >= 0 && idinfos[chn->idx]->usage & BLKID_USAGE_RAID)
453
 
                pr->prob_flags |= BLKID_PARTS_IGNORE_PT;
454
 
 
455
 
        return 0;
456
 
}
457
 
 
458
 
int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
459
 
                        size_t len, unsigned char *magic)
460
 
{
461
 
        int rc = 0;
462
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
463
 
 
464
 
        if (magic && len && (chn->flags & BLKID_SUBLKS_MAGIC)) {
465
 
                rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len);
466
 
                if (!rc)
467
 
                        rc = blkid_probe_sprintf_value(pr, "SBMAGIC_OFFSET",
468
 
                                        "%llu", offset);
469
 
        }
470
 
        return rc;
471
 
}
472
 
 
473
 
int blkid_probe_set_version(blkid_probe pr, const char *version)
474
 
{
475
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
476
 
 
477
 
        if (chn->flags & BLKID_SUBLKS_VERSION)
478
 
                return blkid_probe_set_value(pr, "VERSION",
479
 
                           (unsigned char *) version, strlen(version) + 1);
480
 
        return 0;
481
 
}
482
 
 
483
 
int blkid_probe_sprintf_version(blkid_probe pr, const char *fmt, ...)
484
 
{
485
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
486
 
        int rc = 0;
487
 
 
488
 
        if (chn->flags & BLKID_SUBLKS_VERSION) {
489
 
                va_list ap;
490
 
 
491
 
                va_start(ap, fmt);
492
 
                rc = blkid_probe_vsprintf_value(pr, "VERSION", fmt, ap);
493
 
                va_end(ap);
494
 
        }
495
 
        return rc;
496
 
}
497
 
 
498
 
static int blkid_probe_set_usage(blkid_probe pr, int usage)
499
 
{
500
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
501
 
        char *u = NULL;
502
 
 
503
 
        if (!(chn->flags & BLKID_SUBLKS_USAGE))
504
 
                return 0;
505
 
 
506
 
        if (usage & BLKID_USAGE_FILESYSTEM)
507
 
                u = "filesystem";
508
 
        else if (usage & BLKID_USAGE_RAID)
509
 
                u = "raid";
510
 
        else if (usage & BLKID_USAGE_CRYPTO)
511
 
                u = "crypto";
512
 
        else if (usage & BLKID_USAGE_OTHER)
513
 
                u = "other";
514
 
        else
515
 
                u = "unknown";
516
 
 
517
 
        return blkid_probe_set_value(pr, "USAGE", (unsigned char *) u, strlen(u) + 1);
518
 
}
519
 
 
520
 
int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len)
521
 
{
522
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
523
 
        struct blkid_prval *v;
524
 
        if (len > BLKID_PROBVAL_BUFSIZ)
525
 
                len = BLKID_PROBVAL_BUFSIZ;
526
 
 
527
 
        if ((chn->flags & BLKID_SUBLKS_LABELRAW) &&
528
 
            blkid_probe_set_value(pr, "LABEL_RAW", label, len) < 0)
529
 
                return -1;
530
 
        if (!(chn->flags & BLKID_SUBLKS_LABEL))
531
 
                return 0;
532
 
        v = blkid_probe_assign_value(pr, "LABEL");
533
 
        if (!v)
534
 
                return -1;
535
 
 
536
 
        if (len == BLKID_PROBVAL_BUFSIZ)
537
 
                len--;                          /* make a space for \0 */
538
 
 
539
 
        memcpy(v->data, label, len);
540
 
        v->data[len] = '\0';
541
 
 
542
 
        v->len = blkid_rtrim_whitespace(v->data) + 1;
543
 
        if (v->len == 1)
544
 
                blkid_probe_reset_last_value(pr);
545
 
        return 0;
546
 
}
547
 
 
548
 
int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label,
549
 
                                size_t len, int enc)
550
 
{
551
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
552
 
        struct blkid_prval *v;
553
 
 
554
 
        if ((chn->flags & BLKID_SUBLKS_LABELRAW) &&
555
 
            blkid_probe_set_value(pr, "LABEL_RAW", label, len) < 0)
556
 
                return -1;
557
 
        if (!(chn->flags & BLKID_SUBLKS_LABEL))
558
 
                return 0;
559
 
        v = blkid_probe_assign_value(pr, "LABEL");
560
 
        if (!v)
561
 
                return -1;
562
 
 
563
 
        blkid_encode_to_utf8(enc, v->data, sizeof(v->data), label, len);
564
 
        v->len = blkid_rtrim_whitespace(v->data) + 1;
565
 
        if (v->len == 1)
566
 
                blkid_probe_reset_last_value(pr);
567
 
        return 0;
568
 
}
569
 
 
570
 
/* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */
571
 
static int uuid_is_empty(const unsigned char *buf, size_t len)
572
 
{
573
 
        int i;
574
 
 
575
 
        for (i = 0; i < len; i++)
576
 
                if (buf[i])
577
 
                        return 0;
578
 
        return 1;
579
 
}
580
 
 
581
 
int blkid_probe_sprintf_uuid(blkid_probe pr, unsigned char *uuid,
582
 
                                size_t len, const char *fmt, ...)
583
 
{
584
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
585
 
        int rc = -1;
586
 
        va_list ap;
587
 
 
588
 
        if (len > BLKID_PROBVAL_BUFSIZ)
589
 
                len = BLKID_PROBVAL_BUFSIZ;
590
 
 
591
 
        if (uuid_is_empty(uuid, len))
592
 
                return 0;
593
 
 
594
 
        if ((chn->flags & BLKID_SUBLKS_UUIDRAW) &&
595
 
            blkid_probe_set_value(pr, "UUID_RAW", uuid, len) < 0)
596
 
                return -1;
597
 
        if (!(chn->flags & BLKID_SUBLKS_UUID))
598
 
                return 0;
599
 
 
600
 
        va_start(ap, fmt);
601
 
        rc = blkid_probe_vsprintf_value(pr, "UUID", fmt, ap);
602
 
        va_end(ap);
603
 
 
604
 
        /* convert to lower case (..be paranoid) */
605
 
        if (!rc) {
606
 
                int i;
607
 
                struct blkid_prval *v = __blkid_probe_get_value(pr,
608
 
                                                blkid_probe_numof_values(pr));
609
 
                if (v) {
610
 
                        for (i = 0; i < v->len; i++)
611
 
                                if (v->data[i] >= 'A' && v->data[i] <= 'F')
612
 
                                        v->data[i] = (v->data[i] - 'A') + 'a';
613
 
                }
614
 
        }
615
 
        return rc;
616
 
}
617
 
 
618
 
/* function to set UUIDs that are in suberblocks stored as strings */
619
 
int blkid_probe_strncpy_uuid(blkid_probe pr, unsigned char *str, size_t len)
620
 
{
621
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
622
 
        struct blkid_prval *v;
623
 
 
624
 
        if (str == NULL || *str == '\0')
625
 
                return -1;
626
 
        if (!len)
627
 
                len = strlen((char *) str);
628
 
        if (len > BLKID_PROBVAL_BUFSIZ)
629
 
                len = BLKID_PROBVAL_BUFSIZ;
630
 
 
631
 
        if ((chn->flags & BLKID_SUBLKS_UUIDRAW) &&
632
 
            blkid_probe_set_value(pr, "UUID_RAW", str, len) < 0)
633
 
                return -1;
634
 
        if (!(chn->flags & BLKID_SUBLKS_UUID))
635
 
                return 0;
636
 
 
637
 
        v = blkid_probe_assign_value(pr, "UUID");
638
 
        if (v) {
639
 
                if (len == BLKID_PROBVAL_BUFSIZ)
640
 
                        len--;          /* make a space for \0 */
641
 
 
642
 
                memcpy((char *) v->data, str, len);
643
 
                v->data[len] = '\0';
644
 
                v->len = len + 1;
645
 
                return 0;
646
 
        }
647
 
        return -1;
648
 
}
649
 
 
650
 
/* default _set_uuid function to set DCE UUIDs */
651
 
int blkid_probe_set_uuid_as(blkid_probe pr, unsigned char *uuid, const char *name)
652
 
{
653
 
        struct blkid_chain *chn = blkid_probe_get_chain(pr);
654
 
        struct blkid_prval *v;
655
 
 
656
 
        if (uuid_is_empty(uuid, 16))
657
 
                return 0;
658
 
 
659
 
        if (!name) {
660
 
                if ((chn->flags & BLKID_SUBLKS_UUIDRAW) &&
661
 
                    blkid_probe_set_value(pr, "UUID_RAW", uuid, 16) < 0)
662
 
                        return -1;
663
 
                if (!(chn->flags & BLKID_SUBLKS_UUID))
664
 
                        return 0;
665
 
 
666
 
                v = blkid_probe_assign_value(pr, "UUID");
667
 
        } else
668
 
                v = blkid_probe_assign_value(pr, name);
669
 
 
670
 
        blkid_unparse_uuid(uuid, (char *) v->data, sizeof(v->data));
671
 
        v->len = 37;
672
 
 
673
 
        return 0;
674
 
}
675
 
 
676
 
int blkid_probe_set_uuid(blkid_probe pr, unsigned char *uuid)
677
 
{
678
 
        return blkid_probe_set_uuid_as(pr, uuid, NULL);
679
 
}
680
 
 
681
 
/*
682
 
 * DEPRECATED FUNCTIONS
683
 
 */
684
 
 
685
 
/**
686
 
 * blkid_probe_set_request:
687
 
 * @pr: probe
688
 
 * @flags: BLKID_PROBREQ_* (deprecated) or BLKID_SUBLKS_* flags
689
 
 *
690
 
 * Returns: 0 on success, or -1 in case of error.
691
 
 *
692
 
 * Deprecated: Use blkid_probe_set_superblocks_flags().
693
 
 */
694
 
int blkid_probe_set_request(blkid_probe pr, int flags)
695
 
{
696
 
        return blkid_probe_set_superblocks_flags(pr, flags);
697
 
}
698
 
 
699
 
/**
700
 
 * blkid_probe_reset_filter:
701
 
 * @pr: prober
702
 
 *
703
 
 * Returns: 0 on success, or -1 in case of error.
704
 
 *
705
 
 * Deprecated: Use blkid_probe_reset_superblocks_filter().
706
 
 */
707
 
int blkid_probe_reset_filter(blkid_probe pr)
708
 
{
709
 
        return __blkid_probe_reset_filter(pr, BLKID_CHAIN_SUBLKS);
710
 
}
711
 
 
712
 
/**
713
 
 * blkid_probe_invert_filter:
714
 
 * @pr: prober
715
 
 *
716
 
 * Returns: 0 on success, or -1 in case of error.
717
 
 *
718
 
 * Deprecated: Use blkid_probe_invert_superblocks_filter().
719
 
 */
720
 
int blkid_probe_invert_filter(blkid_probe pr)
721
 
{
722
 
        return __blkid_probe_invert_filter(pr, BLKID_CHAIN_SUBLKS);
723
 
}
724
 
 
725
 
/**
726
 
 * blkid_probe_filter_types
727
 
 * @pr: prober
728
 
 * @flag: filter BLKID_FLTR_{NOTIN,ONLYIN} flag
729
 
 * @names: NULL terminated array of probing function names (e.g. "vfat").
730
 
 *
731
 
 * Returns: 0 on success, or -1 in case of error.
732
 
 *
733
 
 * Deprecated: Use blkid_probe_filter_superblocks_types().
734
 
 */
735
 
int blkid_probe_filter_types(blkid_probe pr, int flag, char *names[])
736
 
{
737
 
        return __blkid_probe_filter_types(pr, BLKID_CHAIN_SUBLKS, flag, names);
738
 
}
739
 
 
740
 
/**
741
 
 * blkid_probe_filter_usage
742
 
 * @pr: prober
743
 
 * @flag: filter BLKID_FLTR_{NOTIN,ONLYIN} flag
744
 
 * @usage: BLKID_USAGE_* flags
745
 
 *
746
 
 * Returns: 0 on success, or -1 in case of error.
747
 
 *
748
 
 * Deprecated: Use blkid_probe_filter_superblocks_usage().
749
 
 */
750
 
int blkid_probe_filter_usage(blkid_probe pr, int flag, int usage)
751
 
{
752
 
        return blkid_probe_filter_superblocks_usage(pr, flag, usage);
753
 
}
754