-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmark_db.cpp
More file actions
553 lines (462 loc) · 16.4 KB
/
mark_db.cpp
File metadata and controls
553 lines (462 loc) · 16.4 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright (c) 2025 Michael Shebanow and Sunil William Savkar
*
* Original work:
* Copyright (c) 1991 Sunil William Savkar. All rights reserved.
*/
#include "mark_db.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <sys/stat.h>
#include <sqlite3.h>
#include <sys/stat.h>
#include <unistd.h>
// MarkDatabase implementation
MarkDatabase::MarkDatabase() : db(nullptr), maxMarkSize(0) {
}
MarkDatabase::~MarkDatabase() {
if (db) {
sqlite3_close(db);
}
}
bool MarkDatabase::isValidMarkName(const std::string& mark) {
if (mark.empty()) return false;
for (char c : mark) {
if (!std::isalnum(c) && c != '_' && c != '.') {
return false;
}
}
return true;
}
std::string MarkDatabase::escapePath(const std::string& path) {
std::string escaped;
for (char c : path) {
if (c == ' ') {
escaped += "\\ ";
} else if (c == '\\') {
escaped += "\\\\";
} else {
escaped += c;
}
}
return escaped;
}
std::string MarkDatabase::unescapePath(const std::string& path) {
std::string unescaped;
for (size_t i = 0; i < path.length(); ++i) {
if (path[i] == '\\' && i + 1 < path.length()) {
if (path[i + 1] == ' ') {
unescaped += ' ';
i++;
} else if (path[i + 1] == '\\') {
unescaped += '\\';
i++;
} else {
unescaped += path[i];
}
} else {
unescaped += path[i];
}
}
return unescaped;
}
bool MarkDatabase::createSchema() {
const char* sql =
"CREATE TABLE IF NOT EXISTS marks ("
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
" name TEXT UNIQUE NOT NULL,"
" path TEXT NOT NULL,"
" created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,"
" updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
");"
"CREATE INDEX IF NOT EXISTS idx_marks_name ON marks(name);";
char* errMsg = nullptr;
int rc = sqlite3_exec(db, sql, nullptr, nullptr, &errMsg);
if (rc != SQLITE_OK) {
std::cerr << "createSchema: SQL error: " << (errMsg ? errMsg : "unknown") << std::endl;
sqlite3_free(errMsg);
return false;
}
return true;
}
bool MarkDatabase::loadMarks() {
const char* sql = "SELECT name, path FROM marks ORDER BY name";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
std::cerr << "loadMarks: Failed to prepare statement" << std::endl;
return false;
}
maxMarkSize = 0;
while (sqlite3_step(stmt) == SQLITE_ROW) {
const char* name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
const char* path = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
if (name && path) {
std::string markName(name);
std::string markPath(path);
if (markName.length() > static_cast<size_t>(maxMarkSize)) {
maxMarkSize = markName.length();
}
}
}
sqlite3_finalize(stmt);
return true;
}
void MarkDatabase::sortMarks() {
// Marks are sorted in SQL queries, no need to sort in memory
}
bool MarkDatabase::initialize(const std::string& directory, bool createIfMissing) {
// Ensure directory exists
struct stat st;
if (stat(directory.c_str(), &st) != 0) {
// Directory doesn't exist, try to create it
std::string cmd = "mkdir -p \"" + directory + "\"";
if (system(cmd.c_str()) != 0) {
std::cerr << "initialize: Failed to create directory: " << directory << std::endl;
return false;
}
} else if (!S_ISDIR(st.st_mode)) {
std::cerr << "initialize: Path exists but is not a directory: " << directory << std::endl;
return false;
}
// Build full path: directory/.mark_db
dbPath = directory;
if (dbPath.back() != '/') {
dbPath += "/";
}
dbPath += ".mark_db";
// Check if file exists
bool exists = (access(dbPath.c_str(), F_OK) == 0);
if (!exists && !createIfMissing) {
// Database doesn't exist and we're not allowed to create it
return false;
}
// Open or create database
int rc = sqlite3_open(dbPath.c_str(), &db);
if (rc != SQLITE_OK) {
std::cerr << "initialize: Cannot open database: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
db = nullptr;
return false;
}
// Create schema if new database
if (!exists || createIfMissing) {
if (!createSchema()) {
sqlite3_close(db);
db = nullptr;
return false;
}
}
// Load marks to calculate maxMarkSize
loadMarks();
return true;
}
bool MarkDatabase::addMark(const std::string& mark, const std::string& path) {
if (!isValidMarkName(mark)) {
std::cerr << "addMark: mark must be alphanumeric" << std::endl;
return false;
}
if (!db) {
std::cerr << "addMark: Database not initialized" << std::endl;
return false;
}
// Use INSERT OR REPLACE to handle updates
const char* sql = "INSERT OR REPLACE INTO marks (name, path, updated_at) VALUES (?, ?, CURRENT_TIMESTAMP)";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
std::cerr << "addMark: Failed to prepare statement" << std::endl;
return false;
}
sqlite3_bind_text(stmt, 1, mark.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, path.c_str(), -1, SQLITE_STATIC);
int rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
std::cerr << "addMark: Failed to execute: " << sqlite3_errmsg(db) << std::endl;
return false;
}
// Update maxMarkSize
if (mark.length() > static_cast<size_t>(maxMarkSize)) {
maxMarkSize = mark.length();
}
std::cerr << "addMark: mark \"" << mark << "\" (" << path << ") set" << std::endl;
return true;
}
bool MarkDatabase::removeMark(const std::string& mark) {
if (!db) {
std::cerr << "removeMark: Database not initialized" << std::endl;
return false;
}
// First check if mark exists and get its path for the message
const char* selectSql = "SELECT path FROM marks WHERE name = ?";
sqlite3_stmt* selectStmt;
if (sqlite3_prepare_v2(db, selectSql, -1, &selectStmt, nullptr) != SQLITE_OK) {
std::cerr << "removeMark: Failed to prepare select statement" << std::endl;
return false;
}
sqlite3_bind_text(selectStmt, 1, mark.c_str(), -1, SQLITE_STATIC);
std::string markPath;
bool found = false;
if (sqlite3_step(selectStmt) == SQLITE_ROW) {
const char* path = reinterpret_cast<const char*>(sqlite3_column_text(selectStmt, 0));
if (path) {
markPath = path;
found = true;
}
}
sqlite3_finalize(selectStmt);
if (!found) {
std::cerr << "removeMark: mark \"" << mark << "\" not found" << std::endl;
return false;
}
// Delete the mark
const char* deleteSql = "DELETE FROM marks WHERE name = ?";
sqlite3_stmt* deleteStmt;
if (sqlite3_prepare_v2(db, deleteSql, -1, &deleteStmt, nullptr) != SQLITE_OK) {
std::cerr << "removeMark: Failed to prepare delete statement" << std::endl;
return false;
}
sqlite3_bind_text(deleteStmt, 1, mark.c_str(), -1, SQLITE_STATIC);
int rc = sqlite3_step(deleteStmt);
sqlite3_finalize(deleteStmt);
if (rc != SQLITE_DONE) {
std::cerr << "removeMark: Failed to execute: " << sqlite3_errmsg(db) << std::endl;
return false;
}
std::cerr << "removeMark: mark \"" << mark << "\" (" << markPath << ") removed" << std::endl;
return true;
}
bool MarkDatabase::resetMarks() {
if (!db) {
std::cerr << "resetMarks: Database not initialized" << std::endl;
return false;
}
const char* sql = "DELETE FROM marks";
char* errMsg = nullptr;
int rc = sqlite3_exec(db, sql, nullptr, nullptr, &errMsg);
if (rc != SQLITE_OK) {
std::cerr << "resetMarks: SQL error: " << (errMsg ? errMsg : "unknown") << std::endl;
sqlite3_free(errMsg);
return false;
}
maxMarkSize = 0;
return true;
}
bool MarkDatabase::refreshMarks() {
// For SQLite, refresh just means reloading (no-op since we query directly)
return true;
}
bool MarkDatabase::listMarks() {
if (!db) {
std::cout << std::endl;
return true;
}
const char* sql = "SELECT name, path FROM marks ORDER BY name";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
std::cout << std::endl;
return true;
}
std::vector<std::pair<std::string, std::string>> markList;
while (sqlite3_step(stmt) == SQLITE_ROW) {
const char* name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
const char* path = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
if (name && path) {
markList.push_back({std::string(name), std::string(path)});
}
}
sqlite3_finalize(stmt);
if (markList.empty()) {
std::cout << std::endl;
return true;
}
std::cout << "MARK";
for (int i = 0; i < maxMarkSize + 1; i++) std::cout << " ";
std::cout << "PATH" << std::endl;
std::cout << "----";
for (int i = 0; i < maxMarkSize + 1; i++) std::cout << " ";
std::cout << "----" << std::endl;
for (const auto& entry : markList) {
std::cout << entry.first << " ";
int spaces = maxMarkSize - entry.first.length() + 3;
for (int i = 0; i < spaces; i++) std::cout << "_";
std::cout << " " << entry.second << std::endl;
}
return true;
}
std::string MarkDatabase::getMarkPath(const std::string& mark) const {
if (!db) {
return "";
}
const char* sql = "SELECT path FROM marks WHERE name = ?";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
return "";
}
sqlite3_bind_text(stmt, 1, mark.c_str(), -1, SQLITE_STATIC);
std::string result;
if (sqlite3_step(stmt) == SQLITE_ROW) {
const char* path = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
if (path) {
result = path;
}
}
sqlite3_finalize(stmt);
return result;
}
// MarkDatabaseManager implementation
MarkDatabaseManager::MarkDatabaseManager() {
}
MarkDatabaseManager::~MarkDatabaseManager() {
}
std::string MarkDatabaseManager::expandPath(const std::string& path) {
std::string expanded = path;
// Expand ~ to home directory
if (expanded[0] == '~') {
const char* home = std::getenv("HOME");
if (home) {
expanded = std::string(home) + expanded.substr(1);
}
}
return expanded;
}
void MarkDatabaseManager::parseMarkPath(const std::string& markPath) {
databases.clear();
if (markPath.empty()) {
// Fallback to MARK_DIR for backward compatibility
const char* markDir = std::getenv("MARK_DIR");
if (markDir) {
DatabaseEntry entry;
entry.alias = "";
entry.path = expandPath(markDir);
entry.db = std::make_unique<MarkDatabase>();
// Auto-create default database if it doesn't exist
if (!entry.db->initialize(entry.path, true)) {
std::cerr << "Warning: Failed to initialize database in " << entry.path << std::endl;
return;
}
databases.push_back(std::move(entry));
}
return;
}
// Parse semicolon-separated list
std::istringstream iss(markPath);
std::string item;
while (std::getline(iss, item, ';')) {
// Trim whitespace
item.erase(0, item.find_first_not_of(" \t"));
item.erase(item.find_last_not_of(" \t") + 1);
if (item.empty()) continue;
DatabaseEntry entry;
// Check for alias=path format
size_t eqPos = item.find('=');
if (eqPos != std::string::npos) {
entry.alias = item.substr(0, eqPos);
entry.path = expandPath(item.substr(eqPos + 1));
} else {
// Direct path (directory)
entry.alias = "";
entry.path = expandPath(item);
}
entry.db = std::make_unique<MarkDatabase>();
// Auto-create databases from MARK_PATH if they don't exist
if (!entry.db->initialize(entry.path, true)) {
std::cerr << "Warning: Failed to initialize database in " << entry.path << std::endl;
continue;
}
databases.push_back(std::move(entry));
}
}
bool MarkDatabaseManager::initialize() {
const char* markPath = std::getenv("MARK_PATH");
if (markPath) {
parseMarkPath(markPath);
} else {
// Fallback: use MARK_DIR and MARK_REMOTE_DIR for backward compatibility
const char* markDir = std::getenv("MARK_DIR");
if (markDir) {
DatabaseEntry entry;
entry.alias = "local";
entry.path = expandPath(markDir);
entry.db = std::make_unique<MarkDatabase>();
// Auto-create local database if it doesn't exist
if (!entry.db->initialize(entry.path, true)) {
std::cerr << "Warning: Failed to initialize local database in " << entry.path << std::endl;
} else {
databases.push_back(std::move(entry));
}
}
const char* remoteDir = std::getenv("MARK_REMOTE_DIR");
if (remoteDir) {
DatabaseEntry entry;
entry.alias = "cloud";
entry.path = expandPath(remoteDir);
entry.db = std::make_unique<MarkDatabase>();
// Auto-create remote database if it doesn't exist
if (!entry.db->initialize(entry.path, true)) {
std::cerr << "Warning: Failed to initialize remote database in " << entry.path << std::endl;
} else {
databases.push_back(std::move(entry));
}
}
}
return !databases.empty();
}
MarkDatabase* MarkDatabaseManager::findDatabase(const std::string& dbSpec) {
// Try to find by alias first
for (auto& entry : databases) {
if (entry.alias == dbSpec) {
return entry.db.get();
}
}
// Try to find by path (expanded)
std::string expandedPath = expandPath(dbSpec);
for (auto& entry : databases) {
if (entry.path == expandedPath) {
return entry.db.get();
}
}
// Not found in existing databases - create a new one
// dbSpec is a directory path, will create directory/.mark_db
DatabaseEntry entry;
entry.alias = "";
entry.path = expandPath(dbSpec);
entry.db = std::make_unique<MarkDatabase>();
if (!entry.db->initialize(entry.path, true)) {
std::cerr << "findDatabase: Failed to create database in " << entry.path << std::endl;
return nullptr;
}
databases.push_back(std::move(entry));
return databases.back().db.get();
}
MarkDatabase* MarkDatabaseManager::getDefaultDatabase() {
if (databases.empty()) return nullptr;
return databases[0].db.get();
}
std::string MarkDatabaseManager::findMark(const std::string& markName, bool warnDuplicates) {
std::string firstMatch;
std::vector<std::string> allMatches;
for (const auto& entry : databases) {
std::string path = entry.db->getMarkPath(markName);
if (!path.empty()) {
if (firstMatch.empty()) {
firstMatch = path;
}
if (warnDuplicates) {
std::string dbName = entry.alias.empty() ? entry.path : entry.alias;
allMatches.push_back(dbName + ":" + path);
}
}
}
if (warnDuplicates && allMatches.size() > 1) {
for (size_t i = 1; i < allMatches.size(); i++) {
std::cerr << "setd: warning: duplicate mark \"" << markName
<< "\" found in " << allMatches[i] << std::endl;
}
}
return firstMatch;
}