rpm  5.4.4
ruby/rpmts-rb.c
Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include "rpm-rb.h"
00008 #include "rpmts-rb.h"
00009 #include "rpmmi-rb.h"
00010 #include "spec-rb.h"
00011 
00012 #include <argv.h>
00013 #include <mire.h>
00014 
00015 #include <rpmdb.h>
00016 
00017 #define _RPMTS_INTERNAL
00018 #include <rpmts.h>
00019 #include <rpmbuild.h>
00020 #include <rpmrc.h>
00021 
00022 #include "../debug.h"
00023 
00024 
00025 VALUE rpmtsClass;
00026 
00027 
00028 /*@unchecked@*/
00029 static int _debug = 0;
00030 
00031 
00032 /* --- helpers */
00033 
00035 static void *
00036 rpmts_ptr(VALUE s)
00037 {
00038     void *ptr;
00039     Data_Get_Struct(s, void, ptr);
00040     return ptr;
00041 }
00042 
00043 
00044 static VALUE
00045 rpmtsLoadNVRA(VALUE s)
00046 {
00047     void *ptr = rpmts_ptr(s);
00048     rpmts ts = ptr;
00049     VALUE NVRA = rb_ary_new();
00050     ARGV_t keys = NULL;
00051     int nkeys;
00052     int xx;
00053     int i;
00054 
00055     if (ts->rdb == NULL)
00056         (void) rpmtsOpenDB(ts, O_RDONLY);
00057 
00058     xx = rpmdbMireApply(rpmtsGetRdb(ts), RPMTAG_NVRA,
00059                 RPMMIRE_STRCMP, NULL, &keys);
00060     nkeys = argvCount(keys);
00061 
00062     if (keys)
00063         for (i = 0; i < nkeys; i++)
00064             rb_ary_push(NVRA, rb_str_new2(keys[i]));
00065 
00066     if (_debug)
00067         fprintf(stderr, "==> %s(0x%lx) ptr %p NVRA 0x%lx\n", 
00068                 __FUNCTION__, s, ptr, NVRA);
00069 
00070     keys = argvFree(keys);
00071     return NVRA;
00072 }
00073 
00074 
00075 /* --- Object methods */
00076 static VALUE
00077 rpmts_mi(int argc, VALUE *argv, VALUE s)
00078 {
00079     VALUE v_tag, v_key;
00080     rpmts ts = rpmts_ptr(s);
00081     rpmTag _tag = RPMDBI_PACKAGES;
00082     void * _key = NULL;
00083     int _len = 0;
00084 
00085     rb_scan_args(argc, argv, "02", &v_tag, &v_key);
00086 
00087     if (!NIL_P(v_tag))
00088         _tag = FIX2INT(v_tag);
00089     if (!NIL_P(v_key))
00090         _key = StringValueCStr(v_key);
00091 
00092     return rpmrb_NewMi(ts, _tag, _key, _len);
00093 }
00094 
00095 
00122 static VALUE
00123 rpmts_parse_spec(int argc, VALUE *argv, VALUE obj)
00124 {
00125     VALUE specfile_v, rootURL_v, recursing_v, passphrase_v, cookie_v,
00126         anyarch_v, force_v, verify_v;
00127     rb_scan_args(argc, argv, "8", &specfile_v, &rootURL_v, &recursing_v,
00128         &passphrase_v, &cookie_v, &anyarch_v, &force_v, &verify_v);
00129 
00130     /* Check and pre-set arguments */
00131 
00132     Check_Type(specfile_v, T_STRING);
00133     char *specfile = RSTRING_PTR(specfile_v);
00134 
00135     Check_Type(rootURL_v, T_STRING);
00136     char *rootURL = RSTRING_PTR(rootURL_v);
00137 
00138     char *cookie = NULL;
00139     switch(TYPE(cookie_v)) {
00140         case T_STRING:
00141             cookie = RSTRING_PTR(cookie_v);
00142             break;
00143         case T_NIL:
00144             cookie = NULL;
00145             break;
00146         default:
00147             rpm_rb_raise(1, "cookie must be either NIL or a string");
00148             break;
00149     }
00150 
00151     Check_Type(passphrase_v, T_STRING);
00152     char *passphrase = RSTRING_PTR(passphrase_v);
00153 
00154     int recursing = 0;
00155     switch(TYPE(recursing_v)) {
00156         case T_TRUE:
00157             recursing = 1;
00158             break;
00159         case T_FALSE:
00160             recursing = 0;
00161             break;
00162         default:
00163             rpm_rb_raise(1,
00164                 "Parameter 'recursing' must be either true or false");
00165             break;
00166     }
00167 
00168     int anyarch = 1;
00169     switch(TYPE(anyarch_v)) {
00170         case T_TRUE:
00171             anyarch = 1;
00172             break;
00173         case T_FALSE:
00174             anyarch = 0;
00175             break;
00176         default:
00177             rpm_rb_raise(1,
00178                 "Parameter 'anyarch' must be either true or false");
00179             break;
00180     }
00181 
00182     int verify = 1;
00183     switch(TYPE(verify_v)) {
00184         case T_TRUE:
00185             verify = 1;
00186             break;
00187         case T_FALSE:
00188             verify = 0;
00189             break;
00190         default:
00191             rpm_rb_raise(1,
00192                 "Parameter 'verify' must be either true or false");
00193             break;
00194     }
00195 
00196     int force = 0;
00197     switch(TYPE(force_v)) {
00198         case T_TRUE:
00199             force = 1;
00200             break;
00201         case T_FALSE:
00202             force = 0;
00203             break;
00204         default:
00205             rpm_rb_raise(1,
00206                 "Parameter 'force' must be either true or false");
00207             break;
00208     }
00209 
00210 
00211     rpmts ts = rpmts_ptr(obj);
00212     int error = parseSpec(ts, specfile, rootURL,
00213             recursing, passphrase, cookie, anyarch, force, verify);
00214     if(error) {
00215         rpm_rb_raise(error, "Could not parse spec file"); 
00216         return Qnil;
00217     }
00218 
00219     /* Wrap spec struct and set a reference to this ts class */
00220 
00221     VALUE spec_v = spec_wrap(rpmtsSpec(ts));
00222     rb_iv_set(spec_v, "ts", obj);
00223 
00224     return spec_v;
00225 }
00226 
00227 
00228 static void
00229 initMethods(VALUE klass)
00230 {
00231     rb_define_method(klass, "mi", &rpmts_mi, -1);
00232     rb_define_method(klass, "parse_spec", &rpmts_parse_spec, -1);
00233 }
00234 
00235 
00236 /* --- Object properties */
00237 static VALUE
00238 rpmts_debug_get(VALUE s)
00239 {
00240 if (_debug)
00241 fprintf(stderr, "==> %s(0x%lx)\n", __FUNCTION__, s);
00242     return INT2FIX(_debug);
00243 }
00244 
00245 static VALUE
00246 rpmts_debug_set(VALUE s, VALUE v)
00247 {
00248 if (_debug)
00249 fprintf(stderr, "==> %s(0x%lx, 0x%lx)\n", __FUNCTION__, s, v);
00250     return INT2FIX(_debug = FIX2INT(v));
00251 }
00252 
00253 static VALUE
00254 rpmts_rootdir_get(VALUE s)
00255 {
00256     void *ptr = rpmts_ptr(s);
00257     rpmts ts = ptr;
00258 if (_debug)
00259 fprintf(stderr, "==> %s(0x%lx) ptr %p\n", __FUNCTION__, s, ptr);
00260     return rb_str_new2(rpmtsRootDir(ts));
00261 }
00262 
00263 static VALUE
00264 rpmts_rootdir_set(VALUE s, VALUE v)
00265 {
00266     void *ptr = rpmts_ptr(s);
00267     rpmts ts = ptr;
00268 if (_debug)
00269 fprintf(stderr, "==> %s(0x%lx, 0x%lx) ptr %p\n", __FUNCTION__, s, v, ptr);
00270     rpmtsSetRootDir(ts, StringValueCStr(v));
00271     return rb_str_new2(rpmtsRootDir(ts));
00272 }
00273 
00274 static VALUE
00275 rpmts_vsflags_get(VALUE s)
00276 {
00277     void *ptr = rpmts_ptr(s);
00278     rpmts ts = ptr;
00279 if (_debug)
00280 fprintf(stderr, "==> %s(0x%lx) ptr %p\n", __FUNCTION__, s, ptr);
00281     return INT2FIX(rpmtsVSFlags(ts));
00282 }
00283 
00284 static VALUE
00285 rpmts_vsflags_set(VALUE s, VALUE v)
00286 {
00287     void *ptr = rpmts_ptr(s);
00288     rpmts ts = ptr;
00289 if (_debug)
00290 fprintf(stderr, "==> %s(0x%lx, 0x%lx) ptr %p\n", __FUNCTION__, s, v, ptr);
00291     rpmtsSetVSFlags(ts, FIX2INT(v));
00292     return INT2FIX(rpmtsVSFlags(ts));
00293 }
00294 
00295 static VALUE
00296 rpmts_NVRA_get(VALUE s)
00297 {
00298     return rpmtsLoadNVRA(s);
00299 }
00300 
00301 static void
00302 initProperties(VALUE klass)
00303 {
00304     rb_define_method(klass, "debug", rpmts_debug_get, 0);
00305     rb_define_method(klass, "debug=", rpmts_debug_set, 1);
00306     rb_define_method(klass, "rootdir", rpmts_rootdir_get, 0);
00307     rb_define_method(klass, "rootdir=", rpmts_rootdir_set, 1);
00308     rb_define_method(klass, "vsflags", rpmts_vsflags_get, 0);
00309     rb_define_method(klass, "vsflags=", rpmts_vsflags_set, 1);
00310     rb_define_method(klass, "NVRA", rpmts_NVRA_get, 0);
00311 }
00312 
00313 
00314 /* --- Object ctors/dtors */
00315 static void
00316 rpmts_free(rpmts ts)
00317 {
00318 if (_debug)
00319 fprintf(stderr, "==> %s(%p)\n", __FUNCTION__, ts);
00320     ts = rpmtsFree(ts);
00321 }
00322 
00323 static VALUE
00324 rpmts_new(int argc, VALUE *argv, VALUE s)
00325 {
00326     VALUE v_rootdir;
00327     char * _rootdir = "/";
00328     rpmts ts;
00329 
00330     rb_scan_args(argc, argv, "01", &v_rootdir);
00331 
00332     if (!NIL_P(v_rootdir))
00333         _rootdir = StringValueCStr(v_rootdir);
00334 
00335     ts = rpmtsCreate();
00336     rpmtsSetRootDir(ts, _rootdir);
00337 
00338 if (_debug)
00339 fprintf(stderr, "==> %s(%p[%d], 0x%lx) ts %p\n", __FUNCTION__, argv, argc, s, ts
00340 );
00341     return Data_Wrap_Struct(s, 0, rpmts_free, ts);
00342 }
00343 
00344 
00345 /* --- Class initialization */
00346 
00347 
00348 void
00349 Init_rpmts(void)
00350 {
00351     rpmtsClass = rb_define_class_under(rpmModule, "Ts", rb_cObject);
00352 if (_debug)
00353 fprintf(stderr, "==> %s() rpmtsClass 0x%lx\n", __FUNCTION__, rpmtsClass);
00354 #ifdef  NOTYET
00355     rb_include_module(rpmtsClass, rb_mEnumerable);
00356 #endif
00357     rb_define_singleton_method(rpmtsClass, "new", &rpmts_new, -1);
00358     initProperties(rpmtsClass);
00359     initMethods(rpmtsClass);
00360 }