Skip to content

Commit 80b87e2

Browse files
committed
split backup file service and pass hash
1 parent e443427 commit 80b87e2

4 files changed

Lines changed: 40 additions & 20 deletions

File tree

src/vs/workbench/services/backup/common/backupFileService.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
2121
import { VSBuffer } from 'vs/base/common/buffer';
2222
import { TextSnapshotReadable } from 'vs/workbench/services/textfile/common/textfiles';
2323
import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
24-
import { isNative } from 'vs/base/common/platform';
2524

2625
export interface IBackupFilesModel {
2726
resolve(backupRoot: URI): Promise<IBackupFilesModel>;
@@ -118,12 +117,17 @@ export class BackupFileService implements IBackupFileService {
118117
) {
119118
const backupWorkspacePath = environmentService.configuration.backupPath;
120119
if (backupWorkspacePath) {
121-
this.impl = new BackupFileServiceImpl(backupWorkspacePath, fileService);
120+
this.impl = new BackupFileServiceImpl(backupWorkspacePath, this.hashPath, fileService);
122121
} else {
123-
this.impl = new InMemoryBackupFileService();
122+
this.impl = new InMemoryBackupFileService(this.hashPath);
124123
}
125124
}
126125

126+
protected hashPath(resource: URI): string {
127+
const str = resource.scheme === Schemas.file || resource.scheme === Schemas.untitled ? resource.fsPath : resource.toString();
128+
return hash(str).toString(16);
129+
}
130+
127131
initialize(backupWorkspacePath: string): void {
128132
if (this.impl instanceof BackupFileServiceImpl) {
129133
this.impl.initialize(backupWorkspacePath);
@@ -179,6 +183,7 @@ class BackupFileServiceImpl implements IBackupFileService {
179183

180184
constructor(
181185
backupWorkspacePath: string,
186+
private readonly hashPath: (resource: URI) => string,
182187
@IFileService private readonly fileService: IFileService
183188
) {
184189
this.isShuttingDown = false;
@@ -357,7 +362,7 @@ class BackupFileServiceImpl implements IBackupFileService {
357362
}
358363

359364
toBackupResource(resource: URI): URI {
360-
return joinPath(this.backupWorkspacePath, resource.scheme, hashPath(resource));
365+
return joinPath(this.backupWorkspacePath, resource.scheme, this.hashPath(resource));
361366
}
362367
}
363368

@@ -367,6 +372,8 @@ export class InMemoryBackupFileService implements IBackupFileService {
367372

368373
private backups: Map<string, ITextSnapshot> = new Map();
369374

375+
constructor(private readonly hashPath: (resource: URI) => string) { }
376+
370377
hasBackups(): Promise<boolean> {
371378
return Promise.resolve(this.backups.size > 0);
372379
}
@@ -413,21 +420,8 @@ export class InMemoryBackupFileService implements IBackupFileService {
413420
}
414421

415422
toBackupResource(resource: URI): URI {
416-
return URI.file(join(resource.scheme, hashPath(resource)));
423+
return URI.file(join(resource.scheme, this.hashPath(resource)));
417424
}
418425
}
419426

420-
/*
421-
* Exported only for testing
422-
*/
423-
export function hashPath(resource: URI): string {
424-
const str = resource.scheme === Schemas.file || resource.scheme === Schemas.untitled ? resource.fsPath : resource.toString();
425-
if (isNative) {
426-
const _crypto: typeof crypto = require.__$__nodeRequire('crypto');
427-
return _crypto['createHash']('md5').update(str).digest('hex');
428-
}
429-
430-
return hash(str).toString(16);
431-
}
432-
433427
registerSingleton(IBackupFileService, BackupFileService);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 { BackupFileService as CommonBackupFileService } from 'vs/workbench/services/backup/common/backupFileService';
7+
import { URI } from 'vs/base/common/uri';
8+
import { Schemas } from 'vs/base/common/network';
9+
import * as crypto from 'crypto';
10+
11+
export class BackupFileService extends CommonBackupFileService {
12+
13+
protected hashPath(resource: URI): string {
14+
return hashPath(resource);
15+
}
16+
17+
}
18+
19+
/*
20+
* Exported only for testing
21+
*/
22+
export function hashPath(resource: URI): string {
23+
const str = resource.scheme === Schemas.file || resource.scheme === Schemas.untitled ? resource.fsPath : resource.toString();
24+
return crypto.createHash('md5').update(str).digest('hex');
25+
}

src/vs/workbench/services/backup/test/node/backupFileService.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as fs from 'fs';
1111
import * as path from 'vs/base/common/path';
1212
import * as pfs from 'vs/base/node/pfs';
1313
import { URI } from 'vs/base/common/uri';
14-
import { BackupFileService, BackupFilesModel, hashPath } from 'vs/workbench/services/backup/common/backupFileService';
14+
import { BackupFilesModel } from 'vs/workbench/services/backup/common/backupFileService';
1515
import { TextModel, createTextBufferFactory } from 'vs/editor/common/model/textModel';
1616
import { getRandomTestPath } from 'vs/base/test/node/testUtils';
1717
import { DefaultEndOfLine } from 'vs/editor/common/model';
@@ -24,6 +24,7 @@ import { WorkbenchEnvironmentService } from 'vs/workbench/services/environment/n
2424
import { parseArgs } from 'vs/platform/environment/node/argv';
2525
import { snapshotToString } from 'vs/workbench/services/textfile/common/textfiles';
2626
import { IFileService } from 'vs/platform/files/common/files';
27+
import { hashPath, BackupFileService } from 'vs/workbench/services/backup/node/backupFileService';
2728

2829
const parentDir = getRandomTestPath(os.tmpdir(), 'vsctests', 'backupfileservice');
2930
const backupHome = path.join(parentDir, 'Backups');

src/vs/workbench/workbench.main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ import 'vs/workbench/services/textmodelResolver/common/textModelResolverService'
112112
import 'vs/workbench/services/textfile/node/textFileService';
113113
import 'vs/workbench/services/dialogs/browser/fileDialogService';
114114
import 'vs/workbench/services/dialogs/electron-browser/dialogService';
115-
import 'vs/workbench/services/backup/common/backupFileService';
115+
import 'vs/workbench/services/backup/node/backupFileService';
116116
import 'vs/workbench/services/editor/browser/editorService';
117117
import 'vs/workbench/services/history/browser/history';
118118
import 'vs/workbench/services/activity/browser/activityService';

0 commit comments

Comments
 (0)