forked from kernelslacker/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockdevs.c
87 lines (68 loc) · 1.55 KB
/
blockdevs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "bdevs.h"
#include "list.h"
#include "types.h"
#include "utils.h"
static unsigned int nr_blockdevs = 0;
struct bdevlist {
struct list_head list;
const char *name;
};
static struct bdevlist *bdevs = NULL;
static void add_to_bdevlist(const char *name)
{
struct bdevlist *newnode;
//TODO: Check if it's a valid /dev node (also check if passed without leading "/dev/")
newnode = zmalloc(sizeof(struct bdevlist));
newnode->name = strdup(name);
list_add_tail(&newnode->list, &bdevs->list);
nr_blockdevs++;
}
static void stat_dev(char *name)
{
struct stat sb;
int ret;
ret = lstat(name, &sb);
if (ret == -1) {
printf("Couldn't open %s\n", name);
exit(EXIT_FAILURE);
}
if (!(S_ISBLK(sb.st_mode))) {
printf("Sorry, %s doesn't look like a block device.\n", name);
exit(EXIT_FAILURE);
}
add_to_bdevlist(name);
}
void process_bdev_param(char *optarg)
{
unsigned int len, i;
char *str = optarg;
len = strlen(optarg);
/* Check if there are any commas. If so, split them into multiple devs. */
for (i = 0; i < len; i++) {
if (optarg[i] == ',') {
optarg[i] = 0;
stat_dev(str);
str = optarg + i + 1;
}
}
stat_dev(str);
}
void init_bdev_list(void)
{
bdevs = zmalloc(sizeof(struct bdevlist));
INIT_LIST_HEAD(&bdevs->list);
}
void dump_bdev_list(void)
{
struct list_head *node;
printf("Found %u block devices.\n", nr_blockdevs);
list_for_each(node, &bdevs->list) {
struct bdevlist *nl;
nl = (struct bdevlist *) node;
printf("%s\n", nl->name);
}
}