Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/code/electron-utility/sharedProcess/contrib/storageDataCleaner.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { RunOnceScheduler } from '../../../../base/common/async.js';
7
import { onUnexpectedError } from '../../../../base/common/errors.js';
8
import { Disposable } from '../../../../base/common/lifecycle.js';
9
import { join } from '../../../../base/common/path.js';
10
import { Promises } from '../../../../base/node/pfs.js';
11
import { INativeEnvironmentService } from '../../../../platform/environment/common/environment.js';
12
import { ILogService } from '../../../../platform/log/common/log.js';
13
import { StorageClient } from '../../../../platform/storage/common/storageIpc.js';
14
import { EXTENSION_DEVELOPMENT_EMPTY_WINDOW_WORKSPACE } from '../../../../platform/workspace/common/workspace.js';
15
import { NON_EMPTY_WORKSPACE_ID_LENGTH } from '../../../../platform/workspaces/node/workspaces.js';
16
import { INativeHostService } from '../../../../platform/native/common/native.js';
17
import { IMainProcessService } from '../../../../platform/ipc/common/mainProcessService.js';
18
import { Schemas } from '../../../../base/common/network.js';
19
20
export class UnusedWorkspaceStorageDataCleaner extends Disposable {
21
22
constructor(
23
@INativeEnvironmentService private readonly environmentService: INativeEnvironmentService,
24
@ILogService private readonly logService: ILogService,
25
@INativeHostService private readonly nativeHostService: INativeHostService,
26
@IMainProcessService private readonly mainProcessService: IMainProcessService
27
) {
28
super();
29
30
const scheduler = this._register(new RunOnceScheduler(() => {
31
this.cleanUpStorage();
32
}, 30 * 1000 /* after 30s */));
33
scheduler.schedule();
34
}
35
36
private async cleanUpStorage(): Promise<void> {
37
this.logService.trace('[storage cleanup]: Starting to clean up workspace storage folders for unused empty workspaces.');
38
39
try {
40
const workspaceStorageHome = this.environmentService.workspaceStorageHome.with({ scheme: Schemas.file }).fsPath;
41
const workspaceStorageFolders = await Promises.readdir(workspaceStorageHome);
42
const storageClient = new StorageClient(this.mainProcessService.getChannel('storage'));
43
44
await Promise.all(workspaceStorageFolders.map(async workspaceStorageFolder => {
45
const workspaceStoragePath = join(workspaceStorageHome, workspaceStorageFolder);
46
47
if (workspaceStorageFolder.length === NON_EMPTY_WORKSPACE_ID_LENGTH) {
48
return; // keep workspace storage for folders/workspaces that can be accessed still
49
}
50
51
if (workspaceStorageFolder === EXTENSION_DEVELOPMENT_EMPTY_WINDOW_WORKSPACE.id) {
52
return; // keep workspace storage for empty extension development workspaces
53
}
54
55
const windows = await this.nativeHostService.getWindows({ includeAuxiliaryWindows: false });
56
if (windows.some(window => window.workspace?.id === workspaceStorageFolder)) {
57
return; // keep workspace storage for empty workspaces opened as window
58
}
59
60
const isStorageUsed = await storageClient.isUsed(workspaceStoragePath);
61
if (isStorageUsed) {
62
return; // keep workspace storage for empty workspaces that are in use
63
}
64
65
this.logService.trace(`[storage cleanup]: Deleting workspace storage folder ${workspaceStorageFolder} as it seems to be an unused empty workspace.`);
66
67
await Promises.rm(workspaceStoragePath);
68
}));
69
} catch (error) {
70
onUnexpectedError(error);
71
}
72
}
73
}
74
75