blob: 311608de102f969bb0129b6d4293537c7355853a [file] [log] [blame]
Theodore Ts'ode23aa12000-08-19 17:00:47 +00001/*
2 * finddev.c -- this routine attempts to find a particular device in
3 * /dev
Theodore Ts'oefc6f622008-08-27 23:07:54 -04004 *
Theodore Ts'ode23aa12000-08-19 17:00:47 +00005 * Copyright (C) 2000 Theodore Ts'o.
6 *
7 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04008 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
Theodore Ts'ode23aa12000-08-19 17:00:47 +000010 * %End-Header%
11 */
12
Theodore Ts'od1154eb2011-09-18 17:34:37 -040013#include "config.h"
Theodore Ts'ode23aa12000-08-19 17:00:47 +000014#include <stdio.h>
15#include <string.h>
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19#include <stdlib.h>
20#include <string.h>
21#if HAVE_SYS_TYPES_H
22#include <sys/types.h>
23#endif
24#if HAVE_SYS_STAT_H
25#include <sys/stat.h>
26#endif
27#include <dirent.h>
28#if HAVE_ERRNO_H
29#include <errno.h>
30#endif
31#if HAVE_SYS_MKDEV_H
32#include <sys/mkdev.h>
33#endif
34
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000035#include "ext2_fs.h"
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000036#include "ext2fs.h"
Niu Yawei08ae93a2011-11-19 23:08:03 -050037#include "ext2fsP.h"
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000038
Theodore Ts'ode23aa12000-08-19 17:00:47 +000039struct dir_list {
40 char *name;
41 struct dir_list *next;
42};
43
Theodore Ts'ode23aa12000-08-19 17:00:47 +000044/*
45 * This function adds an entry to the directory list
46 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000047static void add_to_dirlist(const char *name, struct dir_list **list)
Theodore Ts'ode23aa12000-08-19 17:00:47 +000048{
49 struct dir_list *dp;
50
51 dp = malloc(sizeof(struct dir_list));
52 if (!dp)
53 return;
54 dp->name = malloc(strlen(name)+1);
55 if (!dp->name) {
56 free(dp);
57 return;
58 }
59 strcpy(dp->name, name);
60 dp->next = *list;
61 *list = dp;
62}
63
64/*
65 * This function frees a directory list
66 */
67static void free_dirlist(struct dir_list **list)
68{
69 struct dir_list *dp, *next;
70
71 for (dp = *list; dp; dp = next) {
72 next = dp->next;
73 free(dp->name);
74 free(dp);
75 }
76 *list = 0;
77}
78
79static int scan_dir(char *dirname, dev_t device, struct dir_list **list,
80 char **ret_path)
81{
82 DIR *dir;
83 struct dirent *dp;
84 char path[1024], *cp;
85 int dirlen;
86 struct stat st;
87
88 dirlen = strlen(dirname);
89 if ((dir = opendir(dirname)) == NULL)
90 return errno;
91 dp = readdir(dir);
92 while (dp) {
93 if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path))
94 goto skip_to_next;
95 if (dp->d_name[0] == '.' &&
96 ((dp->d_name[1] == 0) ||
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000097 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
Theodore Ts'ode23aa12000-08-19 17:00:47 +000098 goto skip_to_next;
99 sprintf(path, "%s/%s", dirname, dp->d_name);
100 if (stat(path, &st) < 0)
Theodore Ts'of86fa6c2000-08-20 21:51:24 +0000101 goto skip_to_next;
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000102 if (S_ISDIR(st.st_mode))
103 add_to_dirlist(path, list);
104 if (S_ISBLK(st.st_mode) && st.st_rdev == device) {
105 cp = malloc(strlen(path)+1);
Theodore Ts'o37517212001-07-29 12:01:09 -0400106 if (!cp) {
107 closedir(dir);
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000108 return ENOMEM;
Theodore Ts'o37517212001-07-29 12:01:09 -0400109 }
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000110 strcpy(cp, path);
111 *ret_path = cp;
Theodore Ts'o37517212001-07-29 12:01:09 -0400112 goto success;
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000113 }
114 skip_to_next:
115 dp = readdir(dir);
116 }
Theodore Ts'o37517212001-07-29 12:01:09 -0400117success:
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000118 closedir(dir);
119 return 0;
120}
121
122/*
123 * This function finds the pathname to a block device with a given
124 * device number. It returns a pointer to allocated memory to the
125 * pathname on success, and NULL on failure.
126 */
127char *ext2fs_find_block_device(dev_t device)
128{
129 struct dir_list *list = 0, *new_list = 0;
130 struct dir_list *current;
131 char *ret_path = 0;
Niu Yawei08ae93a2011-11-19 23:08:03 -0500132 int level = 0;
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000133
134 /*
135 * Add the starting directories to search...
136 */
137 add_to_dirlist("/devices", &list);
138 add_to_dirlist("/devfs", &list);
139 add_to_dirlist("/dev", &list);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400140
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000141 while (list) {
142 current = list;
143 list = list->next;
144#ifdef DEBUG
145 printf("Scanning directory %s\n", current->name);
146#endif
147 scan_dir(current->name, device, &new_list, &ret_path);
148 free(current->name);
149 free(current);
150 if (ret_path)
151 break;
152 /*
153 * If we're done checking at this level, descend to
154 * the next level of subdirectories. (breadth-first)
155 */
156 if (list == 0) {
157 list = new_list;
158 new_list = 0;
Niu Yawei08ae93a2011-11-19 23:08:03 -0500159 /* Avoid infinite loop */
160 if (++level >= EXT2FS_MAX_NESTED_LINKS)
161 break;
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000162 }
163 }
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000164 free_dirlist(&list);
165 free_dirlist(&new_list);
166 return ret_path;
167}
168
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400169
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000170#ifdef DEBUG
171int main(int argc, char** argv)
172{
173 char *devname, *tmp;
174 int major, minor;
175 dev_t device;
176 const char *errmsg = "Couldn't parse %s: %s\n";
177
178 if ((argc != 2) && (argc != 3)) {
179 fprintf(stderr, "Usage: %s device_number\n", argv[0]);
180 fprintf(stderr, "\t: %s major minor\n", argv[0]);
181 exit(1);
182 }
183 if (argc == 2) {
184 device = strtoul(argv[1], &tmp, 0);
185 if (*tmp) {
186 fprintf(stderr, errmsg, "device number", argv[1]);
187 exit(1);
188 }
189 } else {
190 major = strtoul(argv[1], &tmp, 0);
191 if (*tmp) {
192 fprintf(stderr, errmsg, "major number", argv[1]);
193 exit(1);
194 }
195 minor = strtoul(argv[2], &tmp, 0);
196 if (*tmp) {
197 fprintf(stderr, errmsg, "minor number", argv[2]);
198 exit(1);
199 }
200 device = makedev(major, minor);
201 printf("Looking for device 0x%04x (%d:%d)\n", device,
202 major, minor);
203 }
204 devname = ext2fs_find_block_device(device);
205 if (devname) {
206 printf("Found device! %s\n", devname);
207 free(devname);
208 } else {
209 printf("Couldn't find device.\n");
210 }
211 return 0;
212}
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400213
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000214#endif