mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
tmpfiles: fix C entries, never copy over what exists
A C entry for a directory does the opposite of tmpfiles.d(5):
C /etc/pam.d - - - -
When /etc/pam.d does not exist the entry fails with "Not a directory",
the files of the source are copied to a file with the name of the
path, one over the other. When it is an empty directory nothing is
copied. When it has files in it, they are overwritten with the ones
from /usr/share/factory, at every boot. For a file, an existing one
is overwritten as well.
is_dir_empty() returns true for a directory that is not empty, and
rsync() from libite copies over what is there and needs a slash at the
end of a destination directory that is missing.
Copy with a tree walk of our own instead. It copies to a path that
is missing or an empty directory, overwrites nothing, and does not
follow links. Mode, owner, and modification time are those of the
source.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
4 files changed
+258
-29
No files matched your search
+194
-29
@@ -131,7 +131,7 @@ static int is_dir_empty(const char *path)
|
||||
free(namelist[i]);
|
||||
free(namelist);
|
||||
|
||||
return num >= 3;
|
||||
return num < 3;
|
||||
}
|
||||
|
||||
static int glob_do(const char *path, int (*cb)(const char *))
|
||||
@@ -183,6 +183,195 @@ static void mkparent(char *path, mode_t mode)
|
||||
mkpath(dirname(strdupa(path)), mode);
|
||||
}
|
||||
|
||||
static int fd_chmod(int fd, mode_t mode)
|
||||
{
|
||||
char proc[32];
|
||||
|
||||
if (!fchmod(fd, mode))
|
||||
return 0;
|
||||
if (errno != EBADF)
|
||||
return -1;
|
||||
|
||||
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
|
||||
|
||||
return chmod(proc, mode);
|
||||
}
|
||||
|
||||
static int copy_errno;
|
||||
|
||||
static void copy_fail(void)
|
||||
{
|
||||
if (!copy_errno)
|
||||
copy_errno = errno;
|
||||
}
|
||||
|
||||
/* Owner before mode, as in perm_set(). Not root when run by hand */
|
||||
static void copy_perm(int fd, const struct stat *st)
|
||||
{
|
||||
if ((fchownat(fd, "", st->st_uid, st->st_gid, AT_EMPTY_PATH) && errno != EPERM) ||
|
||||
fd_chmod(fd, st->st_mode & 07777))
|
||||
copy_fail();
|
||||
}
|
||||
|
||||
static void copy_file(int sdir, const char *sname, int ddir, const char *dname, const struct stat *st)
|
||||
{
|
||||
struct timespec tv[2] = { st->st_atim, st->st_mtim };
|
||||
char buf[BUFSIZ];
|
||||
ssize_t len = -1;
|
||||
int in, out;
|
||||
|
||||
in = openat(sdir, sname, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (in < 0) {
|
||||
copy_fail();
|
||||
return;
|
||||
}
|
||||
|
||||
out = openat(ddir, dname, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
|
||||
if (out < 0) {
|
||||
if (errno != EEXIST)
|
||||
copy_fail();
|
||||
close(in);
|
||||
return;
|
||||
}
|
||||
|
||||
while ((len = read(in, buf, sizeof(buf))) > 0) {
|
||||
for (ssize_t off = 0, num; off < len; off += num) {
|
||||
num = write(out, &buf[off], len - off);
|
||||
if (num < 0) {
|
||||
len = -1;
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
}
|
||||
done:
|
||||
if (len < 0)
|
||||
copy_fail();
|
||||
else
|
||||
copy_perm(out, st);
|
||||
futimens(out, tv);
|
||||
close(in);
|
||||
close(out);
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy @sname in the directory @sdir to @dname in @ddir, with all that
|
||||
* is below it. What exists at the destination is left as it is, and
|
||||
* only a real directory there is gone into, never a link to one.
|
||||
*/
|
||||
static void copy_node(int sdir, const char *sname, int ddir, const char *dname)
|
||||
{
|
||||
char target[PATH_MAX];
|
||||
int sfd, dfd, created;
|
||||
struct dirent *d;
|
||||
struct stat st;
|
||||
ssize_t len;
|
||||
DIR *dir;
|
||||
|
||||
if (fstatat(sdir, sname, &st, AT_SYMLINK_NOFOLLOW)) {
|
||||
copy_fail();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (st.st_mode & S_IFMT) {
|
||||
case S_IFDIR:
|
||||
break;
|
||||
case S_IFREG:
|
||||
copy_file(sdir, sname, ddir, dname, &st);
|
||||
return;
|
||||
case S_IFLNK:
|
||||
len = readlinkat(sdir, sname, target, sizeof(target) - 1);
|
||||
if (len < 0) {
|
||||
copy_fail();
|
||||
return;
|
||||
}
|
||||
target[len] = 0;
|
||||
if (symlinkat(target, ddir, dname)) {
|
||||
if (errno != EEXIST)
|
||||
copy_fail();
|
||||
return;
|
||||
}
|
||||
if (fchownat(ddir, dname, st.st_uid, st.st_gid, AT_SYMLINK_NOFOLLOW) && errno != EPERM)
|
||||
copy_fail();
|
||||
return;
|
||||
default:
|
||||
if (mknodat(ddir, dname, (st.st_mode & S_IFMT) | 0600, st.st_rdev)) {
|
||||
if (errno != EEXIST)
|
||||
copy_fail();
|
||||
return;
|
||||
}
|
||||
dfd = openat(ddir, dname, O_PATH | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (dfd < 0) {
|
||||
copy_fail();
|
||||
return;
|
||||
}
|
||||
copy_perm(dfd, &st);
|
||||
close(dfd);
|
||||
return;
|
||||
}
|
||||
|
||||
created = !mkdirat(ddir, dname, 0700);
|
||||
if (!created && errno != EEXIST) {
|
||||
copy_fail();
|
||||
return;
|
||||
}
|
||||
|
||||
dfd = openat(ddir, dname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (dfd < 0) {
|
||||
if (created)
|
||||
copy_fail();
|
||||
return;
|
||||
}
|
||||
|
||||
sfd = openat(sdir, sname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||
dir = sfd < 0 ? NULL : fdopendir(sfd);
|
||||
if (!dir) {
|
||||
copy_fail();
|
||||
if (sfd >= 0)
|
||||
close(sfd);
|
||||
close(dfd);
|
||||
return;
|
||||
}
|
||||
|
||||
while ((d = readdir(dir))) {
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
copy_node(sfd, d->d_name, dfd, d->d_name);
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
if (created)
|
||||
copy_perm(dfd, &st);
|
||||
close(dfd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy @src, of which @st tells the kind, to @dst. Links are not
|
||||
* followed. A file is not copied when @dst exists. A directory is
|
||||
* when @dst is missing or an empty directory, with @merge also when
|
||||
* @dst has something in it, to add what is missing.
|
||||
*/
|
||||
static int copy_tree(const char *src, const struct stat *st, char *dst, int merge)
|
||||
{
|
||||
struct stat dt;
|
||||
|
||||
if (!lstat(dst, &dt)) {
|
||||
if (!S_ISDIR(st->st_mode) || !S_ISDIR(dt.st_mode))
|
||||
return 0;
|
||||
if (!merge && !is_dir_empty(dst))
|
||||
return 0;
|
||||
}
|
||||
|
||||
mkparent(dst, 0755);
|
||||
copy_errno = 0;
|
||||
copy_node(AT_FDCWD, src, AT_FDCWD, dst);
|
||||
if (copy_errno) {
|
||||
errno = copy_errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char *write_hex(FILE *fp, char *p)
|
||||
{
|
||||
unsigned char val;
|
||||
@@ -495,20 +684,6 @@ struct perm {
|
||||
};
|
||||
|
||||
/* Also for an fd opened with O_PATH, which fchmod() does not take */
|
||||
static int fd_chmod(int fd, mode_t mode)
|
||||
{
|
||||
char proc[32];
|
||||
|
||||
if (!fchmod(fd, mode))
|
||||
return 0;
|
||||
if (errno != EBADF)
|
||||
return -1;
|
||||
|
||||
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
|
||||
|
||||
return chmod(proc, mode);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set mode and owner on an open fd, only what differs is touched, the
|
||||
* path may be immutable. @created tells if the entry made the path.
|
||||
@@ -1739,22 +1914,12 @@ static void tmpfiles(char *line)
|
||||
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
||||
arg = buf;
|
||||
}
|
||||
if (fisdir(path) && !is_dir_empty(path))
|
||||
if (lstat(arg, &ast))
|
||||
break;
|
||||
if (fisdir(arg) && !fisslashdir(arg)) {
|
||||
size_t len = strlen(arg) + 2;
|
||||
|
||||
dst = malloc(len);
|
||||
if (!dst)
|
||||
break;
|
||||
snprintf(dst, len, "%s/", arg);
|
||||
arg = dst;
|
||||
}
|
||||
mkparent(path, 0755);
|
||||
rc = rsync(arg, path, LITE_FOPT_KEEP_MTIME, NULL);
|
||||
if (rc && errno == ENOENT)
|
||||
rc = 0;
|
||||
else if (!rc && (strc || S_ISDIR(st.st_mode)) && perm_path(path, &perm, strc, O_PATH))
|
||||
rc = copy_tree(arg, &ast, path, 0);
|
||||
if (!rc && !lstat(path, &st) && !S_ISLNK(st.st_mode) && !((st.st_mode ^ ast.st_mode) & S_IFMT) &&
|
||||
perm_path(path, &perm, strc, O_PATH))
|
||||
warn("Failed setting mode/owner on %s", path);
|
||||
break;
|
||||
case 'd':
|
||||
|
||||
Reference in new issue
Block a user