-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathblockdevs.c
More file actions
111 lines (90 loc) · 2.06 KB
/
blockdevs.c
File metadata and controls
111 lines (90 loc) · 2.06 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <sys/stat.h>
#include "bdevs.h"
#include "list.h"
#include "trinity.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;
struct stat sb;
char pathbuf[PATH_MAX];
const char *path = name;
/* If the path doesn't start with /dev/, prepend it. */
if (strncmp(name, "/dev/", 5) != 0) {
snprintf(pathbuf, sizeof(pathbuf), "/dev/%s", name);
path = pathbuf;
}
/* Verify the path exists and is a block device. */
if (stat(path, &sb) == -1) {
outputerr("Couldn't stat %s: not a valid device node.\n", path);
return;
}
if (!S_ISBLK(sb.st_mode)) {
outputerr("%s is not a block device.\n", path);
return;
}
newnode = zmalloc(sizeof(struct bdevlist));
newnode->name = strdup(path);
if (!newnode->name) {
free(newnode);
return;
}
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) {
outputerr("Couldn't stat %s\n", name);
exit(EXIT_FAILURE);
}
if (!(S_ISBLK(sb.st_mode))) {
outputerr("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;
output(1, "Found %u block devices.\n", nr_blockdevs);
list_for_each(node, &bdevs->list) {
struct bdevlist *nl;
nl = (struct bdevlist *) node;
output(1, "%s\n", nl->name);
}
}