From 9a56b87717ae6c24576b939b6484f3af175514d6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 14 Dec 2020 16:17:19 +0900 Subject: [PATCH] udev: move util_path_encode() and rename it to escape_path() --- src/libudev/libudev-util.c | 34 ---------------------------------- src/libudev/libudev-util.h | 1 - src/udev/udev-node.c | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/libudev/libudev-util.c b/src/libudev/libudev-util.c index 45977339ac3..f4f686fc88a 100644 --- a/src/libudev/libudev-util.c +++ b/src/libudev/libudev-util.c @@ -75,40 +75,6 @@ int util_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, return 0; } -size_t util_path_encode(const char *src, char *dest, size_t size) { - size_t i, j; - - assert(src); - assert(dest); - - for (i = 0, j = 0; src[i] != '\0'; i++) { - if (src[i] == '/') { - if (j+4 >= size) { - j = 0; - break; - } - memcpy(&dest[j], "\\x2f", 4); - j += 4; - } else if (src[i] == '\\') { - if (j+4 >= size) { - j = 0; - break; - } - memcpy(&dest[j], "\\x5c", 4); - j += 4; - } else { - if (j+1 >= size) { - j = 0; - break; - } - dest[j] = src[i]; - j++; - } - } - dest[j] = '\0'; - return j; -} - /* allow chars in allow list, plain ascii, hex-escaping and valid utf8 */ size_t util_replace_chars(char *str, const char *allow) { size_t i = 0, replaced = 0; diff --git a/src/libudev/libudev-util.h b/src/libudev/libudev-util.h index d2b22f99745..dd0561d90e9 100644 --- a/src/libudev/libudev-util.h +++ b/src/libudev/libudev-util.h @@ -10,7 +10,6 @@ #define UTIL_NAME_SIZE 512 #define UTIL_LINE_SIZE 16384 #define UDEV_ALLOWED_CHARS_INPUT "/ $%?," -size_t util_path_encode(const char *src, char *dest, size_t size); size_t util_replace_chars(char *str, const char *white); int util_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, bool read_value); diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c index 2cc78c9e2f4..9cb78ae8371 100644 --- a/src/udev/udev-node.c +++ b/src/udev/udev-node.c @@ -192,6 +192,40 @@ static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir, return 0; } +static size_t escape_path(const char *src, char *dest, size_t size) { + size_t i, j; + + assert(src); + assert(dest); + + for (i = 0, j = 0; src[i] != '\0'; i++) { + if (src[i] == '/') { + if (j+4 >= size) { + j = 0; + break; + } + memcpy(&dest[j], "\\x2f", 4); + j += 4; + } else if (src[i] == '\\') { + if (j+4 >= size) { + j = 0; + break; + } + memcpy(&dest[j], "\\x5c", 4); + j += 4; + } else { + if (j+1 >= size) { + j = 0; + break; + } + dest[j] = src[i]; + j++; + } + } + dest[j] = '\0'; + return j; +} + /* manage "stack of names" with possibly specified device priorities */ static int link_update(sd_device *dev, const char *slink, bool add) { _cleanup_free_ char *target = NULL, *filename = NULL, *dirname = NULL; @@ -206,7 +240,7 @@ static int link_update(sd_device *dev, const char *slink, bool add) { if (r < 0) return log_device_debug_errno(dev, r, "Failed to get id_filename: %m"); - util_path_encode(slink + STRLEN("/dev"), name_enc, sizeof(name_enc)); + escape_path(slink + STRLEN("/dev"), name_enc, sizeof(name_enc)); dirname = path_join("/run/udev/links/", name_enc); if (!dirname) return log_oom();