int decompress_file(const char *path_in, const char *path_out, mode_t mode_out) { int fdi = open(path_in, O_RDONLY | O_CLOEXEC); if (fdi < 0) { perror_msg("Could not open file: %s", path_in); return -1; } int fdo = open(path_out, O_WRONLY | O_CLOEXEC | O_EXCL | O_CREAT, mode_out); if (fdo < 0) { close(fdi); perror_msg("Could not create file: %s", path_out); return -1; } int ret = decompress_fd(fdi, fdo); close(fdi); close(fdo); if (ret != 0) unlink(path_out); return ret; }
int decompress_file_ext_at(const char *path_in, int dir_fd, const char *path_out, mode_t mode_out, uid_t uid, gid_t gid, int src_flags, int dst_flags) { int fdi = open(path_in, src_flags); if (fdi < 0) { perror_msg("Could not open file: %s", path_in); return -1; } int fdo = openat(dir_fd, path_out, dst_flags, mode_out); if (fdo < 0) { close(fdi); perror_msg("Could not create file: %s", path_out); return -1; } int ret = decompress_fd(fdi, fdo); close(fdi); if (uid != (uid_t)-1L) { if (fchown(fdo, uid, gid) == -1) { perror_msg("Can't change ownership of '%s' to %lu:%lu", path_out, (long)uid, (long)gid); ret = -1; } } close(fdo); if (ret != 0) unlinkat(dir_fd, path_out, /*only files*/0); return ret; }