backupFileService.ts 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*---------------------------------------------------------------------------------------------
 *  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';
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IFileService } from 'vs/platform/files/common/files';
import { TPromise } from 'vs/base/common/winjs.base';

export class BackupFileService implements IBackupFileService {

	public _serviceBrand: any;

	protected backupHome: string;
D
Daniel Imms 已提交
22
	protected workspacesJsonPath: string;
23 24 25

	constructor(
		private currentWorkspace: Uri,
26
		@IEnvironmentService private environmentService: IEnvironmentService,
27 28 29
		@IFileService private fileService: IFileService
	) {
		this.backupHome = environmentService.backupHome;
D
Daniel Imms 已提交
30 31 32
		this.workspacesJsonPath = environmentService.backupWorkspacesPath;
	}

33
	public hasBackup(resource: Uri): TPromise<boolean> {
34 35 36 37
		const backupResource = this.getBackupResource(resource);
		if (!backupResource) {
			return TPromise.as(false);
		}
38
		return pfs.exists(backupResource.fsPath);
39 40 41 42
	}

	public getBackupResource(resource: Uri): Uri {
		// Hot exit is disabled for empty workspaces
43
		if (!this.currentWorkspace || this.environmentService.isExtensionDevelopment) {
44 45 46
			return null;
		}

47 48
		// Only hash the file path if the file is not untitled
		const backupName = resource.scheme === 'untitled' ? resource.fsPath : crypto.createHash('md5').update(resource.fsPath).digest('hex');
49 50 51 52 53 54 55 56 57
		const backupPath = path.join(this.getWorkspaceBackupDirectory(), resource.scheme, backupName);
		return Uri.file(backupPath);
	}

	private getWorkspaceBackupDirectory(): string {
		const workspaceHash = crypto.createHash('md5').update(this.currentWorkspace.fsPath).digest('hex');
		return path.join(this.backupHome, workspaceHash);
	}

58
	public backupResource(resource: Uri, content: string): TPromise<void> {
59
		if (this.environmentService.isExtensionDevelopment) {
60 61 62
			return TPromise.as(void 0);
		}

63
		const backupResource = this.getBackupResource(resource);
64

65 66 67 68
		// Hot exit is disabled for empty workspaces
		if (!backupResource) {
			return TPromise.as(void 0);
		}
69

70
		return this.fileService.updateContent(backupResource, content).then(() => void 0);
71 72
	}

73
	public discardResourceBackup(resource: Uri): TPromise<void> {
74
		if (this.environmentService.isExtensionDevelopment) {
75 76 77
			return TPromise.as(void 0);
		}

78
		const backupResource = this.getBackupResource(resource);
79

80 81 82 83
		// Hot exit is disabled for empty workspaces
		if (!backupResource) {
			return TPromise.as(void 0);
		}
84

85
		return this.fileService.del(backupResource);
86 87
	}

88
	public discardAllWorkspaceBackups(): TPromise<void> {
89
		if (this.environmentService.isExtensionDevelopment) {
90 91 92
			return TPromise.as(void 0);
		}

93 94 95
		return this.fileService.del(Uri.file(this.getWorkspaceBackupDirectory()));
	}
}