|
| 1 | +import { cpSync, existsSync, mkdirSync, readdirSync, rmSync, statSync, unlinkSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import type { Logger } from '../common/logger'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Copies CodeQL databases from `GitHub.vscode-codeql` extension storage |
| 7 | + * to a managed directory, removing `.lock` files that the CodeQL query |
| 8 | + * server creates in `<dataset>/default/cache/`. |
| 9 | + * |
| 10 | + * This avoids lock contention when the `ql-mcp` server runs CLI commands |
| 11 | + * against databases that are simultaneously registered by the |
| 12 | + * `vscode-codeql` query server. |
| 13 | + * |
| 14 | + * Each database is identified by its top-level directory name (which |
| 15 | + * contains `codeql-database.yml`). A database is only re-copied when its |
| 16 | + * source has been modified more recently than the existing copy. |
| 17 | + */ |
| 18 | +export class DatabaseCopier { |
| 19 | + constructor( |
| 20 | + private readonly destinationBase: string, |
| 21 | + private readonly logger: Logger, |
| 22 | + ) {} |
| 23 | + |
| 24 | + /** |
| 25 | + * Synchronise databases from one or more source directories into the |
| 26 | + * managed destination. Only databases that are newer than the existing |
| 27 | + * copy (or missing entirely) are re-copied. |
| 28 | + * |
| 29 | + * @returns The list of database paths in the managed destination that |
| 30 | + * are ready for use (absolute paths). |
| 31 | + */ |
| 32 | + syncAll(sourceDirs: string[]): string[] { |
| 33 | + mkdirSync(this.destinationBase, { recursive: true }); |
| 34 | + |
| 35 | + const copied: string[] = []; |
| 36 | + |
| 37 | + for (const sourceDir of sourceDirs) { |
| 38 | + if (!existsSync(sourceDir)) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + let entries: string[]; |
| 43 | + try { |
| 44 | + entries = readdirSync(sourceDir); |
| 45 | + } catch { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + for (const entry of entries) { |
| 50 | + const srcDbPath = join(sourceDir, entry); |
| 51 | + if (!isCodeQLDatabase(srcDbPath)) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + const destDbPath = join(this.destinationBase, entry); |
| 56 | + |
| 57 | + if (this.needsCopy(srcDbPath, destDbPath)) { |
| 58 | + this.copyDatabase(srcDbPath, destDbPath); |
| 59 | + } |
| 60 | + |
| 61 | + copied.push(destDbPath); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return copied; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Copy a single database directory, then strip any `.lock` files that |
| 70 | + * the CodeQL query server may have left behind. |
| 71 | + */ |
| 72 | + private copyDatabase(src: string, dest: string): void { |
| 73 | + this.logger.info(`Copying database ${src} → ${dest}`); |
| 74 | + try { |
| 75 | + // Remove stale destination if present |
| 76 | + if (existsSync(dest)) { |
| 77 | + rmSync(dest, { recursive: true, force: true }); |
| 78 | + } |
| 79 | + |
| 80 | + cpSync(src, dest, { recursive: true }); |
| 81 | + removeLockFiles(dest); |
| 82 | + this.logger.info(`Database copied successfully: ${dest}`); |
| 83 | + } catch (err) { |
| 84 | + this.logger.error( |
| 85 | + `Failed to copy database ${src}: ${err instanceof Error ? err.message : String(err)}`, |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * A copy is needed when the destination does not exist, or the source |
| 92 | + * `codeql-database.yml` is newer than the destination's. |
| 93 | + */ |
| 94 | + private needsCopy(src: string, dest: string): boolean { |
| 95 | + const destYml = join(dest, 'codeql-database.yml'); |
| 96 | + if (!existsSync(destYml)) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + const srcYml = join(src, 'codeql-database.yml'); |
| 101 | + try { |
| 102 | + const srcMtime = statSync(srcYml).mtimeMs; |
| 103 | + const destMtime = statSync(destYml).mtimeMs; |
| 104 | + return srcMtime > destMtime; |
| 105 | + } catch { |
| 106 | + // If stat fails, re-copy to be safe |
| 107 | + return true; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** Check whether a directory looks like a CodeQL database. */ |
| 113 | +function isCodeQLDatabase(dirPath: string): boolean { |
| 114 | + try { |
| 115 | + return statSync(dirPath).isDirectory() && existsSync(join(dirPath, 'codeql-database.yml')); |
| 116 | + } catch { |
| 117 | + return false; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Recursively remove all `.lock` files under the given directory. |
| 123 | + * These are empty sentinel files created by the CodeQL query server in |
| 124 | + * `<dataset>/default/cache/.lock`. |
| 125 | + */ |
| 126 | +function removeLockFiles(dir: string): void { |
| 127 | + let entries: string[]; |
| 128 | + try { |
| 129 | + entries = readdirSync(dir); |
| 130 | + } catch { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + for (const entry of entries) { |
| 135 | + const fullPath = join(dir, entry); |
| 136 | + try { |
| 137 | + const stat = statSync(fullPath); |
| 138 | + if (stat.isDirectory()) { |
| 139 | + removeLockFiles(fullPath); |
| 140 | + } else if (entry === '.lock') { |
| 141 | + unlinkSync(fullPath); |
| 142 | + } |
| 143 | + } catch { |
| 144 | + // Best-effort removal |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments