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"
36 #include "udev_libc_wrapper.h"
38 #include "udev_utils.h"
42 #define PATH_TO_NAME_CHAR '@'
44 static int get_db_filename(const char *devpath, char *filename, int len)
49 /* replace '/' to transform path into a filename */
50 strlcpy(temp, devpath, sizeof(temp));
51 pos = strchr(&temp[1], '/');
53 pos[0] = PATH_TO_NAME_CHAR;
54 pos = strchr(&pos[1], '/');
56 snprintf(filename, len, "%s%s", udev_db_path, temp);
57 filename[len-1] = '\0';
62 int udev_db_add_device(struct udevice *udev)
64 char filename[PATH_SIZE];
65 struct name_entry *name_loop;
71 get_db_filename(udev->devpath, filename, sizeof(filename));
73 create_path(filename);
75 f = fopen(filename, "w");
77 dbg("unable to create db file '%s'", filename);
80 dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
82 fprintf(f, "P:%s\n", udev->devpath);
83 fprintf(f, "N:%s\n", udev->name);
84 list_for_each_entry(name_loop, &udev->symlink_list, node)
85 fprintf(f, "S:%s\n", name_loop->name);
86 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
87 fprintf(f, "A:%u\n", udev->partitions);
88 fprintf(f, "R:%u\n", udev->ignore_remove);
95 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 > sizeof(udev->devpath))
119 count = sizeof(udev->devpath)-1;
120 strlcpy(udev->devpath, &bufline[2], count-2);
123 if (count > sizeof(udev->name))
124 count = sizeof(udev->name)-1;
125 strlcpy(udev->name, &bufline[2], count-2);
128 if (count > sizeof(line))
129 count = sizeof(line)-1;
130 strlcpy(line, &bufline[2], count-2);
131 sscanf(line, "%u:%u", &major, &minor);
132 udev->devt = makedev(major, minor);
135 if (count > sizeof(line))
136 count = sizeof(line)-1;
137 strlcpy(line, &bufline[2], count-2);
138 name_list_add(&udev->symlink_list, line, 0);
141 if (count > sizeof(line))
142 count = sizeof(line)-1;
143 strlcpy(line, &bufline[2], count-2);
144 udev->partitions = atoi(line);
147 if (count > sizeof(line))
148 count = sizeof(line)-1;
149 strlcpy(line, &bufline[2], count-2);
150 udev->ignore_remove = atoi(line);
154 file_unmap(buf, bufsize);
156 if (udev->name[0] == '\0')
162 int udev_db_delete_device(struct udevice *udev)
164 char filename[PATH_SIZE];
166 get_db_filename(udev->devpath, filename, sizeof(filename));
172 int udev_db_get_device(struct udevice *udev, const char *devpath)
174 char filename[PATH_SIZE];
176 get_db_filename(devpath, filename, sizeof(filename));
178 if (parse_db_file(udev, filename) != 0)
184 int udev_db_search_name(char *devpath, size_t len, const char *name)
188 dir = opendir(udev_db_path);
190 dbg("unable to udev db '%s'", udev_db_path);
196 char filename[PATH_SIZE];
197 char path[PATH_SIZE];
198 char nodename[PATH_SIZE];
206 if (ent == NULL || ent->d_name[0] == '\0')
209 if (ent->d_name[0] == '.')
212 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
213 filename[sizeof(filename)-1] = '\0';
214 dbg("looking at '%s'", filename);
216 if (file_map(filename, &buf, &bufsize) != 0) {
217 dbg("unable to read db file '%s'", filename);
222 while (cur < bufsize) {
223 count = buf_get_line(buf, bufsize, cur);
229 if (count > sizeof(path))
230 count = sizeof(path)-1;
231 strlcpy(path, &bufline[2], count-2);
235 if (count > sizeof(nodename))
236 count = sizeof(nodename)-1;
237 strlcpy(nodename, &bufline[2], count-2);
238 dbg("compare '%s' '%s'", nodename, name);
239 if (strcmp(nodename, name) == 0) {
240 strlcpy(devpath, path, len);
241 file_unmap(buf, bufsize);
250 file_unmap(buf, bufsize);
257 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
261 dir = opendir(udev_db_path);
263 dbg("unable to udev db '%s'", udev_db_path);
269 char filename[PATH_SIZE];
270 char path[PATH_SIZE];
271 char nodename[PATH_SIZE];
279 if (ent == NULL || ent->d_name[0] == '\0')
282 if (ent->d_name[0] == '.')
285 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
286 filename[sizeof(filename)-1] = '\0';
287 dbg("looking at '%s'", filename);
289 if (file_map(filename, &buf, &bufsize) != 0) {
290 dbg("unable to read db file '%s'", filename);
297 while (cur < bufsize) {
298 count = buf_get_line(buf, bufsize, cur);
304 if (count > sizeof(path))
305 count = sizeof(path)-1;
306 strlcpy(path, &bufline[2], count-2);
309 if (count > sizeof(nodename))
310 count = sizeof(nodename)-1;
311 strlcpy(nodename, &bufline[2], count-2);
317 file_unmap(buf, bufsize);
319 if (path[0] == '\0' || nodename[0] == '\0')
322 if (handler_function(path, nodename) != 0)