static int archive_read_file_open(struct archive* a, FILE* f) {
struct pakfire_archive_file* file = NULL;
int fd = -EBADF;
+ int r;
// Fetch the underlying file descriptor
fd = fileno(f);
fd = -EBADF;
// Start reading at the beginning
- rewind(f);
+ r = pakfire_rewind(f);
+ if (r < 0)
+ goto ERROR;
// Allocate an object
file = calloc(1, sizeof(*file));
int r;
// Start reading at the beginning
- rewind(archive->f);
+ r = pakfire_rewind(archive->f);
+ if (r < 0) {
+ ERROR(archive->ctx, "Could not rewind %s: %m\n", archive->path);
+ return -errno;
+ }
// Calculate digest
r = pakfire_digests_compute_from_file(archive->ctx, &archive->digests,
}
int pakfire_archive_copy(struct pakfire_archive* archive, const char* path) {
+ FILE* f = NULL;
+ int r;
+
if (!path)
return -EINVAL;
DEBUG(archive->ctx, "Copying %s to %s...\n", archive->path, path);
// Ensure we copy from the very beginning
- rewind(archive->f);
+ r = pakfire_rewind(archive->f);
+ if (r < 0) {
+ ERROR(archive->ctx, "Could not rewind %s: %m\n", archive->path);
+ r = -errno;
+ goto ERROR;
+ }
// Ensure the parent directory exists
pakfire_mkparentdir(path, 0755);
// Open destination file
- FILE* f = fopen(path, "w");
- if (!f)
- return -errno;
-
- int r = 1;
+ f = fopen(path, "w");
+ if (!f) {
+ r = -errno;
+ goto ERROR;
+ }
// Copy everything
ssize_t bytes_written = sendfile(fileno(f), fileno(archive->f), NULL, size);
if (bytes_written < size) {
- ERROR(archive->ctx, "Could not copy archive (%zd byte(s) written): %m\n",
- bytes_written);
+ ERROR(archive->ctx,
+ "Could not copy archive (%zd byte(s) written): %m\n", bytes_written);
+ r = -errno;
goto ERROR;
}
r = 0;
ERROR:
- fclose(f);
+ if (f)
+ fclose(f);
// Delete the file on error
if (r)
static int pakfire_copy_data_from_file(struct pakfire_ctx* ctx,
struct archive* archive, FILE* f) {
char buffer[BUFFER_SIZE];
+ int r;
ssize_t bytes_read = 0;
ssize_t bytes_written = 0;
// Read file from the very beginning - also allows calling this multiple times
- rewind(f);
+ r = pakfire_rewind(f);
+ if (r < 0) {
+ ERROR(ctx, "Could not rewind file for copying: %m\n");
+ return -errno;
+ }
// Loop through the entire length of the file
while (!feof(f)) {
// File stuff
+static inline int pakfire_rewind(FILE* f) {
+ return fseek(f, 0, SEEK_SET);
+}
+
int pakfire_file_write(struct pakfire* pakfire, const char* path,
uid_t owner, gid_t group, mode_t mode, const char* format, ...)
__attribute__((format(printf, 6, 7)));