Add package_from_file function into the parsepkg module
authorTomas Mlcoch <tmlcoch@redhat.com>
Wed, 20 Jun 2012 13:55:27 +0000 (15:55 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Mon, 25 Jun 2012 10:41:36 +0000 (12:41 +0200)
src/parsepkg.c
src/parsepkg.h

index e3db24c..9984955 100644 (file)
@@ -72,6 +72,110 @@ void free_package_parser()
 }
 
 
+Package *package_from_file(const char *filename, ChecksumType checksum_type,
+                           const char *location_href, const char *location_base,
+                           int changelog_limit, struct stat *stat_buf)
+{
+    Package *result = NULL;
+    const char *checksum_type_str;
+
+    // Set checksum type
+
+    switch (checksum_type) {
+        case PKG_CHECKSUM_MD5:
+            checksum_type_str = "md5";
+            break;
+        case PKG_CHECKSUM_SHA1:
+             checksum_type_str = "sha1";
+            break;
+        case PKG_CHECKSUM_SHA256:
+            checksum_type_str = "sha256";
+            break;
+        default:
+            g_critical(MODULE"%s: Unknown checksum type", __func__);
+            return result;
+            break;
+    };
+
+
+    // Open rpm file
+
+    FD_t fd = NULL;
+    fd = Fopen(filename, "r.ufdio");
+    if (!fd) {
+        g_critical(MODULE"%s: Fopen failed %s", __func__, strerror(errno));
+        return result;
+    }
+
+
+    // Read package
+
+    Header hdr;
+    int rc = rpmReadPackageFile(ts, fd, NULL, &hdr);
+    if (rc != RPMRC_OK) {
+        switch (rc) {
+            case RPMRC_NOKEY:
+                g_debug(MODULE"%s: %s: Public key is unavailable.", __func__, filename);
+                break;
+            case RPMRC_NOTTRUSTED:
+                g_debug(MODULE"%s:  %s: Signature is OK, but key is not trusted.", __func__, filename);
+                break;
+            default:
+                g_critical(MODULE"%s: rpmReadPackageFile() error (%s)", __func__, strerror(errno));
+                return result;
+        }
+    }
+
+
+    // Cleanup
+
+    Fclose(fd);
+
+
+    // Get file stat
+
+    gint64 mtime;
+    gint64 size;
+
+    if (!stat_buf) {
+        struct stat stat_buf_own;
+        if (stat(filename, &stat_buf_own) == -1) {
+            g_critical(MODULE"%s: stat() error (%s)", __func__, strerror(errno));
+            return result;
+        }
+        mtime  = stat_buf_own.st_mtime;
+        size   = stat_buf_own.st_size;
+    } else {
+        mtime  = stat_buf->st_mtime;
+        size   = stat_buf->st_size;
+    }
+
+
+    // Compute checksum
+
+    char *checksum = compute_file_checksum(filename, checksum_type);
+
+
+    // Get header range
+
+    struct HeaderRangeStruct hdr_r = get_header_byte_range(filename);
+
+
+    // Get package object
+
+    result = parse_header(hdr, mtime, size, checksum, checksum_type_str, location_href,
+                          location_base, changelog_limit, hdr_r.start, hdr_r.end);
+
+
+    // Cleanup
+
+    free(checksum);
+    headerFree(hdr);
+
+    return result;
+}
+
+
 
 struct XmlStruct xml_from_package_file(const char *filename, ChecksumType checksum_type,
                 const char *location_href, const char *location_base, int changelog_limit,
index fda7e0b..96b027e 100644 (file)
@@ -46,6 +46,19 @@ void init_package_parser();
 void free_package_parser();
 
 /** \ingroup parsepkg
+ * Generate package object from package file.
+ * @param filename              filename
+ * @param checksum_type         type of checksum to be used
+ * @param location_href         package location inside repository
+ * @param location_base         location (url) of repository
+ * @param changelog_limit       number of changelog entries
+ * @param stat_buf              struct stat of the filename (optional - could be NULL)
+ */
+Package *package_from_file(const char *filename, ChecksumType checksum_type,
+                        const char *location_href, const char *location_base,
+                        int changelog_limit, struct stat *stat_buf);
+
+/** \ingroup parsepkg
  * Generate XML for the specified package.
  * @param filename              filename
  * @param checksum_type         type of checksum to be used