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 err("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);
89 list_for_each_entry(name_loop, &udev->env_list, node)
90 fprintf(f, "E:%s\n", name_loop->name);
96 static int parse_db_file(struct udevice *udev, const char *filename)
99 unsigned int major, minor;
106 if (file_map(filename, &buf, &bufsize) != 0) {
107 err("unable to read db file '%s'", filename);
112 while (cur < bufsize) {
113 count = buf_get_line(buf, bufsize, cur);
119 if (count > sizeof(udev->devpath))
120 count = sizeof(udev->devpath);
121 strlcpy(udev->devpath, &bufline[2], count-1);
124 if (count > sizeof(udev->name))
125 count = sizeof(udev->name);
126 strlcpy(udev->name, &bufline[2], count-1);
129 if (count > sizeof(line))
130 count = sizeof(line);
131 strlcpy(line, &bufline[2], count-1);
132 sscanf(line, "%u:%u", &major, &minor);
133 udev->devt = makedev(major, minor);
136 if (count > sizeof(line))
137 count = sizeof(line);
138 strlcpy(line, &bufline[2], count-1);
139 name_list_add(&udev->symlink_list, line, 0);
142 if (count > sizeof(line))
143 count = sizeof(line);
144 strlcpy(line, &bufline[2], count-1);
145 udev->partitions = atoi(line);
148 if (count > sizeof(line))
149 count = sizeof(line);
150 strlcpy(line, &bufline[2], count-1);
151 udev->ignore_remove = atoi(line);
154 if (count > sizeof(line))
155 count = sizeof(line);
156 strlcpy(line, &bufline[2], count-1);
157 name_list_add(&udev->env_list, line, 0);
161 file_unmap(buf, bufsize);
163 if (udev->name[0] == '\0')
169 int udev_db_delete_device(struct udevice *udev)
171 char filename[PATH_SIZE];
173 get_db_filename(udev->devpath, filename, sizeof(filename));
179 int udev_db_get_device(struct udevice *udev, const char *devpath)
181 char filename[PATH_SIZE];
183 get_db_filename(devpath, filename, sizeof(filename));
185 if (parse_db_file(udev, filename) != 0)
191 int udev_db_search_name(char *devpath, size_t len, const char *name)
195 dir = opendir(udev_db_path);
197 err("unable to open udev_db '%s'", udev_db_path);
203 char filename[PATH_SIZE];
204 char path[PATH_SIZE];
205 char nodename[PATH_SIZE];
213 if (ent == NULL || ent->d_name[0] == '\0')
216 if (ent->d_name[0] == '.')
219 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
220 filename[sizeof(filename)-1] = '\0';
221 dbg("looking at '%s'", filename);
223 if (file_map(filename, &buf, &bufsize) != 0) {
224 err("unable to read db file '%s'", filename);
229 while (cur < bufsize) {
230 count = buf_get_line(buf, bufsize, cur);
236 if (count > sizeof(path))
237 count = sizeof(path);
238 strlcpy(path, &bufline[2], count-1);
242 if (count > sizeof(nodename))
243 count = sizeof(nodename);
244 strlcpy(nodename, &bufline[2], count-1);
245 dbg("compare '%s' '%s'", nodename, name);
246 if (strcmp(nodename, name) == 0) {
247 strlcpy(devpath, path, len);
248 file_unmap(buf, bufsize);
257 file_unmap(buf, bufsize);
264 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
268 dir = opendir(udev_db_path);
270 err("unable to open udev_db '%s'", udev_db_path);
276 char filename[PATH_SIZE];
277 char path[PATH_SIZE];
278 char nodename[PATH_SIZE];
286 if (ent == NULL || ent->d_name[0] == '\0')
289 if (ent->d_name[0] == '.')
292 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
293 filename[sizeof(filename)-1] = '\0';
294 dbg("looking at '%s'", filename);
296 if (file_map(filename, &buf, &bufsize) != 0) {
297 err("unable to read db file '%s'", filename);
304 while (cur < bufsize) {
305 count = buf_get_line(buf, bufsize, cur);
311 if (count > sizeof(path))
312 count = sizeof(path);
313 strlcpy(path, &bufline[2], count-1);
316 if (count > sizeof(nodename))
317 count = sizeof(nodename);
318 strlcpy(nodename, &bufline[2], count-1);
324 file_unmap(buf, bufsize);
326 if (path[0] == '\0' || nodename[0] == '\0')
329 if (handler_function(path, nodename) != 0)