~ubuntu-branches/ubuntu/wily/bcache-tools/wily-proposed

« back to all changes in this revision

Viewing changes to probe-bcache.c

  • Committer: Package Import Robot
  • Author(s): Robie Basak
  • Date: 2014-09-29 10:13:58 UTC
  • Revision ID: package-import@ubuntu.com-20140929101358-49cxzqbrsi3d3l4c
Tags: upstream-1.0.7
ImportĀ upstreamĀ versionĀ 1.0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Author: Kent Overstreet <kmo@daterainc.com>
 
3
 *
 
4
 * GPLv2
 
5
 */
 
6
 
 
7
#define _FILE_OFFSET_BITS       64
 
8
#define __USE_FILE_OFFSET64
 
9
#define _XOPEN_SOURCE 500
 
10
 
 
11
#include <blkid.h>
 
12
#include <fcntl.h>
 
13
#include <linux/fs.h>
 
14
#include <stdbool.h>
 
15
#include <stdint.h>
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <string.h>
 
19
#include <sys/ioctl.h>
 
20
#include <sys/types.h>
 
21
#include <sys/stat.h>
 
22
#include <unistd.h>
 
23
#include <uuid/uuid.h>
 
24
 
 
25
#include "bcache.h"
 
26
 
 
27
int main(int argc, char **argv)
 
28
{
 
29
        bool udev = false;
 
30
        int i, o;
 
31
        extern char *optarg;
 
32
        struct cache_sb sb;
 
33
        char uuid[40];
 
34
        blkid_probe pr;
 
35
 
 
36
        while ((o = getopt(argc, argv, "o:")) != EOF)
 
37
                switch (o) {
 
38
                case 'o':
 
39
                        if (strcmp("udev", optarg)) {
 
40
                                printf("Invalid output format %s\n", optarg);
 
41
                                exit(EXIT_FAILURE);
 
42
                        }
 
43
                        udev = true;
 
44
                        break;
 
45
                }
 
46
 
 
47
 
 
48
        argv += optind;
 
49
        argc -= optind;
 
50
 
 
51
        for (i = 0; i < argc; i++) {
 
52
                int fd = open(argv[i], O_RDONLY);
 
53
                if (fd == -1)
 
54
                        continue;
 
55
 
 
56
                if (!(pr = blkid_new_probe()))
 
57
                        continue;
 
58
                if (blkid_probe_set_device(pr, fd, 0, 0))
 
59
                        continue;
 
60
                /* probe partitions too */
 
61
                if (blkid_probe_enable_partitions(pr, true))
 
62
                        continue;
 
63
                /* bail if anything was found
 
64
                 * probe-bcache isn't needed once blkid recognizes bcache */
 
65
                if (!blkid_do_probe(pr)) {
 
66
                        continue;
 
67
                }
 
68
 
 
69
                if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb))
 
70
                        continue;
 
71
 
 
72
                if (memcmp(sb.magic, bcache_magic, 16))
 
73
                        continue;
 
74
 
 
75
                uuid_unparse(sb.uuid, uuid);
 
76
 
 
77
                if (udev)
 
78
                        printf("ID_FS_UUID=%s\n"
 
79
                               "ID_FS_UUID_ENC=%s\n"
 
80
                               "ID_FS_TYPE=bcache\n",
 
81
                               uuid, uuid);
 
82
                else
 
83
                        printf("%s: UUID=\"\" TYPE=\"bcache\"\n", uuid);
 
84
        }
 
85
 
 
86
        return 0;
 
87
}