diff options
Diffstat (limited to 'e2fsprogs/ext2fs/finddev.c')
-rw-r--r-- | e2fsprogs/ext2fs/finddev.c | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/e2fsprogs/ext2fs/finddev.c b/e2fsprogs/ext2fs/finddev.c new file mode 100644 index 000000000..fa2cadde4 --- /dev/null +++ b/e2fsprogs/ext2fs/finddev.c | |||
@@ -0,0 +1,208 @@ | |||
1 | /* | ||
2 | * finddev.c -- this routine attempts to find a particular device in | ||
3 | * /dev | ||
4 | * | ||
5 | * Copyright (C) 2000 Theodore Ts'o. | ||
6 | * | ||
7 | * %Begin-Header% | ||
8 | * This file may be redistributed under the terms of the GNU Public | ||
9 | * License. | ||
10 | * %End-Header% | ||
11 | */ | ||
12 | |||
13 | #include <stdio.h> | ||
14 | #include <string.h> | ||
15 | #if HAVE_UNISTD_H | ||
16 | #include <unistd.h> | ||
17 | #endif | ||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | #if HAVE_SYS_TYPES_H | ||
21 | #include <sys/types.h> | ||
22 | #endif | ||
23 | #if HAVE_SYS_STAT_H | ||
24 | #include <sys/stat.h> | ||
25 | #endif | ||
26 | #include <dirent.h> | ||
27 | #if HAVE_ERRNO_H | ||
28 | #include <errno.h> | ||
29 | #endif | ||
30 | #if HAVE_SYS_MKDEV_H | ||
31 | #include <sys/mkdev.h> | ||
32 | #endif | ||
33 | |||
34 | #include "ext2_fs.h" | ||
35 | #include "ext2fs.h" | ||
36 | |||
37 | struct dir_list { | ||
38 | char *name; | ||
39 | struct dir_list *next; | ||
40 | }; | ||
41 | |||
42 | /* | ||
43 | * This function adds an entry to the directory list | ||
44 | */ | ||
45 | static void add_to_dirlist(const char *name, struct dir_list **list) | ||
46 | { | ||
47 | struct dir_list *dp; | ||
48 | |||
49 | dp = malloc(sizeof(struct dir_list)); | ||
50 | if (!dp) | ||
51 | return; | ||
52 | dp->name = malloc(strlen(name)+1); | ||
53 | if (!dp->name) { | ||
54 | free(dp); | ||
55 | return; | ||
56 | } | ||
57 | strcpy(dp->name, name); | ||
58 | dp->next = *list; | ||
59 | *list = dp; | ||
60 | } | ||
61 | |||
62 | /* | ||
63 | * This function frees a directory list | ||
64 | */ | ||
65 | static void free_dirlist(struct dir_list **list) | ||
66 | { | ||
67 | struct dir_list *dp, *next; | ||
68 | |||
69 | for (dp = *list; dp; dp = next) { | ||
70 | next = dp->next; | ||
71 | free(dp->name); | ||
72 | free(dp); | ||
73 | } | ||
74 | *list = 0; | ||
75 | } | ||
76 | |||
77 | static int scan_dir(char *dir_name, dev_t device, struct dir_list **list, | ||
78 | char **ret_path) | ||
79 | { | ||
80 | DIR *dir; | ||
81 | struct dirent *dp; | ||
82 | char path[1024], *cp; | ||
83 | int dirlen; | ||
84 | struct stat st; | ||
85 | |||
86 | dirlen = strlen(dir_name); | ||
87 | if ((dir = opendir(dir_name)) == NULL) | ||
88 | return errno; | ||
89 | dp = readdir(dir); | ||
90 | while (dp) { | ||
91 | if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path)) | ||
92 | goto skip_to_next; | ||
93 | if (dp->d_name[0] == '.' && | ||
94 | ((dp->d_name[1] == 0) || | ||
95 | ((dp->d_name[1] == '.') && (dp->d_name[2] == 0)))) | ||
96 | goto skip_to_next; | ||
97 | sprintf(path, "%s/%s", dir_name, dp->d_name); | ||
98 | if (stat(path, &st) < 0) | ||
99 | goto skip_to_next; | ||
100 | if (S_ISDIR(st.st_mode)) | ||
101 | add_to_dirlist(path, list); | ||
102 | if (S_ISBLK(st.st_mode) && st.st_rdev == device) { | ||
103 | cp = malloc(strlen(path)+1); | ||
104 | if (!cp) { | ||
105 | closedir(dir); | ||
106 | return ENOMEM; | ||
107 | } | ||
108 | strcpy(cp, path); | ||
109 | *ret_path = cp; | ||
110 | goto success; | ||
111 | } | ||
112 | skip_to_next: | ||
113 | dp = readdir(dir); | ||
114 | } | ||
115 | success: | ||
116 | closedir(dir); | ||
117 | return 0; | ||
118 | } | ||
119 | |||
120 | /* | ||
121 | * This function finds the pathname to a block device with a given | ||
122 | * device number. It returns a pointer to allocated memory to the | ||
123 | * pathname on success, and NULL on failure. | ||
124 | */ | ||
125 | char *ext2fs_find_block_device(dev_t device) | ||
126 | { | ||
127 | struct dir_list *list = 0, *new_list = 0; | ||
128 | struct dir_list *current; | ||
129 | char *ret_path = 0; | ||
130 | |||
131 | /* | ||
132 | * Add the starting directories to search... | ||
133 | */ | ||
134 | add_to_dirlist("/devices", &list); | ||
135 | add_to_dirlist("/devfs", &list); | ||
136 | add_to_dirlist("/dev", &list); | ||
137 | |||
138 | while (list) { | ||
139 | current = list; | ||
140 | list = list->next; | ||
141 | #ifdef DEBUG | ||
142 | printf("Scanning directory %s\n", current->name); | ||
143 | #endif | ||
144 | scan_dir(current->name, device, &new_list, &ret_path); | ||
145 | free(current->name); | ||
146 | free(current); | ||
147 | if (ret_path) | ||
148 | break; | ||
149 | /* | ||
150 | * If we're done checking at this level, descend to | ||
151 | * the next level of subdirectories. (breadth-first) | ||
152 | */ | ||
153 | if (list == 0) { | ||
154 | list = new_list; | ||
155 | new_list = 0; | ||
156 | } | ||
157 | } | ||
158 | free_dirlist(&list); | ||
159 | free_dirlist(&new_list); | ||
160 | return ret_path; | ||
161 | } | ||
162 | |||
163 | |||
164 | #ifdef DEBUG | ||
165 | int main(int argc, char** argv) | ||
166 | { | ||
167 | char *devname, *tmp; | ||
168 | int major, minor; | ||
169 | dev_t device; | ||
170 | const char *errmsg = "Couldn't parse %s: %s\n"; | ||
171 | |||
172 | if ((argc != 2) && (argc != 3)) { | ||
173 | fprintf(stderr, "Usage: %s device_number\n", argv[0]); | ||
174 | fprintf(stderr, "\t: %s major minor\n", argv[0]); | ||
175 | exit(1); | ||
176 | } | ||
177 | if (argc == 2) { | ||
178 | device = strtoul(argv[1], &tmp, 0); | ||
179 | if (*tmp) { | ||
180 | fprintf(stderr, errmsg, "device number", argv[1]); | ||
181 | exit(1); | ||
182 | } | ||
183 | } else { | ||
184 | major = strtoul(argv[1], &tmp, 0); | ||
185 | if (*tmp) { | ||
186 | fprintf(stderr, errmsg, "major number", argv[1]); | ||
187 | exit(1); | ||
188 | } | ||
189 | minor = strtoul(argv[2], &tmp, 0); | ||
190 | if (*tmp) { | ||
191 | fprintf(stderr, errmsg, "minor number", argv[2]); | ||
192 | exit(1); | ||
193 | } | ||
194 | device = makedev(major, minor); | ||
195 | printf("Looking for device 0x%04x (%d:%d)\n", device, | ||
196 | major, minor); | ||
197 | } | ||
198 | devname = ext2fs_find_block_device(device); | ||
199 | if (devname) { | ||
200 | printf("Found device! %s\n", devname); | ||
201 | free(devname); | ||
202 | } else { | ||
203 | printf("Couldn't find device.\n"); | ||
204 | } | ||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | #endif | ||