Update of /cvsroot/monetdb/sql/src/backends/monet5/merovingian
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv12424

Modified Files:
        database.c database.h monetdb.c 
Removed Files:
        monetdb_destroy.c 
Log Message:
merge destroy back in database.c

--- monetdb_destroy.c DELETED ---

U database.c
Index: database.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/database.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- database.c  20 Aug 2009 10:46:50 -0000      1.1
+++ database.c  20 Aug 2009 11:38:29 -0000      1.2
@@ -17,10 +17,17 @@
  * All Rights Reserved.
  */
 
+/* NOTE: for this file to work correctly, SABAOTHinit must be called,
+ * and the random number generator must have been seeded (srand) with
+ * something like the current time */
+
 #include "sql_config.h"
 #include "mal_sabaoth.h"
 #include <stdio.h> /* fprintf, rename */
 #include <unistd.h> /* stat, rmdir, unlink, ioctl */
+#include <dirent.h> /* readdir */
+#include <sys/stat.h> /* mkdir, stat, umask */
+#include <sys/types.h> /* mkdir, readdir */
 #include <errno.h>
 
 static char seedChars[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
@@ -136,3 +143,107 @@
 
        return(NULL);
 }
+
+/* recursive helper function to delete a directory */
+static char* deletedir(char *dir) {
+       DIR *d;
+       struct dirent *e;
+       struct stat s;
+       char buf[8096];
+       char path[PATHLENGTH + 1];
+
+       d = opendir(dir);
+       if (d == NULL) {
+               /* silently return if we cannot find the directory; it's
+                * probably already deleted */
+               if (errno == ENOENT)
+                       return(NULL);
+               snprintf(buf, sizeof(buf), "unable to open dir %s: %s",
+                               dir, strerror(errno));
+               return(strdup(buf));
+       }
+       while ((e = readdir(d)) != NULL) {
+               snprintf(path, sizeof(path), "%s/%s", dir, e->d_name);
+               if (stat(path, &s) == -1) {
+                       snprintf(buf, sizeof(buf), "unable to stat file %s: %s",
+                                       path, strerror(errno));
+                       closedir(d);
+                       return(strdup(buf));
+               }
+
+               if (S_ISREG(s.st_mode) || S_ISLNK(s.st_mode)) {
+                       if (unlink(path) == -1) {
+                               snprintf(buf, sizeof(buf), "unable to unlink 
file %s: %s",
+                                               path, strerror(errno));
+                               closedir(d);
+                               return(strdup(buf));
+                       }
+               } else if (S_ISDIR(s.st_mode)) {
+                       char* er;
+                       /* recurse, ignore . and .. */
+                       if (strcmp(e->d_name, ".") != 0 &&
+                                       strcmp(e->d_name, "..") != 0 &&
+                                       (er = deletedir(path)) != NULL)
+                       {
+                               closedir(d);
+                               return(er);
+                       }
+               } else {
+                       /* fifos, block, char devices etc, we don't do */
+                       snprintf(buf, sizeof(buf), "not a regular file: %s", 
path);
+                       closedir(d);
+                       return(strdup(buf));
+               }
+       }
+       closedir(d);
+       if (rmdir(dir) == -1) {
+               snprintf(buf, sizeof(buf), "unable to remove directory %s: %s",
+                               dir, strerror(errno));
+               return(strdup(buf));
+       }
+
+       return(NULL);
+}
+
+char* db_destroy(char* dbname) {
+       sabdb* stats;
+       char* e;
+       char buf[8096];
+
+       if (dbname[0] == '\0')
+               return(strdup("database name should not be an empty string"));
+
+       /* the argument is the database to destroy, see what Sabaoth can
+        * tell us about it */
+       if ((e = SABAOTHgetStatus(&stats, dbname)) != MAL_SUCCEED) {
+               snprintf(buf, sizeof(buf), "internal error: %s", e);
+               GDKfree(e);
+               return(strdup(buf));
+       }
+
+       if (stats != NULL) {
+               if (stats->state == SABdbRunning) {
+                       snprintf(buf, sizeof(buf), "database '%s' is still 
running, "
+                                       "please stop database first", dbname);
+                       SABAOTHfreeStatus(&stats);
+                       return(strdup(buf));
+               }
+
+               /* annoyingly we have to delete file by file, and
+                * directories recursively... */
+               if ((e = deletedir(stats->path)) != NULL) {
+                       snprintf(buf, sizeof(buf), "failed to destroy '%s': %s",
+                                       dbname, e);
+                       GDKfree(e);
+                       SABAOTHfreeStatus(&stats);
+                       return(strdup(buf));
+               }
+               SABAOTHfreeStatus(&stats);
+       } else {
+               snprintf(buf, sizeof(buf), "no such database: %s", dbname);
+               return(strdup(buf));
+       }
+
+       return(NULL);
+}
+

U monetdb.c
Index: monetdb.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/monetdb.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- monetdb.c   20 Aug 2009 10:46:50 -0000      1.30
+++ monetdb.c   20 Aug 2009 11:38:29 -0000      1.31
@@ -203,13 +203,13 @@
        /* walk through the arguments and hunt for "options" */
        for (i = 1; i < argc; i++) {
                if (strcmp(argv[i], "--") == 0) {
-                       argv[i][0] = '\0';
+                       argv[i] = NULL;
                        break;
                }
                if (argv[i][0] == '-') {
                        if (argv[i][1] == 'l') {
                                maintenance = 1;
-                               argv[i][0] = '\0';
+                               argv[i] = NULL;
                        } else {
                                fprintf(stderr, "create: unknown option: %s\n", 
argv[i]);
                                command_help(argc + 1, &argv[-1]);
@@ -220,7 +220,12 @@
 
        /* do for each listed database */
        for (i = 1; i < argc; i++) {
-               char *ret = db_create(dbfarm, argv[i], maintenance);
+               char *ret;
+               
+               if (argv[i] == NULL)
+                       continue;
+
+               ret = db_create(dbfarm, argv[i], maintenance);
 
                if (ret == NULL) {
                        printf("successfully created database '%s'%s\n", 
argv[i],
@@ -243,7 +248,82 @@
        exit(state);
 }
 
-#include "monetdb_destroy.c"
+static void
+command_destroy(int argc, char *argv[])
+{
+       int i;
+       int force = 0;    /* ask for confirmation */
+       int state = 0;    /* return status */
+       int hadwork = 0;  /* did we do anything useful? */
+
+       if (argc == 1) {
+               /* print help message for this command */
+               command_help(argc + 1, &argv[-1]);
+               exit(1);
+       }
+
+       /* walk through the arguments and hunt for "options" */
+       for (i = 1; i < argc; i++) {
+               if (strcmp(argv[i], "--") == 0) {
+                       argv[i] = NULL;
+                       break;
+               }
+               if (argv[i][0] == '-') {
+                       if (argv[i][1] == 'f') {
+                               force = 1;
+                               argv[i] = NULL;
+                       } else {
+                               fprintf(stderr, "destroy: unknown option: 
%s\n", argv[i]);
+                               command_help(argc + 1, &argv[-1]);
+                               exit(1);
+                       }
+               }
+       }
+
+       if (force == 0) {
+               char answ;
+               printf("you are about to remove database%s ", argc > 2 ? "s" : 
"");
+               for (i = 1; i < argc; i++)
+                       printf("%s'%s'", i > 1 ? ", " : "", argv[i]);
+               printf("\nALL data in %s will be lost, are you sure? [y/N] ",
+                               argc > 2 ? "these databases" : "this database");
+               if (scanf("%c", &answ) >= 1 &&
+                               (answ == 'y' || answ == 'Y'))
+               {
+                       /* do it! */
+               } else {
+                       printf("aborted\n");
+                       exit(0);
+               }
+       }
+
+       /* do for each listed database */
+       for (i = 1; i < argc; i++) {
+               char* ret;
+               
+               if (argv[i] == NULL)
+                       continue;
+
+               ret = db_destroy(argv[i]);
+
+               if (ret == NULL) {
+                       printf("successfully destroyed database '%s'\n", 
argv[i]);
+               } else {
+                       fprintf(stderr, "destroy: %s\n", ret);
+                       free(ret);
+                       state |= 1;
+               }
+
+               hadwork = 1;
+       }
+
+       if (hadwork == 0) {
+               command_help(2, &argv[-1]);
+               state |= 1;
+       }
+       exit(state);
+}
+
 #include "monetdb_lock.c"
 #include "monetdb_release.c"
 

U database.h
Index: database.h
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/database.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- database.h  20 Aug 2009 10:46:50 -0000      1.1
+++ database.h  20 Aug 2009 11:38:29 -0000      1.2
@@ -21,5 +21,6 @@
 #define _SEEN_DATABASE_H 1
 
 char* db_create(char *dbfarm, char* dbname, char maintenance);
+char* db_destroy(char* dbname);
 
 #endif


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Monetdb-sql-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-sql-checkins

Reply via email to