Debian Codegraph @

scripts/storage/cdb2sql.pl

raw
1 #!/usr/bin/perl
2
3 $bncdb = "bncdb";
4
5 if (scalar(@ARGV) != 1) {
6 &usage;
7 exit;
8 }
9
10 &header;
11
12 # remove a trailing /
13 $dirpath = $ARGV[0];
14 $dirpath =~ s!(.*)/$!$1!;
15
16 opendir FILEDIR, $dirpath or die "Error opening filedir!\n";
17
18 while ($filename = readdir FILEDIR) {
19 if ($filename =~ m/^\./) { next; } #ignore . and ..
20
21 if ( ! (($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
22 $atime,$mtime,$ctime,$blksize,$blocks) = stat "$dirpath/$filename" )) {
23 print STDERR "Error stat-ing the file $pathdir/$filename!\n" ; next; }
24
25 $type = ($mode & 070000) >> 12;
26
27 if ($type != 0) {
28 print STDERR "File $dirpath/$filename its not regular\n";
29 next;
30 }
31 convert_cdb2sql("$dirpath/$filename");
32 }
33
34 closedir FILEDIR;
35
36 sub convert_cdb2sql {
37 my $filen = shift;
38 my ($userid, $count, $alist);
39
40 $pid = open(FILE, "$bncdb -d \"".$filen."\" |");
41 print STDERR "Converting file $filen ... ";
42
43 $count = 0;
44 $userid = ""; undef @alist;
45 while($line = <FILE>) {
46 if ($line =~ m/:.*->.*/) {
47 $line =~ m/^\+([0-9]+),([0-9]+):([A-Za-z0-9]+)\\(.*)$/;
48
49 # key length and value length
50 $keylen = $1; $vallen = $2;
51
52 $tab = $3;
53 $tab =~ tr/A-Z/a-z/;
54 $str = $4;
55
56 # skip Team as we now have another team structure
57 if ($tab =~ m/^team$/i) {
58 print STDERR "WARNING: skipping Team information!\n";
59 next;
60 } elsif ($tab =~ m/^\s*$/) {
61 print STDERR "WARNING: skipping empty tab!\n";
62 next;
63 }
64
65 $alist[$count]{tab} = $tab;
66 $alist[$count]{col} = &escape_key(substr($str, 0, $keylen - length($tab) - 1));
67 $alist[$count]{val} = substr($str, $keylen - length($tab) + 1);
68
69 if ($alist[$count]{col} =~ m!userid$!) {
70 $userid = $alist[$count]{val};
71 }
72 $count++;
73 }
74 }
75 if ($userid ne "") {
76 &db_set($dbh, $userid, $alist);
77 }
78 close FILE;
79 waitpid $pid, 0;
80 print STDERR "done\n";
81 }
82
83 sub header {
84 print STDERR "Account files to db accounts converting tool.\n";
85 print STDERR " Copyright (C) 2002,2005 Dizzy (dizzy\@roedu.net)\n";
86 print STDERR " Player Versus Player Gaming Network (www.pvpgn.org)\n\n";
87 }
88
89 sub usage {
90 &header;
91 print STDERR "Usage:\n\n\tcdb2sql.pl <filedir>\n\n";
92 print STDERR "\t <filedir>\t: directory with the account files\n";
93 }
94
95 sub db_set {
96 my $dbh = shift;
97 my $userid = shift;
98 my $alist = shift;
99 my ($i);
100
101 print("INSERT INTO BNET (uid) VALUES ($userid);\n");
102 print("INSERT INTO profile (uid) VALUES ($userid);\n");
103 print("INSERT INTO Record (uid) VALUES ($userid);\n");
104 print("INSERT INTO friend (uid) VALUES ($userid);\n");
105 for($i=0; $i<scalar(@alist);$i++) {
106 my $tab = $alist[$i]{tab};
107 my $col = $alist[$i]{col};
108 my $val = $alist[$i]{val};
109
110 $nval = &add_slashes($val);
111 print("ALTER TABLE $tab ADD COLUMN '$col' VARCHAR(128);\n");
112 print("UPDATE $tab SET '$col' = '$nval' WHERE uid = $userid;\n");
113 }
114 }
115
116 sub add_slashes {
117 my $str = shift;
118
119 $str =~ s/\'/\'\'/g;
120
121 return $str;
122 }
123
124 sub escape_key {
125 my $str = shift;
126
127 $str =~ tr/A-Z/a-z/;
128 $str =~ s/[^0-9a-zA-Z]/_/g;
129 return $str;
130 }