Skip to content

Commit ce7eb6a

Browse files
committed
sd-path: simplify implementation of sd_path_lookup*()
The two functions were duplicating a lot of functionality and more importantly, they both had explicit lists of types which are search paths. I want to add more types, and I don't want to have to remember to add them to both lists.
1 parent 09e6443 commit ce7eb6a

File tree

1 file changed

+42
-60
lines changed

1 file changed

+42
-60
lines changed

src/libsystemd/sd-path/sd-path.c

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -323,59 +323,55 @@ static int get_path(uint64_t type, char **buffer, const char **ret) {
323323
return -EOPNOTSUPP;
324324
}
325325

326-
_public_ int sd_path_lookup(uint64_t type, const char *suffix, char **path) {
326+
static int get_path_alloc(uint64_t type, const char *suffix, char **path) {
327327
_cleanup_free_ char *buffer = NULL;
328+
char *buffer2 = NULL;
328329
const char *ret;
329-
char *cc;
330330
int r;
331331

332-
assert_return(path, -EINVAL);
333-
334-
if (IN_SET(type,
335-
SD_PATH_SEARCH_BINARIES,
336-
SD_PATH_SEARCH_BINARIES_DEFAULT,
337-
SD_PATH_SEARCH_LIBRARY_PRIVATE,
338-
SD_PATH_SEARCH_LIBRARY_ARCH,
339-
SD_PATH_SEARCH_SHARED,
340-
SD_PATH_SEARCH_CONFIGURATION_FACTORY,
341-
SD_PATH_SEARCH_STATE_FACTORY,
342-
SD_PATH_SEARCH_CONFIGURATION)) {
332+
assert(path);
343333

344-
_cleanup_strv_free_ char **l = NULL;
345-
346-
r = sd_path_lookup_strv(type, suffix, &l);
347-
if (r < 0)
348-
return r;
334+
r = get_path(type, &buffer, &ret);
335+
if (r < 0)
336+
return r;
349337

350-
buffer = strv_join(l, ":");
338+
if (suffix) {
339+
suffix += strspn(suffix, "/");
340+
buffer2 = path_join(ret, suffix);
341+
if (!buffer2)
342+
return -ENOMEM;
343+
} else if (!buffer) {
344+
buffer = strdup(ret);
351345
if (!buffer)
352346
return -ENOMEM;
353-
354-
*path = TAKE_PTR(buffer);
355-
return 0;
356347
}
357348

358-
r = get_path(type, &buffer, &ret);
359-
if (r < 0)
349+
*path = buffer2 ?: TAKE_PTR(buffer);
350+
return 0;
351+
}
352+
353+
_public_ int sd_path_lookup(uint64_t type, const char *suffix, char **path) {
354+
int r;
355+
356+
assert_return(path, -EINVAL);
357+
358+
r = get_path_alloc(type, suffix, path);
359+
if (r != -EOPNOTSUPP)
360360
return r;
361361

362-
if (!suffix) {
363-
if (!buffer) {
364-
buffer = strdup(ret);
365-
if (!buffer)
366-
return -ENOMEM;
367-
}
362+
/* Fall back to sd_path_lookup_strv */
363+
_cleanup_strv_free_ char **l = NULL;
364+
char *buffer;
368365

369-
*path = TAKE_PTR(buffer);
370-
return 0;
371-
}
366+
r = sd_path_lookup_strv(type, suffix, &l);
367+
if (r < 0)
368+
return r;
372369

373-
suffix += strspn(suffix, "/");
374-
cc = path_join(ret, suffix);
375-
if (!cc)
370+
buffer = strv_join(l, ":");
371+
if (!buffer)
376372
return -ENOMEM;
377373

378-
*path = TAKE_PTR(cc);
374+
*path = buffer;
379375
return 0;
380376
}
381377

@@ -546,43 +542,29 @@ static int get_search(uint64_t type, char ***list) {
546542
}
547543

548544
_public_ int sd_path_lookup_strv(uint64_t type, const char *suffix, char ***paths) {
549-
char **i, **j;
550545
_cleanup_strv_free_ char **l = NULL, **n = NULL;
551546
int r;
552547

553548
assert_return(paths, -EINVAL);
554549

555-
if (!IN_SET(type,
556-
SD_PATH_SEARCH_BINARIES,
557-
SD_PATH_SEARCH_BINARIES_DEFAULT,
558-
SD_PATH_SEARCH_LIBRARY_PRIVATE,
559-
SD_PATH_SEARCH_LIBRARY_ARCH,
560-
SD_PATH_SEARCH_SHARED,
561-
SD_PATH_SEARCH_CONFIGURATION_FACTORY,
562-
SD_PATH_SEARCH_STATE_FACTORY,
563-
SD_PATH_SEARCH_CONFIGURATION)) {
564-
565-
char *p;
550+
r = get_search(type, &l);
551+
if (r == -EOPNOTSUPP) {
552+
_cleanup_free_ char *t = NULL;
566553

567-
r = sd_path_lookup(type, suffix, &p);
554+
r = get_path_alloc(type, suffix, &t);
568555
if (r < 0)
569556
return r;
570557

571558
l = new(char*, 2);
572-
if (!l) {
573-
free(p);
559+
if (!l)
574560
return -ENOMEM;
575-
}
576-
577-
l[0] = p;
561+
l[0] = TAKE_PTR(t);
578562
l[1] = NULL;
579563

580564
*paths = TAKE_PTR(l);
581565
return 0;
582-
}
583566

584-
r = get_search(type, &l);
585-
if (r < 0)
567+
} else if (r < 0)
586568
return r;
587569

588570
if (!suffix) {
@@ -594,16 +576,16 @@ _public_ int sd_path_lookup_strv(uint64_t type, const char *suffix, char ***path
594576
if (!n)
595577
return -ENOMEM;
596578

597-
j = n;
579+
char **i, **j = n;
598580
STRV_FOREACH(i, l) {
599581
*j = path_join(*i, suffix);
600582
if (!*j)
601583
return -ENOMEM;
602584

603585
j++;
604586
}
605-
606587
*j = NULL;
588+
607589
*paths = TAKE_PTR(n);
608590
return 0;
609591
}

0 commit comments

Comments
 (0)