[LON-CAPA-cvs] cvs: loncom /debugging_tools db_copy.pl dump_db.c

albertel lon-capa-cvs@mail.lon-capa.org
Thu, 24 Aug 2006 22:46:35 -0000


albertel		Thu Aug 24 18:46:35 2006 EDT

  Added files:                 
    /loncom/debugging_tools	db_copy.pl 

  Modified files:              
    /loncom/debugging_tools	dump_db.c 
  Log:
  - adding scripts to allow morphing 32bit to 64 bit db
  
  
Index: loncom/debugging_tools/dump_db.c
diff -u loncom/debugging_tools/dump_db.c:1.1 loncom/debugging_tools/dump_db.c:1.2
--- loncom/debugging_tools/dump_db.c:1.1	Mon Jun 19 17:21:47 2006
+++ loncom/debugging_tools/dump_db.c	Thu Aug 24 18:46:30 2006
@@ -4,6 +4,42 @@
 #include <gdbm.h>
 #include <errno.h>
 #include <string.h>
+#include <ctype.h>
+
+static const char c2x_table[] = "0123456789abcdef";
+
+void c2x(unsigned what, unsigned char prefix, char *where,
+	 unsigned int offset)
+{
+    where[offset] = prefix;
+    where[offset+1] = c2x_table[what >> 4];
+    where[offset+2] = c2x_table[what & 0xf];
+}
+
+
+datum* http_escape(datum* data)
+{
+  datum *escaped_data;
+  escaped_data = malloc(sizeof(datum));
+  escaped_data->dsize =  (3 * data->dsize) + 3;
+  escaped_data->dptr  = malloc(escaped_data->dsize);
+  unsigned int c,i,j;
+
+  j=0;
+  for (i=0;i<data->dsize;i++) {
+    c = data->dptr[i];
+    if (!isalnum(c)) {
+      c2x(c, '%', escaped_data->dptr, j);
+      j+=3;
+    } else {
+      escaped_data->dptr[j] = c;
+      j++;
+    }
+  }
+  escaped_data->dsize=j;
+  return escaped_data;
+}
+
 
 void usage()
 {
@@ -14,22 +50,32 @@
 {
   GDBM_FILE db;
   datum key, nextkey, content;
+  datum *escaped_key, *escaped_content;
   db = gdbm_open(filename, 0, GDBM_READER, 0, 0);
 
   if (db == NULL) {
-    printf("Unable to open db %s beacsue of %s (%d) -- %s (%d)\n",filename,
-	   gdbm_strerror(gdbm_errno),gdbm_errno,strerror(errno),errno);
+    printf("ERROR:Unable to open db %s because of %s (%d) -- %s (%d)\n",
+	   filename,gdbm_strerror(gdbm_errno),gdbm_errno,strerror(errno),
+	   errno);
     return;
   }
   key = gdbm_firstkey(db);
 
   while ( key.dptr ) {
     content = gdbm_fetch(db, key);
-    fwrite(key.dptr, key.dsize, sizeof(char), stdout);
+    escaped_key     = http_escape(&key);
+    escaped_content = http_escape(&content);
+    fwrite(escaped_key->dptr,
+	   escaped_key->dsize, sizeof(char), stdout);
     printf(" -> ");
-    fwrite(content.dptr, content.dsize, sizeof(char), stdout);
+    fwrite(escaped_content->dptr,
+	   escaped_content->dsize, sizeof(char), stdout);
     printf("\n");
     free(content.dptr);
+    free(escaped_content->dptr);
+    free(escaped_content);
+    free(escaped_key->dptr);
+    free(escaped_key);
     nextkey = gdbm_nextkey(db, key);
     free(key.dptr);
     key = nextkey;
@@ -61,3 +107,4 @@
   return 0;
 }
 
+

Index: loncom/debugging_tools/db_copy.pl
+++ loncom/debugging_tools/db_copy.pl
#!/usr/bin/perl


use strict;
use warnings;
use lib '/home/httpd/lib/perl';
use GDBM_File;
use File::Find;
use LONCAPA;
use LONCAPA::Configuration;
use Cwd;

my $dump_db = './dump_db';
my $dir = './test';

my  %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};

{
    my $straight;
    sub lock_db {
	my ($fname) = @_;
	my $dbref;
	$fname = &Cwd::abs_path($fname);
	if ($fname =~ m/^\Q$perlvar{'lonUsersDir'}\E/) {
	    $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
	    $straight=0;
	} else {
	    if (tie(my %db,'GDBM_File',$fname,&GDBM_READER(),0640)) {
		$dbref = \%db;
	    }
	    $straight=1;
	}
	return $dbref;
    }

    sub unlock_db {
	my ($dbref) = @_;
	if ($straight) {
	    untie($dbref);
	} else {
	    &LONCAPA::locking_hash_untie($dbref);
	}
    }
}

sub process_db {
    return if ($_!~m/\.db$/);
    my $file = $_;
    my $dbref =&lock_db($file);
    print("attempting $dbref\n");
    my %newdb;
    my $new_file = $file.'.new';
    if (!tie(%newdb,'GDBM_File',$new_file,&GDBM_WRCREAT(),0640)) {
	die("unable to create new db $new_file $! $@");
    }
    open(my $fh,"$dump_db -f $file|");
    while (my $entry = <$fh>) {
	chomp($entry);
	if ($entry =~/^ERROR:/) {
	    die($entry);
	}
	my ($key,$value) = split(' -> ',$entry,2);
	$newdb{&unescape($key)} = &unescape($value);
    }
    print("finishing $dbref\n");
    untie(%newdb);
    untie($dbref);
    system("/bin/mv $file $file.old");
    system("/bin/mv $file.new $file");
    &unlock_db($dbref);
}

sub main {
    find(
	 {
	     no_chdir   => 1,
	     wanted     => \&process_db,
	 }, 
	 $dir
	 #$perlvar->{'lonUsersDir'}
	 );
}

&main();