From 7d50b32a129e781401cf897475f388f682de1368 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 27 Oct 2015 01:48:17 +0100 Subject: util-lib: split out globbing related calls into glob-util.[ch] --- Makefile.am | 2 ++ src/analyze/analyze.c | 1 + src/basic/glob-util.c | 71 +++++++++++++++++++++++++++++++++++++ src/basic/glob-util.h | 37 +++++++++++++++++++ src/basic/macro.h | 3 -- src/basic/util.c | 44 ----------------------- src/basic/util.h | 14 ++------ src/core/execute.c | 1 + src/core/path.c | 1 + src/journal-remote/journal-upload.c | 1 + src/journal/fsprg.h | 1 + src/journal/journalctl.c | 1 + src/shared/condition.c | 1 + src/systemctl/systemctl.c | 1 + src/test/test-util.c | 3 +- src/tmpfiles/tmpfiles.c | 1 + src/udev/udev-rules.c | 1 + 17 files changed, 125 insertions(+), 59 deletions(-) create mode 100644 src/basic/glob-util.c create mode 100644 src/basic/glob-util.h diff --git a/Makefile.am b/Makefile.am index c47ad240ab..3c4250a144 100644 --- a/Makefile.am +++ b/Makefile.am @@ -812,6 +812,8 @@ libbasic_la_SOURCES = \ src/basic/mount-util.h \ src/basic/hexdecoct.c \ src/basic/hexdecoct.h \ + src/basic/glob-util.h \ + src/basic/glob-util.c \ src/basic/extract-word.c \ src/basic/extract-word.h \ src/basic/escape.c \ diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index a165152cb2..7e63bfa821 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -30,6 +30,7 @@ #include "analyze-verify.h" #include "bus-error.h" #include "bus-util.h" +#include "glob-util.h" #include "hashmap.h" #include "locale-util.h" #include "log.h" diff --git a/src/basic/glob-util.c b/src/basic/glob-util.c new file mode 100644 index 0000000000..112c6392e5 --- /dev/null +++ b/src/basic/glob-util.c @@ -0,0 +1,71 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include + +#include "glob-util.h" +#include "strv.h" +#include "util.h" + +int glob_exists(const char *path) { + _cleanup_globfree_ glob_t g = {}; + int k; + + assert(path); + + errno = 0; + k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g); + + if (k == GLOB_NOMATCH) + return 0; + if (k == GLOB_NOSPACE) + return -ENOMEM; + if (k != 0) + return errno ? -errno : -EIO; + + return !strv_isempty(g.gl_pathv); +} + +int glob_extend(char ***strv, const char *path) { + _cleanup_globfree_ glob_t g = {}; + int k; + char **p; + + errno = 0; + k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g); + + if (k == GLOB_NOMATCH) + return -ENOENT; + if (k == GLOB_NOSPACE) + return -ENOMEM; + if (k != 0) + return errno ? -errno : -EIO; + if (strv_isempty(g.gl_pathv)) + return -ENOENT; + + STRV_FOREACH(p, g.gl_pathv) { + k = strv_extend(strv, *p); + if (k < 0) + return k; + } + + return 0; +} diff --git a/src/basic/glob-util.h b/src/basic/glob-util.h new file mode 100644 index 0000000000..8817df14b4 --- /dev/null +++ b/src/basic/glob-util.h @@ -0,0 +1,37 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include + +#include "macro.h" +#include "util.h" + +int glob_exists(const char *path); +int glob_extend(char ***strv, const char *path); + +#define _cleanup_globfree_ _cleanup_(globfree) + +_pure_ static inline bool string_is_glob(const char *p) { + /* Check if a string contains any glob patterns. */ + return !!strpbrk(p, GLOB_CHARS); +} diff --git a/src/basic/macro.h b/src/basic/macro.h index 975714da2b..daa7c416f7 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -294,9 +294,6 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) { #define PTR_TO_SIZE(p) ((size_t) ((uintptr_t) (p))) #define SIZE_TO_PTR(u) ((void *) ((uintptr_t) (u))) -#define memzero(x,l) (memset((x), 0, (l))) -#define zero(x) (memzero(&(x), sizeof(x))) - #define CHAR_TO_STR(x) ((char[2]) { x, 0 }) #define char_array_0(x) x[sizeof(x)-1] = 0; diff --git a/src/basic/util.c b/src/basic/util.c index 412ea50b96..6da311ad1e 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -368,49 +367,6 @@ int socket_from_display(const char *display, char **path) { return 0; } -int glob_exists(const char *path) { - _cleanup_globfree_ glob_t g = {}; - int k; - - assert(path); - - errno = 0; - k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g); - - if (k == GLOB_NOMATCH) - return 0; - else if (k == GLOB_NOSPACE) - return -ENOMEM; - else if (k == 0) - return !strv_isempty(g.gl_pathv); - else - return errno ? -errno : -EIO; -} - -int glob_extend(char ***strv, const char *path) { - _cleanup_globfree_ glob_t g = {}; - int k; - char **p; - - errno = 0; - k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g); - - if (k == GLOB_NOMATCH) - return -ENOENT; - else if (k == GLOB_NOSPACE) - return -ENOMEM; - else if (k != 0 || strv_isempty(g.gl_pathv)) - return errno ? -errno : -EIO; - - STRV_FOREACH(p, g.gl_pathv) { - k = strv_extend(strv, *p); - if (k < 0) - break; - } - - return k; -} - int block_get_whole_disk(dev_t d, dev_t *ret) { char *p, *s; int r; diff --git a/src/basic/util.h b/src/basic/util.h index 95c7c75b9c..3074029f38 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -94,9 +94,6 @@ bool plymouth_running(void); bool display_is_local(const char *display) _pure_; int socket_from_display(const char *display, char **path); -int glob_exists(const char *path); -int glob_extend(char ***strv, const char *path); - int block_get_whole_disk(dev_t d, dev_t *ret); #define NULSTR_FOREACH(i, l) \ @@ -132,7 +129,6 @@ static inline void freep(void *p) { } #define _cleanup_free_ _cleanup_(freep) -#define _cleanup_globfree_ _cleanup_(globfree) _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) { if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) @@ -155,13 +151,6 @@ _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_ return memdup(p, a * b); } -/** - * Check if a string contains any glob patterns. - */ -_pure_ static inline bool string_is_glob(const char *p) { - return !!strpbrk(p, GLOB_CHARS); -} - void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg); @@ -173,6 +162,9 @@ static inline void *mempset(void *s, int c, size_t n) { return (uint8_t*)s + n; } +#define memzero(x,l) (memset((x), 0, (l))) +#define zero(x) (memzero(&(x), sizeof(x))) + void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size); void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size); #define GREEDY_REALLOC(array, allocated, need) \ diff --git a/src/core/execute.c b/src/core/execute.c index 3814fd4381..bf13e29bd9 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -70,6 +70,7 @@ #include "fileio.h" #include "formats-util.h" #include "fs-util.h" +#include "glob-util.h" #include "io-util.h" #include "ioprio.h" #include "log.h" diff --git a/src/core/path.c b/src/core/path.c index 761ec265b4..9c5309a58a 100644 --- a/src/core/path.c +++ b/src/core/path.c @@ -28,6 +28,7 @@ #include "bus-util.h" #include "dbus-path.h" #include "fd-util.h" +#include "glob-util.h" #include "macro.h" #include "mkdir.h" #include "path.h" diff --git a/src/journal-remote/journal-upload.c b/src/journal-remote/journal-upload.c index 20e2a2f73b..1925475db8 100644 --- a/src/journal-remote/journal-upload.c +++ b/src/journal-remote/journal-upload.c @@ -31,6 +31,7 @@ #include "fd-util.h" #include "fileio.h" #include "formats-util.h" +#include "glob-util.h" #include "journal-upload.h" #include "log.h" #include "mkdir.h" diff --git a/src/journal/fsprg.h b/src/journal/fsprg.h index 150d034828..5959b1fed2 100644 --- a/src/journal/fsprg.h +++ b/src/journal/fsprg.h @@ -29,6 +29,7 @@ #include #include "macro.h" +#include "util.h" #ifdef __cplusplus extern "C" { diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 1965522dfd..2782ac0329 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -47,6 +47,7 @@ #include "fileio.h" #include "fs-util.h" #include "fsprg.h" +#include "glob-util.h" #include "hostname-util.h" #include "io-util.h" #include "journal-def.h" diff --git a/src/shared/condition.c b/src/shared/condition.c index d06120f0d7..2929e3e821 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -34,6 +34,7 @@ #include "condition.h" #include "extract-word.h" #include "fd-util.h" +#include "glob-util.h" #include "hostname-util.h" #include "ima-util.h" #include "mount-util.h" diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 2166554f4d..2faa93ef88 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -52,6 +52,7 @@ #include "fileio.h" #include "formats-util.h" #include "fs-util.h" +#include "glob-util.h" #include "hostname-util.h" #include "initreq.h" #include "install.h" diff --git a/src/test/test-util.c b/src/test/test-util.c index 6c6fce2d6a..647eff0496 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -39,6 +39,7 @@ #include "fileio.h" #include "fs-util.h" #include "fstab-util.h" +#include "glob-util.h" #include "hexdecoct.h" #include "io-util.h" #include "mkdir.h" @@ -54,8 +55,8 @@ #include "user-util.h" #include "util.h" #include "virt.h" -#include "xattr-util.h" #include "web-util.h" +#include "xattr-util.h" static void test_streq_ptr(void) { assert_se(streq_ptr(NULL, NULL)); diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 41deea70fb..5196447963 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -49,6 +49,7 @@ #include "fileio.h" #include "formats-util.h" #include "fs-util.h" +#include "glob-util.h" #include "io-util.h" #include "label.h" #include "log.h" diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index b87c14efb2..7d5f473d45 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -32,6 +32,7 @@ #include "conf-files.h" #include "escape.h" #include "fd-util.h" +#include "glob-util.h" #include "path-util.h" #include "stat-util.h" #include "strbuf.h" -- cgit v1.2.3-54-g00ecf