summaryrefslogtreecommitdiff
path: root/archiver/config-archiver.c
blob: 56679a84cd8eb7302a7d53f6df44eb9429cff30c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* -*- mode: c; style: linux -*- */

/* main.c
 * Copyright (C) 2000-2001 Ximian, Inc.
 *
 * Written by Bradford Hovinen (hovinen@ximian.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/file.h>
#include <unistd.h>
#include <errno.h>

#include <gnome.h>
#include <bonobo.h>

#include <gnome-xml/parser.h>

#include "archiver-client.h"
#include "util.h"

/* Variables resulting from command line parsing */

static gboolean store;
static gboolean rollback;
static gboolean change_location;
static gboolean rename_location;
static gboolean push_config;
static gboolean garbage_collect;

static gboolean add_location;
static gboolean remove_location;
static gboolean add_backend;
static gboolean remove_backend;

static gboolean global;
static const gchar *location_id;

static gchar *backend_id;

static gboolean compare_parent;
static gboolean mask_previous;

static gchar *date_str;
static gboolean all;
static gchar *revision_id;
static gboolean last;
static guint steps;
static gboolean show;

static gchar *parent_str;
static gchar *new_name;

static gboolean contain_full;
static gboolean contain_partial;
static gboolean master;

static struct poptOption archiver_operations[] = {
	{"store", 's', POPT_ARG_NONE, &store, 0,
	 N_("Store XML data in the archive")},
	{"rollback", 'r', POPT_ARG_NONE, &rollback, 0,
	 N_("Roll back the configuration to a given point")},
	{"change-location", 'c', POPT_ARG_NONE, &change_location, 0,
	 N_("Change the location profile to the given one")},
	{"push-config", 'p', POPT_ARG_NONE, &push_config, 0,
	 N_("Push configuration data out to client machines (UNIMPLEMENTED)")},
	{"rename-location", '\0', POPT_ARG_NONE, &rename_location, 0,
	 N_("Rename a location to a new name")},
	{"add-location", '\0', POPT_ARG_NONE, &add_location, 0,
	 N_("Add a new location to the archive")},
	{"remove-location", '\0', POPT_ARG_NONE, &remove_location, 0,
	 N_("Remove a location from the archive")},
	{"add-backend", '\0', POPT_ARG_NONE, &add_backend, 0,
	 N_("Add a given backend to the given location")},
	{"remove-backend", '\0', POPT_ARG_NONE, &remove_backend, 0,
	 N_("Remove the given backend from the given location")},
	{"garbage-collect", '\0', POPT_ARG_NONE, &garbage_collect, 0,
	 N_("Perform garbage collection on the given location")},
	{NULL, '\0', 0, NULL, 0}
};

static struct poptOption global_options[] = {
        {"global", 'g', POPT_ARG_NONE, &global, 0, 
	 N_("Use the global repository")},
	{"location", 'l', POPT_ARG_STRING, &location_id, 0,
	 N_("Identifier of location profile on which to operate"), 
	 N_("LOCATION")},
        {"backend", 'b', POPT_ARG_STRING, &backend_id, 0, 
	 N_("Backend being used for this operation"), N_("BACKEND_ID")},
        {NULL, '\0', 0, NULL, 0}
};

static struct poptOption store_options[] = {
	{"compare-parent", '\0', POPT_ARG_NONE, &compare_parent, 0,
	 N_("Store only the differences with the parent location's config")},
	{"mask-previous", '\0', POPT_ARG_NONE, &mask_previous, 0,
	 N_("Store only those settings set in the previous config")},
	{NULL, '\0', 0, NULL, 0}
};

static struct poptOption rollback_options[] = {
	{"date", 'd', POPT_ARG_STRING, &date_str, 0,
	 N_("Date to which to roll back"), N_("DATE")},
	{"all", 'a', POPT_ARG_NONE, &all, 0,
	 N_("Roll back all configuration items")},
	{"revision-id", 'i', POPT_ARG_INT, &revision_id, 0,
	 N_("Roll back to the revision REVISION_ID"), N_("REVISION_ID")},
	{"last", 't', POPT_ARG_NONE, &last, 0,
	 N_("Roll back to the last known revision")},
	{"steps", '\0', POPT_ARG_INT, &steps, 0,
	 N_("Roll back by STEPS revisions"), N_("STEPS")},
	{"show", 'h', POPT_ARG_NONE, &show, 0,
	 N_("Don't run the backend, just dump the output")},
	{NULL, '\0', 0, NULL, 0}
};

static struct poptOption add_rename_location_options[] = {
	{"parent", '\0', POPT_ARG_STRING, &parent_str, 0,
	 N_("Parent location for the new location"), N_("PARENT")},
	{"new-name", '\0', POPT_ARG_STRING, &new_name, 0,
	 N_("New name to assign to the location"), N_("NEW_NAME")},
	{NULL, '\0', 0, NULL, 0}
};

static struct poptOption add_remove_backend_options[] = {
	{"master", '\0', POPT_ARG_NONE, &master, 0,
	 N_("Add/remove this backend to/from the master backend list")},
	{"full", '\0', POPT_ARG_NONE, &contain_full, 0,
	 N_("Full containment")},
	{"partial", '\0', POPT_ARG_NONE, &contain_partial, 0,
	 N_("Partial containment")},
	{NULL, '\0', 0, NULL, 0}
};

static xmlDocPtr
xml_load_from_stream (FILE *stream) 
{
	GString *str;
	gchar buffer[4097];
	xmlDocPtr doc;
	size_t t;

	str = g_string_new ("");

	while (!feof (stream)) {
		t = fread (buffer, sizeof (char), 4096, stream);
		buffer[t] = '\0';
		g_string_append (str, buffer);
	}

	doc = xmlParseDoc (str->str);
	g_string_free (str, TRUE);
	return doc;
}

static ConfigArchiver_StringSeq *
make_backend_id_seq (gchar *backend_id, ...) 
{
	ConfigArchiver_StringSeq *seq;

	seq = ConfigArchiver_StringSeq__alloc ();
	seq->_length = 1;
	seq->_buffer = CORBA_sequence_CORBA_string_allocbuf (1);
	seq->_buffer[0] = CORBA_string_dup (backend_id);

	return seq;
}

static void
do_store (ConfigArchiver_Location location, CORBA_Environment *ev) 
{
	ConfigArchiver_StoreType type;
	xmlDocPtr doc;

	if (!backend_id) {
		fprintf (stderr, "No backend specified\n");
		return;
	}

	if (mask_previous)
		type = ConfigArchiver_STORE_MASK_PREVIOUS;
	else if (compare_parent)
		type = ConfigArchiver_STORE_COMPARE_PARENT;
	else
		type = ConfigArchiver_STORE_FULL;

	doc = xml_load_from_stream (stdin);

	location_client_store_xml (location, backend_id, doc, type, ev);
}

static void
do_rollback (ConfigArchiver_Location location, CORBA_Environment *ev) 
{
	struct tm *date = NULL;
	ConfigArchiver_StringSeq *seq;
	ConfigArchiver_Time tm;
	xmlDocPtr doc;

	if (date_str)
		date = parse_date (date_str);
	else if (last || steps > 0)
		date = NULL;
	else if (!revision_id) {
		fprintf (stderr, "No date specified\n");
		return;
	}

	if (backend_id != NULL && (date != NULL || last)) {
		/* FIXME: Need to support specifying multiple backends */
		if (show) {
			doc = location_client_load_rollback_data (location, date, 0, backend_id, TRUE, ev);
			xmlDocDump (stdout, doc);
			xmlFreeDoc (doc);
		} else {
			tm = mktime (date);
			seq = make_backend_id_seq (backend_id, NULL);
			ConfigArchiver_Location_rollbackBackends (location, tm, 0, seq, TRUE, ev);
			CORBA_free (seq);
		}
	}
	else if (backend_id != NULL && steps != 0) {
		if (show) {
			doc = location_client_load_rollback_data (location, date, steps, backend_id, TRUE, ev);
			xmlDocDump (stdout, doc);
			xmlFreeDoc (doc);
		} else {
			seq = make_backend_id_seq (backend_id, NULL);
			ConfigArchiver_Location_rollbackBackends (location, 0, steps, seq, TRUE, ev);
			CORBA_free (seq);
		}
	} else {
		g_message ("No backend specified\n");
		return;
	}
}

static void
do_change_location (ConfigArchiver_Archive archive, ConfigArchiver_Location location, CORBA_Environment *ev) 
{
	ConfigArchiver_Archive__set_currentLocation (archive, location, ev);
}

static void
do_rename_location (ConfigArchiver_Archive archive, ConfigArchiver_Location location, CORBA_Environment *ev) 
{
	gboolean is_current;
	CORBA_char *locid, *cid;

	if (new_name == NULL) {
		fprintf (stderr, "You did not specify a new name. Try --help\n");
	} else {
		locid = ConfigArchiver_Location__get_id (location, ev);
		cid = ConfigArchiver_Archive__get_currentLocationId (archive, ev);

		if (!strcmp (locid, cid))
			is_current = TRUE;
		else
			is_current = FALSE;

		CORBA_free (locid);
		CORBA_free (cid);

		ConfigArchiver_Location__set_id (location, new_name, ev);

		if (is_current) 
			ConfigArchiver_Archive__set_currentLocationId (archive, new_name, ev);
	}
}

static void
do_add_location (ConfigArchiver_Archive archive, CORBA_Environment *ev) 
{
	ConfigArchiver_Location parent_location = CORBA_OBJECT_NIL;

	if (location_id == NULL) {
		fprintf (stderr, "Error: You did not specify a location name\n");
		return;
	}

	if (parent_str != NULL) {
		parent_location = ConfigArchiver_Archive_getLocation (archive, parent_str, ev);

		if (parent_location == NULL && !strcmp (parent_str, "default"))
			parent_location =
				ConfigArchiver_Archive_createLocation (archive, "default", _("Default Location"),
								       CORBA_OBJECT_NIL, ev);
	}

	ConfigArchiver_Archive_createLocation (archive, location_id, location_id, parent_location, ev);
}

static void
do_remove_location (ConfigArchiver_Location location, CORBA_Environment *ev)
{
	ConfigArchiver_Location_delete (location, ev);
}

static void
do_add_backend (ConfigArchiver_Location location, CORBA_Environment *ev) 
{
	ConfigArchiver_ContainmentType type;

	if (contain_full && contain_partial) {
		fprintf (stderr, "Error: Cannot have both full and partial "
			 "containment\n");
		return;
	}
	else if (contain_partial) {
		type = ConfigArchiver_CONTAIN_PARTIAL;
	} else {
		type = ConfigArchiver_CONTAIN_FULL;
	}

	ConfigArchiver_Location_addBackend (location, backend_id, type, ev);
}

static void
do_remove_backend (ConfigArchiver_Location location, CORBA_Environment *ev) 
{
	ConfigArchiver_Location_removeBackend (location, backend_id, ev);
}

static void
do_garbage_collect (ConfigArchiver_Location location, CORBA_Environment *ev) 
{
	ConfigArchiver_Location_garbageCollect (location, ev);
}

int
main (int argc, char **argv) 
{
	CORBA_ORB orb;
	ConfigArchiver_Archive archive;
	ConfigArchiver_Location location = CORBA_OBJECT_NIL;
	CORBA_Environment ev;

	/* For Electric Fence */
	free (malloc (1));

        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        textdomain (GETTEXT_PACKAGE);

	CORBA_exception_init (&ev);

	gnomelib_register_popt_table (global_options, 
				      _("Global archiver options"));
	gnomelib_register_popt_table (archiver_operations, 
				      _("Archiver commands"));
	gnomelib_register_popt_table (store_options, 
				      _("Options for storing data"));
	gnomelib_register_popt_table (rollback_options, 
				      _("Options for rolling back"));
	gnomelib_register_popt_table (add_rename_location_options, 
				      _("Options for adding or renaming " \
					"locations"));
	gnomelib_register_popt_table (add_remove_backend_options, 
				      _("Options for adding and removing " \
					"backends"));

	gtk_type_init ();
	gnomelib_init ("archiver", VERSION);
	gnomelib_parse_args (argc, argv, 0);

	orb = oaf_init (argc, argv);

	if (bonobo_init (orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL) == FALSE)
		g_error ("Cannot initialize Bonobo");

	if (global)
		archive = bonobo_get_object ("archive:global-archive", "IDL:ConfigArchiver/Archive:1.0", &ev);
	else
		archive = bonobo_get_object ("archive:user-archive", "IDL:ConfigArchiver/Archive:1.0", &ev);

	if (archive == CORBA_OBJECT_NIL) {
		g_critical ("Could not open archive\n");
		return -1;
	}

	if (!add_location) {
		if (location_id == NULL)
			location_id = ConfigArchiver_Archive__get_currentLocationId (archive, &ev);

		location = ConfigArchiver_Archive_getLocation (archive, location_id, &ev);

		if (location == CORBA_OBJECT_NIL) {
			g_critical ("Error: Could not open location %s\n", location_id);
			return -1;
		}
	}

	if (store)
		do_store (location, &ev);
	else if (rollback)
		do_rollback (location, &ev);
	else if (change_location)
		do_change_location (archive, location, &ev);
	else if (rename_location)
		do_rename_location (archive, location, &ev);
	else if (add_location)
		do_add_location (archive, &ev);
	else if (remove_location)
		do_remove_location (location, &ev);
	else if (add_backend)
		do_add_backend (location, &ev);
	else if (remove_backend)
		do_remove_backend (location, &ev);
	else if (garbage_collect)
		do_garbage_collect (location, &ev);

	bonobo_object_release_unref (archive, NULL);

	if (location != CORBA_OBJECT_NIL)
		bonobo_object_release_unref (location, NULL);

	return 0;
}