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.
34 #include "libsysfs/sysfs/libsysfs.h"
36 #include "udev_utils.h"
40 #define PATH_TO_NAME_CHAR '@'
42 static int get_db_filename(const char *devpath, char *filename, int len)
44 char temp[SYSFS_PATH_MAX];
47 /* replace '/' to transform path into a filename */
48 strfieldcpy(temp, devpath);
49 pos = strchr(&temp[1], '/');
51 pos[0] = PATH_TO_NAME_CHAR;
52 pos = strchr(&pos[1], '/');
54 snprintf(filename, len, "%s%s", udev_db_path, temp);
55 filename[len-1] = '\0';
60 int udev_db_add_device(struct udevice *udev)
62 char filename[SYSFS_PATH_MAX];
68 get_db_filename(udev->devpath, filename, SYSFS_PATH_MAX);
70 create_path(filename);
72 f = fopen(filename, "w");
74 dbg("unable to create db file '%s'", filename);
77 dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
79 fprintf(f, "P:%s\n", udev->devpath);
80 fprintf(f, "N:%s\n", udev->name);
81 fprintf(f, "S:%s\n", udev->symlink);
82 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
83 fprintf(f, "A:%u\n", udev->partitions);
84 fprintf(f, "R:%u\n", udev->ignore_remove);
91 static int parse_db_file(struct udevice *udev, const char *filename)
95 unsigned int major, minor;
102 if (file_map(filename, &buf, &bufsize) != 0) {
103 dbg("unable to read db file '%s'", filename);
108 while (cur < bufsize) {
109 count = buf_get_line(buf, bufsize, cur);
115 if (count > DEVPATH_SIZE)
116 count = DEVPATH_SIZE-1;
117 strncpy(udev->devpath, &bufline[2], count-2);
118 udev->devpath[count-2] = '\0';
121 if (count > NAME_SIZE)
123 strncpy(udev->name, &bufline[2], count-2);
124 udev->name[count-2] = '\0';
127 if (count > NAME_SIZE)
129 strncpy(temp, &bufline[2], count-2);
130 temp[count-2] = '\0';
131 sscanf(temp, "%u:%u", &major, &minor);
132 udev->devt = makedev(major, minor);
135 if (count > NAME_SIZE)
137 strncpy(udev->symlink, &bufline[2], count-2);
138 udev->symlink[count-2] = '\0';
141 if (count > NAME_SIZE)
143 strncpy(line, &bufline[2], count-2);
144 line[count-2] = '\0';
145 udev->partitions = atoi(line);
148 if (count > NAME_SIZE)
150 strncpy(line, &bufline[2], count-2);
151 line[count-2] = '\0';
152 udev->ignore_remove = atoi(line);
157 if (udev->name[0] == '\0')
163 int udev_db_delete_device(struct udevice *udev)
165 char filename[SYSFS_PATH_MAX];
167 get_db_filename(udev->devpath, filename, SYSFS_PATH_MAX);
173 int udev_db_get_device_by_devpath(struct udevice *udev, const char *devpath)
175 char filename[SYSFS_PATH_MAX];
177 get_db_filename(devpath, filename, SYSFS_PATH_MAX);
179 return parse_db_file(udev, filename);
182 int udev_db_get_device_by_name(struct udevice *udev, const char *name)
186 char filename[NAME_SIZE];
187 struct udevice db_udev;
189 dir = opendir(udev_db_path);
191 dbg("unable to udev db '%s'", udev_db_path);
197 if (ent == NULL || ent->d_name[0] == '\0')
200 if (ent->d_name[0] == '.')
203 snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name);
204 filename[NAME_SIZE-1] = '\0';
206 memset(&db_udev, 0x00, sizeof(struct udevice));
207 if (parse_db_file(&db_udev, filename) == 0) {
211 if (strncmp(name, db_udev.name, NAME_SIZE) == 0) {
215 foreach_strpart(db_udev.symlink, " ", pos, len) {
216 if (strncmp(name, pos, len) != 0)
219 if (len == strlen(name))
233 strfieldcpy(udev->devpath, db_udev.devpath);
234 strfieldcpy(udev->name, db_udev.name);
235 strfieldcpy(udev->symlink, db_udev.symlink);
236 udev->partitions = db_udev.partitions;
237 udev->ignore_remove = db_udev.ignore_remove;
238 udev->devt = db_udev.devt;
243 int udev_db_call_foreach(int (*handler_function)(struct udevice *udev))
247 char filename[NAME_SIZE];
248 struct udevice db_udev;
250 dir = opendir(udev_db_path);
252 dbg("unable to udev db '%s'", udev_db_path);
258 if (ent == NULL || ent->d_name[0] == '\0')
261 if (ent->d_name[0] == '.')
264 snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name);
265 filename[NAME_SIZE-1] = '\0';
267 memset(&db_udev, 0x00, sizeof(struct udevice));
268 if (parse_db_file(&db_udev, filename) == 0) {
269 if (handler_function(&db_udev) != 0)