rpm  5.4.10
rpmts-rb.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include "rpm-rb.h"
8 #include "rpmts-rb.h"
9 #include "rpmmi-rb.h"
10 #include "spec-rb.h"
11 
12 #include <argv.h>
13 #include <mire.h>
14 
15 #include <rpmdb.h>
16 
17 #define _RPMTS_INTERNAL
18 #include <rpmts.h>
19 #include <rpmbuild.h>
20 #include <rpmrc.h>
21 
22 #include "../debug.h"
23 
24 
25 VALUE rpmtsClass;
26 
27 
28 /*@unchecked@*/
29 static int _debug = 0;
30 
31 
32 /* --- helpers */
33 
35 static void *
36 rpmts_ptr(VALUE s)
37 {
38  void *ptr;
39  Data_Get_Struct(s, void, ptr);
40  return ptr;
41 }
42 
43 
44 static VALUE
45 rpmtsLoadNVRA(VALUE s)
46 {
47  void *ptr = rpmts_ptr(s);
48  rpmts ts = ptr;
49  VALUE NVRA = rb_ary_new();
50  ARGV_t keys = NULL;
51  int nkeys;
52  int xx;
53  int i;
54 
55  if (ts->rdb == NULL)
56  (void) rpmtsOpenDB(ts, O_RDONLY);
57 
59  RPMMIRE_STRCMP, NULL, &keys);
60  nkeys = argvCount(keys);
61 
62  if (keys)
63  for (i = 0; i < nkeys; i++)
64  rb_ary_push(NVRA, rb_str_new2(keys[i]));
65 
66  if (_debug)
67  fprintf(stderr, "==> %s(0x%lx) ptr %p NVRA 0x%lx\n",
68  __FUNCTION__, s, ptr, NVRA);
69 
70  keys = argvFree(keys);
71  return NVRA;
72 }
73 
74 
75 /* --- Object methods */
76 static VALUE
77 rpmts_mi(int argc, VALUE *argv, VALUE s)
78 {
79  VALUE v_tag, v_key;
80  rpmts ts = rpmts_ptr(s);
81  rpmTag _tag = RPMDBI_PACKAGES;
82  void * _key = NULL;
83  int _len = 0;
84 
85  rb_scan_args(argc, argv, "02", &v_tag, &v_key);
86 
87  if (!NIL_P(v_tag))
88  _tag = FIX2INT(v_tag);
89  if (!NIL_P(v_key))
90  _key = StringValueCStr(v_key);
91 
92  return rpmrb_NewMi(ts, _tag, _key, _len);
93 }
94 
95 
122 static VALUE
123 rpmts_parse_spec(int argc, VALUE *argv, VALUE obj)
124 {
125  VALUE specfile_v, rootURL_v, recursing_v, passphrase_v, cookie_v,
126  anyarch_v, force_v, verify_v;
127  rb_scan_args(argc, argv, "8", &specfile_v, &rootURL_v, &recursing_v,
128  &passphrase_v, &cookie_v, &anyarch_v, &force_v, &verify_v);
129 
130  /* Check and pre-set arguments */
131 
132  Check_Type(specfile_v, T_STRING);
133  char *specfile = RSTRING_PTR(specfile_v);
134 
135  Check_Type(rootURL_v, T_STRING);
136  char *rootURL = RSTRING_PTR(rootURL_v);
137 
138  char *cookie = NULL;
139  switch(TYPE(cookie_v)) {
140  case T_STRING:
141  cookie = RSTRING_PTR(cookie_v);
142  break;
143  case T_NIL:
144  cookie = NULL;
145  break;
146  default:
147  rpm_rb_raise(1, "cookie must be either NIL or a string");
148  break;
149  }
150 
151  Check_Type(passphrase_v, T_STRING);
152  char *passphrase = RSTRING_PTR(passphrase_v);
153 
154  int recursing = 0;
155  switch(TYPE(recursing_v)) {
156  case T_TRUE:
157  recursing = 1;
158  break;
159  case T_FALSE:
160  recursing = 0;
161  break;
162  default:
163  rpm_rb_raise(1,
164  "Parameter 'recursing' must be either true or false");
165  break;
166  }
167 
168  int anyarch = 1;
169  switch(TYPE(anyarch_v)) {
170  case T_TRUE:
171  anyarch = 1;
172  break;
173  case T_FALSE:
174  anyarch = 0;
175  break;
176  default:
177  rpm_rb_raise(1,
178  "Parameter 'anyarch' must be either true or false");
179  break;
180  }
181 
182  int verify = 1;
183  switch(TYPE(verify_v)) {
184  case T_TRUE:
185  verify = 1;
186  break;
187  case T_FALSE:
188  verify = 0;
189  break;
190  default:
191  rpm_rb_raise(1,
192  "Parameter 'verify' must be either true or false");
193  break;
194  }
195 
196  int force = 0;
197  switch(TYPE(force_v)) {
198  case T_TRUE:
199  force = 1;
200  break;
201  case T_FALSE:
202  force = 0;
203  break;
204  default:
205  rpm_rb_raise(1,
206  "Parameter 'force' must be either true or false");
207  break;
208  }
209 
210 
211  rpmts ts = rpmts_ptr(obj);
212  int error = parseSpec(ts, specfile, rootURL,
213  recursing, passphrase, cookie, anyarch, force, verify);
214  if(error) {
215  rpm_rb_raise(error, "Could not parse spec file");
216  return Qnil;
217  }
218 
219  /* Wrap spec struct and set a reference to this ts class */
220 
221  VALUE spec_v = spec_wrap(rpmtsSpec(ts));
222  rb_iv_set(spec_v, "ts", obj);
223 
224  return spec_v;
225 }
226 
227 
228 static void
229 initMethods(VALUE klass)
230 {
231  rb_define_method(klass, "mi", &rpmts_mi, -1);
232  rb_define_method(klass, "parse_spec", &rpmts_parse_spec, -1);
233 }
234 
235 
236 /* --- Object properties */
237 static VALUE
239 {
240 if (_debug)
241 fprintf(stderr, "==> %s(0x%lx)\n", __FUNCTION__, s);
242  return INT2FIX(_debug);
243 }
244 
245 static VALUE
246 rpmts_debug_set(VALUE s, VALUE v)
247 {
248 if (_debug)
249 fprintf(stderr, "==> %s(0x%lx, 0x%lx)\n", __FUNCTION__, s, v);
250  return INT2FIX(_debug = FIX2INT(v));
251 }
252 
253 static VALUE
255 {
256  void *ptr = rpmts_ptr(s);
257  rpmts ts = ptr;
258 if (_debug)
259 fprintf(stderr, "==> %s(0x%lx) ptr %p\n", __FUNCTION__, s, ptr);
260  return rb_str_new2(rpmtsRootDir(ts));
261 }
262 
263 static VALUE
264 rpmts_rootdir_set(VALUE s, VALUE v)
265 {
266  void *ptr = rpmts_ptr(s);
267  rpmts ts = ptr;
268 if (_debug)
269 fprintf(stderr, "==> %s(0x%lx, 0x%lx) ptr %p\n", __FUNCTION__, s, v, ptr);
270  rpmtsSetRootDir(ts, StringValueCStr(v));
271  return rb_str_new2(rpmtsRootDir(ts));
272 }
273 
274 static VALUE
276 {
277  void *ptr = rpmts_ptr(s);
278  rpmts ts = ptr;
279 if (_debug)
280 fprintf(stderr, "==> %s(0x%lx) ptr %p\n", __FUNCTION__, s, ptr);
281  return INT2FIX(rpmtsVSFlags(ts));
282 }
283 
284 static VALUE
285 rpmts_vsflags_set(VALUE s, VALUE v)
286 {
287  void *ptr = rpmts_ptr(s);
288  rpmts ts = ptr;
289 if (_debug)
290 fprintf(stderr, "==> %s(0x%lx, 0x%lx) ptr %p\n", __FUNCTION__, s, v, ptr);
291  rpmtsSetVSFlags(ts, FIX2INT(v));
292  return INT2FIX(rpmtsVSFlags(ts));
293 }
294 
295 static VALUE
297 {
298  return rpmtsLoadNVRA(s);
299 }
300 
301 static void
302 initProperties(VALUE klass)
303 {
304  rb_define_method(klass, "debug", rpmts_debug_get, 0);
305  rb_define_method(klass, "debug=", rpmts_debug_set, 1);
306  rb_define_method(klass, "rootdir", rpmts_rootdir_get, 0);
307  rb_define_method(klass, "rootdir=", rpmts_rootdir_set, 1);
308  rb_define_method(klass, "vsflags", rpmts_vsflags_get, 0);
309  rb_define_method(klass, "vsflags=", rpmts_vsflags_set, 1);
310  rb_define_method(klass, "NVRA", rpmts_NVRA_get, 0);
311 }
312 
313 
314 /* --- Object ctors/dtors */
315 static void
317 {
318 if (_debug)
319 fprintf(stderr, "==> %s(%p)\n", __FUNCTION__, ts);
320  ts = rpmtsFree(ts);
321 }
322 
323 static VALUE
324 rpmts_new(int argc, VALUE *argv, VALUE s)
325 {
326  VALUE v_rootdir;
327  char * _rootdir = "/";
328  rpmts ts;
329 
330  rb_scan_args(argc, argv, "01", &v_rootdir);
331 
332  if (!NIL_P(v_rootdir))
333  _rootdir = StringValueCStr(v_rootdir);
334 
335  ts = rpmtsCreate();
336  rpmtsSetRootDir(ts, _rootdir);
337 
338 if (_debug)
339 fprintf(stderr, "==> %s(%p[%d], 0x%lx) ts %p\n", __FUNCTION__, argv, argc, s, ts
340 );
341  return Data_Wrap_Struct(s, 0, rpmts_free, ts);
342 }
343 
344 
345 /* --- Class initialization */
346 
347 
348 void
350 {
351  rpmtsClass = rb_define_class_under(rpmModule, "Ts", rb_cObject);
352 if (_debug)
353 fprintf(stderr, "==> %s() rpmtsClass 0x%lx\n", __FUNCTION__, rpmtsClass);
354 #ifdef NOTYET
355  rb_include_module(rpmtsClass, rb_mEnumerable);
356 #endif
357  rb_define_singleton_method(rpmtsClass, "new", &rpmts_new, -1);
360 }