Update of /cvsroot/monetdb/MonetDB5/src/tools
In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv20631

Modified Files:
        monetdb.mx 
Log Message:
Finish create and destroy commands


Index: monetdb.mx
===================================================================
RCS file: /cvsroot/monetdb/MonetDB5/src/tools/monetdb.mx,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- monetdb.mx  17 Aug 2007 21:09:51 -0000      1.1
+++ monetdb.mx  18 Aug 2007 12:49:57 -0000      1.2
@@ -35,8 +35,10 @@
 #include <stdarg.h>    /* variadic stuff */
 #include <stdio.h> /* fprintf */
 #include <string.h> /* strdup, strerror */
-#include <sys/stat.h> /* mkdir */
-#include <sys/types.h> /* mkdir */
+#include <sys/stat.h> /* mkdir, stat */
+#include <sys/types.h> /* mkdir, readdir */
+#include <dirent.h> /* readdir */
+#include <unistd.h> /* stat, rmdir, unlink */
 #include <alloca.h>
 #include <errno.h>
 
@@ -108,7 +110,7 @@
 static void
 command_version()
 {
-       printf("MonetDB Toolkit, v%s\n", TOOLKIT_VERSION);
+       printf("MonetDB Toolkit v%s\n", TOOLKIT_VERSION);
 }
 
 static void
@@ -124,7 +126,8 @@
 {
        if (argc == 1 || argc >= 3) {
                /* print help message for this command */
-               command_help(argc, &argv[-1]);
+               command_help(argc + 1, &argv[-1]);
+               exit(1);
        } else if (argc == 2) {
                sabdb *stats;
                err e;
@@ -138,6 +141,7 @@
 
                if (stats == NULL) {
                        char path[8096];
+                       FILE *f;
                        /* Sabaoth doesn't know, green light for us! */
                        snprintf(path, 8095, "%s/%s", dbfarm, argv[1]);
                        path[8095] = '\0';
@@ -146,6 +150,17 @@
                                                argv[1], strerror(errno));
                                exit(1);
                        }
+                       /* without an .uplog file, Merovingian won't work */
+                       snprintf(path, 8095, "%s/%s/.uplog", dbfarm, argv[1]);
+                       fclose(fopen(path, "w"));
+                       /* avoid GDK from making fugly complaints */
+                       snprintf(path, 8095, "%s/%s/.gdk_lock", dbfarm, 
argv[1]);
+                       f = fopen(path, "w");
+                       /* to all insanity, .gdk_lock is "valid" if it contains 
a
+                        * ':', which it does by pure coincidence of a time 
having a
+                        * ':' in there twice... */
+                       fwrite("bla:", 4, 1, f);
+                       fclose(f);
                        printf("successfully created database %s\n", argv[1]);
                } else {
                        SABAOTHfreeStatus(&stats);
@@ -155,13 +170,150 @@
        }
 }
 
+/* recursive helper function to delete a directory */
+static err
+deletedir(char *dir)
+{
+       DIR *d;
+       struct dirent *e;
+       struct stat s;
+       str buf = alloca(sizeof(char) * (PATHLENGTH + 1));
+       str data = alloca(sizeof(char) * 8096);
+       str path = alloca(sizeof(char) * (PATHLENGTH + 1));
+
+       buf[PATHLENGTH] = '\0';
+       d = opendir(dir);
+       if (d == NULL) {
+               snprintf(data, 8095, "unable to open directory %s: %s",
+                               dir, strerror(errno));
+               return(strdup(data));
+       }
+       while ((e = readdir(d)) != NULL) {
+               snprintf(path, PATHLENGTH, "%s/%s", dir, e->d_name);
+               if (stat(path, &s) == -1) {
+                       snprintf(data, 8095, "unable to stat file %s: %s",
+                                       path, strerror(errno));
+                       closedir(d);
+                       return(strdup(data));
+               }
+
+               if (S_ISREG(s.st_mode) || S_ISLNK(s.st_mode)) {
+                       if (unlink(path) == -1) {
+                               snprintf(data, 8095, "unable to unlink file %s: 
%s",
+                                               path, strerror(errno));
+                               closedir(d);
+                               return(strdup(data));
+                       }
+               } else if (S_ISDIR(s.st_mode)) {
+                       err er;
+                       /* recurse, ignore . and .. */
+                       if (strcmp(e->d_name, ".") != 0 &&
+                                       strcmp(e->d_name, "..") != 0 &&
+                                       (er = deletedir(path)) != NO_ERR)
+                       {
+                               closedir(d);
+                               return(er);
+                       }
+               } else {
+                       /* fifos, block, char devices etc, we don't do */
+                       snprintf(data, 8095, "not a regular file: %s", path);
+                       closedir(d);
+                       return(strdup(data));
+               }
+       }
+       closedir(d);
+       if (rmdir(dir) == -1) {
+               snprintf(data, 8095, "unable to remove directory %s: %s",
+                               dir, strerror(errno));
+               return(strdup(data));
+       }
+
+       return(NO_ERR);
+}
+
 static void
 command_destroy(int argc, char *argv[], char *dbfarm)
 {
-       (void)argc;
-       (void)argv;
-       (void)dbfarm;
-       printf("destroy: not yet implemented\n");
+       if (argc == 1 || argc >= 4) {
+               /* print help message for this command */
+               command_help(argc + 1, &argv[-1]);
+               exit(1);
+       } else if (argc >= 2) {
+               sabdb *stats;
+               err e;
+               char *dbname;
+               int force;
+
+               if (argc == 2) {
+                       force = 0;
+                       if (strcmp(argv[1], "-f") == 0) {
+                               command_help(argc + 1, &argv[-1]);
+                               exit(1);
+                       }
+                       dbname = argv[1];
+               } else {
+                       if (strcmp(argv[1], "-f") == 0) {
+                               force = 1;
+                               /* we accept -f -f, stupid user who uses it */
+                               dbname = argv[2];
+                       } else if (strcmp(argv[2], "-f") == 0) {
+                               force = 1;
+                               dbname = argv[1];
+                       } else {
+                               command_help(argc + 1, &argv[-1]);
+                               exit(1);
+                       }
+               }
+
+               /* the argument is the database to destroy, see what Sabaoth can
+                * tell us about it */
+               if ((e = SABAOTHgetStatus(&stats, dbname)) != MAL_SUCCEED) {
+                       fprintf(stderr, "destroy: internal error: %s\n", e);
+                       GDKfree(e);
+                       exit(2);
+               }
+
+               if (stats != NULL) {
+                       char path[8096];
+                       err e;
+
+                       if (stats->state == SABdbRunning) {
+                               fprintf(stderr, "destroy: database %s is still 
running, "
+                                               "stop database first", dbname);
+                               SABAOTHfreeStatus(&stats);
+                               exit(1);
+                       }
+                       SABAOTHfreeStatus(&stats);
+
+                       if (force == 0) {
+                               printf("you are about to remove database %s\n", 
dbname);
+                               printf("ALL data in this database will get 
lost, "
+                                               "are you sure? [y/N] ");
+                               scanf("%c", path);
+                               if (path[0] == 'y' || path[0] == 'Y') {
+                                       /* do it! */
+                               } else {
+                                       printf("battle control terminated\n");
+                                       exit(0);
+                               }
+                       }
+
+                       snprintf(path, 8095, "%s/%s", dbfarm, dbname);
+                       path[8095] = '\0';
+                       /* annoyingly we have to delete file by file, and
+                        * directories recursively... */
+                       if ((e = deletedir(path)) != NULL) {
+                               fprintf(stderr, "destroy: failed to destroy %s: 
%s\n",
+                                               argv[1], e);
+                               free(e);
+                               exit(1);
+                       }
+                       printf("successfully destroyed database %s\n", dbname);
+               } else {
+                       fprintf(stderr, "destroy: no such database: %s\n", 
dbname);
+                       exit(1);
+               }
+       }
 }
 
 
@@ -199,6 +351,9 @@
                exit(2);
        }
 
+       /* initialise Sabaoth so it knows where to look */
+       SABAOTHinit(dbfarm, NULL);
+
        /* Start handling the arguments.
         * monetdb command [options] [database [...]]
         */


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Monetdb-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-checkins

Reply via email to