backupFileService.ts 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import * as path from 'path';
import * as crypto from 'crypto';
import pfs = require('vs/base/node/pfs');
import Uri from 'vs/base/common/uri';
12
import { ResourceQueue } from 'vs/base/common/async';
13
import { IBackupFileService, BACKUP_FILE_UPDATE_OPTIONS } from 'vs/workbench/services/backup/common/backup';
14 15
import { IFileService } from 'vs/platform/files/common/files';
import { TPromise } from 'vs/base/common/winjs.base';
16
import { readToMatchingString } from 'vs/base/node/stream';
A
Alex Dima 已提交
17 18
import { TextSource, IRawTextSource } from 'vs/editor/common/model/textSource';
import { DefaultEndOfLine } from 'vs/editor/common/editorCommon';
19

B
Benjamin Pasero 已提交
20 21
export interface IBackupFilesModel {
	resolve(backupRoot: string): TPromise<IBackupFilesModel>;
B
Benjamin Pasero 已提交
22 23 24

	add(resource: Uri, versionId?: number): void;
	has(resource: Uri, versionId?: number): boolean;
25
	get(): Uri[];
B
Benjamin Pasero 已提交
26
	remove(resource: Uri): void;
27
	count(): number;
B
Benjamin Pasero 已提交
28 29 30
	clear(): void;
}

B
Benjamin Pasero 已提交
31
export class BackupFilesModel implements IBackupFilesModel {
B
Benjamin Pasero 已提交
32 33
	private cache: { [resource: string]: number /* version ID */ } = Object.create(null);

B
Benjamin Pasero 已提交
34
	public resolve(backupRoot: string): TPromise<IBackupFilesModel> {
B
Benjamin Pasero 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
		return pfs.readDirsInDir(backupRoot).then(backupSchemas => {

			// For all supported schemas
			return TPromise.join(backupSchemas.map(backupSchema => {

				// Read backup directory for backups
				const backupSchemaPath = path.join(backupRoot, backupSchema);
				return pfs.readdir(backupSchemaPath).then(backupHashes => {

					// Remember known backups in our caches
					backupHashes.forEach(backupHash => {
						const backupResource = Uri.file(path.join(backupSchemaPath, backupHash));
						this.add(backupResource);
					});
				});
			}));
		}).then(() => this, error => this);
	}

B
Benjamin Pasero 已提交
54
	public add(resource: Uri, versionId = 0): void {
B
Benjamin Pasero 已提交
55 56 57
		this.cache[resource.toString()] = versionId;
	}

58 59 60 61
	public count(): number {
		return Object.keys(this.cache).length;
	}

B
Benjamin Pasero 已提交
62
	public has(resource: Uri, versionId?: number): boolean {
B
Benjamin Pasero 已提交
63 64 65 66 67 68 69 70 71 72 73 74
		const cachedVersionId = this.cache[resource.toString()];
		if (typeof cachedVersionId !== 'number') {
			return false; // unknown resource
		}

		if (typeof versionId === 'number') {
			return versionId === cachedVersionId; // if we are asked with a specific version ID, make sure to test for it
		}

		return true;
	}

75 76
	public get(): Uri[] {
		return Object.keys(this.cache).map(k => Uri.parse(k));
77 78
	}

B
Benjamin Pasero 已提交
79
	public remove(resource: Uri): void {
B
Benjamin Pasero 已提交
80 81 82
		delete this.cache[resource.toString()];
	}

B
Benjamin Pasero 已提交
83
	public clear(): void {
B
Benjamin Pasero 已提交
84 85 86 87
		this.cache = Object.create(null);
	}
}

88 89 90 91
export class BackupFileService implements IBackupFileService {

	public _serviceBrand: any;

B
Benjamin Pasero 已提交
92 93
	private static readonly META_MARKER = '\n';

94
	private isShuttingDown: boolean;
B
Benjamin Pasero 已提交
95
	private ready: TPromise<IBackupFilesModel>;
96
	private ioOperationQueues: ResourceQueue<void>; // queue IO operations to ensure write order
B
Benjamin Pasero 已提交
97

98
	constructor(
99 100
		private backupWorkspacePath: string,
		@IFileService private fileService: IFileService
101
	) {
102
		this.isShuttingDown = false;
103
		this.ioOperationQueues = new ResourceQueue<void>();
104
		this.ready = this.init();
D
Daniel Imms 已提交
105 106
	}

107
	public get backupEnabled(): boolean {
108
		return !!this.backupWorkspacePath; // Hot exit requires a backup path
B
Benjamin Pasero 已提交
109 110
	}

111
	private init(): TPromise<IBackupFilesModel> {
B
Benjamin Pasero 已提交
112
		const model = new BackupFilesModel();
113

B
Benjamin Pasero 已提交
114
		if (!this.backupEnabled) {
B
Benjamin Pasero 已提交
115
			return TPromise.as(model);
116 117
		}

118
		return model.resolve(this.backupWorkspacePath);
B
Benjamin Pasero 已提交
119 120
	}

121 122 123 124 125 126
	public hasBackups(): TPromise<boolean> {
		return this.ready.then(model => {
			return model.count() > 0;
		});
	}

127
	public loadBackupResource(resource: Uri): TPromise<Uri> {
B
Benjamin Pasero 已提交
128 129 130
		return this.ready.then(model => {
			const backupResource = this.getBackupResource(resource);
			if (!backupResource) {
131
				return void 0;
B
Benjamin Pasero 已提交
132
			}
B
Benjamin Pasero 已提交
133

134 135 136 137
			// Return directly if we have a known backup with that resource
			if (model.has(backupResource)) {
				return backupResource;
			}
138

139
			return void 0;
B
Benjamin Pasero 已提交
140
		});
141 142
	}

B
Benjamin Pasero 已提交
143
	public backupResource(resource: Uri, content: string, versionId?: number): TPromise<void> {
144 145 146 147
		if (this.isShuttingDown) {
			return TPromise.as(void 0);
		}

B
Benjamin Pasero 已提交
148
		return this.ready.then(model => {
B
Benjamin Pasero 已提交
149 150
			const backupResource = this.getBackupResource(resource);
			if (!backupResource) {
B
Benjamin Pasero 已提交
151
				return void 0;
B
Benjamin Pasero 已提交
152 153
			}

B
Benjamin Pasero 已提交
154
			if (model.has(backupResource, versionId)) {
B
Benjamin Pasero 已提交
155
				return void 0; // return early if backup version id matches requested one
B
Benjamin Pasero 已提交
156
			}
157

158
			// Add metadata to top of file
B
Benjamin Pasero 已提交
159
			content = `${resource.toString()}${BackupFileService.META_MARKER}${content}`;
160

161
			return this.ioOperationQueues.queueFor(backupResource).queue(() => {
162 163
				return this.fileService.updateContent(backupResource, content, BACKUP_FILE_UPDATE_OPTIONS).then(() => model.add(backupResource, versionId));
			});
B
Benjamin Pasero 已提交
164
		});
165 166
	}

167
	public discardResourceBackup(resource: Uri): TPromise<void> {
B
Benjamin Pasero 已提交
168
		return this.ready.then(model => {
B
Benjamin Pasero 已提交
169 170
			const backupResource = this.getBackupResource(resource);
			if (!backupResource) {
B
Benjamin Pasero 已提交
171
				return void 0;
B
Benjamin Pasero 已提交
172
			}
B
Benjamin Pasero 已提交
173

174
			return this.ioOperationQueues.queueFor(backupResource).queue(() => {
175 176
				return pfs.del(backupResource.fsPath).then(() => model.remove(backupResource));
			});
B
Benjamin Pasero 已提交
177
		});
178 179
	}

180
	public discardAllWorkspaceBackups(): TPromise<void> {
181 182
		this.isShuttingDown = true;

B
Benjamin Pasero 已提交
183
		return this.ready.then(model => {
B
Benjamin Pasero 已提交
184
			if (!this.backupEnabled) {
B
Benjamin Pasero 已提交
185
				return void 0;
B
Benjamin Pasero 已提交
186
			}
B
Benjamin Pasero 已提交
187

188
			return pfs.del(this.backupWorkspacePath).then(() => model.clear());
B
Benjamin Pasero 已提交
189 190 191
		});
	}

192
	public getWorkspaceFileBackups(): TPromise<Uri[]> {
193
		return this.ready.then(model => {
B
Benjamin Pasero 已提交
194 195
			const readPromises: TPromise<Uri>[] = [];

196
			model.get().forEach(fileBackup => {
K
katainaka0503 已提交
197 198 199 200
				readPromises.push(
					readToMatchingString(fileBackup.fsPath, BackupFileService.META_MARKER, 2000, 10000)
						.then(Uri.parse)
				);
201
			});
B
Benjamin Pasero 已提交
202

203 204 205 206
			return TPromise.join(readPromises);
		});
	}

A
Alex Dima 已提交
207 208 209
	public parseBackupContent(rawTextSource: IRawTextSource): string {
		const textSource = TextSource.fromRawTextSource(rawTextSource, DefaultEndOfLine.LF);
		return textSource.lines.slice(1).join(textSource.EOL); // The first line of a backup text file is the file name
210 211
	}

212
	protected getBackupResource(resource: Uri): Uri {
B
Benjamin Pasero 已提交
213
		if (!this.backupEnabled) {
B
Benjamin Pasero 已提交
214
			return null;
215 216
		}

217
		return Uri.file(path.join(this.backupWorkspacePath, resource.scheme, this.hashPath(resource)));
D
Daniel Imms 已提交
218
	}
219

220 221
	private hashPath(resource: Uri): string {
		return crypto.createHash('md5').update(resource.fsPath).digest('hex');
222
	}
223
}