backupFileService.ts 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/*---------------------------------------------------------------------------------------------
 *  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 * as arrays from 'vs/base/common/arrays';
import pfs = require('vs/base/node/pfs');
import Uri from 'vs/base/common/uri';
import { IBackupFormat } from 'vs/platform/backup/common/backup';
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;
	protected backupWorkspacesPath: string;

	private fileContent: IBackupFormat;

	constructor(
		private currentWorkspace: Uri,
		@IEnvironmentService environmentService: IEnvironmentService,
		@IFileService private fileService: IFileService
	) {
		this.backupHome = environmentService.backupHome;
		this.backupWorkspacesPath = environmentService.backupWorkspacesPath;
	}

	public getWorkspaceBackupPaths(): TPromise<string[]> {
		return this.load().then(() => {
			return Object.keys(this.fileContent.folderWorkspaces);
		});
	}

	public removeWorkspaceBackupPath(workspace: Uri): TPromise<void> {
		return this.load().then(() => {
			if (!this.fileContent.folderWorkspaces) {
				return TPromise.as(void 0);
			}
			delete this.fileContent.folderWorkspaces[workspace.fsPath];
			return this.save();
		});
	}

	public registerResourceForBackup(resource: Uri): TPromise<void> {
		// Hot exit is disabled for empty workspaces
		if (!this.currentWorkspace) {
			return TPromise.as(void 0);
		}

		return this.load().then(() => {
			if (!(this.currentWorkspace.fsPath in this.fileContent.folderWorkspaces)) {
				this.fileContent.folderWorkspaces[this.currentWorkspace.fsPath] = [];
			}
			if (arrays.contains(this.fileContent.folderWorkspaces[this.currentWorkspace.fsPath], resource.fsPath)) {
				return TPromise.as(void 0);
			}
			this.fileContent.folderWorkspaces[this.currentWorkspace.fsPath].push(resource.fsPath);
			return this.save();
		});
	}

	public deregisterResourceForBackup(resource: Uri): TPromise<void> {
		// Hot exit is disabled for empty workspaces
		if (!this.currentWorkspace) {
			return TPromise.as(void 0);
		}

		return this.load().then(() => {
			const workspace = this.fileContent.folderWorkspaces[this.currentWorkspace.fsPath];
			if (workspace) {
				this.fileContent.folderWorkspaces[this.currentWorkspace.fsPath] = workspace.filter(value => value !== resource.fsPath);
				return this.save();
			}
			return TPromise.as(void 0);
		});
	}

	public doesTextFileHaveBackup(resource: Uri): TPromise<boolean> {
		return this.load().then(() => {
			return arrays.contains(this.fileContent.folderWorkspaces[this.currentWorkspace.fsPath] || [], resource.fsPath);
		});
	}

	public getBackupResource(resource: Uri): Uri {
		// Hot exit is disabled for empty workspaces
		if (!this.currentWorkspace) {
			return null;
		}

		const backupName = crypto.createHash('md5').update(resource.fsPath).digest('hex');
		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);
	}

	public backupAndRegisterResource(resource: Uri, content: string): TPromise<void> {
		let registerResourcePromise: TPromise<void>;
		if (resource.scheme === 'file') {
			registerResourcePromise = this.registerResourceForBackup(resource);
		} else {
			registerResourcePromise = TPromise.as(void 0);
		}
		return registerResourcePromise.then(() => {
			const backupResource = this.getBackupResource(resource);

			// Hot exit is disabled for empty workspaces
			if (!backupResource) {
				return TPromise.as(null);
			}

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

	public discardAndDeregisterResource(resource: Uri): TPromise<void> {
		return this.deregisterResourceForBackup(resource).then(() => {
			const backupResource = this.getBackupResource(resource);

			// Hot exit is disabled for empty workspaces
			if (!backupResource) {
				return TPromise.as(null);
			}

			return this.fileService.del(backupResource);
		});
	}

	public discardBackups(): TPromise<void> {
		return this.fileService.del(Uri.file(this.getWorkspaceBackupDirectory()));
	}

	private load(): TPromise<void> {
		return pfs.fileExists(this.backupWorkspacesPath).then(exists => {
			if (!exists) {
				this.fileContent = {
					folderWorkspaces: Object.create(null)
				};
				return TPromise.as(void 0);
			}

			return pfs.readFile(this.backupWorkspacesPath, 'utf8').then(content => {
				try {
					return JSON.parse(content.toString());
				} catch (ex) {
					return Object.create(null);
				}
			}).then(content => {
				this.fileContent = content;
				if (!this.fileContent.folderWorkspaces) {
					this.fileContent.folderWorkspaces = Object.create(null);
				}
				return TPromise.as(void 0);
			});
		});
	}

	private save(): TPromise<void> {
		const data = JSON.stringify(this.fileContent);
		return pfs.mkdirp(this.backupHome).then(() => {
			return pfs.writeFile(this.backupWorkspacesPath, data);
		});
	}
}