Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/files/electron-browser/elevatedFileService.ts
3296 views
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 { localize } from '../../../../nls.js';
7
import { VSBuffer, VSBufferReadable, VSBufferReadableStream } from '../../../../base/common/buffer.js';
8
import { randomPath } from '../../../../base/common/extpath.js';
9
import { Schemas } from '../../../../base/common/network.js';
10
import { URI } from '../../../../base/common/uri.js';
11
import { IFileService, IFileStatWithMetadata, IWriteFileOptions } from '../../../../platform/files/common/files.js';
12
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
13
import { INativeHostService } from '../../../../platform/native/common/native.js';
14
import { IWorkspaceTrustRequestService } from '../../../../platform/workspace/common/workspaceTrust.js';
15
import { INativeWorkbenchEnvironmentService } from '../../environment/electron-browser/environmentService.js';
16
import { IElevatedFileService } from '../common/elevatedFileService.js';
17
import { isWindows } from '../../../../base/common/platform.js';
18
import { ILabelService } from '../../../../platform/label/common/label.js';
19
export class NativeElevatedFileService implements IElevatedFileService {
20
21
readonly _serviceBrand: undefined;
22
23
constructor(
24
@INativeHostService private readonly nativeHostService: INativeHostService,
25
@IFileService private readonly fileService: IFileService,
26
@INativeWorkbenchEnvironmentService private readonly environmentService: INativeWorkbenchEnvironmentService,
27
@IWorkspaceTrustRequestService private readonly workspaceTrustRequestService: IWorkspaceTrustRequestService,
28
@ILabelService private readonly labelService: ILabelService
29
) { }
30
31
isSupported(resource: URI): boolean {
32
// Saving elevated is currently only supported for local
33
// files for as long as we have no generic support from
34
// the file service
35
// (https://github.com/microsoft/vscode/issues/48659)
36
return resource.scheme === Schemas.file;
37
}
38
39
async writeFileElevated(resource: URI, value: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: IWriteFileOptions): Promise<IFileStatWithMetadata> {
40
const trusted = await this.workspaceTrustRequestService.requestWorkspaceTrust({
41
message: isWindows ? localize('fileNotTrustedMessageWindows', "You are about to save '{0}' as admin.", this.labelService.getUriLabel(resource)) : localize('fileNotTrustedMessagePosix', "You are about to save '{0}' as super user.", this.labelService.getUriLabel(resource)),
42
});
43
if (!trusted) {
44
throw new Error(localize('fileNotTrusted', "Workspace is not trusted."));
45
}
46
47
const source = URI.file(randomPath(this.environmentService.userDataPath, 'code-elevated'));
48
try {
49
// write into a tmp file first
50
await this.fileService.writeFile(source, value, options);
51
52
// then sudo prompt copy
53
await this.nativeHostService.writeElevated(source, resource, options);
54
} finally {
55
56
// clean up
57
await this.fileService.del(source);
58
}
59
60
return this.fileService.resolve(resource, { resolveMetadata: true });
61
}
62
}
63
64
registerSingleton(IElevatedFileService, NativeElevatedFileService, InstantiationType.Delayed);
65
66