- val[0] = '\0';
- val = &val[1];
- if (val[0] == '\0')
- val = NULL;
- return udev_device_add_property_internal(udev_device, name, val);
-}
-
-static int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
-{
- const char *pos;
- size_t len;
-
- free(udev_device->syspath);
- udev_device->syspath = strdup(syspath);
- if (udev_device->syspath == NULL)
- return -ENOMEM;
- udev_device->devpath = udev_device->syspath + strlen("/sys");
- udev_device_add_property_internal(udev_device, "DEVPATH", udev_device->devpath);
-
- pos = strrchr(udev_device->syspath, '/');
- if (pos == NULL)
- return -EINVAL;
- udev_device->sysname = strdup(&pos[1]);
- if (udev_device->sysname == NULL)
- return -ENOMEM;
-
- /* some devices have '!' in their name, change that to '/' */
- len = 0;
- while (udev_device->sysname[len] != '\0') {
- if (udev_device->sysname[len] == '!')
- udev_device->sysname[len] = '/';
- len++;
- }
-
- /* trailing number */
- while (len > 0 && isdigit(udev_device->sysname[--len]))
- udev_device->sysnum = &udev_device->sysname[len];
-
- /* sysname is completely numeric */
- if (len == 0)
- udev_device->sysnum = NULL;
-
- return 0;
-}
-
-static void udev_device_set_usec_initialized(struct udev_device *udev_device, usec_t usec_initialized)
-{
- char num[DECIMAL_STR_MAX(usec_t)];
-
- udev_device->usec_initialized = usec_initialized;
- snprintf(num, sizeof(num), USEC_FMT, usec_initialized);
- udev_device_add_property_internal(udev_device, "USEC_INITIALIZED", num);
-}
-
-void udev_device_ensure_usec_initialized(struct udev_device *udev_device, struct udev_device *old_device)
-{
- if (old_device && old_device->usec_initialized != 0)
- udev_device_set_usec_initialized(udev_device, old_device->usec_initialized);
- else
- udev_device_set_usec_initialized(udev_device, now(CLOCK_MONOTONIC));
-}
-
-static int udev_device_set_action(struct udev_device *udev_device, const char *action)
-{
- free(udev_device->action);
- udev_device->action = strdup(action);
- if (udev_device->action == NULL)
- return -ENOMEM;
- udev_device_add_property_internal(udev_device, "ACTION", udev_device->action);
- return 0;
-}
-
-/*
- * parse property string, and if needed, update internal values accordingly
- *
- * udev_device_add_property_from_string_parse_finish() needs to be
- * called after adding properties, and its return value checked
- *
- * udev_device_set_info_loaded() needs to be set, to avoid trying
- * to use a device without a DEVPATH set
- */
-static void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
-{
- if (startswith(property, "DEVPATH=")) {
- char path[UTIL_PATH_SIZE];
-
- strscpyl(path, sizeof(path), "/sys", &property[8], NULL);
- udev_device_set_syspath(udev_device, path);
- } else if (startswith(property, "SUBSYSTEM=")) {
- udev_device_set_subsystem(udev_device, &property[10]);
- } else if (startswith(property, "DEVTYPE=")) {
- udev_device_set_devtype(udev_device, &property[8]);
- } else if (startswith(property, "DEVNAME=")) {
- udev_device_set_devnode(udev_device, &property[8]);
- } else if (startswith(property, "DEVLINKS=")) {
- char devlinks[UTIL_PATH_SIZE];
- char *slink;
- char *next;
-
- strscpy(devlinks, sizeof(devlinks), &property[9]);
- slink = devlinks;
- next = strchr(slink, ' ');
- while (next != NULL) {
- next[0] = '\0';
- udev_device_add_devlink(udev_device, slink);
- slink = &next[1];
- next = strchr(slink, ' ');
- }
- if (slink[0] != '\0')
- udev_device_add_devlink(udev_device, slink);
- } else if (startswith(property, "TAGS=")) {
- char tags[UTIL_PATH_SIZE];
- char *next;
-
- strscpy(tags, sizeof(tags), &property[5]);
- next = strchr(tags, ':');
- if (next != NULL) {
- next++;
- while (next[0] != '\0') {
- char *tag;
-
- tag = next;
- next = strchr(tag, ':');
- if (next == NULL)
- break;
- next[0] = '\0';
- next++;
- udev_device_add_tag(udev_device, tag);
- }
- }
- } else if (startswith(property, "USEC_INITIALIZED=")) {
- udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10));
- } else if (startswith(property, "DRIVER=")) {
- udev_device_set_driver(udev_device, &property[7]);
- } else if (startswith(property, "ACTION=")) {
- udev_device_set_action(udev_device, &property[7]);
- } else if (startswith(property, "MAJOR=")) {
- udev_device->maj = strtoull(&property[6], NULL, 10);
- } else if (startswith(property, "MINOR=")) {
- udev_device->min = strtoull(&property[6], NULL, 10);
- } else if (startswith(property, "DEVPATH_OLD=")) {
- udev_device_set_devpath_old(udev_device, &property[12]);
- } else if (startswith(property, "SEQNUM=")) {
- udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
- } else if (startswith(property, "IFINDEX=")) {
- udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
- } else if (startswith(property, "DEVMODE=")) {
- udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
- } else if (startswith(property, "DEVUID=")) {
- udev_device_set_devnode_uid(udev_device, strtoul(&property[7], NULL, 10));
- } else if (startswith(property, "DEVGID=")) {
- udev_device_set_devnode_gid(udev_device, strtoul(&property[7], NULL, 10));
- } else {
- udev_device_add_property_from_string(udev_device, property);