backupMainService.ts 6.8 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6 7
import * as fs from 'fs';
import * as path from 'path';
8
import * as crypto from 'crypto';
9
import * as platform from 'vs/base/common/platform';
10
import * as extfs from 'vs/base/node/extfs';
11
import Uri from 'vs/base/common/uri';
D
Daniel Imms 已提交
12
import { IBackupWorkspacesFormat, IBackupMainService } from 'vs/platform/backup/common/backup';
13
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
14
import { TPromise } from 'vs/base/common/winjs.base';
15

D
Daniel Imms 已提交
16
export class BackupMainService implements IBackupMainService {
17 18 19 20

	public _serviceBrand: any;

	protected backupHome: string;
D
Daniel Imms 已提交
21
	protected workspacesJsonPath: string;
22

23
	private backups: IBackupWorkspacesFormat;
24

25 26
	private mapWindowToBackupFolder: { [windowId: number]: string; };

27
	constructor(
28
		@IEnvironmentService private environmentService: IEnvironmentService
29 30
	) {
		this.backupHome = environmentService.backupHome;
D
Daniel Imms 已提交
31
		this.workspacesJsonPath = environmentService.backupWorkspacesPath;
32
		this.mapWindowToBackupFolder = Object.create(null);
D
Daniel Imms 已提交
33

34
		this.loadSync();
35 36
	}

37 38 39 40 41 42 43 44
	public get workspaceBackupPaths(): string[] {
		return this.backups.folderWorkspaces;
	}

	public get emptyWorkspaceBackupPaths(): string[] {
		return this.backups.emptyWorkspaces;
	}

45 46 47 48 49 50 51 52
	public getBackupPath(windowId: number): TPromise<string> {
		if (!this.mapWindowToBackupFolder[windowId]) {
			throw new Error(`Unknown backup workspace for window ${windowId}`);
		}

		return TPromise.as(path.join(this.backupHome, this.mapWindowToBackupFolder[windowId]));
	}

53
	public registerWindowForBackups(windowId: number, isEmptyWorkspace: boolean, backupFolder?: string, workspacePath?: string): void {
54 55 56 57 58 59 60 61 62 63
		// Backups and hot exit are disabled during extension development
		if (this.environmentService.isExtensionDevelopment) {
			return;
		}

		// Generate a new folder if this is a new empty workspace
		if (!backupFolder) {
			backupFolder = Date.now().toString();
		}

64 65 66 67 68
		if (workspacePath) {
			const caseAwarePath = platform.isLinux ? workspacePath : workspacePath.toLowerCase();
			backupFolder = crypto.createHash('md5').update(caseAwarePath).digest('hex');
		}

D
Daniel Imms 已提交
69
		this.mapWindowToBackupFolder[windowId] = backupFolder;
70 71

		if (isEmptyWorkspace) {
D
Daniel Imms 已提交
72 73 74 75
			if (this.backups.emptyWorkspaces.indexOf(backupFolder) === -1) {
				this.backups.emptyWorkspaces.push(backupFolder);
				this.saveSync();
			}
76
		} else {
D
Daniel Imms 已提交
77 78 79 80
			const sanitizedPath = this.sanitizePath(workspacePath);
			if (this.backups.folderWorkspaces.indexOf(sanitizedPath) === -1) {
				this.backups.folderWorkspaces.push(sanitizedPath);
				this.saveSync();
81
			}
82 83 84
		}
	}

85 86
	protected removeWorkspaceBackupPathSync(workspace: Uri): void {
		if (!this.backups.folderWorkspaces) {
87 88
			return;
		}
89
		const index = this.backups.folderWorkspaces.indexOf(workspace.fsPath);
90 91 92
		if (index === -1) {
			return;
		}
93
		this.backups.folderWorkspaces.splice(index, 1);
94 95 96
		this.saveSync();
	}

D
Daniel Imms 已提交
97
	// TODO: Remove based on windowId instead?
98 99
	// TODO: Test
	// TODO: Merge with removeWorkspaceBackupPathSync?
D
Daniel Imms 已提交
100
	private removeEmptyWorkspaceBackupWindowIdSync(backupFolder: string): void {
101 102 103
		if (!this.backups.emptyWorkspaces) {
			return;
		}
D
Daniel Imms 已提交
104
		const index = this.backups.emptyWorkspaces.indexOf(backupFolder);
105 106 107 108 109 110 111
		if (index === -1) {
			return;
		}
		this.backups.emptyWorkspaces.splice(index, 1);
		this.saveSync();
	}

112
	protected loadSync(): void {
113
		let backups: IBackupWorkspacesFormat;
D
Daniel Imms 已提交
114
		try {
115
			backups = JSON.parse(fs.readFileSync(this.workspacesJsonPath, 'utf8').toString()); // invalid JSON or permission issue can happen here
D
Daniel Imms 已提交
116
		} catch (error) {
117
			backups = Object.create(null);
D
Daniel Imms 已提交
118 119 120
		}

		// Ensure folderWorkspaces is a string[]
121 122
		if (backups.folderWorkspaces) {
			const fws = backups.folderWorkspaces;
D
Daniel Imms 已提交
123
			if (!Array.isArray(fws) || fws.some(f => typeof f !== 'string')) {
124
				backups.folderWorkspaces = [];
D
Daniel Imms 已提交
125
			}
126 127
		} else {
			backups.folderWorkspaces = [];
128 129
		}

130 131 132 133 134 135 136 137
		// Ensure emptyWorkspaces is a string[]
		if (backups.emptyWorkspaces) {
			const fws = backups.emptyWorkspaces;
			if (!Array.isArray(fws) || fws.some(f => typeof f !== 'string')) {
				backups.emptyWorkspaces = [];
			}
		} else {
			backups.emptyWorkspaces = [];
138 139 140 141 142 143 144 145 146
		}

		this.backups = backups;

		// Validate backup workspaces
		this.validateBackupWorkspaces(backups);
	}

	private validateBackupWorkspaces(backups: IBackupWorkspacesFormat): void {
147 148
		// TODO: Tidy up, improve names, reduce duplication
		const staleBackupWorkspaces: { workspaceIdentifier: string; backupPath: string; }[] = [];
149

150
		backups.folderWorkspaces.forEach(workspacePath => {
151 152
			const backupPath = this.toBackupPath(workspacePath);
			if (!this.hasBackupsSync(backupPath)) {
153 154 155 156 157 158 159 160 161
				staleBackupWorkspaces.push({ workspaceIdentifier: workspacePath, backupPath });
			}
		});
		console.log('checking empty: ' + backups.emptyWorkspaces);
		backups.emptyWorkspaces.forEach(vscodeWindowId => {
			const backupPath = this.toEmptyWorkspaceBackupPath(vscodeWindowId);
			console.log('backupPath: ' + backupPath);
			if (!this.hasBackupsSync(backupPath)) {
				staleBackupWorkspaces.push({ workspaceIdentifier: vscodeWindowId, backupPath });
162 163 164 165
			}
		});

		staleBackupWorkspaces.forEach(staleBackupWorkspace => {
166
			const {backupPath, workspaceIdentifier} = staleBackupWorkspace;
167
			extfs.delSync(backupPath);
168 169
			this.removeWorkspaceBackupPathSync(Uri.file(workspaceIdentifier));
			this.removeEmptyWorkspaceBackupWindowIdSync(workspaceIdentifier);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		});
	}

	private hasBackupsSync(backupPath): boolean {
		try {
			const backupSchemas = extfs.readdirSync(backupPath);
			if (backupSchemas.length === 0) {
				return false; // empty backups
			}

			return backupSchemas.some(backupSchema => {
				try {
					return extfs.readdirSync(path.join(backupPath, backupSchema)).length > 0;
				} catch (error) {
					return false; // invalid folder
				}
			});
		} catch (error) {
			return false; // backup path does not exist
189 190 191 192 193 194 195 196 197
		}
	}

	private saveSync(): void {
		try {
			// The user data directory must exist so only the Backup directory needs to be checked.
			if (!fs.existsSync(this.backupHome)) {
				fs.mkdirSync(this.backupHome);
			}
198
			fs.writeFileSync(this.workspacesJsonPath, JSON.stringify(this.backups));
199
		} catch (ex) {
200
			console.error('Could not save workspaces.json', ex);
201 202
		}
	}
203

D
Daniel Imms 已提交
204 205 206 207
	private sanitizePath(p) {
		return platform.isLinux ? p : p.toLowerCase();
	}

208
	protected toBackupPath(workspacePath: string): string {
D
Daniel Imms 已提交
209
		const caseAwarePath = this.sanitizePath(workspacePath);
210
		const workspaceHash = crypto.createHash('md5').update(caseAwarePath).digest('hex');
211 212 213

		return path.join(this.backupHome, workspaceHash);
	}
214 215 216 217

	protected toEmptyWorkspaceBackupPath(vscodeWindowId: string): string {
		return path.join(this.backupHome, vscodeWindowId);
	}
218
}