2 * developer replaying of bugreport snapshots
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <glib/gmessages.h>
24 #include <glib/giochannel.h>
28 #include <libxml/xmlreader.h>
29 #include <glib-object.h> /* for g_type_init() */
30 #include <glib/ghash.h>
32 #include <captive/libxml.h>
33 #include <captive/options.h>
34 #include <captive/macros.h>
35 #include "../../libcaptive/client/giochannel-blind.h" /* FIXME: pathname */
36 #include <captive/client-vfs.h>
37 #include <captive/client-directory.h>
38 #include <captive/client-file.h>
41 static gchar *optarg_module_path;
43 static const struct poptOption popt_table[]={
44 #define BUG_REPLAY_POPT(longname,argInfoP,argP,descripP,argDescripP) \
46 longName: (longname), \
48 argInfo: (argInfoP), \
51 descrip: (descripP), \
52 argDescrip: (argDescripP), \
55 BUG_REPLAY_POPT("module-path",POPT_ARG_STRING,&optarg_module_path,N_("Disk path for W32 modules and filesystem"),N_("path")),
57 #undef BUG_REPLAY_POPT
63 static void object_hash_key_destroy_func(const xmlChar *xml_object)
65 g_return_if_fail(xml_object!=NULL);
67 xmlFree((xmlChar *)xml_object);
70 static void object_hash_value_destroy_func(GObject *captive_any_object)
72 g_return_if_fail(G_IS_OBJECT(captive_any_object));
74 g_object_unref(captive_any_object);
78 int main(int argc,char **argv)
82 const char **cmd_argv;
83 struct captive_options options;
84 xmlTextReader *xml_reader;
85 GList *action_list; /* of 'xmlNode *' */
87 gboolean module_is_filesystem=FALSE;
88 CaptiveVfsObject *captive_vfs_object;
89 gboolean action_is_first;
91 GHashTable *object_hash;
94 CaptiveDirectoryObject *captive_directory_object;
95 CaptiveFileObject *captive_file_object;
96 GnomeVFSResult errgnomevfsresult;
97 struct captive_libxml_string_drop_stack *drop_stack=NULL;
98 const gchar *xml_object;
100 /* Prevent output block buffering if redirecting stdout to file. */
101 setvbuf(stdout,(char *)NULL,_IONBF,0);
102 setvbuf(stderr,(char *)NULL,_IONBF,0);
104 /* Initialize GObject subsystem of GLib. */
107 captive_options_init(&options);
109 context=poptGetContext(
111 argc,(/*en-const*/const char **)argv, /* argc,argv */
112 popt_table, /* options */
113 POPT_CONTEXT_POSIXMEHARDER); /* flags; && !POPT_CONTEXT_KEEP_FIRST */
115 g_assert_not_reached(); /* argument recognization args_error */
118 errint=poptReadDefaultConfig(context,
121 g_assert_not_reached(); /* argument recognization args_error */
124 errint=poptGetNextOpt(context);
126 g_assert_not_reached(); /* some non-callbacked argument reached */
129 cmd_argv=poptGetArgs(context);
131 if (!cmd_argv || !cmd_argv[0] || cmd_argv[1]) {
132 g_error("Exactly one argument required - pathname to .captivebug.xml.gz bugreport to replay");
136 options.rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
137 options.debug_messages=TRUE;
139 options.image_iochannel=NULL;
141 options.media=CAPTIVE_OPTION_MEDIA_UNKNOWN;
143 xml_reader=xmlNewTextReaderFilename(cmd_argv[0]);
144 g_assert(xml_reader!=NULL);
146 while (preread || 1==xmlTextReaderRead(xml_reader)) {
148 switch (xmlTextReaderNodeType(xml_reader)) {
150 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_COMMENT:
153 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_SIGNIFICANT_WHITESPACE:
156 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_TEXT: /* Even empty nodes have some '#text'. */
159 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_END: /* We do not track tag ends. */
162 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_START: {
163 const xmlChar *xml_name;
165 xml_name=xmlTextReaderName(xml_reader);
166 /**/ if (!xmlStrcmp(xml_name,BAD_CAST "bug")) { /* root tag */
168 else if (!xmlStrcmp(xml_name,BAD_CAST "captive")) {
169 const gchar *xml_captive_version;
171 xml_captive_version=captive_libxml_string_drop(&drop_stack,xmlTextReaderGetAttribute(xml_reader,BAD_CAST "version"));
172 g_assert(xml_captive_version!=NULL);
173 g_assert(!strcmp(xml_captive_version,VERSION));
175 else if (!xmlStrcmp(xml_name,BAD_CAST "filesystem")) { /* contains <module/> */
176 module_is_filesystem=TRUE;
178 else if (!xmlStrcmp(xml_name,BAD_CAST "load_module")) { /* contains <module/> */
179 module_is_filesystem=FALSE;
181 else if (!xmlStrcmp(xml_name,BAD_CAST "module")) {
182 const gchar *xml_module_basename;
183 struct captive_options_module *options_module;
186 if (module_is_filesystem)
187 options_module=&options.filesystem;
189 captive_new(options_module);
190 options.load_module=g_list_append(options.load_module,options_module);
192 xml_module_basename=captive_libxml_string_drop(&drop_stack,xmlTextReaderGetAttribute(xml_reader,BAD_CAST "basename"));
193 g_assert(xml_module_basename!=NULL);
194 g_assert(strchr(xml_module_basename,'/')==NULL); /* a bit of security */
195 errbool=captive_options_module_load(options_module,
196 captive_printf_alloca("%s/%s",
197 (optarg_module_path ? optarg_module_path : "."),xml_module_basename));
198 g_assert(errbool==TRUE);
199 switch (options_module->type) {
200 case CAPTIVE_OPTIONS_MODULE_TYPE_PE32: {
201 const gchar *xml_module_md5,*xml_module_length;
203 if (options_module->u.pe32.md5
204 && (xml_module_md5=captive_libxml_string_drop(&drop_stack,xmlTextReaderGetAttribute(xml_reader,BAD_CAST "version"))))
205 g_assert(!strcmp(xml_module_md5,options_module->u.pe32.md5));
206 if ((xml_module_length=captive_libxml_string_drop(&drop_stack,xmlTextReaderGetAttribute(xml_reader,BAD_CAST "length"))))
207 g_assert(!strcmp(xml_module_length,
208 captive_printf_alloca("%lu",(unsigned long)options_module->u.pe32.length)));
210 case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
211 /* No possibility to check anything. */
213 default: g_assert_not_reached();
216 else if (!xmlStrcmp(xml_name,BAD_CAST "action")) {
217 int xml_action_depth;
218 gboolean action_done,action_preread;
220 g_assert(action_list==NULL);
221 xml_action_depth=xmlTextReaderDepth(xml_reader);
222 action_preread=FALSE;
224 while (!action_done) {
225 if (!action_preread) {
226 errint=xmlTextReaderRead(xml_reader);
229 action_preread=FALSE;
230 g_assert(xml_action_depth<=xmlTextReaderDepth(xml_reader));
231 switch (xmlTextReaderNodeType(xml_reader)) {
232 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_COMMENT:
234 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_SIGNIFICANT_WHITESPACE:
236 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_TEXT: /* Even empty nodes have some '#text'. */
238 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_END: { /* We do not track tag ends. */
239 if ((action_done=(xml_action_depth==xmlTextReaderDepth(xml_reader)))) {
240 const gchar *xml_name_end;
242 xml_name_end=captive_libxml_string_drop(&drop_stack,xmlTextReaderName(xml_reader));
243 g_assert(!strcmp(xml_name_end,"action"));
246 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_START: {
249 xml_node=xmlTextReaderExpand(xml_reader);
250 g_assert(xml_node!=NULL);
251 xml_node=xmlCopyNode(xml_node,
253 errint=xmlTextReaderNext(xml_reader);
256 action_list=g_list_prepend(action_list,xml_node); /* we are prepending to be effective */
258 default: g_assert_not_reached();
262 else if (!xmlStrcmp(xml_name,BAD_CAST "media")) {
263 const xmlChar *xml_media_end_name,*xml_media_type;
265 g_assert(options.image_iochannel==NULL);
266 options.image_iochannel=(GIOChannel *)captive_giochannel_blind_new_from_xml(xml_reader);
267 g_assert(CAPTIVE_XML_TEXT_READER_NODE_TYPE_END==xmlTextReaderNodeType(xml_reader));
268 xml_media_end_name=xmlTextReaderName(xml_reader);
269 g_assert(xml_media_end_name!=NULL);
270 g_assert(!xmlStrcmp(xml_media_end_name,BAD_CAST "media"));
271 xmlFree((xmlChar *)xml_media_end_name);
272 xml_media_type=xmlTextReaderGetAttribute(xml_reader,BAD_CAST "type");
273 g_assert(xml_media_type!=NULL);
274 /**/ if (!xmlStrcmp(xml_media_type,BAD_CAST "cdrom"))
275 options.media=CAPTIVE_OPTION_MEDIA_CDROM;
276 else if (!xmlStrcmp(xml_media_type,BAD_CAST "disk"))
277 options.media=CAPTIVE_OPTION_MEDIA_DISK;
278 else g_assert_not_reached();
279 xmlFree((xmlChar *)xml_media_type);
281 else if (!xmlStrcmp(xml_name,BAD_CAST "log")) {
282 errint=xmlTextReaderNext(xml_reader);
286 else g_error("Unknown START node: %s",xml_name);
287 xmlFree((xmlChar *)xml_name);
290 default: g_assert_not_reached();
292 captive_libxml_string_drop_flush(&drop_stack);
294 xmlFreeTextReader(xml_reader);
296 g_assert(options.image_iochannel!=NULL);
297 action_list=g_list_reverse(action_list); /* we were g_list_prepend()ing for effectivity */
298 g_assert(options.media!=CAPTIVE_OPTION_MEDIA_UNKNOWN);
300 if (GNOME_VFS_OK!=captive_vfs_new(&captive_vfs_object,&options)) {
301 g_error(_("captive_vfs_new() failed"));
304 captive_options_free(&options);
306 /* 'cmd_argv' gets cleared by 'poptFreeContext(context);' below */
307 poptFreeContext(context);
309 object_hash=g_hash_table_new_full(
310 g_str_hash, /* hash_func */
311 g_str_equal, /* key_equal_func */
312 (GDestroyNotify)object_hash_key_destroy_func, /* key_destroy_func */
313 (GDestroyNotify)object_hash_value_destroy_func); /* value_destroy_func */
316 action_is_first=TRUE;
318 xmlFreeNode(xml_node),action_is_first=FALSE) {
320 captive_libxml_string_drop_flush(&drop_stack);
322 #define GET_PROP_STRING(prop_name) ({ \
323 const gchar *_get_prop_string_r=captive_libxml_string_drop(&drop_stack,xmlGetProp(xml_node,BAD_CAST (prop_name))); \
324 g_assert(_get_prop_string_r!=NULL); \
325 _get_prop_string_r; \
327 #define GET_PROP_GINT64(prop_name) (captive_libxml_sscanf_gint64(GET_PROP_STRING((prop_name))))
329 xml_node=action_list->data;
330 g_assert(xml_node!=NULL);
331 action_list=g_list_delete_link(action_list,action_list);
332 g_message("replay action: %s()",xml_node->name);
333 xml_object=GET_PROP_STRING("object");
334 object=g_hash_table_lookup(object_hash,xml_object);
335 g_assert(object==NULL || G_IS_OBJECT(object));
337 if (!xmlStrcmp(xml_node->name,BAD_CAST "vfs_new")) {
338 g_assert(action_is_first==TRUE);
339 g_assert(captive_vfs_object!=NULL);
340 g_assert(object==NULL);
341 g_hash_table_insert(object_hash,g_strdup(xml_object),captive_vfs_object);
344 if (!xmlStrcmp(xml_node->name,BAD_CAST "vfs_commit")) {
345 g_assert(action_is_first==TRUE);
346 g_assert(captive_vfs_object!=NULL);
347 g_assert(object==NULL);
348 g_hash_table_insert(object_hash,g_strdup(xml_object),captive_vfs_object);
351 g_assert(action_is_first==FALSE);
352 if (!xmlStrcmp(xml_node->name,BAD_CAST "vfs_close")) {
353 g_assert(captive_vfs_object!=NULL);
354 g_assert(CAPTIVE_VFS_OBJECT(object)==captive_vfs_object);
355 errbool=g_hash_table_remove(object_hash,xml_object); /* g_object_unref() by object_hash_value_destroy_func() */
356 g_assert(errbool==TRUE);
359 g_assert(captive_vfs_object!=NULL);
360 if (!xmlStrcmp(xml_node->name,BAD_CAST "vfs_volume_info_get")) {
361 CaptiveVfsVolumeInfo volume_info;
363 errgnomevfsresult=captive_vfs_volume_info_get(captive_vfs_object,&volume_info);
364 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
369 if (!xmlStrcmp(xml_node->name,BAD_CAST "directory_new_open")) {
370 g_assert(object==NULL);
371 errgnomevfsresult=captive_directory_new_open(&captive_directory_object,captive_vfs_object,
372 GET_PROP_STRING("pathname"));
373 g_assert((errgnomevfsresult==GNOME_VFS_OK)==GET_PROP_GINT64("result"));
374 if (errgnomevfsresult==GNOME_VFS_OK)
375 g_hash_table_insert(object_hash,g_strdup(xml_object),captive_directory_object);
378 if (!xmlStrcmp(xml_node->name,BAD_CAST "directory_new_make")) {
379 g_assert(object==NULL);
380 errgnomevfsresult=captive_directory_new_make(&captive_directory_object,captive_vfs_object,
381 GET_PROP_STRING("pathname"),
382 GET_PROP_GINT64("perm"));
383 g_assert((errgnomevfsresult==GNOME_VFS_OK)==GET_PROP_GINT64("result"));
384 if (errgnomevfsresult==GNOME_VFS_OK)
385 g_hash_table_insert(object_hash,g_strdup(xml_object),captive_directory_object);
388 if (!xmlStrcmp(xml_node->name,BAD_CAST "directory_close")) {
389 captive_directory_object=CAPTIVE_DIRECTORY_OBJECT(object);
390 errbool=g_hash_table_remove(object_hash,xml_object); /* g_object_unref() by object_hash_value_destroy_func() */
391 g_assert(errbool==TRUE);
394 if (!xmlStrcmp(xml_node->name,BAD_CAST "directory_read")) {
395 GnomeVFSFileInfo file_info;
397 captive_directory_object=CAPTIVE_DIRECTORY_OBJECT(object);
398 errgnomevfsresult=captive_directory_read(captive_directory_object,&file_info);
399 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
402 if (!xmlStrcmp(xml_node->name,BAD_CAST "directory_remove")) {
403 captive_directory_object=CAPTIVE_DIRECTORY_OBJECT(object);
404 errgnomevfsresult=captive_directory_remove(captive_directory_object);
405 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
410 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_new_open")) {
411 g_assert(object==NULL);
412 errgnomevfsresult=captive_file_new_open(&captive_file_object,captive_vfs_object,
413 GET_PROP_STRING("pathname"),
414 GET_PROP_GINT64("mode"));
415 g_assert((errgnomevfsresult==GNOME_VFS_OK)==GET_PROP_GINT64("result"));
416 if (errgnomevfsresult==GNOME_VFS_OK)
417 g_hash_table_insert(object_hash,g_strdup(xml_object),captive_file_object);
420 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_new_create")) {
421 g_assert(object==NULL);
422 errgnomevfsresult=captive_file_new_create(&captive_file_object,captive_vfs_object,
423 GET_PROP_STRING("pathname"),
424 GET_PROP_GINT64("mode"),
425 GET_PROP_GINT64("exclusive"),
426 GET_PROP_GINT64("perm"));
427 g_assert((errgnomevfsresult==GNOME_VFS_OK)==GET_PROP_GINT64("result"));
428 if (errgnomevfsresult==GNOME_VFS_OK)
429 g_hash_table_insert(object_hash,g_strdup(xml_object),captive_file_object);
432 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_close")) {
433 captive_file_object=CAPTIVE_FILE_OBJECT(object);
434 errbool=g_hash_table_remove(object_hash,xml_object); /* g_object_unref() by object_hash_value_destroy_func() */
435 g_assert(errbool==TRUE);
438 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_read")) {
440 GnomeVFSFileSize num_bytes,bytes_read;
442 captive_file_object=CAPTIVE_FILE_OBJECT(object);
443 num_bytes=GET_PROP_GINT64("num_bytes");
444 buffer=g_malloc(num_bytes);
445 errgnomevfsresult=captive_file_read(captive_file_object,buffer,num_bytes,&bytes_read);
446 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
447 if (errgnomevfsresult==GNOME_VFS_OK)
448 g_assert((gint64)bytes_read==GET_PROP_GINT64("bytes_read_return"));
452 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_write")) {
454 GnomeVFSFileSize num_bytes,bytes_written;
456 captive_file_object=CAPTIVE_FILE_OBJECT(object);
457 num_bytes=GET_PROP_GINT64("num_bytes");
458 buffer=g_malloc(num_bytes);
459 memset(buffer,'X',num_bytes); /* FIXME: better pattern */
460 errgnomevfsresult=captive_file_write(captive_file_object,buffer,num_bytes,&bytes_written);
461 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
462 if (errgnomevfsresult==GNOME_VFS_OK)
463 g_assert((gint64)bytes_written==GET_PROP_GINT64("bytes_written_return"));
467 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_seek")) {
468 GnomeVFSSeekPosition whence=GNOME_VFS_SEEK_START; /* Prevent: ... might be used uninitialized in this function */
469 const gchar *whence_string;
471 captive_file_object=CAPTIVE_FILE_OBJECT(object);
472 whence_string=GET_PROP_STRING("whence");
473 /**/ if (!strcmp(whence_string,"start" )) whence=GNOME_VFS_SEEK_START;
474 else if (!strcmp(whence_string,"current")) whence=GNOME_VFS_SEEK_CURRENT;
475 else if (!strcmp(whence_string,"end" )) whence=GNOME_VFS_SEEK_END;
476 else g_assert_not_reached();
477 errgnomevfsresult=captive_file_seek(captive_file_object,whence,
478 GET_PROP_GINT64("offset"));
479 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
482 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_tell")) {
483 GnomeVFSFileOffset offset;
485 captive_file_object=CAPTIVE_FILE_OBJECT(object);
486 errgnomevfsresult=captive_file_tell(captive_file_object,&offset);
487 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
488 if (errgnomevfsresult==GNOME_VFS_OK)
489 g_assert((gint64)offset==GET_PROP_GINT64("offset"));
492 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_remove")) {
493 captive_file_object=CAPTIVE_FILE_OBJECT(object);
494 errgnomevfsresult=captive_file_remove(captive_file_object);
495 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
498 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_file_info_get")) {
499 GnomeVFSFileInfo file_info;
501 captive_file_object=CAPTIVE_FILE_OBJECT(object);
502 errgnomevfsresult=captive_file_file_info_get(captive_file_object,&file_info);
503 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
506 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_file_info_set")) {
507 GnomeVFSFileInfo file_info;
509 captive_file_object=CAPTIVE_FILE_OBJECT(object);
510 /* FIXME: We do not have 'file_info' content in the bug file! */
511 errgnomevfsresult=captive_file_file_info_get(captive_file_object,&file_info);
512 g_assert(errgnomevfsresult==GNOME_VFS_OK);
513 errgnomevfsresult=captive_file_file_info_set(captive_file_object,&file_info,
514 GET_PROP_GINT64("mask"));
515 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
518 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_truncate")) {
519 captive_file_object=CAPTIVE_FILE_OBJECT(object);
520 errgnomevfsresult=captive_file_truncate(captive_file_object,
521 GET_PROP_GINT64("file_size"));
522 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
525 if (!xmlStrcmp(xml_node->name,BAD_CAST "file_move")) {
526 captive_file_object=CAPTIVE_FILE_OBJECT(object);
527 errgnomevfsresult=captive_file_move(captive_file_object,
528 GET_PROP_STRING("pathname_new"),
529 GET_PROP_GINT64("force_replace"));
530 g_assert(!strcmp(gnome_vfs_result_to_string(errgnomevfsresult),GET_PROP_STRING("result")));
534 g_error("Unknown action: %s()",xml_node->name);
536 #undef GET_PROP_GINT64
537 #undef GET_PROP_STRING
540 g_error("All actions were processed, no vfs_close() found");