| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | #ifndef V_ALPM_NATIVE_EXTRACT_H |
| 3 | #define V_ALPM_NATIVE_EXTRACT_H |
| 4 | #include <stdint.h> |
| 5 | #include <unistd.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/sysmacros.h> |
| 9 | #include <sys/xattr.h> |
| 10 | |
| 11 | static int64_t v_alpm_extract_write(int fd, const void *data, size_t size, int64_t offset) { |
| 12 | return pwrite(fd, data, size, (off_t)offset); |
| 13 | } |
| 14 | static int v_alpm_extract_node(int parent, const char *name, uint32_t mode, uint64_t major_number, uint64_t minor_number) { |
| 15 | return mknodat(parent, name, mode, makedev(major_number, minor_number)); |
| 16 | } |
| 17 | static int v_alpm_extract_chown(int fd, const char *path, uint32_t uid, uint32_t gid) { |
| 18 | if(fd >= 0) { return fchown(fd, uid, gid); } |
| 19 | return fchownat(AT_FDCWD, path, uid, gid, AT_SYMLINK_NOFOLLOW); |
| 20 | } |
| 21 | static int v_alpm_extract_chmod(int fd, const char *path, uint32_t mode) { |
| 22 | if(fd >= 0) { return fchmod(fd, mode); } |
| 23 | return fchmodat(AT_FDCWD, path, mode, AT_SYMLINK_NOFOLLOW); |
| 24 | } |
| 25 | static int v_alpm_extract_owner(int fd, const char *path, uint32_t *uid, uint32_t *gid) { |
| 26 | struct stat info; |
| 27 | if((fd >= 0 ? fstat(fd, &info) : lstat(path, &info)) != 0) { return -1; } |
| 28 | *uid = info.st_uid; |
| 29 | *gid = info.st_gid; |
| 30 | return 0; |
| 31 | } |
| 32 | static int v_alpm_extract_times(int fd, const char *path, int atime_set, int64_t atime, int64_t atime_nsec, |
| 33 | int mtime_set, int64_t mtime, int64_t mtime_nsec, int64_t started_at) { |
| 34 | if(!atime_set && !mtime_set) { return 0; } |
| 35 | struct timespec times[2] = { |
| 36 | {.tv_sec = atime_set ? atime : started_at, .tv_nsec = atime_set ? atime_nsec : 0}, |
| 37 | {.tv_sec = mtime_set ? mtime : started_at, .tv_nsec = mtime_set ? mtime_nsec : 0} |
| 38 | }; |
| 39 | if(fd >= 0) { return futimens(fd, times); } |
| 40 | return utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW); |
| 41 | } |
| 42 | static int v_alpm_extract_xattr(int fd, const char *path, const char *name, const void *value, size_t size) { |
| 43 | if(fd >= 0) { return fsetxattr(fd, name, value, size, 0); } |
| 44 | return lsetxattr(path, name, value, size, 0); |
| 45 | } |
| 46 | #endif |
| 47 | |