Fix incomplete removal of databases

This commit is contained in:
Koen Vlaswinkel
2024-03-08 13:45:52 +01:00
parent dbd5078424
commit f426178b96
7 changed files with 35 additions and 4 deletions

View File

@@ -400,6 +400,7 @@ async function databaseArchiveFetcher(
nameOverride,
{
addSourceArchiveFolder,
extensionManagedLocation: unzipPath,
},
);
return item;

View File

@@ -66,6 +66,10 @@ export class DatabaseItemImpl implements DatabaseItem {
return this.options.origin;
}
public get extensionManagedLocation(): string | undefined {
return this.options.extensionManagedLocation;
}
public resolveSourceFile(uriStr: string | undefined): Uri {
const sourceArchive = this.sourceArchive;
const uri = uriStr ? Uri.parse(uriStr, true) : undefined;

View File

@@ -31,6 +31,12 @@ export interface DatabaseItem {
*/
readonly origin: DatabaseOrigin | undefined;
/**
* The location of the base storage location as managed by the extension, or undefined
* if unknown or not managed by the extension.
*/
readonly extensionManagedLocation: string | undefined;
/** If the database is invalid, describes why. */
readonly error: Error | undefined;

View File

@@ -82,6 +82,10 @@ function eventFired<T>(
}
type OpenDatabaseOptions = {
/**
* A location that is managed by the extension.
*/
extensionManagedLocation?: string;
isTutorialDatabase?: boolean;
/**
* Whether to add a workspace folder containing the source archive to the workspace. Default is true.
@@ -141,6 +145,7 @@ export class DatabaseManager extends DisposableObject {
makeSelected = true,
displayName?: string,
{
extensionManagedLocation,
isTutorialDatabase = false,
addSourceArchiveFolder = addDatabaseSourceToWorkspace(),
}: OpenDatabaseOptions = {},
@@ -149,6 +154,7 @@ export class DatabaseManager extends DisposableObject {
uri,
origin,
displayName,
extensionManagedLocation,
);
return await this.addExistingDatabaseItem(
@@ -202,6 +208,7 @@ export class DatabaseManager extends DisposableObject {
uri: vscode.Uri,
origin: DatabaseOrigin | undefined,
displayName: string | undefined,
extensionManagedLocation?: string,
): Promise<DatabaseItemImpl> {
const contents = await DatabaseResolver.resolveDatabaseContents(uri);
const fullOptions: FullDatabaseOptions = {
@@ -210,6 +217,7 @@ export class DatabaseManager extends DisposableObject {
dateAdded: Date.now(),
language: await this.getPrimaryLanguage(uri.fsPath),
origin,
extensionManagedLocation,
};
const databaseItem = new DatabaseItemImpl(uri, contents, fullOptions);
@@ -370,6 +378,7 @@ export class DatabaseManager extends DisposableObject {
let dateAdded = undefined;
let language = undefined;
let origin = undefined;
let extensionManagedLocation = undefined;
if (state.options) {
if (typeof state.options.displayName === "string") {
displayName = state.options.displayName;
@@ -379,6 +388,7 @@ export class DatabaseManager extends DisposableObject {
}
language = state.options.language;
origin = state.options.origin;
extensionManagedLocation = state.options.extensionManagedLocation;
}
const dbBaseUri = vscode.Uri.parse(state.uri, true);
@@ -392,6 +402,7 @@ export class DatabaseManager extends DisposableObject {
dateAdded,
language,
origin,
extensionManagedLocation,
};
const item = new DatabaseItemImpl(dbBaseUri, undefined, fullOptions);
@@ -583,15 +594,20 @@ export class DatabaseManager extends DisposableObject {
// Remove this database item from the allow-list
await this.deregisterDatabase(item);
// Find whether we know directly which directory we should remove
const directoryToRemove = item.extensionManagedLocation
? vscode.Uri.file(item.extensionManagedLocation)
: item.databaseUri;
// Delete folder from file system only if it is controlled by the extension
if (this.isExtensionControlledLocation(item.databaseUri)) {
if (this.isExtensionControlledLocation(directoryToRemove)) {
void extLogger.log("Deleting database from filesystem.");
await remove(item.databaseUri.fsPath).then(
() => void extLogger.log(`Deleted '${item.databaseUri.fsPath}'`),
await remove(directoryToRemove.fsPath).then(
() => void extLogger.log(`Deleted '${directoryToRemove.fsPath}'`),
(e: unknown) =>
void extLogger.log(
`Failed to delete '${
item.databaseUri.fsPath
directoryToRemove.fsPath
}'. Reason: ${getErrorMessage(e)}`,
),
);

View File

@@ -5,10 +5,12 @@ export interface DatabaseOptions {
dateAdded?: number | undefined;
language?: string;
origin?: DatabaseOrigin;
extensionManagedLocation?: string;
}
export interface FullDatabaseOptions extends DatabaseOptions {
dateAdded: number | undefined;
language: string | undefined;
origin: DatabaseOrigin | undefined;
extensionManagedLocation: string | undefined;
}

View File

@@ -14,6 +14,7 @@ export function mockDbOptions(): FullDatabaseOptions {
origin: {
type: "folder",
},
extensionManagedLocation: undefined,
};
}

View File

@@ -604,6 +604,7 @@ describe("local databases", () => {
origin: {
type: "folder",
},
extensionManagedLocation: undefined,
};
mockDbItem = createMockDB(dir, options);