mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
tmpfiles: add the t and T entries, extended attributes
tmpfiles.d(5) has t to set extended attributes on a path, and T for
the path and everything below it:
t /srv/www - - - - user.mime_type=text/html
tmpfiles skipped them as unsupported.
Add both. The argument is one or more NAME=VALUE with space between,
a value with space in it goes in quotes. They are set with
lsetxattr(), a link is never followed.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
5 files changed
+170
-2
No files matched your search
+121
-1
@@ -964,6 +964,118 @@ static int acl_entry(const char *pattern, char *arg, int recursive, int modify)
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* For the entries that take a glob and have a recursive form. @fn
|
||||
* and @what are global, nftw() cannot pass user data.
|
||||
*/
|
||||
static int (*tree_fn)(const char *path);
|
||||
static const char *tree_what;
|
||||
|
||||
static int tree_cb(const char *path, const struct stat *st, int tflag, struct FTW *ftw)
|
||||
{
|
||||
(void)tflag; (void)ftw;
|
||||
|
||||
/* None of this is for a link, and what it points to is not ours to change */
|
||||
if (!S_ISLNK(st->st_mode) && tree_fn(path))
|
||||
warn("Failed setting %s on %s", tree_what, path);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tree_do(const char *pattern, int (*fn)(const char *), const char *what, int recursive)
|
||||
{
|
||||
glob_t gl;
|
||||
|
||||
if (glob(pattern, GLOB_NOESCAPE, NULL, &gl))
|
||||
return 0;
|
||||
|
||||
tree_fn = fn;
|
||||
tree_what = what;
|
||||
for (size_t i = 0; i < gl.gl_pathc; i++) {
|
||||
struct stat st;
|
||||
|
||||
if (recursive)
|
||||
nftw(gl.gl_pathv[i], tree_cb, 20, FTW_PHYS);
|
||||
else if (!lstat(gl.gl_pathv[i], &st))
|
||||
tree_cb(gl.gl_pathv[i], &st, 0, NULL);
|
||||
}
|
||||
globfree(&gl);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Extended attributes of a t or T entry, see xattr_parse() */
|
||||
#define XATTR_MAX 16
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
char *value;
|
||||
} xattrs[XATTR_MAX];
|
||||
static int num_xattrs;
|
||||
|
||||
/*
|
||||
* NAME=VALUE pairs with space between, cut up where they are in @arg.
|
||||
* A value may be in quotes, and a backslash takes the next character
|
||||
* as it is.
|
||||
*/
|
||||
static int xattr_parse(char *arg)
|
||||
{
|
||||
num_xattrs = 0;
|
||||
|
||||
while (arg && *arg) {
|
||||
char quote = 0, *out;
|
||||
|
||||
arg += strspn(arg, "\t ");
|
||||
if (!*arg)
|
||||
break;
|
||||
if (num_xattrs == XATTR_MAX)
|
||||
goto inval;
|
||||
|
||||
xattrs[num_xattrs].name = arg;
|
||||
arg = strchr(arg, '=');
|
||||
if (!arg || arg == xattrs[num_xattrs].name)
|
||||
goto inval;
|
||||
*arg++ = 0;
|
||||
if (strpbrk(xattrs[num_xattrs].name, "\t "))
|
||||
goto inval;
|
||||
|
||||
xattrs[num_xattrs++].value = out = arg;
|
||||
for (; *arg; arg++) {
|
||||
if (*arg == '\\' && arg[1])
|
||||
*out++ = *++arg;
|
||||
else if (quote && *arg == quote)
|
||||
quote = 0;
|
||||
else if (!quote && (*arg == '"' || *arg == '\''))
|
||||
quote = *arg;
|
||||
else if (!quote && (*arg == ' ' || *arg == '\t'))
|
||||
break;
|
||||
else
|
||||
*out++ = *arg;
|
||||
}
|
||||
if (*arg)
|
||||
arg++;
|
||||
*out = 0;
|
||||
if (quote)
|
||||
goto inval;
|
||||
}
|
||||
|
||||
if (num_xattrs)
|
||||
return 0;
|
||||
inval:
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int xattr_path(const char *path)
|
||||
{
|
||||
for (int i = 0; i < num_xattrs; i++) {
|
||||
if (lsetxattr(path, xattrs[i].name, xattrs[i].value, strlen(xattrs[i].value), 0))
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* First line of @file, without @strip characters, empty when it cannot be read */
|
||||
static char *specifier_file(const char *file, const char *strip, char *buf, size_t len)
|
||||
{
|
||||
@@ -1260,7 +1372,7 @@ static void tmpfiles(char *line)
|
||||
if (!arg[0] || !strcmp(arg, "-"))
|
||||
arg = NULL;
|
||||
}
|
||||
if (arg && strchr(arg, '%') && strchr("CfFlLw", type[0])) {
|
||||
if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) {
|
||||
arg = expand(arg, argbuf, sizeof(argbuf));
|
||||
if (!arg) {
|
||||
if (!quiet)
|
||||
@@ -1280,6 +1392,8 @@ static void tmpfiles(char *line)
|
||||
case 'c':
|
||||
case 'C':
|
||||
case 'd':
|
||||
case 't':
|
||||
case 'T':
|
||||
break;
|
||||
case 'D':
|
||||
rmcontents(path);
|
||||
@@ -1455,6 +1569,12 @@ static void tmpfiles(char *line)
|
||||
rc = fclose(fp);
|
||||
break;
|
||||
}
|
||||
case 't':
|
||||
case 'T':
|
||||
rc = xattr_parse(arg);
|
||||
if (!rc)
|
||||
rc = tree_do(path, xattr_path, "extended attributes", type[0] == 'T');
|
||||
break;
|
||||
case 'l': /* Finit extension, like 'L' but only if target exists */
|
||||
if (!arg) {
|
||||
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
||||
|
||||
Reference in new issue
Block a user