6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2004 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"
40 #define PATH_TO_NAME_CHAR '@'
42 static int get_db_filename(struct udevice *udev, char *filename, int len)
44 char devpath[SYSFS_PATH_MAX];
47 /* replace '/' to transform path into a filename */
48 strfieldcpy(devpath, udev->devpath);
49 pos = strchr(&devpath[1], '/');
51 pos[0] = PATH_TO_NAME_CHAR;
52 pos = strchr(&pos[1], '/');
54 snprintf(filename, len-1, "%s%s", udev_db_path, devpath);
55 filename[len-1] = '\0';
60 int udevdb_add_dev(struct udevice *udev)
62 char filename[SYSFS_PATH_MAX];
68 get_db_filename(udev, 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, "A:%d\n", udev->partitions);
89 static int parse_db_file(struct udevice *udev, const char *filename)
98 if (file_map(filename, &buf, &bufsize) != 0) {
99 dbg("unable to read db file '%s'", filename);
104 while (cur < bufsize) {
105 count = buf_get_line(buf, bufsize, cur);
111 if (count > DEVPATH_SIZE)
112 count = DEVPATH_SIZE-1;
113 strncpy(udev->devpath, &bufline[2], count-2);
116 if (count > NAME_SIZE)
118 strncpy(udev->name, &bufline[2], count-2);
121 if (count > NAME_SIZE)
123 strncpy(udev->symlink, &bufline[2], count-2);
126 strfieldcpy(line, &bufline[2]);
127 udev->partitions = atoi(line);
132 if (udev->name[0] == '\0')
138 int udevdb_get_dev(struct udevice *udev)
140 char filename[SYSFS_PATH_MAX];
142 get_db_filename(udev, filename, SYSFS_PATH_MAX);
144 return parse_db_file(udev, filename);
147 int udevdb_delete_dev(struct udevice *udev)
149 char filename[SYSFS_PATH_MAX];
151 get_db_filename(udev, filename, SYSFS_PATH_MAX);
157 int udevdb_get_dev_byname(struct udevice *udev, const char *name)
161 char filename[NAME_SIZE];
162 struct udevice db_udev;
164 dir = opendir(udev_db_path);
166 dbg("unable to udev db '%s'", udev_db_path);
172 if (ent == NULL || ent->d_name[0] == '\0')
175 if (ent->d_name[0] == '.')
178 snprintf(filename, NAME_SIZE-1, "%s/%s", udev_db_path, ent->d_name);
179 filename[NAME_SIZE-1] = '\0';
181 memset(&db_udev, 0x00, sizeof(struct udevice));
182 if (parse_db_file(&db_udev, filename) == 0) {
186 if (strncmp(name, db_udev.name, NAME_SIZE) == 0) {
190 foreach_strpart(db_udev.symlink, " ", pos, len) {
191 if (strncmp(name, pos, len) != 0)
194 if (len == strlen(name))
208 strfieldcpy(udev->devpath, db_udev.devpath);
209 strfieldcpy(udev->name, db_udev.name);
210 strfieldcpy(udev->symlink, db_udev.symlink);
211 udev->partitions = db_udev.partitions;