Class: RPM::DB

Inherits:
Data
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rpm.rb,
ext/rpm/db.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.each_match(tag, val) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rpm.rb', line 46

def DB.each_match(tag, val)
  begin
    db = RPM::DB.new
    db.each_match(tag, val) {|*a| yield a}
  end
  GC.start
end

.init(*args) ⇒ Object

Initialize the package database The database #root / var / lib /rpm is created.

Parameters:

  • root (String)

    Root of the database

  • writable (Boolean)

    Whether the database is writable. Default false.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/rpm/db.c', line 131

static VALUE
db_s_init(int argc, VALUE* argv, VALUE obj)
{
	int writable = 0;
	const char* root;

	switch (argc) {
	case 0:
		rb_raise(rb_eArgError, "too few argument(1..2)");

	case 1: case 2:
		if (TYPE(argv[0]) != T_STRING) {
			rb_raise(rb_eTypeError, "illegal argument type");
		}
		root = RSTRING_PTR(argv[0]);
		if (argc == 2) {
			writable = RTEST(argv[1]);
		}
		break;

	default:
		rb_raise(rb_eArgError, "too many argument(1..2)");
	}

	if (rpmdbInit(root, writable ? O_RDWR | O_CREAT : O_RDONLY)) {
		rb_raise(rb_eRuntimeError, "can not initialize database in %s",
				 RSTRING_PTR(rb_str_concat(rb_str_new2(root),
									   rb_str_new2("/var/lib/rpm"))));
	}

	return Qnil;
}

.new(*args) ⇒ RPM::DB

The package database is opened, but transactional processing (@see RPM::DB#transaction) cannot be done for when writable is false. When writable is false then the generated object gets freezed.

Examples:

db = RPM::DB.open
db.each do |pkg|
  puts pkg.name
end

Parameters:

  • writable (Boolean)

    Whether the database is writable. Default is false.

  • root (String)

    Root path for the database, default is empty.

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/rpm/db.c', line 67

static VALUE
db_s_open(int argc, VALUE* argv, VALUE obj)
{
	VALUE db;
	rpm_db_t* rdb;
	int writable = 0;
	const char* root = "";

	switch (argc) {
	case 0:
		break;

	case 1:
		writable = RTEST(argv[0]);
		break;

	case 2:
		if (!NIL_P(argv[1])) {
			if (TYPE(argv[1]) != T_STRING) {
				rb_raise(rb_eTypeError, "illegal argument type");
			}
			root = RSTRING_PTR(argv[1]);
		}
		writable = RTEST(argv[0]);
		break;

	default:
		rb_raise(rb_eArgError, "too many argument(0..2)");
	}


	rdb = ALLOC_N(rpm_db_t,1);
	if (rpmdbOpen(root, &(rdb->db), writable ? O_RDWR | O_CREAT : O_RDONLY, 0644)) {
		free(rdb);
		rb_raise(rb_eRuntimeError, "can not open database in %s",
				 RSTRING_PTR(rb_str_concat(rb_str_new2(root),
			         rb_str_new2("/var/lib/rpm"))));
	}

	rdb->ref_count = 0;
	db_ref(rdb);
	db = Data_Wrap_Struct(rpm_cDB, NULL, db_free, rdb);
	if (!writable) {
		rb_obj_freeze(db);
	}
	return db;
}

.open(*args) ⇒ RPM::DB

The package database is opened, but transactional processing (@see RPM::DB#transaction) cannot be done for when writable is false. When writable is false then the generated object gets freezed.

Examples:

db = RPM::DB.open
db.each do |pkg|
  puts pkg.name
end

Parameters:

  • writable (Boolean)

    Whether the database is writable. Default is false.

  • root (String)

    Root path for the database, default is empty.

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/rpm/db.c', line 67

static VALUE
db_s_open(int argc, VALUE* argv, VALUE obj)
{
	VALUE db;
	rpm_db_t* rdb;
	int writable = 0;
	const char* root = "";

	switch (argc) {
	case 0:
		break;

	case 1:
		writable = RTEST(argv[0]);
		break;

	case 2:
		if (!NIL_P(argv[1])) {
			if (TYPE(argv[1]) != T_STRING) {
				rb_raise(rb_eTypeError, "illegal argument type");
			}
			root = RSTRING_PTR(argv[1]);
		}
		writable = RTEST(argv[0]);
		break;

	default:
		rb_raise(rb_eArgError, "too many argument(0..2)");
	}


	rdb = ALLOC_N(rpm_db_t,1);
	if (rpmdbOpen(root, &(rdb->db), writable ? O_RDWR | O_CREAT : O_RDONLY, 0644)) {
		free(rdb);
		rb_raise(rb_eRuntimeError, "can not open database in %s",
				 RSTRING_PTR(rb_str_concat(rb_str_new2(root),
			         rb_str_new2("/var/lib/rpm"))));
	}

	rdb->ref_count = 0;
	db_ref(rdb);
	db = Data_Wrap_Struct(rpm_cDB, NULL, db_free, rdb);
	if (!writable) {
		rb_obj_freeze(db);
	}
	return db;
}

.packages(label = nil) ⇒ Object

def DB.packages



54
55
56
57
58
59
60
61
62
# File 'lib/rpm.rb', line 54

def DB.packages
  packages = nil
  begin
    db = RPM::DB.new
    packages = db.packages
  end
  GC.start
  packages
end

.rebuild(*args) ⇒ Object

Rebuild the package database It should reside in root / var / lib /rpm

Parameters:

  • root (String)

    Root path of the database



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'ext/rpm/db.c', line 181

static VALUE
db_s_rebuild(int argc, VALUE* argv, VALUE obj)
{
	const char* root = "";
	int ret;

	switch (argc) {
	case 0:
		break;

	case 1:
		if (!NIL_P(argv[0])) {
			if (TYPE(argv[0]) != T_STRING) {
				rb_raise(rb_eTypeError, "illegal argument type");
			}
			root = RSTRING_PTR(argv[0]);
		}
		break;

	default:
		rb_raise(rb_eArgError, "too many arguments(0..1)");
		break;
	}

#if RPM_VERSION_CODE < RPM_VERSION(4,1,0)
	ret = rpmdbRebuild(root);
#elif RPM_VERSION_CODE < RPM_VERSION(5,0,0)
	ret = rpmdbRebuild(root, NULL, NULL);
#else
	ret = rpmdbRebuild(root, NULL);
#endif
	if (ret) {
		rb_raise(rb_eRuntimeError, "can not rebuild database in %s",
				 RSTRING_PTR(rb_str_concat(rb_str_new2(root),
									   rb_str_new2("/var/lib/rpm"))));
	}

	return Qnil;
}

Instance Method Details

#closeObject

Closes the database



232
233
234
235
236
237
238
239
# File 'ext/rpm/db.c', line 232

VALUE
rpm_db_close(VALUE db)
{
	db_unref((rpm_db_t*)DATA_PTR(db));
	DATA_PTR(db) = NULL;
    rb_gc();
    return Qnil;
}

#closed?Boolean

Returns true if the database is closed.

Returns:

  • (Boolean)

    true if the database is closed



244
245
246
247
248
# File 'ext/rpm/db.c', line 244

VALUE
rpm_db_is_closed(VALUE vdb)
{
	return DATA_PTR(vdb) ? Qfalse : Qtrue;
}

#each {|Package| ... } ⇒ Object

Examples:

db.each do |pkg|
  puts pkg.name
end

Yields:

  • (Package)

    Called for each package in the database



320
321
322
323
324
325
# File 'ext/rpm/db.c', line 320

VALUE
rpm_db_each(VALUE db)
{
	check_closed(db);
	return rpm_db_each_match(db,INT2NUM(RPMDBI_PACKAGES),Qnil);
}

#each_match(key, val) {|Package| ... } ⇒ Object

Examples:

db.each_match(RPM::TAG_ARCH, "x86_64") do |pkg|
  puts pkg.name
end

Parameters:

  • key (Number)

    RPM tag key

  • val (String)

    Value to match

Yields:

  • (Package)

    Called for each match



299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'ext/rpm/db.c', line 299

VALUE
rpm_db_each_match(VALUE db, VALUE key, VALUE val)
{
	VALUE mi;

	check_closed(db);

	mi = rpm_db_init_iterator (db, key, val);

	if (!NIL_P(mi))
		return rpm_mi_each (mi);
        return Qnil;
}

#homeString

Returns The home path of the database.

Returns:

  • (String)

    The home path of the database



272
273
274
275
276
277
# File 'ext/rpm/db.c', line 272

VALUE
rpm_db_get_home(VALUE db)
{
	check_closed(db);
	return rb_str_new2(RPM_DB(db)->db_home);
}

#init_iterator(key, val) ⇒ Object



1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
# File 'ext/rpm/db.c', line 1148

VALUE
rpm_db_init_iterator(VALUE db, VALUE key, VALUE val)
{
	rpm_mi_t* mi;

	check_closed(db);

	if (!NIL_P(val) && TYPE(val) != T_STRING) {
		rb_raise(rb_eTypeError, "illegal argument type");
	}

	mi = ALLOC_N(rpm_mi_t,1);
	if ((mi->mi = rpmdbInitIterator(RPM_DB(db), NUM2INT(rb_Integer(key)),
						   NIL_P(val) ? NULL : RSTRING_PTR(val),
                           NIL_P(val) ? 0 : RSTRING_LEN(val)))){
		mi->db = (rpm_db_t*)DATA_PTR(db);
		db_ref(mi->db);
		return Data_Wrap_Struct(rpm_cMatchIterator, NULL, mi_free, mi);
	}
	free(mi);
    /* FIXME: returning nil here is a pain; for ruby, it would be nicer
       to return an empty array */
	return Qnil;
}

#rootString

Returns The root path of the database.

Returns:

  • (String)

    The root path of the database



262
263
264
265
266
267
# File 'ext/rpm/db.c', line 262

VALUE
rpm_db_get_root(VALUE db)
{
	check_closed(db);
	return rb_str_new2(RPM_DB(db)->db_root);
}

#transaction(*args) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'ext/rpm/db.c', line 356

VALUE
rpm_db_transaction(int argc, VALUE* argv, VALUE db)
{
	VALUE trans;
	rpm_trans_t* ts;
	const char* root = "/";

#if 0
	if (OBJ_FROZEN(db)) {
		rb_error_frozen("RPM::DB");
	}
#endif
	switch (argc) {
	case 0:
		break;

	case 1:
		if (TYPE(argv[0]) != T_STRING) {
			rb_raise(rb_eTypeError, "illegal argument type");
		}
		root = RSTRING_PTR(argv[0]);
		break;

	default:
		rb_raise(rb_eArgError, "argument too many(0..1)");
	}

	ts = ALLOC(rpm_trans_t);
#if RPM_VERSION_CODE < RPM_VERSION(4,1,0)
	ts->ts = rpmtransCreateSet(RPM_DB(db), root);
#else
	ts->ts = rpmtsCreate();
	rpmtsSetRootDir(ts->ts, root);
#endif
	ts->script_fd = 0;
	ts->db = DATA_PTR(db);
	trans = Data_Wrap_Struct(rpm_cTransaction, NULL, transaction_free, ts);
	db_ref(ts->db);
	rb_ivar_set(trans, id_db, db);

	rb_catch("abort", transaction_yield, trans);

	if (rb_ivar_get(trans, id_aborted) == Qtrue) {
		return Qfalse;
	} else if (rb_ivar_get(trans, id_commited) != Qtrue && !OBJ_FROZEN(db)) {
		rb_catch("abort", transaction_commit, trans);
	}

	return rb_ivar_get(trans, id_pl);
}

#writable?Boolean

Returns true if the database is writable.

Returns:

  • (Boolean)

    true if the database is writable



283
284
285
286
287
288
# File 'ext/rpm/db.c', line 283

VALUE
rpm_db_is_writable(VALUE db)
{
	check_closed(db);
	return OBJ_FROZEN(db) ? Qfalse : Qtrue;
}