plover/repository.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Jul 05 12:48:24 2016 +0100 (2016-07-05)
changeset 55 8c80282a3888
parent 50 a4f43ad0e0c8
child 99 0121592e2512
permissions -rw-r--r--
Port to razor 0.6.3.94
     1 /*
     2  * Copyright (C) 2014  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include "config.h"
    20 #include <stdlib.h>
    21 #include <string.h>
    22 #include <glib-object.h>
    23 #include <razor.h>
    24 #include <plover/plover.h>
    25 #include <plover/repository.h>
    26 #include "plover/uri-handler.h"
    27 
    28 G_DEFINE_TYPE(PloverRepository,plover_repository,G_TYPE_OBJECT);
    29 
    30 typedef struct _PloverRepositoryPrivate {
    31     PloverPackageSet *set;
    32     gchar **uris;
    33 } PloverRepositoryPrivate;
    34 
    35 #define PLOVER_REPOSITORY_GET_PRIVATE(obj)\
    36 				G_TYPE_INSTANCE_GET_PRIVATE(obj,\
    37 				  PLOVER_TYPE_REPOSITORY,\
    38 				  PloverRepositoryPrivate)
    39 
    40 static void plover_repository_finalize(GObject *obj)
    41 {
    42     PloverRepositoryPrivate *priv=PLOVER_REPOSITORY_GET_PRIVATE(obj);
    43     g_strfreev(priv->uris);
    44     if (G_OBJECT_CLASS(plover_repository_parent_class)->finalize)
    45 	G_OBJECT_CLASS(plover_repository_parent_class)->finalize(obj);
    46 }
    47 
    48 static void plover_repository_dispose(GObject *obj)
    49 {
    50     PloverRepositoryPrivate *priv=PLOVER_REPOSITORY_GET_PRIVATE(obj);
    51     g_clear_object(&priv->set);
    52     if (G_OBJECT_CLASS(plover_repository_parent_class)->dispose)
    53 	G_OBJECT_CLASS(plover_repository_parent_class)->dispose(obj);
    54 }
    55 
    56 static void plover_repository_class_init(PloverRepositoryClass *klass)
    57 {
    58     GObjectClass *oclass=G_OBJECT_CLASS(klass);
    59     plover__uri_handler_init();
    60     oclass->finalize=plover_repository_finalize;
    61     oclass->dispose=plover_repository_dispose;
    62     g_type_class_add_private(klass,sizeof(PloverRepositoryPrivate));
    63 }
    64 
    65 static void plover_repository_init(PloverRepository *repository)
    66 {
    67 }
    68 
    69 PloverRepository *plover_repository_new_from_package_set(PloverPackageSet *set,
    70   const char **rpm_uris)
    71 {
    72     PloverRepository *repository;
    73     PloverRepositoryPrivate *priv;
    74     g_return_val_if_fail(PLOVER_IS_PACKAGE_SET(set),NULL);
    75     g_return_val_if_fail(rpm_uris != NULL,NULL);
    76     repository=g_object_new(PLOVER_TYPE_REPOSITORY,NULL);
    77     priv=PLOVER_REPOSITORY_GET_PRIVATE(repository);
    78     priv->uris=g_strdupv((char **)rpm_uris);
    79     priv->set=g_object_ref(set);
    80     return repository;
    81 }
    82 
    83 PloverRepository *plover_repository_new_from_files(const char **filenames,
    84   GError **error)
    85 {
    86     int i;
    87     PloverPackageSet *set;
    88     PloverRepository *repository;
    89     PloverRepositoryPrivate *priv;
    90     GFile *file;
    91     gchar *uri;
    92     set=plover_package_set_new_from_rpms(filenames,error);
    93     if (!set)
    94 	return NULL;
    95     repository=g_object_new(PLOVER_TYPE_REPOSITORY,NULL);
    96     priv=PLOVER_REPOSITORY_GET_PRIVATE(repository);
    97     priv->uris=g_new(char *,g_strv_length((char **)filenames)+1);
    98     for(i=0;filenames[i];i++)
    99     {
   100 	file=g_file_new_for_path(filenames[i]);
   101 	priv->uris[i]=g_file_get_uri(file);
   102 	g_object_unref(file);
   103     }
   104     priv->uris[i]=NULL;
   105     priv->set=set;
   106     return repository;
   107 }
   108 
   109 static char *rpm_filename(const char *name,const char *version,const char *arch)
   110 {
   111     const char *v;
   112     v=strchr(version,':');            /* Skip epoch */
   113     if (v)
   114 	v++;
   115     else
   116 	v=version;
   117     return g_strconcat(name,"-",v,".",arch,".rpm",NULL);
   118 }
   119 
   120 PloverRepository *plover_repository_new_from_yum(const char *base,
   121   GError **error)
   122 {
   123     PloverRepository *repository;
   124     gchar *base_uri;
   125     GFile *file;
   126     file=g_file_new_for_path(base);
   127     base_uri=g_file_get_uri(file);
   128     g_object_unref(file);
   129     repository=plover_repository_new_from_yum_uri(base_uri,error);
   130     g_free(base_uri);
   131     return repository;
   132 }
   133 
   134 PloverPackageSet *
   135   plover_repository_get_package_set(PloverRepository *repository)
   136 {
   137     PloverRepositoryPrivate *priv;
   138     g_return_val_if_fail(PLOVER_IS_REPOSITORY(repository),NULL);
   139     priv=PLOVER_REPOSITORY_GET_PRIVATE(repository);
   140     return priv->set;
   141 }
   142 
   143 struct razor_rpm *plover_repository_open_rpm(PloverRepository *repository,
   144   PloverPackage *package,GError **error)
   145 {
   146     int nth;
   147     struct razor_rpm *rpm;
   148     struct razor_error *tmp_error=NULL;
   149     PloverRepositoryPrivate *priv;
   150     PloverPackage *internal;
   151     g_return_val_if_fail(PLOVER_IS_REPOSITORY(repository),NULL);
   152     g_return_val_if_fail(PLOVER_IS_PACKAGE(package),NULL);
   153     priv=PLOVER_REPOSITORY_GET_PRIVATE(repository);
   154     nth=g_slist_index(plover_package_set_get_packages(priv->set),package);
   155     if (nth<0)
   156     {
   157 	internal=plover_package_set_find_matching(priv->set,package);
   158 	nth=g_slist_index(plover_package_set_get_packages(priv->set),internal);
   159     }
   160     if (nth<0)
   161     {
   162 	g_set_error(error,PLOVER_GENERAL_ERROR,
   163 	  PLOVER_GENERAL_ERROR_NO_SUCH_PACKAGE,
   164 	  "%s-%s.%s: Package not in repository",
   165 	  plover_package_get_name(package),plover_package_get_version(package),
   166 	  plover_package_get_arch(package));
   167 	return NULL;
   168     }
   169     rpm=razor_rpm_open(priv->uris[nth],&tmp_error);
   170     if (!rpm)
   171 	plover_propagate_razor_error(error,tmp_error);
   172     return rpm;
   173 }