]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7d50b32a LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
48d7c648 | 21 | #include <dirent.h> |
11c3a366 | 22 | #include <errno.h> |
7d50b32a | 23 | #include <glob.h> |
48d7c648 | 24 | #include <sys/types.h> |
7d50b32a | 25 | |
48d7c648 | 26 | #include "dirent-util.h" |
7d50b32a | 27 | #include "glob-util.h" |
11c3a366 | 28 | #include "macro.h" |
48d7c648 | 29 | #include "path-util.h" |
7d50b32a | 30 | #include "strv.h" |
7d50b32a | 31 | |
48d7c648 | 32 | int safe_glob(const char *path, int flags, glob_t *pglob) { |
7d50b32a LP |
33 | int k; |
34 | ||
48d7c648 ZJS |
35 | /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */ |
36 | assert(!(flags & GLOB_ALTDIRFUNC)); | |
37 | ||
38 | if (!pglob->gl_closedir) | |
39 | pglob->gl_closedir = (void (*)(void *)) closedir; | |
40 | if (!pglob->gl_readdir) | |
41 | pglob->gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot; | |
42 | if (!pglob->gl_opendir) | |
43 | pglob->gl_opendir = (void *(*)(const char *)) opendir; | |
44 | if (!pglob->gl_lstat) | |
45 | pglob->gl_lstat = lstat; | |
46 | if (!pglob->gl_stat) | |
47 | pglob->gl_stat = stat; | |
7d50b32a LP |
48 | |
49 | errno = 0; | |
48d7c648 | 50 | k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob); |
7d50b32a LP |
51 | |
52 | if (k == GLOB_NOMATCH) | |
48d7c648 | 53 | return -ENOENT; |
7d50b32a LP |
54 | if (k == GLOB_NOSPACE) |
55 | return -ENOMEM; | |
56 | if (k != 0) | |
f5e5c28f | 57 | return errno > 0 ? -errno : -EIO; |
48d7c648 ZJS |
58 | if (strv_isempty(pglob->gl_pathv)) |
59 | return -ENOENT; | |
7d50b32a | 60 | |
48d7c648 | 61 | return 0; |
7d50b32a LP |
62 | } |
63 | ||
48d7c648 | 64 | int glob_exists(const char *path) { |
7d50b32a LP |
65 | _cleanup_globfree_ glob_t g = {}; |
66 | int k; | |
7d50b32a | 67 | |
48d7c648 | 68 | assert(path); |
7d50b32a | 69 | |
48d7c648 ZJS |
70 | k = safe_glob(path, GLOB_NOSORT|GLOB_BRACE, &g); |
71 | if (k == -ENOENT) | |
72 | return false; | |
73 | if (k < 0) | |
74 | return k; | |
75 | return true; | |
76 | } | |
77 | ||
78 | int glob_extend(char ***strv, const char *path) { | |
79 | _cleanup_globfree_ glob_t g = {}; | |
80 | int k; | |
7d50b32a | 81 | |
48d7c648 ZJS |
82 | k = safe_glob(path, GLOB_NOSORT|GLOB_BRACE, &g); |
83 | if (k < 0) | |
84 | return k; | |
7d50b32a | 85 | |
48d7c648 | 86 | return strv_extend_strv(strv, g.gl_pathv, false); |
7d50b32a | 87 | } |