2017-10-11 20:57:18 +02:00
|
|
|
/* file.c - Contains all files related utilities
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2017-11-15 14:00:52 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2017-11-27 08:37:28 +01:00
|
|
|
#include <libgen.h>
|
2017-10-11 20:57:18 +02:00
|
|
|
#include <sys/sendfile.h>
|
2017-11-05 22:41:03 +01:00
|
|
|
#include <sys/mman.h>
|
2017-11-27 08:37:28 +01:00
|
|
|
#include <sys/inotify.h>
|
2017-11-19 20:40:37 +01:00
|
|
|
#include <linux/fs.h>
|
2017-10-14 15:10:22 +02:00
|
|
|
|
|
|
|
#ifndef NO_SELINUX
|
2017-10-11 20:57:18 +02:00
|
|
|
#include <selinux/selinux.h>
|
2017-10-14 15:10:22 +02:00
|
|
|
#endif
|
2017-10-11 20:57:18 +02:00
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
char **excl_list = (char *[]) { NULL };
|
|
|
|
|
|
|
|
static int is_excl(const char *name) {
|
|
|
|
for (int i = 0; excl_list[i]; ++i) {
|
|
|
|
if (strcmp(name, excl_list[i]) == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 15:10:22 +02:00
|
|
|
int fd_getpath(int fd, char *path, size_t size) {
|
|
|
|
snprintf(path, size, "/proc/self/fd/%d", fd);
|
|
|
|
if (xreadlink(path, path, size) == -1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:57:18 +02:00
|
|
|
int mkdir_p(const char *pathname, mode_t mode) {
|
|
|
|
char *path = strdup(pathname), *p;
|
|
|
|
errno = 0;
|
|
|
|
for (p = path + 1; *p; ++p) {
|
|
|
|
if (*p == '/') {
|
|
|
|
*p = '\0';
|
|
|
|
if (mkdir(path, mode) == -1) {
|
|
|
|
if (errno != EEXIST)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mkdir(path, mode) == -1) {
|
|
|
|
if (errno != EEXIST)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rm_rf(const char *path) {
|
|
|
|
int fd = xopen(path, O_RDONLY | O_CLOEXEC);
|
2017-11-05 22:41:03 +01:00
|
|
|
if (fd < 0)
|
|
|
|
return;
|
2017-10-11 20:57:18 +02:00
|
|
|
frm_rf(fd);
|
|
|
|
close(fd);
|
|
|
|
rmdir(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void frm_rf(int dirfd) {
|
|
|
|
struct dirent *entry;
|
|
|
|
int newfd;
|
2017-10-13 18:08:12 +02:00
|
|
|
DIR *dir = xfdopendir(dirfd);
|
2017-10-11 20:57:18 +02:00
|
|
|
|
2017-10-13 18:08:12 +02:00
|
|
|
while ((entry = xreaddir(dir))) {
|
2017-10-11 20:57:18 +02:00
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (is_excl(entry->d_name))
|
|
|
|
continue;
|
|
|
|
switch (entry->d_type) {
|
|
|
|
case DT_DIR:
|
2017-10-13 18:08:12 +02:00
|
|
|
newfd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
|
2017-10-11 20:57:18 +02:00
|
|
|
frm_rf(newfd);
|
|
|
|
close(newfd);
|
|
|
|
unlinkat(dirfd, entry->d_name, AT_REMOVEDIR);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unlinkat(dirfd, entry->d_name, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This will only on the same file system */
|
|
|
|
void mv_f(const char *source, const char *destination) {
|
|
|
|
struct stat st;
|
|
|
|
xlstat(source, &st);
|
|
|
|
int src, dest;
|
|
|
|
struct file_attr a;
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
xmkdir_p(destination, st.st_mode & 0777);
|
|
|
|
src = xopen(source, O_RDONLY | O_CLOEXEC);
|
|
|
|
dest = xopen(destination, O_RDONLY | O_CLOEXEC);
|
|
|
|
fclone_attr(src, dest);
|
|
|
|
mv_dir(src, dest);
|
|
|
|
close(src);
|
|
|
|
close(dest);
|
|
|
|
} else{
|
|
|
|
getattr(source, &a);
|
2017-10-13 18:08:12 +02:00
|
|
|
xrename(source, destination);
|
2017-10-11 20:57:18 +02:00
|
|
|
setattr(destination, &a);
|
|
|
|
}
|
|
|
|
rmdir(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This will only on the same file system */
|
|
|
|
void mv_dir(int src, int dest) {
|
|
|
|
struct dirent *entry;
|
|
|
|
DIR *dir;
|
|
|
|
int newsrc, newdest;
|
|
|
|
struct file_attr a;
|
|
|
|
|
2017-10-13 18:08:12 +02:00
|
|
|
dir = xfdopendir(src);
|
|
|
|
while ((entry = xreaddir(dir))) {
|
2017-10-11 20:57:18 +02:00
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (is_excl(entry->d_name))
|
|
|
|
continue;
|
|
|
|
getattrat(src, entry->d_name, &a);
|
|
|
|
switch (entry->d_type) {
|
|
|
|
case DT_DIR:
|
2017-10-13 18:08:12 +02:00
|
|
|
xmkdirat(dest, entry->d_name, a.st.st_mode & 0777);
|
|
|
|
newsrc = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC);
|
|
|
|
newdest = xopenat(dest, entry->d_name, O_RDONLY | O_CLOEXEC);
|
2017-10-11 20:57:18 +02:00
|
|
|
fsetattr(newdest, &a);
|
|
|
|
mv_dir(newsrc, newdest);
|
|
|
|
close(newsrc);
|
|
|
|
close(newdest);
|
|
|
|
unlinkat(src, entry->d_name, AT_REMOVEDIR);
|
|
|
|
break;
|
|
|
|
case DT_LNK:
|
|
|
|
case DT_REG:
|
|
|
|
renameat(src, entry->d_name, dest, entry->d_name);
|
|
|
|
setattrat(dest, entry->d_name, &a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cp_afc(const char *source, const char *destination) {
|
|
|
|
int src, dest;
|
|
|
|
struct file_attr a;
|
|
|
|
getattr(source, &a);
|
|
|
|
|
|
|
|
if (S_ISDIR(a.st.st_mode)) {
|
|
|
|
xmkdir_p(destination, a.st.st_mode & 0777);
|
|
|
|
src = xopen(source, O_RDONLY | O_CLOEXEC);
|
|
|
|
dest = xopen(destination, O_RDONLY | O_CLOEXEC);
|
|
|
|
fsetattr(dest, &a);
|
|
|
|
clone_dir(src, dest);
|
|
|
|
close(src);
|
|
|
|
close(dest);
|
|
|
|
} else{
|
|
|
|
unlink(destination);
|
|
|
|
if (S_ISREG(a.st.st_mode)) {
|
|
|
|
src = xopen(source, O_RDONLY);
|
|
|
|
dest = xopen(destination, O_WRONLY | O_CREAT | O_TRUNC);
|
|
|
|
xsendfile(dest, src, NULL, a.st.st_size);
|
|
|
|
fsetattr(src, &a);
|
|
|
|
close(src);
|
|
|
|
close(dest);
|
|
|
|
} else if (S_ISLNK(a.st.st_mode)) {
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
xreadlink(source, buf, sizeof(buf));
|
|
|
|
xsymlink(buf, destination);
|
|
|
|
setattr(destination, &a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clone_dir(int src, int dest) {
|
|
|
|
struct dirent *entry;
|
|
|
|
DIR *dir;
|
|
|
|
int srcfd, destfd, newsrc, newdest;
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
struct file_attr a;
|
|
|
|
|
2017-10-13 18:08:12 +02:00
|
|
|
dir = xfdopendir(src);
|
|
|
|
while ((entry = xreaddir(dir))) {
|
2017-10-11 20:57:18 +02:00
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (is_excl(entry->d_name))
|
|
|
|
continue;
|
|
|
|
getattrat(src, entry->d_name, &a);
|
|
|
|
switch (entry->d_type) {
|
|
|
|
case DT_DIR:
|
2017-10-13 18:08:12 +02:00
|
|
|
xmkdirat(dest, entry->d_name, a.st.st_mode & 0777);
|
2017-10-11 20:57:18 +02:00
|
|
|
setattrat(dest, entry->d_name, &a);
|
2017-10-13 18:08:12 +02:00
|
|
|
newsrc = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC);
|
|
|
|
newdest = xopenat(dest, entry->d_name, O_RDONLY | O_CLOEXEC);
|
2017-10-11 20:57:18 +02:00
|
|
|
clone_dir(newsrc, newdest);
|
|
|
|
close(newsrc);
|
|
|
|
close(newdest);
|
|
|
|
break;
|
|
|
|
case DT_REG:
|
2017-10-13 18:08:12 +02:00
|
|
|
destfd = xopenat(dest, entry->d_name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC);
|
|
|
|
srcfd = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC);
|
|
|
|
xsendfile(destfd, srcfd, 0, a.st.st_size);
|
2017-10-11 20:57:18 +02:00
|
|
|
fsetattr(destfd, &a);
|
|
|
|
close(destfd);
|
|
|
|
close(srcfd);
|
|
|
|
break;
|
|
|
|
case DT_LNK:
|
2017-10-13 18:08:12 +02:00
|
|
|
xreadlinkat(src, entry->d_name, buf, sizeof(buf));
|
2017-10-11 20:57:18 +02:00
|
|
|
symlinkat(buf, dest, entry->d_name);
|
|
|
|
setattrat(dest, entry->d_name, &a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 08:37:28 +01:00
|
|
|
void wait_till_exists(const char *target) {
|
|
|
|
if (access(target, F_OK) == 0)
|
|
|
|
return;
|
|
|
|
int fd = inotify_init();
|
|
|
|
char *dir = dirname(target);
|
|
|
|
char crap[PATH_MAX];
|
|
|
|
inotify_add_watch(fd, dir, IN_CREATE);
|
|
|
|
while (1) {
|
|
|
|
struct inotify_event event;
|
|
|
|
read(fd, &event, sizeof(event));
|
|
|
|
read(fd, crap, event.len);
|
|
|
|
if (access(target, F_OK) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:57:18 +02:00
|
|
|
int getattr(const char *path, struct file_attr *a) {
|
2017-10-14 15:10:22 +02:00
|
|
|
if (xlstat(path, &a->st) == -1)
|
2017-10-11 20:57:18 +02:00
|
|
|
return -1;
|
2017-10-14 15:10:22 +02:00
|
|
|
char *con = "";
|
|
|
|
#ifndef NO_SELINUX
|
|
|
|
if (lgetfilecon(path, &con) == -1)
|
|
|
|
return -1;
|
|
|
|
strcpy(a->con, con);
|
|
|
|
freecon(con);
|
|
|
|
#else
|
|
|
|
a->con[0] = '\0';
|
|
|
|
#endif
|
|
|
|
return 0;
|
2017-10-11 20:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int getattrat(int dirfd, const char *pathname, struct file_attr *a) {
|
2017-10-14 15:10:22 +02:00
|
|
|
int fd = xopenat(dirfd, pathname, O_PATH | O_NOFOLLOW | O_CLOEXEC);
|
2017-10-11 20:57:18 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
int ret = fgetattr(fd, a);
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fgetattr(int fd, struct file_attr *a) {
|
2017-10-14 15:10:22 +02:00
|
|
|
#ifndef NO_SELINUX
|
|
|
|
char path[PATH_MAX];
|
|
|
|
fd_getpath(fd, path, sizeof(path));
|
|
|
|
return getattr(path, a);
|
|
|
|
#else
|
|
|
|
if (fstat(fd, &a->st) == -1)
|
2017-10-11 20:57:18 +02:00
|
|
|
return -1;
|
2017-10-14 15:10:22 +02:00
|
|
|
a->con[0] = '\0';
|
2017-10-11 20:57:18 +02:00
|
|
|
return 0;
|
2017-10-14 15:10:22 +02:00
|
|
|
#endif
|
2017-10-11 20:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int setattr(const char *path, struct file_attr *a) {
|
2017-10-14 15:10:22 +02:00
|
|
|
if (chmod(path, a->st.st_mode & 0777) < 0)
|
2017-10-11 20:57:18 +02:00
|
|
|
return -1;
|
2017-10-14 15:10:22 +02:00
|
|
|
if (chown(path, a->st.st_uid, a->st.st_gid) < 0)
|
|
|
|
return -1;
|
|
|
|
#ifndef NO_SELINUX
|
|
|
|
if (strlen(a->con) && lsetfilecon(path, a->con) < 0)
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
2017-10-11 20:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int setattrat(int dirfd, const char *pathname, struct file_attr *a) {
|
2017-10-14 15:10:22 +02:00
|
|
|
int fd = xopenat(dirfd, pathname, O_PATH | O_NOFOLLOW | O_CLOEXEC);
|
2017-10-11 20:57:18 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
int ret = fsetattr(fd, a);
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fsetattr(int fd, struct file_attr *a) {
|
2017-10-14 15:10:22 +02:00
|
|
|
#ifndef NO_SELINUX
|
|
|
|
char path[PATH_MAX];
|
|
|
|
fd_getpath(fd, path, sizeof(path));
|
|
|
|
return setattr(path, a);
|
|
|
|
#else
|
2017-10-11 20:57:18 +02:00
|
|
|
if (fchmod(fd, a->st.st_mode & 0777) < 0)
|
|
|
|
return -1;
|
|
|
|
if (fchown(fd, a->st.st_uid, a->st.st_gid) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2017-10-14 15:10:22 +02:00
|
|
|
#endif
|
2017-10-11 20:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void clone_attr(const char *source, const char *target) {
|
|
|
|
struct file_attr a;
|
|
|
|
getattr(source, &a);
|
|
|
|
setattr(target, &a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fclone_attr(const int sourcefd, const int targetfd) {
|
|
|
|
struct file_attr a;
|
|
|
|
fgetattr(sourcefd, &a);
|
|
|
|
fsetattr(targetfd, &a);
|
|
|
|
}
|
2017-10-11 21:39:39 +02:00
|
|
|
|
2017-10-14 15:10:22 +02:00
|
|
|
#ifndef NO_SELINUX
|
|
|
|
|
2017-10-11 21:39:39 +02:00
|
|
|
#define UNLABEL_CON "u:object_r:unlabeled:s0"
|
|
|
|
#define SYSTEM_CON "u:object_r:system_file:s0"
|
|
|
|
|
|
|
|
void restorecon(int dirfd, int force) {
|
|
|
|
struct dirent *entry;
|
|
|
|
DIR *dir;
|
|
|
|
int fd;
|
2017-10-14 15:10:22 +02:00
|
|
|
char path[PATH_MAX], *con;
|
2017-10-11 21:39:39 +02:00
|
|
|
|
2017-10-14 15:10:22 +02:00
|
|
|
fd_getpath(dirfd, path, sizeof(path));
|
|
|
|
lgetfilecon(path, &con);
|
2017-10-11 21:39:39 +02:00
|
|
|
if (force || strlen(con) == 0 || strcmp(con, UNLABEL_CON) == 0)
|
2017-10-14 15:10:22 +02:00
|
|
|
lsetfilecon(path, SYSTEM_CON);
|
2017-10-11 21:39:39 +02:00
|
|
|
freecon(con);
|
|
|
|
|
2017-10-13 18:08:12 +02:00
|
|
|
dir = xfdopendir(dirfd);
|
|
|
|
while ((entry = xreaddir(dir))) {
|
2017-10-11 21:39:39 +02:00
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (entry->d_type == DT_DIR) {
|
2017-10-13 18:08:12 +02:00
|
|
|
fd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
|
2017-10-11 21:39:39 +02:00
|
|
|
restorecon(fd, force);
|
|
|
|
} else {
|
2017-10-13 18:08:12 +02:00
|
|
|
fd = xopenat(dirfd, entry->d_name, O_PATH | O_NOFOLLOW | O_CLOEXEC);
|
2017-10-14 15:10:22 +02:00
|
|
|
fd_getpath(fd, path, sizeof(path));
|
|
|
|
lgetfilecon(path, &con);
|
2017-10-11 21:39:39 +02:00
|
|
|
if (force || strlen(con) == 0 || strcmp(con, UNLABEL_CON) == 0)
|
2017-10-14 15:10:22 +02:00
|
|
|
lsetfilecon(path, SYSTEM_CON);
|
2017-10-11 21:39:39 +02:00
|
|
|
freecon(con);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
2017-10-14 15:10:22 +02:00
|
|
|
|
|
|
|
#endif // NO_SELINUX
|
2017-11-09 18:51:41 +01:00
|
|
|
|
2017-11-19 20:40:37 +01:00
|
|
|
static void _mmap(int rw, const char *filename, void **buf, size_t *size) {
|
|
|
|
struct stat st;
|
|
|
|
stat(filename, &st);
|
|
|
|
int fd = xopen(filename, rw ? O_RDWR : O_RDONLY);
|
|
|
|
if (S_ISBLK(st.st_mode))
|
|
|
|
ioctl(fd, BLKGETSIZE64, size);
|
|
|
|
else
|
|
|
|
*size = st.st_size;
|
|
|
|
*buf = *size > 0 ? xmmap(NULL, *size, PROT_READ | (rw ? PROT_WRITE : 0), MAP_SHARED, fd, 0) : NULL;
|
2017-11-09 18:51:41 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2017-11-19 20:40:37 +01:00
|
|
|
void mmap_ro(const char *filename, void **buf, size_t *size) {
|
|
|
|
_mmap(0, filename, buf, size);
|
|
|
|
}
|
|
|
|
|
2017-11-09 18:51:41 +01:00
|
|
|
void mmap_rw(const char *filename, void **buf, size_t *size) {
|
2017-11-19 20:40:37 +01:00
|
|
|
_mmap(1, filename, buf, size);
|
2017-11-09 18:51:41 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 13:25:41 +01:00
|
|
|
void full_read(int fd, void **buf, size_t *size) {
|
|
|
|
size_t cap = 1 << 20;
|
|
|
|
uint8_t tmp[1 << 20];
|
|
|
|
*buf = xmalloc(cap);
|
|
|
|
ssize_t read;
|
|
|
|
*size = 0;
|
|
|
|
while (1) {
|
|
|
|
read = xread(fd, tmp, sizeof(tmp));
|
|
|
|
if (read <= 0)
|
|
|
|
break;
|
|
|
|
if (*size + read > cap) {
|
|
|
|
cap *= 2;
|
|
|
|
*buf = realloc(*buf, cap);
|
|
|
|
}
|
|
|
|
memcpy(*buf + *size, tmp, read);
|
|
|
|
*size += read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 18:51:41 +01:00
|
|
|
void write_zero(int fd, size_t size) {
|
|
|
|
size_t pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
ftruncate(fd, pos + size);
|
|
|
|
lseek(fd, pos + size, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mem_align(size_t *pos, size_t align) {
|
|
|
|
size_t mask = align - 1;
|
|
|
|
if (*pos & mask) {
|
|
|
|
*pos += align - (*pos & mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_align(int fd, size_t align, int out) {
|
|
|
|
size_t pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
size_t mask = align - 1;
|
|
|
|
size_t off;
|
|
|
|
if (pos & mask) {
|
|
|
|
off = align - (pos & mask);
|
|
|
|
if (out) {
|
|
|
|
write_zero(fd, off);
|
|
|
|
} else {
|
|
|
|
lseek(fd, pos + off, SEEK_SET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|