6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include "libsysfs/sysfs/libsysfs.h"
37 #include "udev_utils.h"
41 #define PATH_TO_NAME_CHAR '@'
43 static int get_db_filename(const char *devpath, char *filename, int len)
45 char temp[SYSFS_PATH_MAX];
48 /* replace '/' to transform path into a filename */
49 strfieldcpy(temp, devpath);
50 pos = strchr(&temp[1], '/');
52 pos[0] = PATH_TO_NAME_CHAR;
53 pos = strchr(&pos[1], '/');
55 snprintf(filename, len, "%s%s", udev_db_path, temp);
56 filename[len-1] = '\0';
61 int udev_db_add_device(struct udevice *udev)
63 char filename[SYSFS_PATH_MAX];
64 struct name_entry *name_loop;
70 get_db_filename(udev->devpath, filename, SYSFS_PATH_MAX);
72 create_path(filename);
74 f = fopen(filename, "w");
76 dbg("unable to create db file '%s'", filename);
79 dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
81 fprintf(f, "P:%s\n", udev->devpath);
82 fprintf(f, "N:%s\n", udev->name);
83 list_for_each_entry(name_loop, &udev->symlink_list, node)
84 fprintf(f, "S:%s\n", name_loop->name);
85 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
86 fprintf(f, "A:%u\n", udev->partitions);
87 fprintf(f, "R:%u\n", udev->ignore_remove);
94 static int parse_db_file(struct udevice *udev, const char *filename)
98 unsigned int major, minor;
105 if (file_map(filename, &buf, &bufsize) != 0) {
106 dbg("unable to read db file '%s'", filename);
111 while (cur < bufsize) {
112 count = buf_get_line(buf, bufsize, cur);
118 if (count > DEVPATH_SIZE)
119 count = DEVPATH_SIZE-1;
120 strncpy(udev->devpath, &bufline[2], count-2);
121 udev->devpath[count-2] = '\0';
124 if (count > NAME_SIZE)
126 strncpy(udev->name, &bufline[2], count-2);
127 udev->name[count-2] = '\0';
130 if (count > NAME_SIZE)
132 strncpy(temp, &bufline[2], count-2);
133 temp[count-2] = '\0';
134 sscanf(temp, "%u:%u", &major, &minor);
135 udev->devt = makedev(major, minor);
138 if (count > NAME_SIZE)
140 strncpy(temp, &bufline[2], count-2);
141 temp[count-2] = '\0';
142 name_list_add(&udev->symlink_list, temp, 0);
145 if (count > NAME_SIZE)
147 strncpy(line, &bufline[2], count-2);
148 line[count-2] = '\0';
149 udev->partitions = atoi(line);
152 if (count > NAME_SIZE)
154 strncpy(line, &bufline[2], count-2);
155 line[count-2] = '\0';
156 udev->ignore_remove = atoi(line);
160 file_unmap(buf, bufsize);
162 if (udev->name[0] == '\0')
168 int udev_db_delete_device(struct udevice *udev)
170 char filename[SYSFS_PATH_MAX];
172 get_db_filename(udev->devpath, filename, SYSFS_PATH_MAX);
178 int udev_db_get_device(struct udevice *udev, const char *devpath)
180 char filename[SYSFS_PATH_MAX];
182 get_db_filename(devpath, filename, SYSFS_PATH_MAX);
184 if (parse_db_file(udev, filename) != 0)
190 int udev_db_search_name(char *devpath, size_t len, const char *name)
194 dir = opendir(udev_db_path);
196 dbg("unable to udev db '%s'", udev_db_path);
202 char filename[NAME_SIZE];
203 char path[DEVPATH_SIZE];
204 char nodename[NAME_SIZE];
212 if (ent == NULL || ent->d_name[0] == '\0')
215 if (ent->d_name[0] == '.')
218 snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name);
219 filename[NAME_SIZE-1] = '\0';
220 dbg("looking at '%s'", filename);
222 if (file_map(filename, &buf, &bufsize) != 0) {
223 dbg("unable to read db file '%s'", filename);
228 while (cur < bufsize) {
229 count = buf_get_line(buf, bufsize, cur);
235 if (count > DEVPATH_SIZE)
236 count = DEVPATH_SIZE-1;
237 strncpy(path, &bufline[2], count-2);
238 path[count-2] = '\0';
242 if (count > NAME_SIZE)
244 strncpy(nodename, &bufline[2], count-2);
245 nodename[count-2] = '\0';
246 dbg("compare '%s' '%s'", nodename, name);
247 if (strcmp(nodename, name) == 0) {
248 strncpy(devpath, path, len);
250 file_unmap(buf, bufsize);
259 file_unmap(buf, bufsize);
266 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
270 dir = opendir(udev_db_path);
272 dbg("unable to udev db '%s'", udev_db_path);
278 char filename[NAME_SIZE];
279 char path[DEVPATH_SIZE];
280 char nodename[NAME_SIZE];
288 if (ent == NULL || ent->d_name[0] == '\0')
291 if (ent->d_name[0] == '.')
294 snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name);
295 filename[NAME_SIZE-1] = '\0';
296 dbg("looking at '%s'", filename);
298 if (file_map(filename, &buf, &bufsize) != 0) {
299 dbg("unable to read db file '%s'", filename);
306 while (cur < bufsize) {
307 count = buf_get_line(buf, bufsize, cur);
313 if (count > DEVPATH_SIZE)
314 count = DEVPATH_SIZE-1;
315 strncpy(path, &bufline[2], count-2);
316 path[count-2] = '\0';
319 if (count > NAME_SIZE)
321 strncpy(nodename, &bufline[2], count-2);
322 nodename[count-2] = '\0';
328 file_unmap(buf, bufsize);
330 if (path[0] == '\0' || nodename[0] == '\0')
333 if (handler_function(path, nodename) != 0)