util-lib: split out globbing related calls into glob-util.[ch]

This commit is contained in:
Lennart Poettering 2015-10-27 01:48:17 +01:00
parent 872a590ef8
commit 7d50b32a12
17 changed files with 125 additions and 59 deletions

View File

@ -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 \

View File

@ -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"

71
src/basic/glob-util.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
***/
#include <glob.h>
#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;
}

37
src/basic/glob-util.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
***/
#include <string.h>
#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);
}

View File

@ -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;

View File

@ -23,7 +23,6 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <grp.h>
#include <langinfo.h>
#include <libintl.h>
@ -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;

View File

@ -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) \

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -29,6 +29,7 @@
#include <inttypes.h>
#include "macro.h"
#include "util.h"
#ifdef __cplusplus
extern "C" {

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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));

View File

@ -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"

View File

@ -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"