Skip to content

Commit 6fdfade

Browse files
authored
Merge pull request #374 from aeisenberg/path-fix
Fix paths on windows when opening archive databases
2 parents f38d0fd + e31f8b7 commit 6fdfade

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { ProgressCallback, showAndLogErrorMessage, withProgress, showAndLogInfor
1212
* @param databasesManager the DatabaseManager
1313
* @param storagePath where to store the unzipped database.
1414
*/
15-
export async function promptImportInternetDatabase(databasesManager: DatabaseManager, storagePath: string): Promise<DatabaseItem | undefined> {
15+
export async function promptImportInternetDatabase(databasesManager: DatabaseManager, storagePath: string): Promise<DatabaseItem | undefined> {
1616
let item: DatabaseItem | undefined = undefined;
1717

1818
try {
@@ -46,7 +46,7 @@ export async function promptImportInternetDatabase(databasesManager: DatabaseMan
4646
* @param databasesManager the DatabaseManager
4747
* @param storagePath where to store the unzipped database.
4848
*/
49-
export async function importArchiveDatabase(databaseUrl: string, databasesManager: DatabaseManager, storagePath: string): Promise<DatabaseItem | undefined> {
49+
export async function importArchiveDatabase(databaseUrl: string, databasesManager: DatabaseManager, storagePath: string): Promise<DatabaseItem | undefined> {
5050
let item: DatabaseItem | undefined = undefined;
5151
try {
5252
const progressOptions: ProgressOptions = {
@@ -106,7 +106,7 @@ async function databaseArchiveFetcher(
106106
// find the path to the database. The actual database might be in a sub-folder
107107
const dbPath = await findDirWithFile(unzipPath, '.dbinfo', 'codeql-database.yml');
108108
if (dbPath) {
109-
const item = await databasesManager.openDatabase(Uri.parse(dbPath));
109+
const item = await databasesManager.openDatabase(Uri.parse(`file:${dbPath}`));
110110
databasesManager.setCurrentDatabaseItem(item);
111111
return item;
112112
} else {
@@ -115,14 +115,21 @@ async function databaseArchiveFetcher(
115115
}
116116

117117
async function getStorageFolder(storagePath: string, urlStr: string) {
118+
// we need to generate a folder name for the unzipped archive,
119+
// this needs to be human readable since we may use this name as the initial
120+
// name for the database
118121
const url = Uri.parse(urlStr);
119-
let lastName = path.basename(url.path).substring(0, 255);
122+
// MacOS has a max filename length of 255
123+
// and remove a few extra chars in case we need to add a counter at the end.
124+
let lastName = path.basename(url.path).substring(0, 250);
120125
if (lastName.endsWith(".zip")) {
121126
lastName = lastName.substring(0, lastName.length - 4);
122127
}
123128

124129
const realpath = await fs.realpath(storagePath);
125130
let folderName = path.join(realpath, lastName);
131+
132+
// avoid overwriting existing folders
126133
let counter = 0;
127134
while (await fs.pathExists(folderName)) {
128135
counter++;
@@ -155,7 +162,7 @@ async function readAndUnzip(databaseUrl: string, unzipPath: string) {
155162

156163
await new Promise((resolve, reject) => {
157164
// we already know this is a file scheme
158-
const databaseFile = Uri.parse(databaseUrl).path;
165+
const databaseFile = Uri.parse(databaseUrl).fsPath;
159166
const stream = fs.createReadStream(databaseFile);
160167
stream.on('error', reject);
161168
unzipStream.on('error', reject);

extensions/ql-vscode/src/databases-ui.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class DatabaseUI extends DisposableObject {
292292
private handleSetCurrentDatabase = async (uri: Uri): Promise<DatabaseItem | undefined> => {
293293
// Assume user has selected an archive if the file has a .zip extension
294294
if (uri.path.endsWith('.zip')) {
295-
return await importArchiveDatabase(uri.toString(), this.databaseManager, this.storagePath);
295+
return await importArchiveDatabase(uri.toString(true), this.databaseManager, this.storagePath);
296296
}
297297

298298
return await this.setCurrentDatabase(uri);
@@ -366,7 +366,7 @@ export class DatabaseUI extends DisposableObject {
366366
else {
367367
// we are selecting a database archive. Must unzip into a workspace-controlled area
368368
// before importing.
369-
return await importArchiveDatabase(uri.toString(), this.databaseManager, this.storagePath);
369+
return await importArchiveDatabase(uri.toString(true), this.databaseManager, this.storagePath);
370370
}
371371
}
372372
}

0 commit comments

Comments
 (0)