-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_with_db_merge.sh
More file actions
132 lines (110 loc) · 3.55 KB
/
build_with_db_merge.sh
File metadata and controls
132 lines (110 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
DB_FILE="telegram_clone.db"
OLD_KEY_FILE="build/db_key.h"
BACKUP_DIR="db_backup"
echo "=== Database Merge Build Script ==="
# Check if database exists
if [ -f "$DB_FILE" ]; then
echo "✓ Existing database found: $DB_FILE"
# Check if old key exists
if [ -f "$OLD_KEY_FILE" ]; then
echo "✓ Previous database key found"
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Backup current database
cp "$DB_FILE" "$BACKUP_DIR/telegram_clone_backup_$(date +%Y%m%d_%H%M%S).db"
echo "✓ Database backed up to $BACKUP_DIR/"
# Test database access with old key
echo "Testing database access with existing key..."
# Create test program
cat > build/test_db_access.c << 'EOF'
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include "../include/db_security.h"
int main() {
sqlite3 *db;
char password[256];
char pragma_cmd[512];
get_db_password(password, sizeof(password));
int rc = sqlite3_open("telegram_clone.db", &db);
if (rc) {
printf("ERROR: Cannot open database: %s\n", sqlite3_errmsg(db));
return 1;
}
snprintf(pragma_cmd, sizeof(pragma_cmd), "PRAGMA key = '%s';", password);
rc = sqlite3_exec(db, pragma_cmd, 0, 0, 0);
if (rc != SQLITE_OK) {
printf("ERROR: Key verification failed\n");
sqlite3_close(db);
return 1;
}
// Test query
sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db, "SELECT COUNT(*) FROM users;", -1, &stmt, NULL);
if (rc == SQLITE_OK) {
printf("SUCCESS: Database access granted\n");
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
} else {
printf("ERROR: Database query failed\n");
sqlite3_close(db);
return 1;
}
}
EOF
# Compile and test
gcc -I include -I build -o build/test_db_access build/test_db_access.c -lsqlite3
if ./build/test_db_access; then
echo "✓ Database access verified - keeping existing key"
KEEP_OLD_KEY=true
else
echo "✗ Database access failed - will generate new key"
KEEP_OLD_KEY=false
fi
# Cleanup test files
rm -f build/test_db_access build/test_db_access.c build/test_db_access.exe
else
echo "✗ No previous key found - will generate new key"
KEEP_OLD_KEY=false
fi
else
echo "ℹ No existing database - will create new one"
KEEP_OLD_KEY=false
fi
# Build process
echo ""
echo "=== Starting Build Process ==="
if [ "$KEEP_OLD_KEY" = true ]; then
echo "Building with existing database key..."
# Build without regenerating key
make install-libmingw32
mkdir -p build
# Skip gen-db-key step and build directly
make $(make -n all | grep -v gen-db-key | tail -1)
else
echo "Building with new database key..."
# Normal build process
make all
fi
if [ $? -eq 0 ]; then
echo ""
echo "=== Build Successful ==="
if [ "$KEEP_OLD_KEY" = true ]; then
echo "✓ Database preserved with existing access"
echo "✓ All existing data remains accessible"
else
echo "✓ New database key generated"
if [ -f "$DB_FILE" ]; then
echo "⚠ Previous database will need re-initialization"
fi
fi
echo ""
echo "Database backups available in: $BACKUP_DIR/"
echo "Run './build/telegram_clone' to start the server"
else
echo ""
echo "=== Build Failed ==="
exit 1
fi