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

« back to all changes in this revision

Viewing changes to libblkid/samples/mkfs.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
 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
 
3
 *
 
4
 * This file may be redistributed under the terms of the
 
5
 * GNU Lesser General Public License.
 
6
 */
 
7
 
 
8
#include <stdio.h>
 
9
#include <stdlib.h>
 
10
#include <sys/types.h>
 
11
#include <sys/stat.h>
 
12
#include <fcntl.h>
 
13
#include <errno.h>
 
14
 
 
15
#include <blkid.h>
 
16
 
 
17
#include "c.h"
 
18
 
 
19
int main(int argc, char *argv[])
 
20
{
 
21
        int rc;
 
22
        char *devname;
 
23
        blkid_probe pr;
 
24
        blkid_topology tp;
 
25
 
 
26
        if (argc < 2) {
 
27
                fprintf(stderr, "usage: %s <device>  "
 
28
                        "-- checks based on libblkid for mkfs-like programs.\n",
 
29
                        program_invocation_short_name);
 
30
                return EXIT_FAILURE;
 
31
        }
 
32
 
 
33
        devname = argv[1];
 
34
        pr = blkid_new_probe_from_filename(devname);
 
35
        if (!pr)
 
36
                err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
 
37
                                devname);
 
38
 
 
39
        /*
 
40
         * check Filesystems / Partitions overwrite
 
41
         */
 
42
 
 
43
        /* enable partitions probing (superblocks are enabled by default) */
 
44
        blkid_probe_enable_partitions(pr, TRUE);
 
45
 
 
46
        rc = blkid_do_fullprobe(pr);
 
47
        if (rc == -1)
 
48
                errx(EXIT_FAILURE, "%s: blkid_do_fullprobe() failed", devname);
 
49
        else if (rc == 0) {
 
50
                const char *type;
 
51
 
 
52
                if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL))
 
53
                        errx(EXIT_FAILURE, "%s: appears to contain an existing "
 
54
                                        "%s superblock", devname, type);
 
55
 
 
56
                if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL))
 
57
                        errx(EXIT_FAILURE, "%s: appears to contain an partition "
 
58
                                        "table (%s)", devname, type);
 
59
        }
 
60
 
 
61
        /*
 
62
         * get topology details
 
63
         */
 
64
        tp = blkid_probe_get_topology(pr);
 
65
        if (!tp)
 
66
                errx(EXIT_FAILURE, "%s: failed to read topology", devname);
 
67
 
 
68
 
 
69
        /* ... your mkfs.<type> code or so ...
 
70
 
 
71
        off = blkid_topology_get_alignment_offset(tp);
 
72
 
 
73
         */
 
74
 
 
75
        blkid_free_probe(pr);
 
76
 
 
77
        return EXIT_SUCCESS;
 
78
}