tmpfiles: add the h and H entries, file attributes

tmpfiles.d(5) has h to set file attributes, as chattr(1) does, and H
for the path and everything below it:

    h /var/log/journal - - - - +C

tmpfiles skipped them as unsupported.

Add both.  The argument is the letters chattr(1) has, after a + to
add them, which is the default, a - to remove them, or a = to have
just these set.  Only files and directories have such attributes,
anything else is left alone.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 19:10:02 +01:00
1 parent 1ef0da9c50
commit 3ff5c33217
8 files changed
+131 -7

No files matched your search

+96
View File
@@ -33,6 +33,7 @@
#include <libgen.h>
#include <grp.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <sys/sysmacros.h>
#include <sys/utsname.h>
@@ -41,6 +42,7 @@
#include <time.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <linux/fs.h>
#include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h>
#ifdef _LIBITE_LITE
@@ -1076,6 +1078,92 @@ static int xattr_path(const char *path)
return 0;
}
/* File attributes of an h or H entry, as in chattr(1) */
static const struct {
char c;
unsigned flag;
} attrs[] = {
{ 'A', FS_NOATIME_FL }, { 'a', FS_APPEND_FL }, { 'c', FS_COMPR_FL },
{ 'C', FS_NOCOW_FL }, { 'd', FS_NODUMP_FL }, { 'D', FS_DIRSYNC_FL },
{ 'e', FS_EXTENT_FL }, { 'i', FS_IMMUTABLE_FL }, { 'j', FS_JOURNAL_DATA_FL },
{ 'P', FS_PROJINHERIT_FL },
{ 's', FS_SECRM_FL }, { 'S', FS_SYNC_FL }, { 't', FS_NOTAIL_FL },
{ 'T', FS_TOPDIR_FL }, { 'u', FS_UNRM_FL },
};
static unsigned attr_flags;
static char attr_op;
/* [+-=]LETTERS, to add, which is the default, remove, or set just these */
static int attr_parse(const char *arg)
{
attr_flags = 0;
attr_op = '+';
if (arg && strchr("+-=", *arg))
attr_op = *arg++;
for (; arg && *arg; arg++) {
size_t i;
for (i = 0; i < NELEMS(attrs); i++) {
if (attrs[i].c == *arg)
break;
}
if (i == NELEMS(attrs))
break;
attr_flags |= attrs[i].flag;
}
if (arg && !*arg && (attr_flags || attr_op == '='))
return 0;
errno = EINVAL;
return -1;
}
/*
* Only files and directories have them, anything else is left alone,
* and never opened: that starts a watchdog and rewinds a tape.
*/
static int attr_path(const char *path)
{
unsigned all = 0, flags, old;
struct stat st;
int fd, rc;
if (lstat(path, &st) || (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)))
return 0;
fd = open(path, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0)
return -1;
/* = leaves what is not in the table, and extents, which cannot be taken away */
for (size_t i = 0; i < NELEMS(attrs); i++)
all |= attrs[i].flag;
all &= ~FS_EXTENT_FL;
rc = ioctl(fd, FS_IOC_GETFLAGS, &old);
if (!rc) {
if (attr_op == '+')
flags = old | attr_flags;
else if (attr_op == '-')
flags = old & ~attr_flags;
else
flags = (old & ~all) | attr_flags;
if (flags != old)
rc = ioctl(fd, FS_IOC_SETFLAGS, &flags);
}
close(fd);
/* Not every file system has file attributes */
if (rc && (errno == ENOTTY || errno == EOPNOTSUPP || errno == ENOSYS))
rc = 0;
return rc;
}
/* 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)
{
@@ -1392,6 +1480,8 @@ static void tmpfiles(char *line)
case 'c':
case 'C':
case 'd':
case 'h':
case 'H':
case 't':
case 'T':
break;
@@ -1569,6 +1659,12 @@ static void tmpfiles(char *line)
rc = fclose(fp);
break;
}
case 'h':
case 'H':
rc = attr_parse(arg);
if (!rc)
rc = tree_do(path, attr_path, "file attributes", type[0] == 'H');
break;
case 't':
case 'T':
rc = xattr_parse(arg);