From: Michael Tremer Date: Tue, 23 Aug 2022 16:18:25 +0000 (+0000) Subject: packager: Add new function that accepts a file object X-Git-Tag: 0.9.28~393 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85b5929645dd34ed1ea2868ed295ea14b391919d;p=pakfire.git packager: Add new function that accepts a file object Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index f2db85de6..0e4fa45ea 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -640,73 +640,81 @@ ERROR: return r; } -int pakfire_packager_add(struct pakfire_packager* packager, - const char* sourcepath, const char* path) { +static int pakfire_packager_add_file(struct pakfire_packager* packager, + struct pakfire_file* file) { + struct archive_entry* entry = NULL; FILE* f = NULL; + int r; - // Check if path is set - if (!sourcepath) { + // Check input + if (!file) { errno = EINVAL; return 1; } - // Use basename if path isn't set - if (!path) { - path = strrchr(sourcepath, '/'); - if (path) - path++; - } - // Payload has already been closed - if (!packager->payload) - return EINVAL; - - // Hidden files cannot be added - if (*path == '.') { + if (!packager->payload) { + ERROR(packager->pakfire, "Payload has already been closed\n"); errno = EPERM; return 1; } - // Create a new file entry - struct archive_entry* entry = archive_entry_new(); - if (!entry) - return 1; + // Fetch path + const char* path = pakfire_file_get_path(file); - DEBUG(packager->pakfire, "Adding '%s' to archive (from %s)\n", path, sourcepath); + // Fetch filetype + const mode_t filetype = pakfire_file_get_type(file); - // Set the source path - archive_entry_copy_sourcepath(entry, sourcepath); - - // Set path in archive - if (path) - archive_entry_set_pathname(entry, path); + // Files cannot have an empty path + if (!*path) { + ERROR(packager->pakfire, "Cannot add a file with an empty path\n"); + errno = EPERM; + return 1; - // Read all attributes from file - int r = archive_read_disk_entry_from_file(packager->reader, entry, -1, NULL); - if (r) { - ERROR(packager->pakfire, "Could not read attributes from %s: %m\n", path); - goto ERROR; + // Hidden files cannot be added + } else if (*path == '.') { + ERROR(packager->pakfire, "Hidden files cannot be added to a package: %s\n", path); + errno = EPERM; + return 1; } + DEBUG(packager->pakfire, "Adding file to payload: %s\n", path); + // Overwrite a couple of things for source archives if (pakfire_package_is_source(packager->pkg)) { +#if 0 // Reset permissions - archive_entry_set_perm(entry, 0644); + pakfire_file_set_perms(file, 0644); +#endif // Reset file ownership - archive_entry_set_uname(entry, "root"); - archive_entry_set_gname(entry, "root"); - archive_entry_set_uid(entry, 0); - archive_entry_set_gid(entry, 0); + pakfire_file_set_user(file, "root"); + pakfire_file_set_group(file, "root"); } - // Open the source file - f = fopen(sourcepath, "r"); - if (!f) { - ERROR(packager->pakfire, "Could not open %s: %m\n", sourcepath); - r = errno; + // Open the file + f = pakfire_file_open(file); + if (!f) goto ERROR; + +#if 0 + // Add digests for regular files + if (filetype == AE_IFREG) { + r = pakfire_packager_compute_digests(packager, entry, f); + if (r) { + ERROR(packager->pakfire, "Could not compute digests: %m\n") + goto ERROR; + } + + // Rewind the file descriptor + rewind(f); } +#endif + + // Generate file metadata into an archive entry + entry = pakfire_file_archive_entry(file); + if (!entry) + goto ERROR; // Write the header r = archive_write_header(packager->payload, entry); @@ -717,7 +725,7 @@ int pakfire_packager_add(struct pakfire_packager* packager, } // Copy the data of regular files - if (archive_entry_filetype(entry) == AE_IFREG) { + if (filetype == S_IFREG) { // Copy the payload into the archive r = pakfire_archive_copy_data_from_file(packager->pakfire, packager->payload, f); if (r) @@ -725,24 +733,72 @@ int pakfire_packager_add(struct pakfire_packager* packager, } // Increment installsize - packager->installsize += archive_entry_size(entry); + packager->installsize += pakfire_file_get_size(file); // Increment file counter packager->files++; - // Successful - r = 0; - ERROR: if (entry) archive_entry_free(entry); - if (f) fclose(f); return r; } +int pakfire_packager_add(struct pakfire_packager* packager, + const char* sourcepath, const char* path) { + struct pakfire_file* file = NULL; + int r = 1; + + // Check if path is set + if (!sourcepath) { + errno = EINVAL; + return 1; + } + + // Use basename if path isn't set + if (!path) { + path = strrchr(sourcepath, '/'); + if (path) + path++; + } + + // Create a new file entry + struct archive_entry* entry = archive_entry_new(); + if (!entry) + return 1; + + // Set the source path + archive_entry_copy_sourcepath(entry, sourcepath); + + // Set path in archive + if (path) + archive_entry_set_pathname(entry, path); + + // Read all attributes from file + r = archive_read_disk_entry_from_file(packager->reader, entry, -1, NULL); + if (r) { + ERROR(packager->pakfire, "Could not read attributes from %s: %m\n", path); + goto ERROR; + } + + // Convert to file + r = pakfire_file_create_from_archive_entry(&file, packager->pakfire, entry); + if (r) + goto ERROR; + + // Call the main function + r = pakfire_packager_add_file(packager, file); + +ERROR: + if (file) + pakfire_file_unref(file); + + return r; +} + int pakfire_packager_add_scriptlet(struct pakfire_packager* packager, struct pakfire_scriptlet* scriptlet) { if (!scriptlet) {