diff options
author | Tom Gundersen <teg@jklm.no> | 2014-11-21 19:14:13 +0100 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-12-11 13:54:19 +0100 |
commit | 3be9a22ccb3028bb293952b0aa7b47c33093fff5 (patch) | |
tree | f11634512f62adae811305b9b77181cffb44432b | |
parent | 338fb2173bbdb46c1fc1415f0102fcada2cd7aad (diff) |
sd-device: split set_sysname from set_syspath
This should make it a bit cheaper to allocate lots of unused devices.
-rw-r--r-- | src/libsystemd/sd-device/sd-device.c | 83 |
1 files changed, 46 insertions, 37 deletions
diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 4375d8e37..5ef9d399e 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -84,6 +84,7 @@ struct sd_device { bool parent_set; bool subsystem_set; bool driver_set; + bool sysname_set; bool tags_uptodate; bool devlinks_uptodate; @@ -259,10 +260,8 @@ static int device_add_devlink(sd_device *device, const char *devlink) { } static int device_set_syspath(sd_device *device, const char *_syspath, bool verify) { - _cleanup_free_ char *syspath = NULL, *sysname = NULL; - const char *devpath, *sysnum; - const char *pos; - size_t len = 0; + _cleanup_free_ char *syspath = NULL; + const char *devpath; int r; assert(device); @@ -314,34 +313,6 @@ static int device_set_syspath(sd_device *device, const char *_syspath, bool veri devpath = syspath + strlen("/sys"); - pos = strrchr(syspath, '/'); - if (!pos) - return -EINVAL; - pos ++; - - /* devpath is not a root directory */ - if (*pos == '\0' || pos <= devpath) - return -EINVAL; - - sysname = strdup(pos); - if (!sysname) - return -ENOMEM; - - /* some devices have '!' in their name, change that to '/' */ - while (sysname[len] != '\0') { - if (sysname[len] == '!') - sysname[len] = '/'; - - len ++; - } - - /* trailing number */ - while (len > 0 && isdigit(sysname[--len])) - sysnum = &sysname[len]; - - if (len == 0) - sysnum = NULL; - r = device_add_property(device, "DEVPATH", devpath); if (r < 0) return r; @@ -350,12 +321,7 @@ static int device_set_syspath(sd_device *device, const char *_syspath, bool veri device->syspath = syspath; syspath = NULL; - free(device->sysname); - device->sysname = sysname; - sysname = NULL; - device->devpath = devpath; - device->sysnum = sysnum; return 0; } @@ -375,6 +341,49 @@ _public_ int sd_device_get_sysname(sd_device *device, const char **ret) { assert_return(device, -EINVAL); assert_return(ret, -EINVAL); + if (!device->sysname_set) { + _cleanup_free_ char *sysname = NULL; + const char *sysnum; + const char *pos; + size_t len = 0; + + pos = strrchr(device->devpath, '/'); + if (!pos) + return -EINVAL; + pos ++; + + /* devpath is not a root directory */ + if (*pos == '\0' || pos <= device->devpath) + return -EINVAL; + + sysname = strdup(pos); + if (!sysname) + return -ENOMEM; + + /* some devices have '!' in their name, change that to '/' */ + while (sysname[len] != '\0') { + if (sysname[len] == '!') + sysname[len] = '/'; + + len ++; + } + + /* trailing number */ + while (len > 0 && isdigit(sysname[--len])) + sysnum = &sysname[len]; + + if (len == 0) + sysnum = NULL; + + free(device->sysname); + device->sysname = sysname; + sysname = NULL; + + device->sysnum = sysnum; + + device->sysname_set = true; + } + *ret = device->sysname; return 0; |