#include <stdlib.h>
#include <string.h>
#include <sys/capability.h>
+#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
return pakfire_path_match(pattern, path);
}
+
+int pakfire_file_consume(struct pakfire_file* file, int srcfd) {
+ ssize_t bytes_written = 0;
+ struct stat st = {};
+ int fd = -EBADF;
+ int r;
+
+ // Stat the source
+ r = fstat(srcfd, &st);
+ if (r < 0) {
+ ERROR(file->ctx, "Could not stat the source file descriptor: %m\n");
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Open the file for writing
+ fd = r = pakfire_file_open(file, O_WRONLY|O_TRUNC);
+ if (r < 0)
+ goto ERROR;
+
+ // Write everything to the file
+ bytes_written = sendfile(fd, srcfd, 0, st.st_size);
+ if (bytes_written < st.st_size) {
+ ERROR(file->ctx, "Could not sendfile() to %s: %m\n", pakfire_file_get_path(file));
+ r = -errno;
+ goto ERROR;
+ }
+
+ DEBUG(file->ctx, "Wrote %zd byte(s) to %s\n", bytes_written, pakfire_file_get_abspath(file));
+
+ // Reset r
+ r = 0;
+
+ERROR:
+ if (fd >= 0)
+ close(fd);
+
+ return r;
+}
int pakfire_file_verify(struct pakfire_file* file, int* status);
+int pakfire_file_consume(struct pakfire_file* file, int srcfd);
+
#endif /* PAKFIRE_PRIVATE */
#endif /* PAKFIRE_FILE_H */