OSDN Git Service

Fix "mingw-get deletes itself" bug in last rites handling.
[mingw/mingw-get.git] / src / climain.cpp
index 63ee2d0..651fae3 100644 (file)
@@ -4,7 +4,7 @@
  * $Id$
  *
  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
- * Copyright (C) 2009, MinGW Project
+ * Copyright (C) 2009, 2010, MinGW Project
  *
  *
  * Implementation of the main program function, which is invoked by
  */
 #include <stdio.h>
 #include <string.h>
+#include <fcntl.h>
 
 #include "dmh.h"
 
 #include "pkgbase.h"
+#include "pkgkeys.h"
 #include "pkgtask.h"
 
 EXTERN_C int climain( int argc, char **argv )
 {
+  try
+  {
+
   /* Set up the diagnostic message handler, using the console's
    * `stderr' stream for notifications...
    */
@@ -61,7 +66,21 @@ EXTERN_C int climain( int argc, char **argv )
    * local `profile' configuration, and invoke the operation.
    */
   const char *dfile;
-  pkgXmlDocument dbase( dfile = xmlfile( "profile" ) );
+  if( access( dfile = xmlfile( profile_key ), R_OK ) != 0 )
+  {
+    /* The user hasn't provided a custom configuration profile...
+     */
+    dmh_notify( DMH_WARNING, "%s: user configuration file missing\n", dfile );
+
+    /* ...release the memory allocated by xmlfile(), to store its path name,
+     * then try the mingw-get distribution default profile instead.
+     */
+    free( (void *)(dfile) );
+    dmh_notify( DMH_INFO, "%s: trying system default configuration\n",
+       dfile = xmlfile( defaults_key ) );
+  }
+
+  pkgXmlDocument dbase( dfile );
   if( dbase.IsOk() )
   {
     /* We successfully loaded the basic settings...
@@ -72,30 +91,39 @@ EXTERN_C int climain( int argc, char **argv )
      */
     free( (void *)(dfile) );
 
-#if 0
     /* Merge all package lists, as specified in the "repository"
      * section of the "profile", into the XML database tree...
      */
-    if( dbase.BindRepositories() == NULL )
+    if( dbase.BindRepositories( action == ACTION_UPDATE ) == NULL )
       /*
        * ...bailing out, on an invalid profile specification...
        */
       dmh_notify( DMH_FATAL, "%s: invalid application profile\n", dbase.Value() );
 
-    /* Now schedule the specified action for each additionally
-     * specified command line argument, (each of which is assumed
-     * to represent a package name...
+    /* If the requested action was "update", then we've already done it,
+     * as a side effect of binding the cached repository catalogues...
      */
-    while( --argc )
-      dbase.Schedule( (unsigned long)(action), *++argv );
+    if( action != ACTION_UPDATE )
+    {
+      /* ...otherwise, we need to load the system map...
+       */
+      dbase.LoadSystemMap();
 
-    /* ...and finally, execute all scheduled actions...
-     */
-    dbase.ExecuteActions();
-#endif
+      /* ...schedule the specified action for each additional command line
+       * argument, (each of which is assumed to represent a package name)...
+       */
+      while( --argc )
+       dbase.Schedule( (unsigned long)(action), *++argv );
+
+      /* ...finally, execute all scheduled actions, and update the
+       * system map accordingly.
+       */
+      dbase.ExecuteActions();
+      dbase.UpdateSystemMap();
+    }
 
     /* If we get this far, then all actions completed successfully;
-     * we are done...
+     * we are done.
      */
     return EXIT_SUCCESS;
   }
@@ -109,6 +137,12 @@ EXTERN_C int climain( int argc, char **argv )
    * function without a return value assignment...
    */
   return dmh_notify( DMH_FATAL, "%s: cannot load configuration\n", dfile );
+
+  }
+  catch (dmh_exception &e)
+  {
+    return EXIT_FAILURE;
+  }
 }
 
 /* $RCSfile$: end of file */