chiark / gitweb /
libudev: get rid of udev_utils.c
authorKay Sievers <kay.sievers@vrfy.org>
Wed, 10 Sep 2008 15:08:24 +0000 (17:08 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Wed, 10 Sep 2008 15:08:24 +0000 (17:08 +0200)
14 files changed:
extras/ata_id/Makefile.am
extras/cdrom_id/Makefile.am
extras/edd_id/Makefile.am
extras/floppy/Makefile.am
extras/fstab_import/Makefile.am
extras/scsi_id/Makefile.am
extras/usb_id/Makefile.am
extras/volume_id/Makefile.am
udev/lib/Makefile.am
udev/lib/libudev-device.c
udev/lib/libudev-enumerate.c
udev/lib/libudev-private.h
udev/lib/libudev-utils.c
udev/lib/libudev.c

index f749648..61c02fb 100644 (file)
@@ -12,6 +12,7 @@ ata_id_SOURCES = \
        ata_id.c \
        ../../udev/lib/libudev.h \
        ../../udev/lib/libudev.c \
+       ../../udev/lib/libudev-utils.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 9a39ee5..f0916ba 100644 (file)
@@ -15,6 +15,7 @@ cdrom_id_SOURCES = \
        cdrom_id.c \
        ../../udev/lib/libudev.h \
        ../../udev/lib/libudev.c \
+       ../../udev/lib/libudev-utils.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 575de85..d5dedda 100644 (file)
@@ -15,6 +15,7 @@ edd_id_SOURCES = \
        edd_id.c \
        ../../udev/lib/libudev.h \
        ../../udev/lib/libudev.c \
+       ../../udev/lib/libudev-utils.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 44e9b2f..de3876e 100644 (file)
@@ -11,6 +11,7 @@ create_floppy_devices_SOURCES = \
        create_floppy_devices.c \
        ../../udev/lib/libudev.h \
        ../../udev/lib/libudev.c \
+       ../../udev/lib/libudev-utils.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 92be9a4..07aadaf 100644 (file)
@@ -15,6 +15,7 @@ fstab_import_SOURCES = \
        fstab_import.c \
        ../../udev/lib/libudev.h \
        ../../udev/lib/libudev.c \
+       ../../udev/lib/libudev-utils.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index ffa9bf3..61813fb 100644 (file)
@@ -15,6 +15,7 @@ scsi_id_SOURCES = \
        bsg.h \
        ../../udev/lib/libudev.h \
        ../../udev/lib/libudev.c \
+       ../../udev/lib/libudev-utils.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index 9eb6f16..d69cfe4 100644 (file)
@@ -11,6 +11,7 @@ usb_id_SOURCES = \
        usb_id.c \
        ../../udev/lib/libudev.h \
        ../../udev/lib/libudev.c \
+       ../../udev/lib/libudev-utils.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_sysfs.c \
        ../../udev/udev_utils.c \
index 8560763..b4e7618 100644 (file)
@@ -14,6 +14,7 @@ vol_id_SOURCES = \
        vol_id.c \
        ../../udev/lib/libudev.h \
        ../../udev/lib/libudev.c \
+       ../../udev/lib/libudev-utils.c \
        ../../udev/udev_sysdeps.c \
        ../../udev/udev_utils.c \
        ../../udev/udev_utils_string.c
index a768daa..b33addf 100644 (file)
@@ -30,8 +30,6 @@ libudev_la_SOURCES =\
        libudev-monitor.c \
        ../list.h \
        ../udev.h \
-       ../udev_utils.c \
-       ../udev_utils_string.c \
        ../udev_sysdeps.c
 
 libudev_la_LDFLAGS = \
index 416cae4..78b644b 100644 (file)
@@ -61,7 +61,7 @@ static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *f
        strlcpy(filename, udev_get_dev_path(udev), len);
        start = strlcat(filename, "/.udev/db/", len);
        strlcat(filename, devpath, len);
-       return path_encode(&filename[start], len - start);
+       return util_path_encode(&filename[start], len - start);
 }
 
 static int device_read_db(struct udev_device *udev_device)
@@ -264,8 +264,8 @@ void udev_device_unref(struct udev_device *udev_device)
        free(udev_device->syspath);
        free(udev_device->devname);
        free(udev_device->subsystem);
-       name_list_cleanup(udev_device->udev, &udev_device->link_list);
-       name_list_cleanup(udev_device->udev, &udev_device->env_list);
+       util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
+       util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
        free(udev_device->action);
        free(udev_device->driver);
        free(udev_device->devpath_old);
@@ -477,14 +477,14 @@ int device_set_devname(struct udev_device *udev_device, const char *devname)
 
 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
 {
-       if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
+       if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
                return -ENOMEM;
        return 0;
 }
 
 int device_add_property(struct udev_device *udev_device, const char *property)
 {
-       if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
+       if (util_name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
                return -ENOMEM;
        return 0;
 }
index f36eafa..72b97ce 100644 (file)
@@ -59,7 +59,7 @@ static int devices_scan_subsystem(struct udev *udev,
                strlcat(devpath, "/", sizeof(devpath));
                strlcat(devpath, dent->d_name, sizeof(devpath));
                util_resolve_sys_link(udev, devpath, sizeof(devpath));
-               name_list_add(udev, device_list, devpath, 1);
+               util_name_list_add(udev, device_list, devpath, 1);
        }
        closedir(dir);
        return 0;
index 1579df6..a715067 100644 (file)
@@ -107,4 +107,11 @@ extern int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg);
 extern ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size);
 extern ssize_t util_get_sys_driver(struct udev *udev, const char *devpath, char *driver, size_t size);
 extern int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size);
+extern struct util_name_entry *util_name_list_add(struct udev *udev, struct list_head *name_list,
+                                                 const char *name, int sort);
+extern void util_name_list_cleanup(struct udev *udev, struct list_head *name_list);
+extern int util_log_priority(const char *priority);
+extern size_t util_path_encode(char *s, size_t len);
+extern size_t util_path_decode(char *s);
+extern void util_remove_trailing_chars(char *path, char c);
 #endif
index e5f1ed7..dfd5f3c 100644 (file)
@@ -94,3 +94,121 @@ int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size)
        strlcat(devpath, &link_target[back * 3], size);
        return 0;
 }
+
+struct util_name_entry {
+       struct list_head node;
+       char *name;
+       int *i;
+};
+
+struct util_name_entry *util_name_list_add(struct udev *udev, struct list_head *name_list,
+                                          const char *name, int sort)
+{
+       struct util_name_entry *name_loop;
+       struct util_name_entry *name_new;
+
+       /* avoid duplicate entries */
+       list_for_each_entry(name_loop, name_list, node) {
+               if (strcmp(name_loop->name, name) == 0) {
+                       dbg(udev, "'%s' is already in the list\n", name);
+                       return name_loop;
+               }
+       }
+
+       if (sort) {
+               list_for_each_entry(name_loop, name_list, node) {
+                       if (strcmp(name_loop->name, name) > 0)
+                               break;
+               }
+       }
+
+       name_new->name = strdup(name);
+       if (name_new->name == NULL)
+               return NULL;
+       dbg(udev, "adding '%s'\n", name_new->name);
+       list_add_tail(&name_new->node, &name_loop->node);
+       return name_new;
+}
+
+void util_name_list_cleanup(struct udev *udev, struct list_head *name_list)
+{
+       struct util_name_entry *name_loop;
+       struct util_name_entry *name_tmp;
+
+       list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
+               list_del(&name_loop->node);
+               free(name_loop->name);
+               free(name_loop);
+       }
+}
+
+int util_log_priority(const char *priority)
+{
+       char *endptr;
+       int prio;
+
+       prio = strtol(priority, &endptr, 10);
+       if (endptr[0] == '\0')
+               return prio;
+       if (strncasecmp(priority, "err", 3) == 0)
+               return LOG_ERR;
+       if (strcasecmp(priority, "info") == 0)
+               return LOG_INFO;
+       if (strcasecmp(priority, "debug") == 0)
+               return LOG_DEBUG;
+       return 0;
+}
+
+size_t util_path_encode(char *s, size_t len)
+{
+       char t[(len * 3)+1];
+       size_t i, j;
+
+       t[0] = '\0';
+       for (i = 0, j = 0; s[i] != '\0'; i++) {
+               if (s[i] == '/') {
+                       memcpy(&t[j], "\\x2f", 4);
+                       j += 4;
+               } else if (s[i] == '\\') {
+                       memcpy(&t[j], "\\x5c", 4);
+                       j += 4;
+               } else {
+                       t[j] = s[i];
+                       j++;
+               }
+       }
+       t[j] = '\0';
+       strncpy(s, t, len);
+       return j;
+}
+
+size_t util_path_decode(char *s)
+{
+       size_t i, j;
+
+       for (i = 0, j = 0; s[i] != '\0'; j++) {
+               if (memcmp(&s[i], "\\x2f", 4) == 0) {
+                       s[j] = '/';
+                       i += 4;
+               }else if (memcmp(&s[i], "\\x5c", 4) == 0) {
+                       s[j] = '\\';
+                       i += 4;
+               } else {
+                       s[j] = s[i];
+                       i++;
+               }
+       }
+       s[j] = '\0';
+       return j;
+}
+
+void util_remove_trailing_chars(char *path, char c)
+{
+       size_t len;
+
+       if (path == NULL)
+               return;
+       len = strlen(path);
+       while (len > 0 && path[len-1] == c)
+               path[--len] = '\0';
+}
index 3bcafcd..d1dd9e4 100644 (file)
@@ -184,18 +184,18 @@ struct udev *udev_new(void)
        if (env != NULL) {
                free(udev->sys_path);
                udev->sys_path = strdup(env);
-               remove_trailing_chars(udev->sys_path, '/');
+               util_remove_trailing_chars(udev->sys_path, '/');
        }
 
        env = getenv("UDEV_RUN");
-       if (env != NULL && !string_is_true(env))
+       if (env != NULL && strcmp(env, "0") == 0)
                udev->run = 0;
 
        env = getenv("UDEV_CONFIG_FILE");
        if (env != NULL) {
                free(config_file);
                config_file = strdup(env);
-               remove_trailing_chars(config_file, '/');
+               util_remove_trailing_chars(config_file, '/');
        }
        if (config_file == NULL)
                goto err;
@@ -263,19 +263,19 @@ struct udev *udev_new(void)
                        }
 
                        if (strcasecmp(key, "udev_log") == 0) {
-                               udev->log_priority = log_priority(val);
+                               udev->log_priority = util_log_priority(val);
                                continue;
                        }
                        if (strcasecmp(key, "udev_root") == 0) {
                                free(udev->dev_path);
                                udev->dev_path = strdup(val);
-                               remove_trailing_chars(udev->dev_path, '/');
+                               util_remove_trailing_chars(udev->dev_path, '/');
                                continue;
                        }
                        if (strcasecmp(key, "udev_rules") == 0) {
                                free(udev->rules_path);
                                udev->rules_path = strdup(val);
-                               remove_trailing_chars(udev->rules_path, '/');
+                               util_remove_trailing_chars(udev->rules_path, '/');
                                continue;
                        }
                }
@@ -286,12 +286,12 @@ struct udev *udev_new(void)
        if (env != NULL) {
                free(udev->dev_path);
                udev->dev_path = strdup(env);
-               remove_trailing_chars(udev->dev_path, '/');
+               util_remove_trailing_chars(udev->dev_path, '/');
        }
 
        env = getenv("UDEV_LOG");
        if (env != NULL)
-               udev->log_priority = log_priority(env);
+               udev->log_priority = util_log_priority(env);
 
        if (udev->dev_path == NULL || udev->sys_path == NULL)
                goto err;