-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase-copier.ts
More file actions
147 lines (130 loc) · 4.12 KB
/
database-copier.ts
File metadata and controls
147 lines (130 loc) · 4.12 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
import { cpSync, existsSync, mkdirSync, readdirSync, rmSync, statSync, unlinkSync } from 'fs';
import { join } from 'path';
import type { Logger } from '../common/logger';
/**
* Copies CodeQL databases from `GitHub.vscode-codeql` extension storage
* to a managed directory, removing `.lock` files that the CodeQL query
* server creates in `<dataset>/default/cache/`.
*
* This avoids lock contention when the `ql-mcp` server runs CLI commands
* against databases that are simultaneously registered by the
* `vscode-codeql` query server.
*
* Each database is identified by its top-level directory name (which
* contains `codeql-database.yml`). A database is only re-copied when its
* source has been modified more recently than the existing copy.
*/
export class DatabaseCopier {
constructor(
private readonly destinationBase: string,
private readonly logger: Logger,
) {}
/**
* Synchronise databases from one or more source directories into the
* managed destination. Only databases that are newer than the existing
* copy (or missing entirely) are re-copied.
*
* @returns The list of database paths in the managed destination that
* are ready for use (absolute paths).
*/
syncAll(sourceDirs: string[]): string[] {
mkdirSync(this.destinationBase, { recursive: true });
const copied: string[] = [];
for (const sourceDir of sourceDirs) {
if (!existsSync(sourceDir)) {
continue;
}
let entries: string[];
try {
entries = readdirSync(sourceDir);
} catch {
continue;
}
for (const entry of entries) {
const srcDbPath = join(sourceDir, entry);
if (!isCodeQLDatabase(srcDbPath)) {
continue;
}
const destDbPath = join(this.destinationBase, entry);
if (this.needsCopy(srcDbPath, destDbPath)) {
this.copyDatabase(srcDbPath, destDbPath);
}
copied.push(destDbPath);
}
}
return copied;
}
/**
* Copy a single database directory, then strip any `.lock` files that
* the CodeQL query server may have left behind.
*/
private copyDatabase(src: string, dest: string): void {
this.logger.info(`Copying database ${src} → ${dest}`);
try {
// Remove stale destination if present
if (existsSync(dest)) {
rmSync(dest, { recursive: true, force: true });
}
cpSync(src, dest, { recursive: true });
removeLockFiles(dest);
this.logger.info(`Database copied successfully: ${dest}`);
} catch (err) {
this.logger.error(
`Failed to copy database ${src}: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
/**
* A copy is needed when the destination does not exist, or the source
* `codeql-database.yml` is newer than the destination's.
*/
private needsCopy(src: string, dest: string): boolean {
const destYml = join(dest, 'codeql-database.yml');
if (!existsSync(destYml)) {
return true;
}
const srcYml = join(src, 'codeql-database.yml');
try {
const srcMtime = statSync(srcYml).mtimeMs;
const destMtime = statSync(destYml).mtimeMs;
return srcMtime > destMtime;
} catch {
// If stat fails, re-copy to be safe
return true;
}
}
}
/** Check whether a directory looks like a CodeQL database. */
function isCodeQLDatabase(dirPath: string): boolean {
try {
return statSync(dirPath).isDirectory() && existsSync(join(dirPath, 'codeql-database.yml'));
} catch {
return false;
}
}
/**
* Recursively remove all `.lock` files under the given directory.
* These are empty sentinel files created by the CodeQL query server in
* `<dataset>/default/cache/.lock`.
*/
function removeLockFiles(dir: string): void {
let entries: string[];
try {
entries = readdirSync(dir);
} catch {
return;
}
for (const entry of entries) {
const fullPath = join(dir, entry);
try {
const stat = statSync(fullPath);
if (stat.isDirectory()) {
removeLockFiles(fullPath);
} else if (entry === '.lock') {
unlinkSync(fullPath);
}
} catch {
// Best-effort removal
}
}
}