From 02fa9ae58920b431bc37182716dd863f1c482651 Mon Sep 17 00:00:00 2001 From: "kay.sievers@vrfy.org" Date: Fri, 12 Nov 2004 06:32:19 +0100 Subject: [PATCH] rename udevdb* to udev_db* --- Makefile | 6 +- dev_d.c | 1 - namedev.c | 4 +- udev.c | 3 +- udev_add.c | 6 +- udev_db.c | 214 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ udev_db.h | 34 ++++++++++ udev_remove.c | 8 +-- udevdb.c | 214 ---------------------------------------------------------- udevdb.h | 34 ---------- udevinfo.c | 6 +- udevtest.c | 2 +- 12 files changed, 265 insertions(+), 267 deletions(-) create mode 100644 udev_db.c create mode 100644 udev_db.h delete mode 100644 udevdb.c delete mode 100644 udevdb.h diff --git a/Makefile b/Makefile index 50c72279f8..09b74fcde7 100644 --- a/Makefile +++ b/Makefile @@ -210,7 +210,7 @@ OBJS = udev_lib.o \ udev_add.o \ udev_remove.o \ udev_sysfs.o \ - udevdb.o \ + udev_db.o \ namedev.o \ namedev_parse.o \ dev_d.o \ @@ -220,7 +220,7 @@ HEADERS = udev.h \ udev_lib.h \ namedev.h \ udev_version.h \ - udevdb.h \ + udev_db.h \ udev_sysfs.h \ logging.h \ selinux.h \ @@ -292,7 +292,7 @@ $(TESTER): $(LIBC) $(TESTER).o $(OBJS) $(HEADERS) $(QUIET) $(STRIPCMD) $@ $(INFO): $(LIBC) $(INFO).o $(OBJS) $(HEADERS) - $(QUIET) $(LD) $(LDFLAGS) -o $@ $(CRT0) udevinfo.o udev_lib.o udev_config.o udevdb.o $(SYSFS) $(LIB_OBJS) $(ARCH_LIB_OBJS) + $(QUIET) $(LD) $(LDFLAGS) -o $@ $(CRT0) udevinfo.o udev_lib.o udev_config.o udev_db.o $(SYSFS) $(LIB_OBJS) $(ARCH_LIB_OBJS) $(QUIET) $(STRIPCMD) $@ $(DAEMON): $(LIBC) $(DAEMON).o $(OBJS) udevd.h diff --git a/dev_d.c b/dev_d.c index b3d05d9220..d14c70c256 100644 --- a/dev_d.c +++ b/dev_d.c @@ -29,7 +29,6 @@ #include "udev.h" #include "udev_lib.h" -#include "udevdb.h" #include "logging.h" static int run_program(const char *filename, void *data) diff --git a/namedev.c b/namedev.c index 9c94e69082..3ab45d4ec6 100644 --- a/namedev.c +++ b/namedev.c @@ -40,7 +40,7 @@ #include "udev_version.h" #include "logging.h" #include "namedev.h" -#include "udevdb.h" +#include "udev_db.h" static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr); @@ -194,7 +194,7 @@ static int find_free_number(struct udevice *udev, const char *name) while (1) { dbg("look for existing node '%s'", filename); memset(&db_udev, 0x00, sizeof(struct udevice)); - if (udevdb_get_dev_byname(&db_udev, filename) != 0) { + if (udev_db_get_device_byname(&db_udev, filename) != 0) { dbg("free num=%d", num); return num; } diff --git a/udev.c b/udev.c index bb3385e67d..a41f222778 100644 --- a/udev.c +++ b/udev.c @@ -34,9 +34,8 @@ #include "udev_lib.h" #include "udev_sysfs.h" #include "udev_version.h" -#include "logging.h" #include "namedev.h" -#include "udevdb.h" +#include "logging.h" #ifdef LOG diff --git a/udev_add.c b/udev_add.c index c139bd38bf..4fb5ef18c0 100644 --- a/udev_add.c +++ b/udev_add.c @@ -42,7 +42,7 @@ #include "udev_version.h" #include "logging.h" #include "namedev.h" -#include "udevdb.h" +#include "udev_db.h" #include "selinux.h" @@ -303,8 +303,8 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev) if (retval != 0) goto exit; - if (udevdb_add_dev(udev) != 0) - dbg("udevdb_add_dev failed, but we create the node anyway, " + if (udev_db_add_device(udev) != 0) + dbg("udev_db_add_dev failed, but we create the node anyway, " "remove might not work for custom names"); /* use full path to the environment */ diff --git a/udev_db.c b/udev_db.c new file mode 100644 index 0000000000..f2a890ea42 --- /dev/null +++ b/udev_db.c @@ -0,0 +1,214 @@ +/* + * udev_db.c + * + * Userspace devfs + * + * Copyright (C) 2003 Greg Kroah-Hartman + * Copyright (C) 2004 Kay Sievers + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libsysfs/sysfs/libsysfs.h" +#include "udev.h" +#include "udev_lib.h" +#include "logging.h" +#include "udev_db.h" + +#define PATH_TO_NAME_CHAR '@' + +static int get_db_filename(struct udevice *udev, char *filename, int len) +{ + char devpath[SYSFS_PATH_MAX]; + char *pos; + + /* replace '/' to transform path into a filename */ + strfieldcpy(devpath, udev->devpath); + pos = strchr(&devpath[1], '/'); + while (pos) { + pos[0] = PATH_TO_NAME_CHAR; + pos = strchr(&pos[1], '/'); + } + snprintf(filename, len, "%s%s", udev_db_path, devpath); + filename[len-1] = '\0'; + + return 0; +} + +int udev_db_add_device(struct udevice *udev) +{ + char filename[SYSFS_PATH_MAX]; + FILE *f; + + if (udev->test_run) + return 0; + + get_db_filename(udev, filename, SYSFS_PATH_MAX); + + create_path(filename); + + f = fopen(filename, "w"); + if (f == NULL) { + dbg("unable to create db file '%s'", filename); + return -1; + } + dbg("storing data for device '%s' in '%s'", udev->devpath, filename); + + fprintf(f, "P:%s\n", udev->devpath); + fprintf(f, "N:%s\n", udev->name); + fprintf(f, "S:%s\n", udev->symlink); + fprintf(f, "A:%d\n", udev->partitions); + + fclose(f); + + return 0; +} + +static int parse_db_file(struct udevice *udev, const char *filename) +{ + char line[NAME_SIZE]; + char *bufline; + char *buf; + size_t bufsize; + size_t cur; + size_t count; + + if (file_map(filename, &buf, &bufsize) != 0) { + dbg("unable to read db file '%s'", filename); + return -1; + } + + cur = 0; + while (cur < bufsize) { + count = buf_get_line(buf, bufsize, cur); + bufline = &buf[cur]; + cur += count+1; + + switch(bufline[0]) { + case 'P': + if (count > DEVPATH_SIZE) + count = DEVPATH_SIZE-1; + strncpy(udev->devpath, &bufline[2], count-2); + break; + case 'N': + if (count > NAME_SIZE) + count = NAME_SIZE-1; + strncpy(udev->name, &bufline[2], count-2); + break; + case 'S': + if (count > NAME_SIZE) + count = NAME_SIZE-1; + strncpy(udev->symlink, &bufline[2], count-2); + break; + case 'A': + strfieldcpy(line, &bufline[2]); + udev->partitions = atoi(line); + break; + } + } + + if (udev->name[0] == '\0') + return -1; + + return 0; +} + +int udev_db_get_device(struct udevice *udev) +{ + char filename[SYSFS_PATH_MAX]; + + get_db_filename(udev, filename, SYSFS_PATH_MAX); + + return parse_db_file(udev, filename); +} + +int udev_db_delete_device(struct udevice *udev) +{ + char filename[SYSFS_PATH_MAX]; + + get_db_filename(udev, filename, SYSFS_PATH_MAX); + unlink(filename); + + return 0; +} + +int udev_db_get_device_byname(struct udevice *udev, const char *name) +{ + struct dirent *ent; + DIR *dir; + char filename[NAME_SIZE]; + struct udevice db_udev; + + dir = opendir(udev_db_path); + if (dir == NULL) { + dbg("unable to udev db '%s'", udev_db_path); + return -1; + } + + while (1) { + ent = readdir(dir); + if (ent == NULL || ent->d_name[0] == '\0') + break; + + if (ent->d_name[0] == '.') + continue; + + snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name); + filename[NAME_SIZE-1] = '\0'; + + memset(&db_udev, 0x00, sizeof(struct udevice)); + if (parse_db_file(&db_udev, filename) == 0) { + char *pos; + int len; + + if (strncmp(name, db_udev.name, NAME_SIZE) == 0) { + goto found; + } + + foreach_strpart(db_udev.symlink, " ", pos, len) { + if (strncmp(name, pos, len) != 0) + continue; + + if (len == strlen(name)) + goto found; + } + + } + } + + closedir(dir); + + return -1; + +found: + closedir(dir); + + strfieldcpy(udev->devpath, db_udev.devpath); + strfieldcpy(udev->name, db_udev.name); + strfieldcpy(udev->symlink, db_udev.symlink); + udev->partitions = db_udev.partitions; + + return 0; +} diff --git a/udev_db.h b/udev_db.h new file mode 100644 index 0000000000..141c586f24 --- /dev/null +++ b/udev_db.h @@ -0,0 +1,34 @@ +/* + * udev_db.h + * + * Userspace devfs + * + * Copyright (C) 2003 Greg Kroah-Hartman + * Copyright (C) 2004 Kay Sievers + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#ifndef _UDEV_DB_H_ +#define _UDEV_DB_H_ + + +extern int udev_db_add_device(struct udevice *dev); +extern int udev_db_get_device(struct udevice *dev); +extern int udev_db_delete_device(struct udevice *dev); + +extern int udev_db_get_device_byname(struct udevice *udev, const char *name); + +#endif /* _UDEV_DB_H_ */ diff --git a/udev_remove.c b/udev_remove.c index 56834a45b9..32cd785984 100644 --- a/udev_remove.c +++ b/udev_remove.c @@ -33,9 +33,9 @@ #include "udev.h" #include "udev_lib.h" #include "udev_version.h" -#include "logging.h" #include "namedev.h" -#include "udevdb.h" +#include "udev_db.h" +#include "logging.h" static int delete_path(const char *path) { @@ -175,7 +175,7 @@ int udev_remove_device(struct udevice *udev) if (udev->type != 'b' && udev->type != 'c') return 0; - retval = udevdb_get_dev(udev); + retval = udev_db_get_device(udev); if (retval) { /* fall back to kernel name */ temp = strrchr(udev->devpath, '/'); @@ -186,7 +186,7 @@ int udev_remove_device(struct udevice *udev) } dbg("remove name='%s'", udev->name); - udevdb_delete_dev(udev); + udev_db_delete_device(udev); /* use full path to the environment */ snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name); diff --git a/udevdb.c b/udevdb.c deleted file mode 100644 index 38830b098e..0000000000 --- a/udevdb.c +++ /dev/null @@ -1,214 +0,0 @@ -/* - * udevdb.c - * - * Userspace devfs - * - * Copyright (C) 2003 Greg Kroah-Hartman - * Copyright (C) 2004 Kay Sievers - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation version 2 of the License. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "libsysfs/sysfs/libsysfs.h" -#include "udev.h" -#include "udev_lib.h" -#include "logging.h" -#include "udevdb.h" - -#define PATH_TO_NAME_CHAR '@' - -static int get_db_filename(struct udevice *udev, char *filename, int len) -{ - char devpath[SYSFS_PATH_MAX]; - char *pos; - - /* replace '/' to transform path into a filename */ - strfieldcpy(devpath, udev->devpath); - pos = strchr(&devpath[1], '/'); - while (pos) { - pos[0] = PATH_TO_NAME_CHAR; - pos = strchr(&pos[1], '/'); - } - snprintf(filename, len, "%s%s", udev_db_path, devpath); - filename[len-1] = '\0'; - - return 0; -} - -int udevdb_add_dev(struct udevice *udev) -{ - char filename[SYSFS_PATH_MAX]; - FILE *f; - - if (udev->test_run) - return 0; - - get_db_filename(udev, filename, SYSFS_PATH_MAX); - - create_path(filename); - - f = fopen(filename, "w"); - if (f == NULL) { - dbg("unable to create db file '%s'", filename); - return -1; - } - dbg("storing data for device '%s' in '%s'", udev->devpath, filename); - - fprintf(f, "P:%s\n", udev->devpath); - fprintf(f, "N:%s\n", udev->name); - fprintf(f, "S:%s\n", udev->symlink); - fprintf(f, "A:%d\n", udev->partitions); - - fclose(f); - - return 0; -} - -static int parse_db_file(struct udevice *udev, const char *filename) -{ - char line[NAME_SIZE]; - char *bufline; - char *buf; - size_t bufsize; - size_t cur; - size_t count; - - if (file_map(filename, &buf, &bufsize) != 0) { - dbg("unable to read db file '%s'", filename); - return -1; - } - - cur = 0; - while (cur < bufsize) { - count = buf_get_line(buf, bufsize, cur); - bufline = &buf[cur]; - cur += count+1; - - switch(bufline[0]) { - case 'P': - if (count > DEVPATH_SIZE) - count = DEVPATH_SIZE-1; - strncpy(udev->devpath, &bufline[2], count-2); - break; - case 'N': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(udev->name, &bufline[2], count-2); - break; - case 'S': - if (count > NAME_SIZE) - count = NAME_SIZE-1; - strncpy(udev->symlink, &bufline[2], count-2); - break; - case 'A': - strfieldcpy(line, &bufline[2]); - udev->partitions = atoi(line); - break; - } - } - - if (udev->name[0] == '\0') - return -1; - - return 0; -} - -int udevdb_get_dev(struct udevice *udev) -{ - char filename[SYSFS_PATH_MAX]; - - get_db_filename(udev, filename, SYSFS_PATH_MAX); - - return parse_db_file(udev, filename); -} - -int udevdb_delete_dev(struct udevice *udev) -{ - char filename[SYSFS_PATH_MAX]; - - get_db_filename(udev, filename, SYSFS_PATH_MAX); - unlink(filename); - - return 0; -} - -int udevdb_get_dev_byname(struct udevice *udev, const char *name) -{ - struct dirent *ent; - DIR *dir; - char filename[NAME_SIZE]; - struct udevice db_udev; - - dir = opendir(udev_db_path); - if (dir == NULL) { - dbg("unable to udev db '%s'", udev_db_path); - return -1; - } - - while (1) { - ent = readdir(dir); - if (ent == NULL || ent->d_name[0] == '\0') - break; - - if (ent->d_name[0] == '.') - continue; - - snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name); - filename[NAME_SIZE-1] = '\0'; - - memset(&db_udev, 0x00, sizeof(struct udevice)); - if (parse_db_file(&db_udev, filename) == 0) { - char *pos; - int len; - - if (strncmp(name, db_udev.name, NAME_SIZE) == 0) { - goto found; - } - - foreach_strpart(db_udev.symlink, " ", pos, len) { - if (strncmp(name, pos, len) != 0) - continue; - - if (len == strlen(name)) - goto found; - } - - } - } - - closedir(dir); - - return -1; - -found: - closedir(dir); - - strfieldcpy(udev->devpath, db_udev.devpath); - strfieldcpy(udev->name, db_udev.name); - strfieldcpy(udev->symlink, db_udev.symlink); - udev->partitions = db_udev.partitions; - - return 0; -} diff --git a/udevdb.h b/udevdb.h deleted file mode 100644 index 4f6b5de90c..0000000000 --- a/udevdb.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * udevdb.h - * - * Userspace devfs - * - * Copyright (C) 2003 Greg Kroah-Hartman - * Copyright (C) 2004 Kay Sievers - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation version 2 of the License. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#ifndef _UDEVDB_H_ -#define _UDEVDB_H_ - - -extern int udevdb_add_dev(struct udevice *dev); -extern int udevdb_get_dev(struct udevice *dev); -extern int udevdb_delete_dev(struct udevice *dev); - -extern int udevdb_get_dev_byname(struct udevice *udev, const char *name); - -#endif /* _UDEVDB_H_ */ diff --git a/udevinfo.c b/udevinfo.c index 1148272743..08c25d3a04 100644 --- a/udevinfo.c +++ b/udevinfo.c @@ -32,8 +32,8 @@ #include "udev.h" #include "udev_lib.h" #include "udev_version.h" +#include "udev_db.h" #include "logging.h" -#include "udevdb.h" #define SYSFS_VALUE_SIZE 256 @@ -355,7 +355,7 @@ static int process_options(int argc, char *argv[]) } memset(&udev, 0x00, sizeof(struct udevice)); strfieldcpy(udev.devpath, pos); - retval = udevdb_get_dev(&udev); + retval = udev_db_get_device(&udev); if (retval != 0) { printf("device not found in database\n"); goto exit; @@ -374,7 +374,7 @@ static int process_options(int argc, char *argv[]) memset(&udev, 0x00, sizeof(struct udevice)); strfieldcpy(udev.name, pos); - retval = udevdb_get_dev_byname(&udev, pos); + retval = udev_db_get_device_byname(&udev, pos); if (retval != 0) { printf("device not found in database\n"); goto exit; diff --git a/udevtest.c b/udevtest.c index 14c511584b..b9a5fea489 100644 --- a/udevtest.c +++ b/udevtest.c @@ -31,8 +31,8 @@ #include "udev.h" #include "udev_lib.h" #include "udev_version.h" -#include "logging.h" #include "namedev.h" +#include "logging.h" #ifdef LOG -- cgit v1.2.3-54-g00ecf